View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package javax.mail;
21  
22  /**
23   * The context in which a piece of message content is contained.
24   *
25   * @version $Rev: 578802 $ $Date: 2007-09-24 09:16:44 -0400 (Mon, 24 Sep 2007) $
26   */
27  public class MessageContext {
28      private final Part part;
29  
30      /**
31       * Create a MessageContext object describing the context of the supplied Part.
32       *
33       * @param part the containing part
34       */
35      public MessageContext(Part part) {
36          this.part = part;
37      }
38  
39      /**
40       * Return the {@link Part} that contains the content.
41       *
42       * @return the part
43       */
44      public Part getPart() {
45          return part;
46      }
47  
48      /**
49       * Return the message that contains the content; if the Part is a {@link Multipart}
50       * then recurse up the chain until a {@link Message} is found.
51       *
52       * @return
53       */
54      public Message getMessage() {
55          return getMessageFrom(part);
56      }
57  
58      /**
59       * Return the session associated with the Message containing this Part.
60       *
61       * @return the session associated with this context's root message
62       */
63      public Session getSession() {
64          Message message = getMessage();
65          if (message == null) {
66              return null;
67          } else {
68              return message.session;
69          }
70      }
71  
72      /**
73       * recurse up the chain of MultiPart/BodyPart parts until we hit a message
74       * 
75       * @param p      The starting part.
76       * 
77       * @return The encountered Message or null if no Message parts
78       *         are found.
79       */
80      private Message getMessageFrom(Part p) {
81          while (p != null) {
82              if (p instanceof Message) {
83                  return (Message) p;
84              }
85              Multipart mp = ((BodyPart) p).getParent();
86              if (mp == null) {
87                  return null;
88              }
89              p = mp.getParent();
90          }
91          return null;
92      }
93  }