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 java.awt.datatransfer.DataFlavor; 026 import java.io.IOException; 027 import java.io.OutputStream; 028 029 import javax.mail.internet.MimeMultipart; 030 import javax.mail.internet.MimeMessage; 031 import javax.mail.MessagingException; 032 033 public class MultipartHandler implements DataContentHandler { 034 /** 035 * Field dataFlavor 036 */ 037 ActivationDataFlavor dataFlavor; 038 039 public MultipartHandler(){ 040 dataFlavor = new ActivationDataFlavor(javax.mail.internet.MimeMultipart.class, "multipart/mixed", "Multipart"); 041 } 042 043 /** 044 * Constructor TextHandler 045 * 046 * @param dataFlavor 047 */ 048 public MultipartHandler(ActivationDataFlavor dataFlavor) { 049 this.dataFlavor = dataFlavor; 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 try { 095 return new MimeMultipart(datasource); 096 } catch (MessagingException e) { 097 // if there is a syntax error from the datasource parsing, the content is 098 // just null. 099 return null; 100 } 101 } 102 103 /** 104 * Method writeTo 105 * 106 * @param object 107 * @param s 108 * @param outputstream 109 * @throws IOException 110 */ 111 public void writeTo(Object object, String s, OutputStream outputstream) throws IOException { 112 // if this object is a MimeMultipart, then delegate to the part. 113 if (object instanceof MimeMultipart) { 114 try { 115 ((MimeMultipart)object).writeTo(outputstream); 116 } catch (MessagingException e) { 117 // we need to transform any exceptions into an IOException. 118 throw new IOException("Exception writing MimeMultipart: " + e.toString()); 119 } 120 } 121 } 122 }