Skip to content

Package: MessageContext

MessageContext

nameinstructionbranchcomplexitylinemethod
MessageContext(Part)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getMessage()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getMessage(Part)
M: 24 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getPart()
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%
getSession()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 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 Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail;
18:
19: /**
20: * The context in which a piece of Message content is contained. A
21: * <code>MessageContext</code> object is returned by the
22: * <code>getMessageContext</code> method of the
23: * <code>MessageAware</code> interface. <code>MessageAware</code> is
24: * typically implemented by <code>DataSources</code> to allow a
25: * <code>DataContentHandler</code> to pass on information about the
26: * context in which a data content object is operating.
27: *
28: * @see jakarta.mail.MessageAware
29: * @see jakarta.activation.DataSource
30: * @see jakarta.activation.DataContentHandler
31: * @since JavaMail 1.1
32: */
33: public class MessageContext {
34: private Part part;
35:
36: /**
37: * Create a MessageContext object describing the context of the given Part.
38: *
39: * @param part the Part
40: */
41: public MessageContext(Part part) {
42: this.part = part;
43: }
44:
45: /**
46: * Return the Part that contains the content.
47: *
48: * @return the containing Part, or null if not known
49: */
50: public Part getPart() {
51: return part;
52: }
53:
54: /**
55: * Return the Message that contains the content.
56: * Follows the parent chain up through containing Multipart
57: * objects until it comes to a Message object, or null.
58: *
59: * @return the containing Message, or null if not known
60: */
61: public Message getMessage() {
62: try {
63: return getMessage(part);
64: } catch (MessagingException ex) {
65: return null;
66: }
67: }
68:
69: /**
70: * Return the Message containing an arbitrary Part.
71: * Follows the parent chain up through containing Multipart
72: * objects until it comes to a Message object, or null.
73: *
74: * @return the containing Message, or null if none
75: * @see jakarta.mail.BodyPart#getParent
76: * @see jakarta.mail.Multipart#getParent
77: */
78: private static Message getMessage(Part p) throws MessagingException {
79:• while (p != null) {
80:• if (p instanceof Message)
81: return (Message) p;
82: BodyPart bp = (BodyPart) p;
83: Multipart mp = bp.getParent();
84:• if (mp == null) // MimeBodyPart might not be in a MimeMultipart
85: return null;
86: p = mp.getParent();
87: }
88: return null;
89: }
90:
91: /**
92: * Return the Session we're operating in.
93: *
94: * @return the Session, or null if not known
95: */
96: public Session getSession() {
97: Message msg = getMessage();
98:• return msg != null ? msg.getSession() : null;
99: }
100: }