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.message;
214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.IOException;
234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.OutputStream;
244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.james.mime4j.field.ContentTransferEncodingField;
264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.james.mime4j.field.ContentTypeField;
274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.james.mime4j.field.Field;
284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/**
304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * MIME entity. An entity has a header and a body (see RFC 2045).
314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Id: Entity.java,v 1.3 2004/10/02 12:41:11 ntherning Exp $
344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic abstract class Entity {
364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private Header header = null;
374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private Body body = null;
384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private Entity parent = null;
394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Gets the parent entity of this entity.
424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Returns <code>null</code> if this is the root entity.
434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the parent or <code>null</code>.
454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public Entity getParent() {
474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return parent;
484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Sets the parent entity of this entity.
524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param parent the parent entity or <code>null</code> if
544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *        this will be the root entity.
554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public void setParent(Entity parent) {
574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.parent = parent;
584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Gets the entity header.
624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the header.
644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public Header getHeader() {
664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return header;
674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Sets the entity header.
714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param header the header.
734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public void setHeader(Header header) {
754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.header = header;
764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Gets the body of this entity.
804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the body,
824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public Body getBody() {
844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return body;
854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Sets the body of this entity.
894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param body the body.
914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public void setBody(Body body) {
934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.body = body;
944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        body.setParent(this);
954fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
964fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
974fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
984fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Determines the MIME type of this <code>Entity</code>. The MIME type
994fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * is derived by looking at the parent's Content-Type field if no
1004fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Content-Type field is set for this <code>Entity</code>.
1014fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1024fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the MIME type.
1034fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1044fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public String getMimeType() {
1054fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        ContentTypeField child =
1064fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            (ContentTypeField) getHeader().getField(Field.CONTENT_TYPE);
1074fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        ContentTypeField parent = getParent() != null
1084fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            ? (ContentTypeField) getParent().getHeader().
1094fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy                                                getField(Field.CONTENT_TYPE)
1104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            : null;
1114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return ContentTypeField.getMimeType(child, parent);
1134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Determines the MIME character set encoding of this <code>Entity</code>.
1174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the MIME character set encoding.
1194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public String getCharset() {
1214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return ContentTypeField.getCharset(
1224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            (ContentTypeField) getHeader().getField(Field.CONTENT_TYPE));
1234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Determines the transfer encoding of this <code>Entity</code>.
1274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the transfer encoding.
1294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public String getContentTransferEncoding() {
1314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        ContentTransferEncodingField f = (ContentTransferEncodingField)
1324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy                        getHeader().getField(Field.CONTENT_TRANSFER_ENCODING);
1334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return ContentTransferEncodingField.getEncoding(f);
1354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Determines if the MIME type of this <code>Entity</code> matches the
1394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * given one. MIME types are case-insensitive.
1404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param type the MIME type to match against.
1424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return <code>true</code> on match, <code>false</code> otherwise.
1434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public boolean isMimeType(String type) {
1454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return getMimeType().equalsIgnoreCase(type);
1464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Determines if the MIME type of this <code>Entity</code> is
1504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * <code>multipart/*</code>. Since multipart-entities must have
1514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * a boundary parameter in the <code>Content-Type</code> field this
1524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * method returns <code>false</code> if no boundary exists.
1534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return <code>true</code> on match, <code>false</code> otherwise.
1554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public boolean isMultipart() {
1574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        ContentTypeField f =
1584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            (ContentTypeField) getHeader().getField(Field.CONTENT_TYPE);
1594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return f != null && f.getBoundary() != null
1604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            && getMimeType().startsWith(ContentTypeField.TYPE_MULTIPART_PREFIX);
1614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Write the content to the given outputstream
1654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param out the outputstream to write to
1674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IOException
1684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public abstract void writeTo(OutputStream out) throws IOException;
1704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy}
171