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.mailet.base.mail;
021    
022    import java.io.BufferedReader;
023    import java.io.BufferedWriter;
024    import java.io.IOException;
025    import java.io.InputStreamReader;
026    import java.io.OutputStream;
027    import java.io.OutputStreamWriter;
028    import java.io.Reader;
029    import java.io.StringWriter;
030    import java.io.UnsupportedEncodingException;
031    import java.io.Writer;
032    
033    import javax.activation.ActivationDataFlavor;
034    import javax.activation.DataSource;
035    import javax.mail.MessagingException;
036    import javax.mail.internet.ContentType;
037    import javax.mail.internet.MimeUtility;
038    import javax.mail.internet.ParseException;
039    
040    /**
041     * <p>Data Content Handler for...</p>
042     * <dl>
043     * <dt>MIME type name</dt><dd>message</dd>
044     * <dt>MIME subtype name</dt><dd>disposition-notification</dd>
045     * </dl>
046     */
047    public class message_disposition_notification
048            extends
049                AbstractDataContentHandler
050    {
051    
052        /**
053         * Default Constructor.
054         */
055        public message_disposition_notification()
056        {
057            super();
058        }
059    
060        /**
061         * @see org.apache.mailet.base.mail.AbstractDataContentHandler#computeDataFlavor()
062         */
063        protected ActivationDataFlavor computeDataFlavor()
064        {
065            return new ActivationDataFlavor(String.class,
066                    "message/disposition-notification", "Message String");
067        }
068    
069        /**
070         * @see org.apache.mailet.base.mail.AbstractDataContentHandler#computeContent(javax.activation.DataSource)
071         */
072        protected Object computeContent(DataSource aDataSource)
073                throws MessagingException
074        {
075            String encoding = getCharacterSet(aDataSource.getContentType());
076            Reader reader = null;
077            Writer writer = new StringWriter(2048);
078            String content = null;
079            try
080            {
081                reader = new BufferedReader(new InputStreamReader(aDataSource
082                        .getInputStream(), encoding), 2048);
083                while (reader.ready())
084                    writer.write(reader.read());
085                writer.flush();
086                content = writer.toString();
087            }
088            catch (IllegalArgumentException e)
089            {
090                throw new MessagingException("Encoding = \"" + encoding + "\"", e);
091            }
092            catch (IOException e)
093            {
094                throw new MessagingException(
095                        "Exception obtaining content from DataSource", e);
096            }
097            finally
098            {
099                try
100                {
101                    writer.close();
102                }
103                catch (IOException e1)
104                {
105                    // No-op
106                }
107                try
108                {
109                    if (reader != null) {
110                        reader.close();
111                    }
112                }
113                catch (IOException e1)
114                {
115                    // No-op
116                }
117            }
118            return content;
119        }
120    
121        /**
122         * @see javax.activation.DataContentHandler#writeTo(java.lang.Object,
123         *      java.lang.String, java.io.OutputStream)
124         */
125        public void writeTo(Object aPart, String aMimeType, OutputStream aStream)
126                throws IOException
127        {
128            if (!(aPart instanceof String))
129                throw new IOException("Type \"" + aPart.getClass().getName()
130                        + "\" is not supported.");
131    
132            String encoding = getCharacterSet(getDataFlavor().getMimeType());
133            Writer writer;
134            try
135            {
136                writer = new BufferedWriter(new OutputStreamWriter(aStream,
137                        encoding), 2048);
138            }
139            catch (IllegalArgumentException e)
140            {
141                throw new UnsupportedEncodingException(encoding);
142            }
143            writer.write((String) aPart);
144            writer.flush();
145        }
146    
147        protected String getCharacterSet(String aType)
148        {
149            String characterSet = null;
150            try
151            {
152                characterSet = new ContentType(aType).getParameter("charset");
153            }
154            catch (ParseException e)
155            {
156                // no-op
157            }
158            finally
159            {
160                if (null == characterSet)
161                    characterSet = "us-ascii";
162            }
163            return MimeUtility.javaCharset(characterSet);
164        }
165    
166    }