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