325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.org.jvnet.mimepull;
325N/A
325N/Aimport java.io.File;
325N/Aimport java.io.InputStream;
325N/Aimport java.nio.ByteBuffer;
325N/Aimport java.util.List;
325N/A
325N/A/**
325N/A * Represents an attachment part in a MIME message. MIME message parsing is done
325N/A * lazily using a pull parser, so the part may not have all the data. {@link #read}
325N/A * and {@link #readOnce} may trigger the actual parsing the message. In fact,
325N/A * parsing of an attachment part may be triggered by calling {@link #read} methods
325N/A * on some other attachemnt parts. All this happens behind the scenes so the
325N/A * application developer need not worry about these details.
325N/A *
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Apublic class MIMEPart {
325N/A
325N/A private volatile InternetHeaders headers;
325N/A private volatile String contentId;
325N/A private String contentType;
325N/A volatile boolean parsed; // part is parsed or not
325N/A final MIMEMessage msg;
325N/A private final DataHead dataHead;
325N/A
325N/A MIMEPart(MIMEMessage msg) {
325N/A this.msg = msg;
325N/A this.dataHead = new DataHead(this);
325N/A }
325N/A
325N/A MIMEPart(MIMEMessage msg, String contentId) {
325N/A this(msg);
325N/A this.contentId = contentId;
325N/A }
325N/A
325N/A /**
325N/A * Can get the attachment part's content multiple times. That means
325N/A * the full content needs to be there in memory or on the file system.
325N/A * Calling this method would trigger parsing for the part's data. So
325N/A * do not call this unless it is required(otherwise, just wrap MIMEPart
325N/A * into a object that returns InputStream for e.g DataHandler)
325N/A *
325N/A * @return data for the part's content
325N/A */
325N/A public InputStream read() {
325N/A return dataHead.read();
325N/A }
325N/A
325N/A /**
325N/A * Cleans up any resources that are held by this part (for e.g. deletes
325N/A * the temp file that is used to serve this part's content). After
325N/A * calling this, one shouldn't call {@link #read()} or {@link #readOnce()}
325N/A */
325N/A public void close() {
325N/A dataHead.close();
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Can get the attachment part's content only once. The content
325N/A * will be lost after the method. Content data is not be stored
325N/A * on the file system or is not kept in the memory for the
325N/A * following case:
325N/A * - Attachement parts contents are accessed sequentially
325N/A *
325N/A * In general, take advantage of this when the data is used only
325N/A * once.
325N/A *
325N/A * @return data for the part's content
325N/A */
325N/A public InputStream readOnce() {
325N/A return dataHead.readOnce();
325N/A }
325N/A
325N/A public void moveTo(File f) {
325N/A dataHead.moveTo(f);
325N/A }
325N/A
325N/A /**
325N/A * Returns Content-ID MIME header for this attachment part
325N/A *
325N/A * @return Content-ID of the part
325N/A */
325N/A public String getContentId() {
325N/A if (contentId == null) {
325N/A getHeaders();
325N/A }
325N/A return contentId;
325N/A }
325N/A
325N/A /**
325N/A * Returns Content-Type MIME header for this attachment part
325N/A *
325N/A * @return Content-Type of the part
325N/A */
325N/A public String getContentType() {
325N/A if (contentType == null) {
325N/A getHeaders();
325N/A }
325N/A return contentType;
325N/A }
325N/A
325N/A private void getHeaders() {
325N/A // Trigger parsing for the part headers
325N/A while(headers == null) {
325N/A if (!msg.makeProgress()) {
325N/A if (headers == null) {
325N/A throw new IllegalStateException("Internal Error. Didn't get Headers even after complete parsing.");
325N/A }
325N/A }
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Return all the values for the specified header.
325N/A * Returns <code>null</code> if no headers with the
325N/A * specified name exist.
325N/A *
325N/A * @param name header name
325N/A * @return list of header values, or null if none
325N/A */
325N/A public List<String> getHeader(String name) {
325N/A getHeaders();
325N/A assert headers != null;
325N/A return headers.getHeader(name);
325N/A }
325N/A
325N/A /**
325N/A * Return all the headers
325N/A *
325N/A * @return list of Header objects
325N/A */
325N/A public List<? extends Header> getAllHeaders() {
325N/A getHeaders();
325N/A assert headers != null;
325N/A return headers.getAllHeaders();
325N/A }
325N/A
325N/A /**
325N/A * Callback to set headers
325N/A *
325N/A * @param headers MIME headers for the part
325N/A */
325N/A void setHeaders(InternetHeaders headers) {
325N/A this.headers = headers;
325N/A List<String> ct = getHeader("Content-Type");
325N/A this.contentType = (ct == null) ? "application/octet-stream" : ct.get(0);
325N/A }
325N/A
325N/A /**
325N/A * Callback to notify that there is a partial content for the part
325N/A *
325N/A * @param buf content data for the part
325N/A */
325N/A void addBody(ByteBuffer buf) {
325N/A dataHead.addBody(buf);
325N/A }
325N/A
325N/A /**
325N/A * Callback to indicate that parsing is done for this part
325N/A * (no more update events for this part)
325N/A */
325N/A void doneParsing() {
325N/A parsed = true;
325N/A dataHead.doneParsing();
325N/A }
325N/A
325N/A /**
325N/A * Callback to set Content-ID for this part
325N/A * @param cid Content-ID of the part
325N/A */
325N/A void setContentId(String cid) {
325N/A this.contentId = cid;
325N/A }
325N/A
325N/A @Override
325N/A public String toString() {
325N/A return "Part="+contentId;
325N/A }
325N/A
325N/A}