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