14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/****************************************************************
24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one   *
34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * or more contributor license agreements.  See the NOTICE file *
44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed with this work for additional information        *
54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * regarding copyright ownership.  The ASF licenses this file   *
64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * to you under the Apache License, Version 2.0 (the            *
74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * "License"); you may not use this file except in compliance   *
84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * with the License.  You may obtain a copy of the License at   *
94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *                                                              *
104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *   http://www.apache.org/licenses/LICENSE-2.0                 *
114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *                                                              *
124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing,   *
134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * software distributed under the License is distributed on an  *
144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * KIND, either express or implied.  See the License for the    *
164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * specific language governing permissions and limitations      *
174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * under the License.                                           *
184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy ****************************************************************/
194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.james.mime4j;
214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.james.mime4j.decoder.Base64InputStream;
234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.james.mime4j.decoder.QuotedPrintableInputStream;
244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.james.mime4j.field.Field;
254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.james.mime4j.message.Header;
264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.InputStream;
284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.IOException;
294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/**
314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Abstract implementation of ContentHandler that automates common
324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * tasks. Currently performs header parsing and applies content-transfer
334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * decoding to body parts.
344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic abstract class SimpleContentHandler extends  AbstractContentHandler {
384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Called after headers are parsed.
414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public abstract void headers(Header header);
434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Called when the body of a discrete (non-multipart) entity is encountered.
464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param bd encapsulates the values (either read from the
484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *        message stream or, if not present, determined implictly
494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *        as described in the
504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *        MIME rfc:s) of the <code>Content-Type</code> and
514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *        <code>Content-Transfer-Encoding</code> header fields.
524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param is the contents of the body. Base64 or quoted-printable
534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *        decoding will be applied transparently.
544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IOException should be thrown on I/O errors.
554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public abstract void bodyDecoded(BodyDescriptor bd, InputStream is) throws IOException;
574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /* Implement introduced callbacks. */
604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private Header currHeader;
624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @see org.apache.james.mime4j.AbstractContentHandler#startHeader()
654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public final void startHeader() {
674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        currHeader = new Header();
684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @see org.apache.james.mime4j.AbstractContentHandler#field(java.lang.String)
724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public final void field(String fieldData) {
744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        currHeader.addField(Field.parse(fieldData));
754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @see org.apache.james.mime4j.AbstractContentHandler#endHeader()
794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public final void endHeader() {
814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        Header tmp = currHeader;
824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        currHeader = null;
834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        headers(tmp);
844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @see org.apache.james.mime4j.AbstractContentHandler#body(org.apache.james.mime4j.BodyDescriptor, java.io.InputStream)
884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public final void body(BodyDescriptor bd, InputStream is) throws IOException {
904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (bd.isBase64Encoded()) {
914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            bodyDecoded(bd, new Base64InputStream(is));
924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        else if (bd.isQuotedPrintableEncoded()) {
944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            bodyDecoded(bd, new QuotedPrintableInputStream(is));
954fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
964fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        else {
974fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            bodyDecoded(bd, is);
984fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
994fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1004fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy}
101