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 * Method writeTo
122 *
123 * @param object
124 * @param s
125 * @param outputstream
126 * @throws IOException
127 */
128 public void writeTo(Object object, String s, OutputStream outputstream)
129 throws IOException {
130 OutputStreamWriter os;
131 try {
132 String charset = getCharSet(s);
133 os = new OutputStreamWriter(outputstream, charset);
134 } catch (Exception ex) {
135 throw new UnsupportedEncodingException(ex.toString());
136 }
137 String content = (String) object;
138 os.write(content, 0, content.length());
139 os.flush();
140 }
141
142 /**
143 * get the character set from content type
144 * @param contentType
145 * @return
146 * @throws ParseException
147 */
148 protected String getCharSet(String contentType) throws ParseException {
149 ContentType type = new ContentType(contentType);
150 String charset = type.getParameter("charset");
151 if (charset == null) {
152 charset = "us-ascii";
153 }
154 return MimeUtility.javaCharset(charset);
155 }
156 }