1 /** 2 * 3 * Copyright 2003-2004 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package javax.mail.internet; 19 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.io.OutputStream; 23 import java.net.UnknownServiceException; 24 import javax.activation.DataSource; 25 import javax.mail.MessageAware; 26 import javax.mail.MessageContext; 27 import javax.mail.MessagingException; 28 29 /** 30 * @version $Rev: 149344 $ $Date: 2005-01-31 17:07:44 -0800 (Mon, 31 Jan 2005) $ 31 */ 32 public class MimePartDataSource implements DataSource, MessageAware { 33 private final MimePart part; 34 35 public MimePartDataSource(MimePart part) { 36 this.part = part; 37 } 38 39 public InputStream getInputStream() throws IOException { 40 try { 41 InputStream stream; 42 if (part instanceof MimeMessage) { 43 stream = ((MimeMessage) part).getContentStream(); 44 } else if (part instanceof MimeBodyPart) { 45 stream = ((MimeBodyPart) part).getContentStream(); 46 } else { 47 throw new MessagingException("Unknown part"); 48 } 49 String encoding = part.getEncoding(); 50 return encoding == null ? stream : MimeUtility.decode(stream, encoding); 51 } catch (MessagingException e) { 52 throw (IOException) new IOException(e.getMessage()).initCause(e); 53 } 54 } 55 56 public OutputStream getOutputStream() throws IOException { 57 throw new UnknownServiceException(); 58 } 59 60 public String getContentType() { 61 try { 62 return part.getContentType(); 63 } catch (MessagingException e) { 64 return null; 65 } 66 } 67 68 public String getName() { 69 return ""; 70 } 71 72 public synchronized MessageContext getMessageContext() { 73 return new MessageContext(part); 74 } 75 }