View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package javax.mail.internet;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.net.UnknownServiceException;
26  import javax.activation.DataSource;
27  import javax.mail.MessageAware;
28  import javax.mail.MessageContext;
29  import javax.mail.MessagingException;
30  
31  /**
32   * @version $Rev: 702432 $ $Date: 2008-10-07 07:18:08 -0400 (Tue, 07 Oct 2008) $
33   */
34  public class MimePartDataSource implements DataSource, MessageAware {
35      // the part that provides the data form this data source.
36      protected MimePart part;
37  
38      public MimePartDataSource(MimePart part) {
39          this.part = part;
40      }
41  
42      public InputStream getInputStream() throws IOException {
43          try {
44              InputStream stream;
45              if (part instanceof MimeMessage) {
46                  stream = ((MimeMessage) part).getContentStream();
47              } else if (part instanceof MimeBodyPart) {
48                  stream = ((MimeBodyPart) part).getContentStream();
49              } else {
50                  throw new MessagingException("Unknown part");
51              }
52              return checkPartEncoding(part, stream);
53          } catch (MessagingException e) {
54              throw (IOException) new IOException(e.getMessage()).initCause(e);
55          }
56      }
57      
58      
59      /**
60       * For a given part, decide it the data stream requires
61       * wrappering with a stream for decoding a particular 
62       * encoding. 
63       * 
64       * @param part   The part we're extracting.
65       * @param stream The raw input stream for the part.
66       * 
67       * @return An input stream configured for reading the 
68       *         source part and decoding it into raw bytes.
69       */
70      private InputStream checkPartEncoding(MimePart part, InputStream stream) throws MessagingException {
71          String encoding = part.getEncoding();
72          // if nothing is specified, there's nothing to do 
73          if (encoding == null) {
74              return stream; 
75          }
76          // now screen out the ones that never need decoding 
77          encoding = encoding.toLowerCase(); 
78          if (encoding.equals("7bit") || encoding.equals("8bit") || encoding.equals("binary")) {
79              return stream; 
80          }
81          // now we need to check the content type to prevent 
82          // MultiPart types from getting decoded, since the part is just an envelope around other 
83          // parts 
84          String contentType = part.getContentType(); 
85          if (contentType != null) {
86              try {
87                  ContentType type = new ContentType(contentType); 
88                  // no decoding done here 
89                  if (type.match("multipart/*")) {
90                      return stream; 
91                  }
92              } catch (ParseException e) {
93                  // ignored....bad content type means we handle as a normal part 
94              }
95          }
96          // ok, wrap this is a decoding stream if required 
97          return MimeUtility.decode(stream, encoding);
98      }
99      
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 }