src/share/jaxws_classes/com/sun/xml/internal/ws/encoding/MimeMultipartParser.java

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, 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.ws.encoding;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.istack.internal.Nullable;
ohair@286 30 import com.sun.xml.internal.ws.api.message.Attachment;
ohair@286 31 import com.sun.xml.internal.ws.api.message.AttachmentEx;
ohair@286 32 import com.sun.xml.internal.ws.developer.StreamingAttachmentFeature;
ohair@286 33 import com.sun.xml.internal.ws.developer.StreamingDataHandler;
ohair@286 34 import com.sun.xml.internal.ws.util.ByteArrayBuffer;
ohair@286 35 import com.sun.xml.internal.ws.util.ByteArrayDataSource;
ohair@286 36
ohair@286 37 import com.sun.xml.internal.org.jvnet.mimepull.Header;
ohair@286 38 import com.sun.xml.internal.org.jvnet.mimepull.MIMEMessage;
ohair@286 39 import com.sun.xml.internal.org.jvnet.mimepull.MIMEPart;
ohair@286 40
ohair@286 41 import javax.activation.DataHandler;
ohair@286 42 import javax.xml.soap.SOAPException;
ohair@286 43 import javax.xml.soap.SOAPMessage;
ohair@286 44 import javax.xml.transform.Source;
ohair@286 45 import javax.xml.transform.stream.StreamSource;
ohair@286 46 import javax.xml.ws.WebServiceException;
ohair@286 47 import java.io.ByteArrayInputStream;
ohair@286 48 import java.io.IOException;
ohair@286 49 import java.io.InputStream;
ohair@286 50 import java.io.OutputStream;
ohair@286 51 import java.util.HashMap;
ohair@286 52 import java.util.Iterator;
ohair@286 53 import java.util.List;
ohair@286 54 import java.util.Map;
alanb@368 55 import java.util.logging.Level;
alanb@368 56 import java.util.logging.Logger;
ohair@286 57
ohair@286 58 /**
ohair@286 59 * Parses Mime multipart message into primary part and attachment parts. It
ohair@286 60 * parses the stream lazily as and when required.
ohair@286 61 *
ohair@286 62 * @author Vivek Pandey
ohair@286 63 * @author Jitendra Kotamraju
ohair@286 64 */
ohair@286 65 public final class MimeMultipartParser {
ohair@286 66
ohair@286 67 private final String start;
ohair@286 68 private final MIMEMessage message;
ohair@286 69 private Attachment root;
alanb@368 70 private ContentTypeImpl contentType;
ohair@286 71
ohair@286 72 // Attachments without root part
ohair@286 73 private final Map<String, Attachment> attachments = new HashMap<String, Attachment>();
ohair@286 74
ohair@286 75 private boolean gotAll;
ohair@286 76
alanb@368 77 public MimeMultipartParser(InputStream in, String cType, StreamingAttachmentFeature feature) {
alanb@368 78 this.contentType = new ContentTypeImpl(cType);
alanb@368 79 // ContentType ct = new ContentType(cType);
alanb@368 80 // String boundary = ct.getParameter("boundary");
alanb@368 81 String boundary = contentType.getBoundary();
ohair@286 82 if (boundary == null || boundary.equals("")) {
ohair@286 83 throw new WebServiceException("MIME boundary parameter not found" + contentType);
ohair@286 84 }
ohair@286 85 message = (feature != null)
ohair@286 86 ? new MIMEMessage(in, boundary, feature.getConfig())
ohair@286 87 : new MIMEMessage(in, boundary);
ohair@286 88 // Strip <...> from root part's Content-ID
alanb@368 89 // String st = ct.getParameter("start");
alanb@368 90 String st = contentType.getRootId();
ohair@286 91 if (st != null && st.length() > 2 && st.charAt(0) == '<' && st.charAt(st.length()-1) == '>') {
ohair@286 92 st = st.substring(1, st.length()-1);
ohair@286 93 }
ohair@286 94 start = st;
ohair@286 95 }
ohair@286 96
ohair@286 97 /**
ohair@286 98 * Parses the stream and returns the root part. If start parameter is
ohair@286 99 * present in Content-Type, it is used to determine the root part, otherwise
ohair@286 100 * root part is the first part.
ohair@286 101 *
ohair@286 102 * @return StreamAttachment for root part
ohair@286 103 * null if root part cannot be found
ohair@286 104 *
ohair@286 105 */
ohair@286 106 public @Nullable Attachment getRootPart() {
ohair@286 107 if (root == null) {
ohair@286 108 root = new PartAttachment((start != null) ? message.getPart(start) : message.getPart(0));
ohair@286 109 }
ohair@286 110 return root;
ohair@286 111 }
ohair@286 112
ohair@286 113 /**
ohair@286 114 * Parses the entire stream and returns all MIME parts except root MIME part.
ohair@286 115 *
ohair@286 116 * @return Map<String, StreamAttachment> for all attachment parts
ohair@286 117 */
ohair@286 118 public @NotNull Map<String, Attachment> getAttachmentParts() {
ohair@286 119 if (!gotAll) {
ohair@286 120 MIMEPart rootPart = (start != null) ? message.getPart(start) : message.getPart(0);
ohair@286 121 List<MIMEPart> parts = message.getAttachments();
ohair@286 122 for(MIMEPart part : parts) {
ohair@286 123 if (part != rootPart) {
alanb@368 124 String cid = part.getContentId();
alanb@368 125 if (!attachments.containsKey(cid)) {
alanb@368 126 PartAttachment attach = new PartAttachment(part);
alanb@368 127 attachments.put(attach.getContentId(), attach);
alanb@368 128 }
ohair@286 129 }
ohair@286 130 }
ohair@286 131 gotAll = true;
ohair@286 132 }
ohair@286 133 return attachments;
ohair@286 134 }
ohair@286 135
ohair@286 136 /**
ohair@286 137 * This method can be called to get a matching MIME attachment part for the
ohair@286 138 * given contentId. It parses the stream until it finds a matching part.
ohair@286 139 *
ohair@286 140 * @return StreamAttachment attachment for contentId
ohair@286 141 * null if there is no attachment for contentId
ohair@286 142 */
ohair@286 143 public @Nullable Attachment getAttachmentPart(String contentId) throws IOException {
ohair@286 144 //first see if this attachment is already parsed, if so return it
ohair@286 145 Attachment attach = attachments.get(contentId);
ohair@286 146 if (attach == null) {
ohair@286 147 MIMEPart part = message.getPart(contentId);
ohair@286 148 attach = new PartAttachment(part);
ohair@286 149 attachments.put(contentId, attach);
ohair@286 150 }
ohair@286 151 return attach;
ohair@286 152 }
ohair@286 153
ohair@286 154 static class PartAttachment implements AttachmentEx {
ohair@286 155
ohair@286 156 final MIMEPart part;
ohair@286 157 byte[] buf;
alanb@368 158 private StreamingDataHandler streamingDataHandler;
ohair@286 159
ohair@286 160 PartAttachment(MIMEPart part) {
ohair@286 161 this.part = part;
ohair@286 162 }
ohair@286 163
alanb@368 164 public @NotNull @Override String getContentId() {
ohair@286 165 return part.getContentId();
ohair@286 166 }
ohair@286 167
alanb@368 168 public @NotNull @Override String getContentType() {
ohair@286 169 return part.getContentType();
ohair@286 170 }
ohair@286 171
alanb@368 172 @Override
ohair@286 173 public byte[] asByteArray() {
ohair@286 174 if (buf == null) {
ohair@286 175 ByteArrayBuffer baf = new ByteArrayBuffer();
ohair@286 176 try {
ohair@286 177 baf.write(part.readOnce());
ohair@286 178 } catch(IOException ioe) {
ohair@286 179 throw new WebServiceException(ioe);
alanb@368 180 } finally {
alanb@368 181 if (baf != null) {
alanb@368 182 try {
alanb@368 183 baf.close();
alanb@368 184 } catch (IOException ex) {
alanb@368 185 Logger.getLogger(MimeMultipartParser.class.getName()).log(Level.FINE, null, ex);
alanb@368 186 }
alanb@368 187 }
ohair@286 188 }
ohair@286 189 buf = baf.toByteArray();
ohair@286 190 }
ohair@286 191 return buf;
ohair@286 192 }
ohair@286 193
alanb@368 194 @Override
ohair@286 195 public DataHandler asDataHandler() {
alanb@368 196 if (streamingDataHandler == null) {
alanb@368 197 streamingDataHandler = (buf != null)
alanb@368 198 ? new DataSourceStreamingDataHandler(new ByteArrayDataSource(buf,getContentType()))
alanb@368 199 : new MIMEPartStreamingDataHandler(part);
alanb@368 200 }
alanb@368 201 return streamingDataHandler;
ohair@286 202 }
ohair@286 203
alanb@368 204 @Override
ohair@286 205 public Source asSource() {
ohair@286 206 return (buf != null)
ohair@286 207 ? new StreamSource(new ByteArrayInputStream(buf))
ohair@286 208 : new StreamSource(part.read());
ohair@286 209 }
ohair@286 210
alanb@368 211 @Override
ohair@286 212 public InputStream asInputStream() {
ohair@286 213 return (buf != null)
ohair@286 214 ? new ByteArrayInputStream(buf) : part.read();
ohair@286 215 }
ohair@286 216
alanb@368 217 @Override
ohair@286 218 public void writeTo(OutputStream os) throws IOException {
ohair@286 219 if (buf != null) {
ohair@286 220 os.write(buf);
ohair@286 221 } else {
ohair@286 222 InputStream in = part.read();
ohair@286 223 byte[] temp = new byte[8192];
ohair@286 224 int len;
ohair@286 225 while((len=in.read(temp)) != -1) {
ohair@286 226 os.write(temp, 0, len);
ohair@286 227 }
ohair@286 228 in.close();
ohair@286 229 }
ohair@286 230 }
ohair@286 231
alanb@368 232 @Override
ohair@286 233 public void writeTo(SOAPMessage saaj) throws SOAPException {
ohair@286 234 saaj.createAttachmentPart().setDataHandler(asDataHandler());
ohair@286 235 }
ohair@286 236
ohair@286 237 // AttachmentEx methods begin here
alanb@368 238 @Override
ohair@286 239 public Iterator<MimeHeader> getMimeHeaders() {
ohair@286 240 final Iterator<? extends Header> ih = part.getAllHeaders()
ohair@286 241 .iterator();
ohair@286 242 return new Iterator<MimeHeader>() {
alanb@368 243 @Override
ohair@286 244 public boolean hasNext() {
ohair@286 245 return ih.hasNext();
ohair@286 246 }
ohair@286 247
alanb@368 248 @Override
ohair@286 249 public MimeHeader next() {
ohair@286 250 final Header hdr = ih.next();
ohair@286 251 return new AttachmentEx.MimeHeader() {
alanb@368 252 @Override
ohair@286 253 public String getValue() {
ohair@286 254 return hdr.getValue();
ohair@286 255 }
alanb@368 256 @Override
ohair@286 257 public String getName() {
ohair@286 258 return hdr.getName();
ohair@286 259 }
ohair@286 260 };
ohair@286 261 }
ohair@286 262
alanb@368 263 @Override
ohair@286 264 public void remove() {
ohair@286 265 throw new UnsupportedOperationException();
ohair@286 266 }
ohair@286 267 };
ohair@286 268 }
ohair@286 269 }
ohair@286 270
alanb@368 271 public ContentTypeImpl getContentType() {
alanb@368 272 return contentType;
alanb@368 273 }
alanb@368 274
ohair@286 275 }

mercurial