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