001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.javamail.handlers;
018    
019    import java.awt.datatransfer.DataFlavor;
020    import java.awt.datatransfer.UnsupportedFlavorException;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.InputStreamReader;
024    import java.io.OutputStream;
025    import java.io.OutputStreamWriter;
026    import java.io.Reader;
027    import java.io.UnsupportedEncodingException;
028    
029    import javax.activation.DataContentHandler;
030    import javax.activation.DataSource;
031    
032    import javax.mail.internet.ContentType; 
033    import javax.mail.internet.MimeUtility;
034    
035    /**
036     * @version $Rev: 669902 $ $Date: 2008-06-20 10:04:41 -0400 (Fri, 20 Jun 2008) $
037     */
038    public class AbstractTextHandler implements DataContentHandler {
039        private final DataFlavor flavour;
040    
041        public AbstractTextHandler(DataFlavor flavour) {
042            this.flavour = flavour;
043        }
044    
045        public DataFlavor[] getTransferDataFlavors() {
046            return new DataFlavor[] {flavour};
047        }
048    
049        public Object getTransferData(DataFlavor dataFlavor, DataSource dataSource) throws UnsupportedFlavorException, IOException {
050            return flavour.equals(dataFlavor) ? getContent(dataSource) : null;
051        }
052    
053        /**
054         * Read the content from the DataSource and transform 
055         * it into a text object (String). 
056         * 
057         * @param ds     The source DataSource.
058         * 
059         * @return The content object. 
060         * @exception IOException
061         */
062        public Object getContent(DataSource ds) throws IOException {
063            InputStream is = ds.getInputStream(); 
064            InputStreamReader reader;
065            // process any encoding to make sure the chars get transformed into the 
066            // correct byte types. 
067            try {
068                String charset = getCharSet(ds.getContentType());
069                reader = new InputStreamReader(is, charset);
070            } catch (Exception ex) {
071                throw new UnsupportedEncodingException(ex.toString());
072            }
073            StringBuffer result = new StringBuffer(1024);
074            char[] buffer = new char[32768];
075            int count;
076            while ((count = reader.read(buffer)) > 0) {
077                result.append(buffer, 0, count);
078            }
079            return result.toString();
080        }
081    
082        
083        /**
084         * Write an object of "our" type out to the provided 
085         * output stream.  The content type might modify the 
086         * result based on the content type parameters. 
087         * 
088         * @param object The object to write.
089         * @param contentType
090         *               The content mime type, including parameters.
091         * @param outputstream
092         *               The target output stream.
093         * 
094         * @throws IOException
095         */
096        public void writeTo(Object o, String contentType, OutputStream outputstream) throws IOException {
097            String s;
098            if (o instanceof String) {
099                s = (String) o;
100            } else if (o != null) {
101                s = o.toString();
102            } else {
103                return;
104            }
105            // process any encoding to make sure the chars get transformed into the 
106            // correct byte types. 
107            OutputStreamWriter writer;
108            try {
109                String charset = getCharSet(contentType);
110                writer = new OutputStreamWriter(outputstream, charset);
111            } catch (Exception ex) {
112                ex.printStackTrace(); 
113                throw new UnsupportedEncodingException(ex.toString());
114            }
115            writer.write(s);
116            writer.flush();
117        }
118        
119    
120        /**
121         * get the character set from content type
122         * @param contentType
123         * @return
124         * @throws ParseException
125         */
126        protected String getCharSet(String contentType) throws Exception {
127            ContentType type = new ContentType(contentType);
128            String charset = type.getParameter("charset");
129            if (charset == null) {
130                charset = "us-ascii";
131            }
132            return MimeUtility.javaCharset(charset);
133        }
134    }