1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12 * language governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16 * Sun designates this particular file as subject to the "Classpath" exception
17 * as provided by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the License
19 * Header, with the fields enclosed by brackets [] replaced by your own
20 * identifying information: "Portions Copyrighted [year]
21 * [name of copyright owner]"
22 *
23 * Contributor(s):
24 *
25 * If you wish your version of this file to be governed by only the CDDL or
26 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27 * elects to include this software in this distribution under the [CDDL or GPL
28 * Version 2] license." If you don't indicate a single choice of license, a
29 * recipient has the option to distribute your version of this file under
30 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31 * its licensees as provided above. However, if you add GPL Version 2 code
32 * and therefore, elected the GPL Version 2 license, then the option applies
33 * only if the new code is made subject to such option by the copyright
34 * holder.
35 */
36
37 /*
38 * @(#)MessageContext.java 1.7 07/05/04
39 */
40
41 package javax.mail;
42
43 /**
44 * The context in which a piece of Message content is contained. A
45 * <code>MessageContext</code> object is returned by the
46 * <code>getMessageContext</code> method of the
47 * <code>MessageAware</code> interface. <code>MessageAware</code> is
48 * typically implemented by <code>DataSources</code> to allow a
49 * <code>DataContentHandler</code> to pass on information about the
50 * context in which a data content object is operating.
51 *
52 * @see javax.mail.MessageAware
53 * @see javax.activation.DataSource
54 * @see javax.activation.DataContentHandler
55 * @since JavaMail 1.1
56 */
57 public class MessageContext {
58 private Part part;
59
60 /**
61 * Create a MessageContext object describing the context of the given Part.
62 */
63 public MessageContext(Part part) {
64 this.part = part;
65 }
66
67 /**
68 * Return the Part that contains the content.
69 *
70 * @return the containing Part, or null if not known
71 */
72 public Part getPart() {
73 return part;
74 }
75
76 /**
77 * Return the Message that contains the content.
78 * Follows the parent chain up through containing Multipart
79 * objects until it comes to a Message object, or null.
80 *
81 * @return the containing Message, or null if not known
82 */
83 public Message getMessage() {
84 try {
85 return getMessage(part);
86 } catch (MessagingException ex) {
87 return null;
88 }
89 }
90
91 /**
92 * Return the Message containing an arbitrary Part.
93 * Follows the parent chain up through containing Multipart
94 * objects until it comes to a Message object, or null.
95 *
96 * @return the containing Message, or null if none
97 * @see javax.mail.BodyPart#getParent
98 * @see javax.mail.Multipart#getParent
99 */
100 private static Message getMessage(Part p) throws MessagingException {
101 while (p != null) {
102 if (p instanceof Message)
103 return (Message)p;
104 BodyPart bp = (BodyPart)p;
105 Multipart mp = bp.getParent();
106 if (mp == null) // MimeBodyPart might not be in a MimeMultipart
107 return null;
108 p = mp.getParent();
109 }
110 return null;
111 }
112
113 /**
114 * Return the Session we're operating in.
115 *
116 * @return the Session, or null if not known
117 */
118 public Session getSession() {
119 Message msg = getMessage();
120 return msg != null ? msg.session : null;
121 }
122 }