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