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 org.apache.geronimo.mail.handlers;
021    
022    import javax.activation.ActivationDataFlavor;
023    import javax.activation.DataContentHandler;
024    import javax.activation.DataSource;
025    import java.awt.datatransfer.DataFlavor;
026    import java.io.IOException;
027    import java.io.OutputStream;
028    
029    import javax.mail.internet.MimeMultipart;
030    import javax.mail.internet.MimeMessage;
031    import javax.mail.MessagingException;
032    
033    /**
034     * @version $Rev: 467553 $ $Date: 2006-10-25 06:01:51 +0200 (Mi, 25. Okt 2006) $
035     */
036    public class MultipartHandler implements DataContentHandler {
037        /**
038         * Field dataFlavor
039         */
040        ActivationDataFlavor dataFlavor;
041    
042        public MultipartHandler(){
043            dataFlavor = new ActivationDataFlavor(javax.mail.internet.MimeMultipart.class, "multipart/mixed", "Multipart");
044        }
045    
046        /**
047         * Constructor TextHandler
048         *
049         * @param dataFlavor
050         */
051        public MultipartHandler(ActivationDataFlavor dataFlavor) {
052            this.dataFlavor = dataFlavor;
053        }
054    
055        /**
056         * Method getDF
057         *
058         * @return dataflavor
059         */
060        protected ActivationDataFlavor getDF() {
061            return dataFlavor;
062        }
063    
064        /**
065         * Method getTransferDataFlavors
066         *
067         * @return dataflavors
068         */
069        public DataFlavor[] getTransferDataFlavors() {
070            return (new DataFlavor[]{dataFlavor});
071        }
072    
073        /**
074         * Method getTransferData
075         *
076         * @param dataflavor
077         * @param datasource
078         * @return
079         * @throws IOException
080         */
081        public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
082                throws IOException {
083            if (getDF().equals(dataflavor)) {
084                return getContent(datasource);
085            }
086            return null;
087        }
088    
089        /**
090         * Method getContent
091         *
092         * @param datasource
093         * @return
094         * @throws IOException
095         */
096        public Object getContent(DataSource datasource) throws IOException {
097            try {
098                return new MimeMultipart(datasource);
099            } catch (MessagingException e) {
100                // if there is a syntax error from the datasource parsing, the content is
101                // just null.
102                return null;
103            }
104        }
105    
106        /**
107         * Method writeTo
108         *
109         * @param object
110         * @param s
111         * @param outputstream
112         * @throws IOException
113         */
114        public void writeTo(Object object, String s, OutputStream outputstream) throws IOException {
115            // if this object is a MimeMultipart, then delegate to the part.
116            if (object instanceof MimeMultipart) {
117                try {
118                    ((MimeMultipart)object).writeTo(outputstream);
119                } catch (MessagingException e) {
120                    // we need to transform any exceptions into an IOException.
121                    throw new IOException("Exception writing MimeMultipart: " + e.toString());
122                }
123            }
124        }
125    }