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 javax.mail.internet.ContentType;
022    import javax.mail.Message;
023    import javax.mail.MessageAware;
024    import javax.mail.MessageContext;
025    import javax.mail.MessagingException;
026    import javax.mail.internet.MimeMessage;
027    import javax.mail.internet.MimeUtility;
028    import javax.mail.internet.ParseException;
029    import java.awt.datatransfer.DataFlavor;
030    import java.io.IOException;
031    import java.io.InputStreamReader;
032    import java.io.OutputStream;
033    import java.io.OutputStreamWriter;
034    import java.io.StringWriter;
035    import java.io.UnsupportedEncodingException;
036    
037    public class MessageHandler implements DataContentHandler {
038        /**
039         * Field dataFlavor
040         */
041        ActivationDataFlavor dataFlavor;
042    
043        public MessageHandler(){
044            dataFlavor = new ActivationDataFlavor(java.lang.String.class, "message/rfc822", "Text");
045        }
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    
091            try {
092                // if this is a proper message, it implements the MessageAware interface.  We need this to
093                // get the associated session.
094                if (datasource instanceof MessageAware) {
095                    MessageContext context = ((MessageAware)datasource).getMessageContext();
096                    // construct a mime message instance from the stream, associating it with the
097                    // data source session.
098                    return new MimeMessage(context.getSession(), datasource.getInputStream());
099                }
100            } catch (MessagingException e) {
101                // we need to transform any exceptions into an IOException.
102                throw new IOException("Exception writing MimeMultipart: " + e.toString());
103            }
104            return null;
105        }
106    
107        /**
108         * Method writeTo
109         *
110         * @param object
111         * @param s
112         * @param outputstream
113         * @throws IOException
114         */
115        public void writeTo(Object object, String s, OutputStream outputstream) throws IOException {
116            // proper message type?
117            if (object instanceof Message) {
118                try {
119                    ((Message)object).writeTo(outputstream);
120                } catch (MessagingException e) {
121                    throw new IOException("Error parsing message: " + e.toString());
122                }
123            }
124        }
125    }
126