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: 689126 $ $Date: 2008-08-26 12:17:53 -0400 (Tue, 26 Aug 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                  // this never gets encoded, so we can skip other steps 
47                  return ((MimeMessage) part).getContentStream();
48              } else if (part instanceof MimeBodyPart) {
49                  stream = ((MimeBodyPart) part).getContentStream();
50              } else {
51                  throw new MessagingException("Unknown part");
52              }
53              return checkPartEncoding(part, stream);
54          } catch (MessagingException e) {
55              throw (IOException) new IOException(e.getMessage()).initCause(e);
56          }
57      }
58      
59      
60      /**
61       * For a given part, decide it the data stream requires
62       * wrappering with a stream for decoding a particular 
63       * encoding. 
64       * 
65       * @param part   The part we're extracting.
66       * @param stream The raw input stream for the part.
67       * 
68       * @return An input stream configured for reading the 
69       *         source part and decoding it into raw bytes.
70       */
71      private InputStream checkPartEncoding(MimePart part, InputStream stream) throws MessagingException {
72          String encoding = part.getEncoding();
73          // if nothing is specified, there's nothing to do 
74          if (encoding == null) {
75              return stream; 
76          }
77          // now screen out the ones that never need decoding 
78          encoding = encoding.toLowerCase(); 
79          if (encoding.equals("7bit") || encoding.equals("8bit") || encoding.equals("binary")) {
80              return stream; 
81          }
82          // now we need to check the content type to prevent 
83          // MultiPart types from getting decoded, since the part is just an envelope around other 
84          // parts 
85          String contentType = part.getContentType(); 
86          if (contentType != null) {
87              try {
88                  ContentType type = new ContentType(contentType); 
89                  // no decoding done here 
90                  if (type.match("multipart/*")) {
91                      return stream; 
92                  }
93              } catch (ParseException e) {
94                  // ignored....bad content type means we handle as a normal part 
95              }
96          }
97          // ok, wrap this is a decoding stream if required 
98          return MimeUtility.decode(stream, encoding);
99      }
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 }