001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package org.jvnet.mimepull;
037:
038: import java.io.File;
039: import java.io.InputStream;
040: import java.nio.ByteBuffer;
041: import java.util.List;
042:
043: /**
044: * Represents an attachment part in a MIME message. MIME message parsing is done
045: * lazily using a pull parser, so the part may not have all the data. {@link #read}
046: * and {@link #readOnce} may trigger the actual parsing the message. In fact,
047: * parsing of an attachment part may be triggered by calling {@link #read} methods
048: * on some other attachemnt parts. All this happens behind the scenes so the
049: * application developer need not worry about these details.
050: *
051: * @author Jitendra Kotamraju
052: */
053: public class MIMEPart {
054:
055: private volatile InternetHeaders headers;
056: private volatile String contentId;
057: private String contentType;
058: volatile boolean parsed; // part is parsed or not
059: final MIMEMessage msg;
060: private final DataHead dataHead;
061:
062: MIMEPart(MIMEMessage msg) {
063: this .msg = msg;
064: this .dataHead = new DataHead(this );
065: }
066:
067: MIMEPart(MIMEMessage msg, String contentId) {
068: this (msg);
069: this .contentId = contentId;
070: }
071:
072: /**
073: * Can get the attachment part's content multiple times. That means
074: * the full content needs to be there in memory or on the file system.
075: * Calling this method would trigger parsing for the part's data. So
076: * do not call this unless it is required(otherwise, just wrap MIMEPart
077: * into a object that returns InputStream for e.g DataHandler)
078: *
079: * @return data for the part's content
080: */
081: public InputStream read() {
082: return dataHead.read();
083: }
084:
085: /**
086: * Cleans up any resources that are held by this part (for e.g. deletes
087: * the temp file that is used to serve this part's content). After
088: * calling this, one shouldn't call {@link #read()} or {@link #readOnce()}
089: */
090: public void close() {
091: dataHead.close();
092: }
093:
094: /**
095: * Can get the attachment part's content only once. The content
096: * will be lost after the method. Content data is not be stored
097: * on the file system or is not kept in the memory for the
098: * following case:
099: * - Attachement parts contents are accessed sequentially
100: *
101: * In general, take advantage of this when the data is used only
102: * once.
103: *
104: * @return data for the part's content
105: */
106: public InputStream readOnce() {
107: return dataHead.readOnce();
108: }
109:
110: public void moveTo(File f) {
111: /*
112: if(tail==null) {
113: dataFile = new DataFile(f);
114: tail = head = new Chunk(new FileData(dataFile));
115: } else {
116: if(head==null)
117: throw new IllegalStateException("already read once");
118:
119: if(dataFile!=null) {
120: dataFile.renameTo(f);
121: } else {
122:
123: }
124: }
125: */
126: }
127:
128: /**
129: * Returns Content-ID MIME header for this attachment part
130: *
131: * @return Content-ID of the part
132: */
133: public String getContentId() {
134: if (contentId == null) {
135: getHeaders();
136: }
137: return contentId;
138: }
139:
140: /**
141: * Returns Content-Type MIME header for this attachment part
142: *
143: * @return Content-Type of the part
144: */
145: public String getContentType() {
146: if (contentType == null) {
147: getHeaders();
148: }
149: return contentType;
150: }
151:
152: private void getHeaders() {
153: // Trigger parsing for the part headers
154: while (headers == null) {
155: if (!msg.makeProgress()) {
156: if (headers == null) {
157: throw new IllegalStateException(
158: "Internal Error. Didn't get Headers even after complete parsing.");
159: }
160: }
161: }
162: }
163:
164: /**
165: * Return all the values for the specified header.
166: * Returns <code>null</code> if no headers with the
167: * specified name exist.
168: *
169: * @param name header name
170: * @return list of header values, or null if none
171: */
172: public List<String> getHeader(String name) {
173: getHeaders();
174: assert headers != null;
175: return headers.getHeader(name);
176: }
177:
178: /**
179: * Return all the headers
180: *
181: * @return list of Header objects
182: */
183: public List<? extends Header> getAllHeaders() {
184: getHeaders();
185: assert headers != null;
186: return headers.getAllHeaders();
187: }
188:
189: /**
190: * Callback to set headers
191: *
192: * @param headers MIME headers for the part
193: */
194: void setHeaders(InternetHeaders headers) {
195: this .headers = headers;
196: List<String> ct = getHeader("Content-Type");
197: this .contentType = (ct == null) ? "application/octet-stream"
198: : ct.get(0);
199: }
200:
201: /**
202: * Callback to notify that there is a partial content for the part
203: *
204: * @param buf content data for the part
205: */
206: void addBody(ByteBuffer buf) {
207: dataHead.addBody(buf);
208: }
209:
210: /**
211: * Callback to indicate that parsing is done for this part
212: * (no more update events for this part)
213: */
214: void doneParsing() {
215: parsed = true;
216: dataHead.doneParsing();
217: }
218:
219: /**
220: * Callback to set Content-ID for this part
221: * @param cid Content-ID of the part
222: */
223: void setContentId(String cid) {
224: this .contentId = cid;
225: }
226:
227: @Override
228: public String toString() {
229: return "Part=" + contentId;
230: }
231:
232: }
|