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 java.awt.datatransfer.DataFlavor;
023    import java.io.IOException;
024    import java.io.InputStreamReader;
025    import java.io.OutputStream;
026    import java.io.OutputStreamWriter;
027    import java.io.StringWriter;
028    import java.io.UnsupportedEncodingException;
029    
030    import javax.activation.ActivationDataFlavor;
031    import javax.activation.DataContentHandler;
032    import javax.activation.DataSource;
033    import javax.mail.internet.ContentType;
034    import javax.mail.internet.MimeUtility;
035    import javax.mail.internet.ParseException;
036    
037    /**
038     * @version $Rev: 467553 $ $Date: 2006-10-25 06:01:51 +0200 (Mi, 25. Okt 2006) $
039     */
040    public class TextHandler implements DataContentHandler {
041        /**
042         * Field dataFlavor
043         */
044        ActivationDataFlavor dataFlavor;
045    
046        public TextHandler(){
047            dataFlavor = new ActivationDataFlavor(java.lang.String.class, "text/plain", "Text String");
048        }
049    
050        /**
051         * Constructor TextHandler
052         *
053         * @param dataFlavor
054         */
055        public TextHandler(ActivationDataFlavor dataFlavor) {
056            this.dataFlavor = dataFlavor;
057        }
058    
059        /**
060         * Method getDF
061         *
062         * @return dataflavor
063         */
064        protected ActivationDataFlavor getDF() {
065            return dataFlavor;
066        }
067    
068        /**
069         * Method getTransferDataFlavors
070         *
071         * @return dataflavors
072         */
073        public DataFlavor[] getTransferDataFlavors() {
074            return (new DataFlavor[]{dataFlavor});
075        }
076    
077        /**
078         * Method getTransferData
079         *
080         * @param dataflavor
081         * @param datasource
082         * @return
083         * @throws IOException
084         */
085        public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
086                throws IOException {
087            if (getDF().equals(dataflavor)) {
088                return getContent(datasource);
089            }
090            return null;
091        }
092    
093        /**
094         * Method getContent
095         *
096         * @param datasource
097         * @return
098         * @throws IOException
099         */
100        public Object getContent(DataSource datasource) throws IOException {
101            InputStreamReader reader;
102            try {
103                String s = getCharSet(datasource.getContentType());
104                reader = new InputStreamReader(datasource.getInputStream(), s);
105            } catch (Exception ex) {
106                throw new UnsupportedEncodingException(ex.toString());
107            }
108            StringWriter writer = new StringWriter();
109            int ch;
110            while ((ch = reader.read()) != -1) {
111                writer.write(ch);
112            }
113            return writer.getBuffer().toString();
114        }
115    
116        /**
117         * Method writeTo
118         *
119         * @param object
120         * @param s
121         * @param outputstream
122         * @throws IOException
123         */
124        public void writeTo(Object object, String s, OutputStream outputstream)
125                throws IOException {
126            OutputStreamWriter os;
127            try {
128                String charset = getCharSet(s);
129                os = new OutputStreamWriter(outputstream, charset);
130            } catch (Exception ex) {
131                throw new UnsupportedEncodingException(ex.toString());
132            }
133            String content = (String) object;
134            os.write(content, 0, content.length());
135            os.flush();
136        }
137    
138        /**
139         * get the character set from content type
140         * @param contentType
141         * @return
142         * @throws ParseException
143         */
144        protected String getCharSet(String contentType) throws ParseException {
145            ContentType type = new ContentType(contentType);
146            String charset = type.getParameter("charset");
147            if (charset == null) {
148                charset = "us-ascii";
149            }
150            return MimeUtility.javaCharset(charset);
151        }
152    }