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    
021    package org.apache.james.protocols.smtp;
022    
023    import java.io.ByteArrayInputStream;
024    import java.io.ByteArrayOutputStream;
025    import java.io.InputStream;
026    import java.io.OutputStream;
027    import java.util.List;
028    
029    import org.apache.mailet.MailAddress;
030    
031    /**
032     * MailEnvelope implementation which stores everything in memory
033     * 
034     *
035     */
036    public class MailEnvelopeImpl implements MailEnvelope{
037    
038        private List<MailAddress> recipients;
039    
040        private MailAddress sender;
041    
042        private ByteArrayOutputStream outputStream;
043    
044        /**
045         * @see org.apache.james.protocols.smtp.MailEnvelope#getSize()
046         */
047        public int getSize() {
048            if (outputStream == null)
049                return -1;
050            return outputStream.size();
051        }
052    
053        /**
054         * @see org.apache.james.protocols.smtp.MailEnvelope#getRecipients()
055         */
056        public List<MailAddress> getRecipients() {
057            return recipients;
058        }
059    
060        /**
061         * @see org.apache.james.protocols.smtp.MailEnvelope#getSender()
062         */
063        public MailAddress getSender() {
064            return sender;
065        }
066    
067        /**
068         * Set the recipients of the mail
069         * 
070         * @param recipientCollection
071         */
072        public void setRecipients(List<MailAddress> recipientCollection) {
073            this.recipients = recipientCollection;
074        }
075    
076        /**
077         * Set the sender of the mail
078         * 
079         * @param sender
080         */
081        public void setSender(MailAddress sender) {
082            this.sender = sender;
083        }
084    
085        /**
086         * @see org.apache.james.protocols.smtp.MailEnvelope#getMessageOutputStream()
087         */
088        public OutputStream getMessageOutputStream() {
089            if (outputStream == null) {
090                this.outputStream = new ByteArrayOutputStream(100000);
091            }
092            return outputStream;
093        }
094    
095        /**
096         * @see org.apache.james.protocols.smtp.MailEnvelope#getMessageInputStream()
097         */
098        public InputStream getMessageInputStream() {
099            return new ByteArrayInputStream(outputStream.toByteArray());
100        }
101    }
102    
103