src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MIMEPart.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.org.jvnet.mimepull;
ohair@286 27
ohair@286 28 import java.io.File;
ohair@286 29 import java.io.InputStream;
ohair@286 30 import java.nio.ByteBuffer;
ohair@286 31 import java.util.List;
alanb@368 32 import java.util.logging.Level;
alanb@368 33 import java.util.logging.Logger;
ohair@286 34
ohair@286 35 /**
ohair@286 36 * Represents an attachment part in a MIME message. MIME message parsing is done
ohair@286 37 * lazily using a pull parser, so the part may not have all the data. {@link #read}
ohair@286 38 * and {@link #readOnce} may trigger the actual parsing the message. In fact,
ohair@286 39 * parsing of an attachment part may be triggered by calling {@link #read} methods
alanb@368 40 * on some other attachment parts. All this happens behind the scenes so the
ohair@286 41 * application developer need not worry about these details.
ohair@286 42 *
alanb@368 43 * @author Jitendra Kotamraju, Martin Grebac
ohair@286 44 */
ohair@286 45 public class MIMEPart {
ohair@286 46
alanb@368 47 private static final Logger LOGGER = Logger.getLogger(MIMEPart.class.getName());
alanb@368 48
ohair@286 49 private volatile InternetHeaders headers;
ohair@286 50 private volatile String contentId;
ohair@286 51 private String contentType;
alanb@368 52 private String contentTransferEncoding;
alanb@368 53
ohair@286 54 volatile boolean parsed; // part is parsed or not
ohair@286 55 final MIMEMessage msg;
ohair@286 56 private final DataHead dataHead;
ohair@286 57
ohair@286 58 MIMEPart(MIMEMessage msg) {
ohair@286 59 this.msg = msg;
ohair@286 60 this.dataHead = new DataHead(this);
ohair@286 61 }
ohair@286 62
ohair@286 63 MIMEPart(MIMEMessage msg, String contentId) {
ohair@286 64 this(msg);
ohair@286 65 this.contentId = contentId;
ohair@286 66 }
ohair@286 67
ohair@286 68 /**
ohair@286 69 * Can get the attachment part's content multiple times. That means
ohair@286 70 * the full content needs to be there in memory or on the file system.
ohair@286 71 * Calling this method would trigger parsing for the part's data. So
ohair@286 72 * do not call this unless it is required(otherwise, just wrap MIMEPart
ohair@286 73 * into a object that returns InputStream for e.g DataHandler)
ohair@286 74 *
ohair@286 75 * @return data for the part's content
ohair@286 76 */
ohair@286 77 public InputStream read() {
alanb@368 78 InputStream is = null;
alanb@368 79 try {
alanb@368 80 is = MimeUtility.decode(dataHead.read(), contentTransferEncoding);
alanb@368 81 } catch (DecodingException ex) { //ignore
alanb@368 82 if (LOGGER.isLoggable(Level.WARNING)) {
alanb@368 83 LOGGER.log(Level.WARNING, null, ex);
alanb@368 84 }
alanb@368 85 }
alanb@368 86 return is;
ohair@286 87 }
ohair@286 88
ohair@286 89 /**
ohair@286 90 * Cleans up any resources that are held by this part (for e.g. deletes
ohair@286 91 * the temp file that is used to serve this part's content). After
ohair@286 92 * calling this, one shouldn't call {@link #read()} or {@link #readOnce()}
ohair@286 93 */
ohair@286 94 public void close() {
ohair@286 95 dataHead.close();
ohair@286 96 }
ohair@286 97
ohair@286 98 /**
ohair@286 99 * Can get the attachment part's content only once. The content
ohair@286 100 * will be lost after the method. Content data is not be stored
ohair@286 101 * on the file system or is not kept in the memory for the
ohair@286 102 * following case:
ohair@286 103 * - Attachement parts contents are accessed sequentially
ohair@286 104 *
ohair@286 105 * In general, take advantage of this when the data is used only
ohair@286 106 * once.
ohair@286 107 *
ohair@286 108 * @return data for the part's content
ohair@286 109 */
ohair@286 110 public InputStream readOnce() {
alanb@368 111 InputStream is = null;
alanb@368 112 try {
alanb@368 113 is = MimeUtility.decode(dataHead.readOnce(), contentTransferEncoding);
alanb@368 114 } catch (DecodingException ex) { //ignore
alanb@368 115 if (LOGGER.isLoggable(Level.WARNING)) {
alanb@368 116 LOGGER.log(Level.WARNING, null, ex);
alanb@368 117 }
alanb@368 118 }
alanb@368 119 return is;
ohair@286 120 }
ohair@286 121
ohair@286 122 public void moveTo(File f) {
ohair@286 123 dataHead.moveTo(f);
ohair@286 124 }
ohair@286 125
ohair@286 126 /**
ohair@286 127 * Returns Content-ID MIME header for this attachment part
ohair@286 128 *
ohair@286 129 * @return Content-ID of the part
ohair@286 130 */
ohair@286 131 public String getContentId() {
ohair@286 132 if (contentId == null) {
ohair@286 133 getHeaders();
ohair@286 134 }
ohair@286 135 return contentId;
ohair@286 136 }
ohair@286 137
ohair@286 138 /**
alanb@368 139 * Returns Content-Transfer-Encoding MIME header for this attachment part
alanb@368 140 *
alanb@368 141 * @return Content-Transfer-Encoding of the part
alanb@368 142 */
alanb@368 143 public String getContentTransferEncoding() {
alanb@368 144 if (contentTransferEncoding == null) {
alanb@368 145 getHeaders();
alanb@368 146 }
alanb@368 147 return contentTransferEncoding;
alanb@368 148 }
alanb@368 149
alanb@368 150 /**
ohair@286 151 * Returns Content-Type MIME header for this attachment part
ohair@286 152 *
ohair@286 153 * @return Content-Type of the part
ohair@286 154 */
ohair@286 155 public String getContentType() {
ohair@286 156 if (contentType == null) {
ohair@286 157 getHeaders();
ohair@286 158 }
ohair@286 159 return contentType;
ohair@286 160 }
ohair@286 161
ohair@286 162 private void getHeaders() {
ohair@286 163 // Trigger parsing for the part headers
ohair@286 164 while(headers == null) {
ohair@286 165 if (!msg.makeProgress()) {
ohair@286 166 if (headers == null) {
ohair@286 167 throw new IllegalStateException("Internal Error. Didn't get Headers even after complete parsing.");
ohair@286 168 }
ohair@286 169 }
ohair@286 170 }
ohair@286 171 }
ohair@286 172
ohair@286 173 /**
ohair@286 174 * Return all the values for the specified header.
ohair@286 175 * Returns <code>null</code> if no headers with the
ohair@286 176 * specified name exist.
ohair@286 177 *
ohair@286 178 * @param name header name
ohair@286 179 * @return list of header values, or null if none
ohair@286 180 */
ohair@286 181 public List<String> getHeader(String name) {
ohair@286 182 getHeaders();
ohair@286 183 assert headers != null;
ohair@286 184 return headers.getHeader(name);
ohair@286 185 }
ohair@286 186
ohair@286 187 /**
ohair@286 188 * Return all the headers
ohair@286 189 *
ohair@286 190 * @return list of Header objects
ohair@286 191 */
ohair@286 192 public List<? extends Header> getAllHeaders() {
ohair@286 193 getHeaders();
ohair@286 194 assert headers != null;
ohair@286 195 return headers.getAllHeaders();
ohair@286 196 }
ohair@286 197
ohair@286 198 /**
ohair@286 199 * Callback to set headers
ohair@286 200 *
ohair@286 201 * @param headers MIME headers for the part
ohair@286 202 */
ohair@286 203 void setHeaders(InternetHeaders headers) {
ohair@286 204 this.headers = headers;
ohair@286 205 List<String> ct = getHeader("Content-Type");
ohair@286 206 this.contentType = (ct == null) ? "application/octet-stream" : ct.get(0);
alanb@368 207 List<String> cte = getHeader("Content-Transfer-Encoding");
alanb@368 208 this.contentTransferEncoding = (cte == null) ? "binary" : cte.get(0);
ohair@286 209 }
ohair@286 210
ohair@286 211 /**
ohair@286 212 * Callback to notify that there is a partial content for the part
ohair@286 213 *
ohair@286 214 * @param buf content data for the part
ohair@286 215 */
ohair@286 216 void addBody(ByteBuffer buf) {
ohair@286 217 dataHead.addBody(buf);
ohair@286 218 }
ohair@286 219
ohair@286 220 /**
ohair@286 221 * Callback to indicate that parsing is done for this part
ohair@286 222 * (no more update events for this part)
ohair@286 223 */
ohair@286 224 void doneParsing() {
ohair@286 225 parsed = true;
ohair@286 226 dataHead.doneParsing();
ohair@286 227 }
ohair@286 228
ohair@286 229 /**
ohair@286 230 * Callback to set Content-ID for this part
ohair@286 231 * @param cid Content-ID of the part
ohair@286 232 */
ohair@286 233 void setContentId(String cid) {
ohair@286 234 this.contentId = cid;
ohair@286 235 }
ohair@286 236
alanb@368 237 /**
alanb@368 238 * Callback to set Content-Transfer-Encoding for this part
alanb@368 239 * @param cte Content-Transfer-Encoding of the part
alanb@368 240 */
alanb@368 241 void setContentTransferEncoding(String cte) {
alanb@368 242 this.contentTransferEncoding = cte;
alanb@368 243 }
alanb@368 244
ohair@286 245 @Override
ohair@286 246 public String toString() {
alanb@368 247 return "Part="+contentId+":"+contentTransferEncoding;
ohair@286 248 }
ohair@286 249
ohair@286 250 }

mercurial