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, 2021 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 java.io.*;
14: import jakarta.mail.*;
15: import jakarta.servlet.jsp.*;
16: import jakarta.servlet.jsp.tagext.*;
17:
18: /**
19: * Custom tag for listing message attachments. The scripting variable is only
20: * within the body of the tag.
21: */
22: public class ListAttachmentsTag extends BodyTagSupport {
23: private String messageinfo;
24: private int partNum = 1;
25: private int numParts = 0;
26: private AttachmentInfo attachmentinfo;
27: private MessageInfo messageInfo;
28: private Multipart multipart;
29:
30: /**
31: * messageinfo attribute getter method.
32: */
33: public String getMessageinfo() {
34:         return messageinfo;
35: }
36:
37: /**
38: * messageinfo attribute setter method.
39: */
40: public void setMessageinfo(String messageinfo) {
41:         this.messageinfo = messageinfo;
42: }
43:
44: /**
45: * Method for processing the start of the tag.
46: */
47: public int doStartTag() throws JspException {
48:         messageInfo = (MessageInfo)pageContext.getAttribute(getMessageinfo());
49:         attachmentinfo = new AttachmentInfo();
50:         
51:         try {
52:          multipart = (Multipart)messageInfo.getMessage().getContent();
53:          numParts = multipart.getCount();
54:         } catch (Exception ex) {
55:          throw new JspException(ex.getMessage());
56:         }
57:
58:         getPart();
59:
60:         return BodyTag.EVAL_BODY_TAG;
61: }
62:
63: /**
64: * Method for processing the body content of the tag.
65: */
66: public int doAfterBody() throws JspException {
67:         
68:         BodyContent body = getBodyContent();
69:         try {
70:          body.writeOut(getPreviousOut());
71:         } catch (IOException e) {
72:          throw new JspTagException("IterationTag: " + e.getMessage());
73:         }
74:         
75:         // clear up so the next time the body content is empty
76:         body.clearBody();
77:
78:         partNum++;
79:•        if (partNum < numParts) {
80:          getPart();
81:          return BodyTag.EVAL_BODY_TAG;
82:         } else {
83:          return BodyTag.SKIP_BODY;
84:         }
85: }
86:
87: /**
88: * Helper method for retrieving message parts.
89: */
90: private void getPart() throws JspException {
91:         try {
92:          attachmentinfo.setPart(partNum, multipart.getBodyPart(partNum));
93:          pageContext.setAttribute(getId(), attachmentinfo);
94:         } catch (Exception ex) {
95:          throw new JspException(ex.getMessage());
96:         }
97: }
98: }
99: