Skip to content

Package: MessageInfo

MessageInfo

nameinstructionbranchcomplexitylinemethod
MessageInfo()
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%
formatAddresses(Address[])
M: 34 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getBcc()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getBody()
M: 70 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
getCc()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getDate()
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getDisplayAddress(Address)
M: 34 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getFrom()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMessage()
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%
getNum()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getReceivedDate()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getReplyTo()
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getSentDate()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getSubject()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getTo()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
hasAttachments()
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
hasBcc()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasCc()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasDate()
M: 10 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasFrom()
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasMimeType(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasReceivedDate()
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasSentDate()
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasSubject()
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasTo()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setMessage(Message)
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.text.*;
14: import java.util.*;
15: import jakarta.mail.*;
16: import jakarta.mail.internet.*;
17:
18: /**
19: * Used to store message information.
20: */
21: public class MessageInfo {
22: private Message message;
23:
24:
25: /**
26: * Returns the bcc field.
27: */
28: public String getBcc() throws MessagingException {
29:         return formatAddresses(
30:          message.getRecipients(Message.RecipientType.BCC));
31: }
32:
33: /**
34: * Returns the body of the message (if it's plain text).
35: */
36: public String getBody() throws MessagingException, java.io.IOException {
37:         Object content = message.getContent();
38:•        if (message.isMimeType("text/plain")) {
39:          return (String)content;
40:•        } else if (message.isMimeType("multipart/alternative")) {
41:          Multipart mp = (Multipart)message.getContent();
42:          int numParts = mp.getCount();
43:•         for (int i = 0; i < numParts; ++i) {
44:•                if (mp.getBodyPart(i).isMimeType("text/plain"))
45:                  return (String)mp.getBodyPart(i).getContent();
46:          }
47:          return "";
48:•        } else if (message.isMimeType("multipart/*")) {
49:          Multipart mp = (Multipart)content;
50:•         if (mp.getBodyPart(0).isMimeType("text/plain"))
51:                 return (String)mp.getBodyPart(0).getContent();
52:          else
53:                 return "";
54:         } else
55:          return "";
56: }
57:
58: /**
59: * Returns the cc field.
60: */
61: public String getCc() throws MessagingException {
62:         return formatAddresses(
63:          message.getRecipients(Message.RecipientType.CC));
64: }
65:
66: /**
67: * Returns the date the message was sent (or received if the sent date
68: * is null.
69: */
70: public String getDate() throws MessagingException {
71:         Date date;
72:         SimpleDateFormat df = new SimpleDateFormat("EE M/d/yy");
73:•        if ((date = message.getSentDate()) != null)
74:          return (df.format(date));
75:•        else if ((date = message.getReceivedDate()) != null)
76:          return (df.format(date));
77:         else
78:          return "";
79: }
80:
81: /**
82: * Returns the from field.
83: */
84: public String getFrom() throws MessagingException {
85:         return formatAddresses(message.getFrom());
86: }
87:
88: /**
89: * Returns the address to reply to.
90: */
91: public String getReplyTo() throws MessagingException {
92:         Address[] a = message.getReplyTo();
93:•        if (a.length > 0)
94:          return ((InternetAddress)a[0]).getAddress();
95:         else
96:          return "";
97: }
98:
99: /**
100: * Returns the jakarta.mail.Message object.
101: */
102: public Message getMessage() {
103:         return message;
104: }
105:
106: /**
107: * Returns the message number.
108: */
109: public String getNum() {
110:         return (Integer.toString(message.getMessageNumber()));
111: }
112:
113: /**
114: * Returns the received date field.
115: */
116: public String getReceivedDate() throws MessagingException {
117:•        if (hasReceivedDate())
118:          return (message.getReceivedDate().toString());
119:         else
120:          return "";
121: }
122:
123: /**
124: * Returns the sent date field.
125: */
126: public String getSentDate() throws MessagingException {
127:•        if (hasSentDate())
128:          return (message.getSentDate().toString());
129:         else
130:          return "";
131: }
132:
133: /**
134: * Returns the subject field.
135: */
136: public String getSubject() throws MessagingException {
137:•        if (hasSubject())
138:          return message.getSubject();
139:         else
140:          return "";
141: }
142:
143: /**
144: * Returns the to field.
145: */
146: public String getTo() throws MessagingException {
147:         return formatAddresses(
148:          message.getRecipients(Message.RecipientType.TO));
149: }
150:
151: /**
152: * Method for checking if the message has attachments.
153: */
154: public boolean hasAttachments() throws java.io.IOException,
155:                                          MessagingException {
156:         boolean hasAttachments = false;
157:•        if (message.isMimeType("multipart/*")) {
158:          Multipart mp = (Multipart)message.getContent();
159:•         if (mp.getCount() > 1)
160:                 hasAttachments = true;
161:         }
162:         
163:         return hasAttachments;
164: }
165:
166: /**
167: * Method for checking if the message has a bcc field.
168: */
169: public boolean hasBcc() throws MessagingException {
170:•        return (message.getRecipients(Message.RecipientType.BCC) != null);
171: }
172:
173: /**
174: * Method for checking if the message has a cc field.
175: */
176: public boolean hasCc() throws MessagingException {
177:•        return (message.getRecipients(Message.RecipientType.CC) != null);
178: }
179:
180: /**
181: * Method for checking if the message has a date field.
182: */
183: public boolean hasDate() throws MessagingException {
184:•        return (hasSentDate() || hasReceivedDate());
185: }
186:
187: /**
188: * Method for checking if the message has a from field.
189: */
190: public boolean hasFrom() throws MessagingException {
191:•        return (message.getFrom() != null);
192: }
193:
194: /**
195: * Method for checking if the message has the desired mime type.
196: */
197: public boolean hasMimeType(String mimeType) throws MessagingException {
198:         return message.isMimeType(mimeType);
199: }
200:
201: /**
202: * Method for checking if the message has a received date field.
203: */
204: public boolean hasReceivedDate() throws MessagingException {
205:•        return (message.getReceivedDate() != null);
206: }
207:
208: /**
209: * Method for checking if the message has a sent date field.
210: */
211: public boolean hasSentDate() throws MessagingException {
212:•        return (message.getSentDate() != null);
213: }
214:
215: /**
216: * Method for checking if the message has a subject field.
217: */
218: public boolean hasSubject() throws MessagingException {
219:•        return (message.getSubject() != null);
220: }
221:
222: /**
223: * Method for checking if the message has a to field.
224: */
225: public boolean hasTo() throws MessagingException {
226:•        return (message.getRecipients(Message.RecipientType.TO) != null);
227: }
228:
229: /**
230: * Method for mapping a message to this MessageInfo class.
231: */
232: public void setMessage(Message message) {
233:         this.message = message;
234: }
235:
236: /**
237: * Utility method for formatting msg header addresses.
238: */
239: private String formatAddresses(Address[] addrs) {
240:•        if (addrs == null)
241:          return "";
242:         StringBuilder strBuf = new StringBuilder(getDisplayAddress(addrs[0]));
243:•        for (int i = 1; i < addrs.length; i++) {
244:          strBuf.append(", ").append(getDisplayAddress(addrs[i]));
245:         }
246:         return strBuf.toString();
247: }
248:
249: /**
250: * Utility method which returns a string suitable for msg header display.
251: */
252: private String getDisplayAddress(Address a) {
253:         String pers = null;
254:         String addr = null;
255:•        if (a instanceof InternetAddress &&
256:•         ((pers = ((InternetAddress)a).getPersonal()) != null)) {
257:          addr = pers + " "+"<"+((InternetAddress)a).getAddress()+">";
258:         } else
259:          addr = a.toString();
260:         return addr;
261: }
262: }
263: