Skip to content

Package: ListAttachmentsTag

ListAttachmentsTag

nameinstructionbranchcomplexitylinemethod
ListAttachmentsTag()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
doAfterBody()
M: 41 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
doStartTag()
M: 37 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
getMessageinfo()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPart()
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
setMessageinfo(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2001, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package demo;
12:
13: import jakarta.mail.Multipart;
14: import jakarta.servlet.jsp.JspException;
15: import jakarta.servlet.jsp.JspTagException;
16: import jakarta.servlet.jsp.tagext.BodyContent;
17: import jakarta.servlet.jsp.tagext.BodyTag;
18: import jakarta.servlet.jsp.tagext.BodyTagSupport;
19:
20: import java.io.IOException;
21:
22: /**
23: * Custom tag for listing message attachments. The scripting variable is only
24: * within the body of the tag.
25: */
26: public class ListAttachmentsTag extends BodyTagSupport {
27: private String messageinfo;
28: private int partNum = 1;
29: private int numParts = 0;
30: private AttachmentInfo attachmentinfo;
31: private MessageInfo messageInfo;
32: private Multipart multipart;
33:
34: /**
35: * messageinfo attribute getter method.
36: */
37: public String getMessageinfo() {
38: return messageinfo;
39: }
40:
41: /**
42: * messageinfo attribute setter method.
43: */
44: public void setMessageinfo(String messageinfo) {
45: this.messageinfo = messageinfo;
46: }
47:
48: /**
49: * Method for processing the start of the tag.
50: */
51: public int doStartTag() throws JspException {
52: messageInfo = (MessageInfo) pageContext.getAttribute(getMessageinfo());
53: attachmentinfo = new AttachmentInfo();
54:
55: try {
56: multipart = (Multipart) messageInfo.getMessage().getContent();
57: numParts = multipart.getCount();
58: } catch (Exception ex) {
59: throw new JspException(ex.getMessage());
60: }
61:
62: getPart();
63:
64: return BodyTag.EVAL_BODY_TAG;
65: }
66:
67: /**
68: * Method for processing the body content of the tag.
69: */
70: public int doAfterBody() throws JspException {
71:
72: BodyContent body = getBodyContent();
73: try {
74: body.writeOut(getPreviousOut());
75: } catch (IOException e) {
76: throw new JspTagException("IterationTag: " + e.getMessage());
77: }
78:
79: // clear up so the next time the body content is empty
80: body.clearBody();
81:
82: partNum++;
83:• if (partNum < numParts) {
84: getPart();
85: return BodyTag.EVAL_BODY_TAG;
86: } else {
87: return BodyTag.SKIP_BODY;
88: }
89: }
90:
91: /**
92: * Helper method for retrieving message parts.
93: */
94: private void getPart() throws JspException {
95: try {
96: attachmentinfo.setPart(partNum, multipart.getBodyPart(partNum));
97: pageContext.setAttribute(getId(), attachmentinfo);
98: } catch (Exception ex) {
99: throw new JspException(ex.getMessage());
100: }
101: }
102: }
103: