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.OutputStream; 036 import java.io.UnsupportedEncodingException; 037 038 public class MessageHandler implements DataContentHandler { 039 /** 040 * Field dataFlavor 041 */ 042 ActivationDataFlavor dataFlavor; 043 044 public MessageHandler(){ 045 dataFlavor = new ActivationDataFlavor(java.lang.String.class, "message/rfc822", "Text"); 046 } 047 048 049 /** 050 * Method getDF 051 * 052 * @return dataflavor 053 */ 054 protected ActivationDataFlavor getDF() { 055 return dataFlavor; 056 } 057 058 /** 059 * Method getTransferDataFlavors 060 * 061 * @return dataflavors 062 */ 063 public DataFlavor[] getTransferDataFlavors() { 064 return (new DataFlavor[]{dataFlavor}); 065 } 066 067 /** 068 * Method getTransferData 069 * 070 * @param dataflavor 071 * @param datasource 072 * @return 073 * @throws IOException 074 */ 075 public Object getTransferData(DataFlavor dataflavor, DataSource datasource) 076 throws IOException { 077 if (getDF().equals(dataflavor)) { 078 return getContent(datasource); 079 } 080 return null; 081 } 082 083 /** 084 * Method getContent 085 * 086 * @param datasource 087 * @return 088 * @throws IOException 089 */ 090 public Object getContent(DataSource datasource) throws IOException { 091 092 try { 093 // if this is a proper message, it implements the MessageAware interface. We need this to 094 // get the associated session. 095 if (datasource instanceof MessageAware) { 096 MessageContext context = ((MessageAware)datasource).getMessageContext(); 097 // construct a mime message instance from the stream, associating it with the 098 // data source session. 099 return new MimeMessage(context.getSession(), datasource.getInputStream()); 100 } 101 } catch (MessagingException e) { 102 // we need to transform any exceptions into an IOException. 103 throw new IOException("Exception writing MimeMultipart: " + e.toString()); 104 } 105 return null; 106 } 107 108 /** 109 * Method writeTo 110 * 111 * @param object 112 * @param s 113 * @param outputstream 114 * @throws IOException 115 */ 116 public void writeTo(Object object, String s, OutputStream outputstream) throws IOException { 117 // proper message type? 118 if (object instanceof Message) { 119 try { 120 ((Message)object).writeTo(outputstream); 121 } catch (MessagingException e) { 122 throw new IOException("Error parsing message: " + e.toString()); 123 } 124 } 125 } 126 } 127