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