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: ****************************************************************/package org.apache.james.util.mail.handlers;
019:
020: import java.io.BufferedReader;
021: import java.io.BufferedWriter;
022: import java.io.IOException;
023: import java.io.InputStreamReader;
024: import java.io.OutputStream;
025: import java.io.OutputStreamWriter;
026: import java.io.Reader;
027: import java.io.StringWriter;
028: import java.io.UnsupportedEncodingException;
029: import java.io.Writer;
030:
031: import javax.activation.ActivationDataFlavor;
032: import javax.activation.DataSource;
033: import javax.mail.MessagingException;
034: import javax.mail.internet.ContentType;
035: import javax.mail.internet.MimeUtility;
036: import javax.mail.internet.ParseException;
037:
038: /**
039: * <p>Data Content Handler for...</p>
040: * <dl>
041: * <dt>MIME type name</dt><dd>message</dd>
042: * <dt>MIME subtype name</dt><dd>disposition-notification</dd>
043: * </dl>
044: */
045: public class message_disposition_notification extends
046: AbstractDataContentHandler {
047:
048: /**
049: * Default Constructor.
050: */
051: public message_disposition_notification() {
052: super ();
053: }
054:
055: /**
056: * @see org.apache.james.util.mail.handlers.AbstractDataContentHandler#computeDataFlavor()
057: */
058: protected ActivationDataFlavor computeDataFlavor() {
059: return new ActivationDataFlavor(String.class,
060: "message/disposition-notification", "Message String");
061: }
062:
063: /**
064: * @see org.apache.james.util.mail.handlers.AbstractDataContentHandler#computeContent(javax.activation.DataSource)
065: */
066: protected Object computeContent(DataSource aDataSource)
067: throws MessagingException {
068: String encoding = getCharacterSet(aDataSource.getContentType());
069: Reader reader = null;
070: Writer writer = new StringWriter(2048);
071: String content = null;
072: try {
073: reader = new BufferedReader(new InputStreamReader(
074: aDataSource.getInputStream(), encoding), 2048);
075: while (reader.ready())
076: writer.write(reader.read());
077: writer.flush();
078: content = writer.toString();
079: } catch (IllegalArgumentException e) {
080: throw new MessagingException("Encoding = \"" + encoding
081: + "\"", e);
082: } catch (IOException e) {
083: throw new MessagingException(
084: "Exception obtaining content from DataSource", e);
085: } finally {
086: try {
087: writer.close();
088: } catch (IOException e1) {
089: // No-op
090: }
091: }
092: return content;
093: }
094:
095: /**
096: * @see javax.activation.DataContentHandler#writeTo(java.lang.Object,
097: * java.lang.String, java.io.OutputStream)
098: */
099: public void writeTo(Object aPart, String aMimeType,
100: OutputStream aStream) throws IOException {
101: if (!(aPart instanceof String))
102: throw new IOException("Type \""
103: + aPart.getClass().getName()
104: + "\" is not supported.");
105:
106: String encoding = getCharacterSet(getDataFlavor().getMimeType());
107: Writer writer = null;
108: try {
109: writer = new BufferedWriter(new OutputStreamWriter(aStream,
110: encoding), 2048);
111: } catch (IllegalArgumentException e) {
112: throw new UnsupportedEncodingException(encoding);
113: }
114: writer.write((String) aPart);
115: writer.flush();
116: }
117:
118: protected String getCharacterSet(String aType) {
119: String characterSet = null;
120: try {
121: characterSet = new ContentType(aType)
122: .getParameter("charset");
123: } catch (ParseException e) {
124: // no-op
125: } finally {
126: if (null == characterSet)
127: characterSet = "us-ascii";
128: }
129: return MimeUtility.javaCharset(characterSet);
130: }
131:
132: }
|