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.ByteArrayOutputStream;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.io.InputStreamReader;
027    import java.io.OutputStream;
028    import java.io.OutputStreamWriter;
029    import java.io.StringWriter;
030    import java.io.UnsupportedEncodingException;
031    
032    import javax.activation.ActivationDataFlavor;
033    import javax.activation.DataContentHandler;
034    import javax.activation.DataSource;
035    import javax.mail.internet.ContentType;
036    import javax.mail.internet.MimeUtility;
037    import javax.mail.internet.ParseException;
038    
039    public class TextHandler implements DataContentHandler {
040        /**
041         * Field dataFlavor
042         */
043        ActivationDataFlavor dataFlavor;
044    
045        public TextHandler(){
046            dataFlavor = new ActivationDataFlavor(java.lang.String.class, "text/plain", "Text String");
047        }
048    
049        /**
050         * Constructor TextHandler
051         *
052         * @param dataFlavor
053         */
054        public TextHandler(ActivationDataFlavor dataFlavor) {
055            this.dataFlavor = dataFlavor;
056        }
057    
058        /**
059         * Method getDF
060         *
061         * @return dataflavor
062         */
063        protected ActivationDataFlavor getDF() {
064            return dataFlavor;
065        }
066    
067        /**
068         * Method getTransferDataFlavors
069         *
070         * @return dataflavors
071         */
072        public DataFlavor[] getTransferDataFlavors() {
073            return (new DataFlavor[]{dataFlavor});
074        }
075    
076        /**
077         * Method getTransferData
078         *
079         * @param dataflavor
080         * @param datasource
081         * @return
082         * @throws IOException
083         */
084        public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
085                throws IOException {
086            if (getDF().equals(dataflavor)) {
087                return getContent(datasource);
088            }
089            return null;
090        }
091    
092        /**
093         * Method getContent
094         *
095         * @param datasource
096         * @return
097         * @throws IOException
098         */
099        public Object getContent(DataSource datasource) throws IOException {
100            InputStream is = datasource.getInputStream(); 
101            ByteArrayOutputStream os = new ByteArrayOutputStream(); 
102            
103            int count;  
104            byte[] buffer = new byte[1000]; 
105                
106            try {
107                while ((count = is.read(buffer, 0, buffer.length)) > 0) {
108                    os.write(buffer, 0, count); 
109                }
110            } finally {
111                is.close(); 
112            }
113            try {   
114                return os.toString(getCharSet(datasource.getContentType())); 
115            } catch (ParseException e) {
116                throw new UnsupportedEncodingException(e.getMessage()); 
117            }
118        }
119    
120        
121        /**
122         * Write an object of "our" type out to the provided 
123         * output stream.  The content type might modify the 
124         * result based on the content type parameters. 
125         * 
126         * @param object The object to write.
127         * @param contentType
128         *               The content mime type, including parameters.
129         * @param outputstream
130         *               The target output stream.
131         * 
132         * @throws IOException
133         */
134        public void writeTo(Object object, String contentType, OutputStream outputstream)
135                throws IOException {
136            OutputStreamWriter os;
137            try {
138                String charset = getCharSet(contentType);
139                os = new OutputStreamWriter(outputstream, charset);
140            } catch (Exception ex) {
141                throw new UnsupportedEncodingException(ex.toString());
142            }
143            String content = (String) object;
144            os.write(content, 0, content.length());
145            os.flush();
146        }
147    
148        /**
149         * get the character set from content type
150         * @param contentType
151         * @return
152         * @throws ParseException
153         */
154        protected String getCharSet(String contentType) throws ParseException {
155            ContentType type = new ContentType(contentType);
156            String charset = type.getParameter("charset");
157            if (charset == null) {
158                charset = "us-ascii";
159            }
160            return MimeUtility.javaCharset(charset);
161        }
162    }