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: 702432 $ $Date: 2008-10-07 07:18:08 -0400 (Tue, 07 Oct 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 stream = ((MimeMessage) part).getContentStream(); 047 } else if (part instanceof MimeBodyPart) { 048 stream = ((MimeBodyPart) part).getContentStream(); 049 } else { 050 throw new MessagingException("Unknown part"); 051 } 052 return checkPartEncoding(part, stream); 053 } catch (MessagingException e) { 054 throw (IOException) new IOException(e.getMessage()).initCause(e); 055 } 056 } 057 058 059 /** 060 * For a given part, decide it the data stream requires 061 * wrappering with a stream for decoding a particular 062 * encoding. 063 * 064 * @param part The part we're extracting. 065 * @param stream The raw input stream for the part. 066 * 067 * @return An input stream configured for reading the 068 * source part and decoding it into raw bytes. 069 */ 070 private InputStream checkPartEncoding(MimePart part, InputStream stream) throws MessagingException { 071 String encoding = part.getEncoding(); 072 // if nothing is specified, there's nothing to do 073 if (encoding == null) { 074 return stream; 075 } 076 // now screen out the ones that never need decoding 077 encoding = encoding.toLowerCase(); 078 if (encoding.equals("7bit") || encoding.equals("8bit") || encoding.equals("binary")) { 079 return stream; 080 } 081 // now we need to check the content type to prevent 082 // MultiPart types from getting decoded, since the part is just an envelope around other 083 // parts 084 String contentType = part.getContentType(); 085 if (contentType != null) { 086 try { 087 ContentType type = new ContentType(contentType); 088 // no decoding done here 089 if (type.match("multipart/*")) { 090 return stream; 091 } 092 } catch (ParseException e) { 093 // ignored....bad content type means we handle as a normal part 094 } 095 } 096 // ok, wrap this is a decoding stream if required 097 return MimeUtility.decode(stream, encoding); 098 } 099 100 101 public OutputStream getOutputStream() throws IOException { 102 throw new UnknownServiceException(); 103 } 104 105 public String getContentType() { 106 try { 107 return part.getContentType(); 108 } catch (MessagingException e) { 109 return null; 110 } 111 } 112 113 public String getName() { 114 return ""; 115 } 116 117 public synchronized MessageContext getMessageContext() { 118 return new MessageContext(part); 119 } 120 }