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 javax.mail.internet; 021 022 import java.io.IOException; 023 import java.io.InputStream; 024 import java.io.OutputStream; 025 import java.net.UnknownServiceException; 026 import javax.activation.DataSource; 027 import javax.mail.MessageAware; 028 import javax.mail.MessageContext; 029 import javax.mail.MessagingException; 030 031 /** 032 * @version $Rev: 689126 $ $Date: 2008-08-26 12:17:53 -0400 (Tue, 26 Aug 2008) $ 033 */ 034 public class MimePartDataSource implements DataSource, MessageAware { 035 // the part that provides the data form this data source. 036 protected MimePart part; 037 038 public MimePartDataSource(MimePart part) { 039 this.part = part; 040 } 041 042 public InputStream getInputStream() throws IOException { 043 try { 044 InputStream stream; 045 if (part instanceof MimeMessage) { 046 // this never gets encoded, so we can skip other steps 047 return ((MimeMessage) part).getContentStream(); 048 } else if (part instanceof MimeBodyPart) { 049 stream = ((MimeBodyPart) part).getContentStream(); 050 } else { 051 throw new MessagingException("Unknown part"); 052 } 053 return checkPartEncoding(part, stream); 054 } catch (MessagingException e) { 055 throw (IOException) new IOException(e.getMessage()).initCause(e); 056 } 057 } 058 059 060 /** 061 * For a given part, decide it the data stream requires 062 * wrappering with a stream for decoding a particular 063 * encoding. 064 * 065 * @param part The part we're extracting. 066 * @param stream The raw input stream for the part. 067 * 068 * @return An input stream configured for reading the 069 * source part and decoding it into raw bytes. 070 */ 071 private InputStream checkPartEncoding(MimePart part, InputStream stream) throws MessagingException { 072 String encoding = part.getEncoding(); 073 // if nothing is specified, there's nothing to do 074 if (encoding == null) { 075 return stream; 076 } 077 // now screen out the ones that never need decoding 078 encoding = encoding.toLowerCase(); 079 if (encoding.equals("7bit") || encoding.equals("8bit") || encoding.equals("binary")) { 080 return stream; 081 } 082 // now we need to check the content type to prevent 083 // MultiPart types from getting decoded, since the part is just an envelope around other 084 // parts 085 String contentType = part.getContentType(); 086 if (contentType != null) { 087 try { 088 ContentType type = new ContentType(contentType); 089 // no decoding done here 090 if (type.match("multipart/*")) { 091 return stream; 092 } 093 } catch (ParseException e) { 094 // ignored....bad content type means we handle as a normal part 095 } 096 } 097 // ok, wrap this is a decoding stream if required 098 return MimeUtility.decode(stream, encoding); 099 } 100 101 102 public OutputStream getOutputStream() throws IOException { 103 throw new UnknownServiceException(); 104 } 105 106 public String getContentType() { 107 try { 108 return part.getContentType(); 109 } catch (MessagingException e) { 110 return null; 111 } 112 } 113 114 public String getName() { 115 return ""; 116 } 117 118 public synchronized MessageContext getMessageContext() { 119 return new MessageContext(part); 120 } 121 }