001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.geronimo.javamail.store.imap;
019    
020    import javax.activation.DataHandler;
021    
022    import javax.mail.Flags;
023    import javax.mail.MessagingException;
024    import javax.mail.MethodNotSupportedException;
025    
026    import org.apache.geronimo.javamail.store.imap.connection.IMAPEnvelope;
027    import org.apache.geronimo.javamail.store.imap.connection.IMAPBodyStructure;
028    
029    /**
030     * A nested message attachement inside of another 
031     * IMAP message.  This is a less-functional version 
032     * of the top-level message.
033     */
034    public class IMAPAttachedMessage extends IMAPMessage {
035        // the parent enclosing message.
036        protected IMAPMessage parent;
037    
038        /**
039         * Constructor for an attached message part.
040         * 
041         * @param parent   The parent message (outer-most message).
042         * @param section  The section identifier for this embedded part
043         *                 in IMAP section format.  This will identify
044         *                 the part hierarchy used to locate this part within
045         *                 the message.
046         * @param envelope The Envelope that describes this part.
047         * @param bodyStructure
048         *                 The Body structure element that describes this part.
049         */
050        public IMAPAttachedMessage(IMAPMessage parent, String section, IMAPEnvelope envelope, IMAPBodyStructure bodyStructure) {
051            super((IMAPFolder)parent.getFolder(), parent.store, parent.getMessageNumber(), parent.sequenceNumber);
052            this.parent = parent;
053            // sets the subset we're looking for 
054            this.section = section;
055            // the envelope and body structure are loaded from the server by the parent 
056            this.envelope = envelope;
057            this.bodyStructure = bodyStructure;
058        }
059    
060        /**
061         * Check if this message is still valid.  This is 
062         * delegated to the outer-most message.
063         * 
064         * @exception MessagingException
065         */
066        protected void checkValidity() throws MessagingException {
067            parent.checkValidity();
068        }
069    
070        /**
071         * Check if the outer-most message has been expunged.
072         * 
073         * @return true if the message has been expunged.
074         */
075        public boolean isExpunged() {
076            return parent.isExpunged();
077        }
078    
079        /**
080         * Get the size of this message part.
081         * 
082         * @return The estimate size of this message part, in bytes.
083         */
084        public int getSize() {
085            return bodyStructure.bodySize;
086        }
087    
088        
089        /**
090         * Return a copy the flags associated with this message.
091         *
092         * @return a copy of the flags for this message
093         * @throws MessagingException if there was a problem accessing the Store
094         */
095        public Flags getFlags() throws MessagingException {
096            return parent.getFlags(); 
097        }
098    
099    
100        /**
101         * Check whether the supplied flag is set.
102         * The default implementation checks the flags returned by {@link #getFlags()}.
103         *
104         * @param flag the flags to check for
105         * @return true if the flags is set
106         * @throws MessagingException if there was a problem accessing the Store
107         */
108        public boolean isSet(Flags.Flag flag) throws MessagingException {
109            // load the flags, if needed 
110            return parent.isSet(flag); 
111        }
112    
113        /**
114         * Set or clear a flag value.
115         *
116         * @param flags  The set of flags to effect.
117         * @param set    The value to set the flag to (true or false).
118         *
119         * @exception MessagingException
120         */
121        public void setFlags(Flags flag, boolean set) throws MessagingException {
122            throw new MethodNotSupportedException("Flags cannot be set on message attachements"); 
123        }
124    }
125