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 javax.activation.ActivationDataFlavor; 023 import javax.activation.DataContentHandler; 024 import javax.activation.DataSource; 025 import javax.mail.internet.ContentType; 026 import javax.mail.Message; 027 import javax.mail.MessageAware; 028 import javax.mail.MessageContext; 029 import javax.mail.MessagingException; 030 import javax.mail.internet.MimeMessage; 031 import javax.mail.internet.MimeUtility; 032 import javax.mail.internet.ParseException; 033 import java.awt.datatransfer.DataFlavor; 034 import java.io.IOException; 035 import java.io.InputStreamReader; 036 import java.io.OutputStream; 037 import java.io.OutputStreamWriter; 038 import java.io.StringWriter; 039 import java.io.UnsupportedEncodingException; 040 041 public class MessageHandler implements DataContentHandler { 042 /** 043 * Field dataFlavor 044 */ 045 ActivationDataFlavor dataFlavor; 046 047 public MessageHandler(){ 048 dataFlavor = new ActivationDataFlavor(java.lang.String.class, "message/rfc822", "Text"); 049 } 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 095 try { 096 // if this is a proper message, it implements the MessageAware interface. We need this to 097 // get the associated session. 098 if (datasource instanceof MessageAware) { 099 MessageContext context = ((MessageAware)datasource).getMessageContext(); 100 // construct a mime message instance from the stream, associating it with the 101 // data source session. 102 return new MimeMessage(context.getSession(), datasource.getInputStream()); 103 } 104 } catch (MessagingException e) { 105 // we need to transform any exceptions into an IOException. 106 throw new IOException("Exception writing MimeMultipart: " + e.toString()); 107 } 108 return null; 109 } 110 111 /** 112 * Method writeTo 113 * 114 * @param object 115 * @param s 116 * @param outputstream 117 * @throws IOException 118 */ 119 public void writeTo(Object object, String s, OutputStream outputstream) throws IOException { 120 // proper message type? 121 if (object instanceof Message) { 122 try { 123 ((Message)object).writeTo(outputstream); 124 } catch (MessagingException e) { 125 throw new IOException("Error parsing message: " + e.toString()); 126 } 127 } 128 } 129 } 130