001    /*
002     * Copyright 2004,2005 The Apache Software Foundation.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.geronimo.mail.handlers;
017    
018    import javax.activation.ActivationDataFlavor;
019    import javax.activation.DataContentHandler;
020    import javax.activation.DataSource;
021    import java.awt.datatransfer.DataFlavor;
022    import java.io.IOException;
023    import java.io.OutputStream;
024    
025    import javax.mail.internet.MimeMultipart;
026    import javax.mail.internet.MimeMessage;
027    import javax.mail.MessagingException;
028    
029    public class MultipartHandler implements DataContentHandler {
030        /**
031         * Field dataFlavor
032         */
033        ActivationDataFlavor dataFlavor;
034    
035        public MultipartHandler(){
036            dataFlavor = new ActivationDataFlavor(javax.mail.internet.MimeMultipart.class, "multipart/mixed", "Multipart");
037        }
038    
039        /**
040         * Constructor TextHandler
041         *
042         * @param dataFlavor
043         */
044        public MultipartHandler(ActivationDataFlavor dataFlavor) {
045            this.dataFlavor = dataFlavor;
046        }
047    
048        /**
049         * Method getDF
050         *
051         * @return dataflavor
052         */
053        protected ActivationDataFlavor getDF() {
054            return dataFlavor;
055        }
056    
057        /**
058         * Method getTransferDataFlavors
059         *
060         * @return dataflavors
061         */
062        public DataFlavor[] getTransferDataFlavors() {
063            return (new DataFlavor[]{dataFlavor});
064        }
065    
066        /**
067         * Method getTransferData
068         *
069         * @param dataflavor
070         * @param datasource
071         * @return
072         * @throws IOException
073         */
074        public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
075                throws IOException {
076            if (getDF().equals(dataflavor)) {
077                return getContent(datasource);
078            }
079            return null;
080        }
081    
082        /**
083         * Method getContent
084         *
085         * @param datasource
086         * @return
087         * @throws IOException
088         */
089        public Object getContent(DataSource datasource) throws IOException {
090            try {
091                return new MimeMultipart(datasource);
092            } catch (MessagingException e) {
093                // if there is a syntax error from the datasource parsing, the content is
094                // just null.
095                return null;
096            }
097        }
098    
099        /**
100         * Method writeTo
101         *
102         * @param object
103         * @param s
104         * @param outputstream
105         * @throws IOException
106         */
107        public void writeTo(Object object, String s, OutputStream outputstream) throws IOException {
108            // if this object is a MimeMultipart, then delegate to the part.
109            if (object instanceof MimeMultipart) {
110                try {
111                    ((MimeMultipart)object).writeTo(outputstream);
112                } catch (MessagingException e) {
113                    // we need to transform any exceptions into an IOException.
114                    throw new IOException("Exception writing MimeMultipart: " + e.toString());
115                }
116            }
117        }
118    }