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 org.apache.james.mime4j.message;
21  
22  import org.apache.james.mime4j.MimeException;
23  import org.apache.james.mime4j.codec.Base64InputStream;
24  import org.apache.james.mime4j.codec.QuotedPrintableInputStream;
25  import org.apache.james.mime4j.descriptor.BodyDescriptor;
26  import org.apache.james.mime4j.field.AbstractField;
27  import org.apache.james.mime4j.parser.AbstractContentHandler;
28  import org.apache.james.mime4j.parser.Field;
29  import org.apache.james.mime4j.util.MimeUtil;
30  
31  import java.io.InputStream;
32  import java.io.IOException;
33  
34  /**
35   * Abstract implementation of ContentHandler that automates common
36   * tasks. Currently performs header parsing and applies content-transfer
37   * decoding to body parts.
38   *
39   * 
40   */
41  public abstract class SimpleContentHandler extends  AbstractContentHandler {
42  
43      /**
44       * Called after headers are parsed.
45       */
46      public abstract void headers(Header header);
47  
48      /**
49       * Called when the body of a discrete (non-multipart) entity is encountered.
50  
51       * @param bd encapsulates the values (either read from the
52       *        message stream or, if not present, determined implictly
53       *        as described in the
54       *        MIME rfc:s) of the <code>Content-Type</code> and
55       *        <code>Content-Transfer-Encoding</code> header fields.
56       * @param is the contents of the body. Base64 or quoted-printable
57       *        decoding will be applied transparently.
58       * @throws IOException should be thrown on I/O errors.
59       */
60      public abstract void bodyDecoded(BodyDescriptor bd, InputStream is) throws IOException;
61  
62  
63      /* Implement introduced callbacks. */
64  
65      private Header currHeader;
66  
67      /**
68       * @see org.apache.james.mime4j.parser.AbstractContentHandler#startHeader()
69       */
70      @Override
71      public final void startHeader() {
72          currHeader = new Header();
73      }
74  
75      /**
76       * @see org.apache.james.mime4j.parser.AbstractContentHandler#field(Field)
77       */
78      @Override
79      public final void field(Field field) throws MimeException {
80          Field parsedField = AbstractField.parse(field.getRaw()); 
81          currHeader.addField(parsedField);
82      }
83  
84      /**
85       * @see org.apache.james.mime4j.parser.AbstractContentHandler#endHeader()
86       */
87      @Override
88      public final void endHeader() {
89          Header tmp = currHeader;
90          currHeader = null;
91          headers(tmp);
92      }
93  
94      /**
95       * @see org.apache.james.mime4j.parser.AbstractContentHandler#body(org.apache.james.mime4j.descriptor.BodyDescriptor, java.io.InputStream)
96       */
97      @Override
98      public final void body(BodyDescriptor bd, InputStream is) throws IOException {
99          if (MimeUtil.isBase64Encoding(bd.getTransferEncoding())) {
100             bodyDecoded(bd, new Base64InputStream(is));
101         }
102         else if (MimeUtil.isQuotedPrintableEncoded(bd.getTransferEncoding())) {
103             bodyDecoded(bd, new QuotedPrintableInputStream(is));
104         }
105         else {
106             bodyDecoded(bd, is);
107         }
108     }
109 }