KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > MessageContext


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)MessageContext.java 1.6 05/08/29
24  *
25  * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail;
29
30 /**
31  * The context in which a piece of Message content is contained. A
32  * <code>MessageContext</code> object is returned by the
33  * <code>getMessageContext</code> method of the
34  * <code>MessageAware</code> interface. <code>MessageAware</code> is
35  * typically implemented by <code>DataSources</code> to allow a
36  * <code>DataContentHandler</code> to pass on information about the
37  * context in which a data content object is operating.
38  *
39  * @see javax.mail.MessageAware
40  * @see javax.activation.DataSource
41  * @see javax.activation.DataContentHandler
42  * @since JavaMail 1.1
43  */

44 public class MessageContext {
45     private Part JavaDoc part;
46
47     /**
48      * Create a MessageContext object describing the context of the given Part.
49      */

50     public MessageContext(Part JavaDoc part) {
51     this.part = part;
52     }
53
54     /**
55      * Return the Part that contains the content.
56      *
57      * @return the containing Part, or null if not known
58      */

59     public Part JavaDoc getPart() {
60     return part;
61     }
62
63     /**
64      * Return the Message that contains the content.
65      * Follows the parent chain up through containing Multipart
66      * objects until it comes to a Message object, or null.
67      *
68      * @return the containing Message, or null if not known
69      */

70     public Message JavaDoc getMessage() {
71     try {
72         return getMessage(part);
73     } catch (MessagingException JavaDoc ex) {
74         return null;
75     }
76     }
77
78     /**
79      * Return the Message containing an arbitrary Part.
80      * Follows the parent chain up through containing Multipart
81      * objects until it comes to a Message object, or null.
82      *
83      * @return the containing Message, or null if none
84      * @see javax.mail.BodyPart#getParent
85      * @see javax.mail.Multipart#getParent
86      */

87     private static Message JavaDoc getMessage(Part JavaDoc p) throws MessagingException JavaDoc {
88     while (p != null) {
89         if (p instanceof Message JavaDoc)
90         return (Message JavaDoc)p;
91         BodyPart JavaDoc bp = (BodyPart JavaDoc)p;
92         Multipart JavaDoc mp = bp.getParent();
93         if (mp == null) // MimeBodyPart might not be in a MimeMultipart
94
return null;
95         p = mp.getParent();
96     }
97     return null;
98     }
99
100     /**
101      * Return the Session we're operating in.
102      *
103      * @return the Session, or null if not known
104      */

105     public Session JavaDoc getSession() {
106     Message JavaDoc msg = getMessage();
107     return msg != null ? msg.session : null;
108     }
109 }
110
Popular Tags