001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package javax.mail;
021    
022    /**
023     * The context in which a piece of message content is contained.
024     *
025     * @version $Rev: 578802 $ $Date: 2007-09-24 09:16:44 -0400 (Mon, 24 Sep 2007) $
026     */
027    public class MessageContext {
028        private final Part part;
029    
030        /**
031         * Create a MessageContext object describing the context of the supplied Part.
032         *
033         * @param part the containing part
034         */
035        public MessageContext(Part part) {
036            this.part = part;
037        }
038    
039        /**
040         * Return the {@link Part} that contains the content.
041         *
042         * @return the part
043         */
044        public Part getPart() {
045            return part;
046        }
047    
048        /**
049         * Return the message that contains the content; if the Part is a {@link Multipart}
050         * then recurse up the chain until a {@link Message} is found.
051         *
052         * @return
053         */
054        public Message getMessage() {
055            return getMessageFrom(part);
056        }
057    
058        /**
059         * Return the session associated with the Message containing this Part.
060         *
061         * @return the session associated with this context's root message
062         */
063        public Session getSession() {
064            Message message = getMessage();
065            if (message == null) {
066                return null;
067            } else {
068                return message.session;
069            }
070        }
071    
072        /**
073         * recurse up the chain of MultiPart/BodyPart parts until we hit a message
074         * 
075         * @param p      The starting part.
076         * 
077         * @return The encountered Message or null if no Message parts
078         *         are found.
079         */
080        private Message getMessageFrom(Part p) {
081            while (p != null) {
082                if (p instanceof Message) {
083                    return (Message) p;
084                }
085                Multipart mp = ((BodyPart) p).getParent();
086                if (mp == null) {
087                    return null;
088                }
089                p = mp.getParent();
090            }
091            return null;
092        }
093    }