Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.encoding; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.message.Attachment; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.message.AttachmentEx; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.developer.StreamingAttachmentFeature; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.developer.StreamingDataHandler; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.util.ByteArrayBuffer; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.util.ByteArrayDataSource; |
aoqi@0 | 36 | |
aoqi@0 | 37 | import com.sun.xml.internal.org.jvnet.mimepull.Header; |
aoqi@0 | 38 | import com.sun.xml.internal.org.jvnet.mimepull.MIMEMessage; |
aoqi@0 | 39 | import com.sun.xml.internal.org.jvnet.mimepull.MIMEPart; |
aoqi@0 | 40 | |
aoqi@0 | 41 | import javax.activation.DataHandler; |
aoqi@0 | 42 | import javax.xml.soap.SOAPException; |
aoqi@0 | 43 | import javax.xml.soap.SOAPMessage; |
aoqi@0 | 44 | import javax.xml.transform.Source; |
aoqi@0 | 45 | import javax.xml.transform.stream.StreamSource; |
aoqi@0 | 46 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 47 | import java.io.ByteArrayInputStream; |
aoqi@0 | 48 | import java.io.IOException; |
aoqi@0 | 49 | import java.io.InputStream; |
aoqi@0 | 50 | import java.io.OutputStream; |
aoqi@0 | 51 | import java.util.HashMap; |
aoqi@0 | 52 | import java.util.Iterator; |
aoqi@0 | 53 | import java.util.List; |
aoqi@0 | 54 | import java.util.Map; |
aoqi@0 | 55 | import java.util.logging.Level; |
aoqi@0 | 56 | import java.util.logging.Logger; |
aoqi@0 | 57 | |
aoqi@0 | 58 | /** |
aoqi@0 | 59 | * Parses Mime multipart message into primary part and attachment parts. It |
aoqi@0 | 60 | * parses the stream lazily as and when required. |
aoqi@0 | 61 | * |
aoqi@0 | 62 | * @author Vivek Pandey |
aoqi@0 | 63 | * @author Jitendra Kotamraju |
aoqi@0 | 64 | */ |
aoqi@0 | 65 | public final class MimeMultipartParser { |
aoqi@0 | 66 | |
aoqi@0 | 67 | private final String start; |
aoqi@0 | 68 | private final MIMEMessage message; |
aoqi@0 | 69 | private Attachment root; |
aoqi@0 | 70 | private ContentTypeImpl contentType; |
aoqi@0 | 71 | |
aoqi@0 | 72 | // Attachments without root part |
aoqi@0 | 73 | private final Map<String, Attachment> attachments = new HashMap<String, Attachment>(); |
aoqi@0 | 74 | |
aoqi@0 | 75 | private boolean gotAll; |
aoqi@0 | 76 | |
aoqi@0 | 77 | public MimeMultipartParser(InputStream in, String cType, StreamingAttachmentFeature feature) { |
aoqi@0 | 78 | this.contentType = new ContentTypeImpl(cType); |
aoqi@0 | 79 | // ContentType ct = new ContentType(cType); |
aoqi@0 | 80 | // String boundary = ct.getParameter("boundary"); |
aoqi@0 | 81 | String boundary = contentType.getBoundary(); |
aoqi@0 | 82 | if (boundary == null || boundary.equals("")) { |
aoqi@0 | 83 | throw new WebServiceException("MIME boundary parameter not found" + contentType); |
aoqi@0 | 84 | } |
aoqi@0 | 85 | message = (feature != null) |
aoqi@0 | 86 | ? new MIMEMessage(in, boundary, feature.getConfig()) |
aoqi@0 | 87 | : new MIMEMessage(in, boundary); |
aoqi@0 | 88 | // Strip <...> from root part's Content-ID |
aoqi@0 | 89 | // String st = ct.getParameter("start"); |
aoqi@0 | 90 | String st = contentType.getRootId(); |
aoqi@0 | 91 | if (st != null && st.length() > 2 && st.charAt(0) == '<' && st.charAt(st.length()-1) == '>') { |
aoqi@0 | 92 | st = st.substring(1, st.length()-1); |
aoqi@0 | 93 | } |
aoqi@0 | 94 | start = st; |
aoqi@0 | 95 | } |
aoqi@0 | 96 | |
aoqi@0 | 97 | /** |
aoqi@0 | 98 | * Parses the stream and returns the root part. If start parameter is |
aoqi@0 | 99 | * present in Content-Type, it is used to determine the root part, otherwise |
aoqi@0 | 100 | * root part is the first part. |
aoqi@0 | 101 | * |
aoqi@0 | 102 | * @return StreamAttachment for root part |
aoqi@0 | 103 | * null if root part cannot be found |
aoqi@0 | 104 | * |
aoqi@0 | 105 | */ |
aoqi@0 | 106 | public @Nullable Attachment getRootPart() { |
aoqi@0 | 107 | if (root == null) { |
aoqi@0 | 108 | root = new PartAttachment((start != null) ? message.getPart(start) : message.getPart(0)); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | return root; |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | /** |
aoqi@0 | 114 | * Parses the entire stream and returns all MIME parts except root MIME part. |
aoqi@0 | 115 | * |
aoqi@0 | 116 | * @return Map<String, StreamAttachment> for all attachment parts |
aoqi@0 | 117 | */ |
aoqi@0 | 118 | public @NotNull Map<String, Attachment> getAttachmentParts() { |
aoqi@0 | 119 | if (!gotAll) { |
aoqi@0 | 120 | MIMEPart rootPart = (start != null) ? message.getPart(start) : message.getPart(0); |
aoqi@0 | 121 | List<MIMEPart> parts = message.getAttachments(); |
aoqi@0 | 122 | for(MIMEPart part : parts) { |
aoqi@0 | 123 | if (part != rootPart) { |
aoqi@0 | 124 | String cid = part.getContentId(); |
aoqi@0 | 125 | if (!attachments.containsKey(cid)) { |
aoqi@0 | 126 | PartAttachment attach = new PartAttachment(part); |
aoqi@0 | 127 | attachments.put(attach.getContentId(), attach); |
aoqi@0 | 128 | } |
aoqi@0 | 129 | } |
aoqi@0 | 130 | } |
aoqi@0 | 131 | gotAll = true; |
aoqi@0 | 132 | } |
aoqi@0 | 133 | return attachments; |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | /** |
aoqi@0 | 137 | * This method can be called to get a matching MIME attachment part for the |
aoqi@0 | 138 | * given contentId. It parses the stream until it finds a matching part. |
aoqi@0 | 139 | * |
aoqi@0 | 140 | * @return StreamAttachment attachment for contentId |
aoqi@0 | 141 | * null if there is no attachment for contentId |
aoqi@0 | 142 | */ |
aoqi@0 | 143 | public @Nullable Attachment getAttachmentPart(String contentId) throws IOException { |
aoqi@0 | 144 | //first see if this attachment is already parsed, if so return it |
aoqi@0 | 145 | Attachment attach = attachments.get(contentId); |
aoqi@0 | 146 | if (attach == null) { |
aoqi@0 | 147 | MIMEPart part = message.getPart(contentId); |
aoqi@0 | 148 | attach = new PartAttachment(part); |
aoqi@0 | 149 | attachments.put(contentId, attach); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | return attach; |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | static class PartAttachment implements AttachmentEx { |
aoqi@0 | 155 | |
aoqi@0 | 156 | final MIMEPart part; |
aoqi@0 | 157 | byte[] buf; |
aoqi@0 | 158 | private StreamingDataHandler streamingDataHandler; |
aoqi@0 | 159 | |
aoqi@0 | 160 | PartAttachment(MIMEPart part) { |
aoqi@0 | 161 | this.part = part; |
aoqi@0 | 162 | } |
aoqi@0 | 163 | |
aoqi@0 | 164 | public @NotNull @Override String getContentId() { |
aoqi@0 | 165 | return part.getContentId(); |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | public @NotNull @Override String getContentType() { |
aoqi@0 | 169 | return part.getContentType(); |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | @Override |
aoqi@0 | 173 | public byte[] asByteArray() { |
aoqi@0 | 174 | if (buf == null) { |
aoqi@0 | 175 | ByteArrayBuffer baf = new ByteArrayBuffer(); |
aoqi@0 | 176 | try { |
aoqi@0 | 177 | baf.write(part.readOnce()); |
aoqi@0 | 178 | } catch(IOException ioe) { |
aoqi@0 | 179 | throw new WebServiceException(ioe); |
aoqi@0 | 180 | } finally { |
aoqi@0 | 181 | if (baf != null) { |
aoqi@0 | 182 | try { |
aoqi@0 | 183 | baf.close(); |
aoqi@0 | 184 | } catch (IOException ex) { |
aoqi@0 | 185 | Logger.getLogger(MimeMultipartParser.class.getName()).log(Level.FINE, null, ex); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | } |
aoqi@0 | 188 | } |
aoqi@0 | 189 | buf = baf.toByteArray(); |
aoqi@0 | 190 | } |
aoqi@0 | 191 | return buf; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | |
aoqi@0 | 194 | @Override |
aoqi@0 | 195 | public DataHandler asDataHandler() { |
aoqi@0 | 196 | if (streamingDataHandler == null) { |
aoqi@0 | 197 | streamingDataHandler = (buf != null) |
aoqi@0 | 198 | ? new DataSourceStreamingDataHandler(new ByteArrayDataSource(buf,getContentType())) |
aoqi@0 | 199 | : new MIMEPartStreamingDataHandler(part); |
aoqi@0 | 200 | } |
aoqi@0 | 201 | return streamingDataHandler; |
aoqi@0 | 202 | } |
aoqi@0 | 203 | |
aoqi@0 | 204 | @Override |
aoqi@0 | 205 | public Source asSource() { |
aoqi@0 | 206 | return (buf != null) |
aoqi@0 | 207 | ? new StreamSource(new ByteArrayInputStream(buf)) |
aoqi@0 | 208 | : new StreamSource(part.read()); |
aoqi@0 | 209 | } |
aoqi@0 | 210 | |
aoqi@0 | 211 | @Override |
aoqi@0 | 212 | public InputStream asInputStream() { |
aoqi@0 | 213 | return (buf != null) |
aoqi@0 | 214 | ? new ByteArrayInputStream(buf) : part.read(); |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | @Override |
aoqi@0 | 218 | public void writeTo(OutputStream os) throws IOException { |
aoqi@0 | 219 | if (buf != null) { |
aoqi@0 | 220 | os.write(buf); |
aoqi@0 | 221 | } else { |
aoqi@0 | 222 | InputStream in = part.read(); |
aoqi@0 | 223 | byte[] temp = new byte[8192]; |
aoqi@0 | 224 | int len; |
aoqi@0 | 225 | while((len=in.read(temp)) != -1) { |
aoqi@0 | 226 | os.write(temp, 0, len); |
aoqi@0 | 227 | } |
aoqi@0 | 228 | in.close(); |
aoqi@0 | 229 | } |
aoqi@0 | 230 | } |
aoqi@0 | 231 | |
aoqi@0 | 232 | @Override |
aoqi@0 | 233 | public void writeTo(SOAPMessage saaj) throws SOAPException { |
aoqi@0 | 234 | saaj.createAttachmentPart().setDataHandler(asDataHandler()); |
aoqi@0 | 235 | } |
aoqi@0 | 236 | |
aoqi@0 | 237 | // AttachmentEx methods begin here |
aoqi@0 | 238 | @Override |
aoqi@0 | 239 | public Iterator<MimeHeader> getMimeHeaders() { |
aoqi@0 | 240 | final Iterator<? extends Header> ih = part.getAllHeaders() |
aoqi@0 | 241 | .iterator(); |
aoqi@0 | 242 | return new Iterator<MimeHeader>() { |
aoqi@0 | 243 | @Override |
aoqi@0 | 244 | public boolean hasNext() { |
aoqi@0 | 245 | return ih.hasNext(); |
aoqi@0 | 246 | } |
aoqi@0 | 247 | |
aoqi@0 | 248 | @Override |
aoqi@0 | 249 | public MimeHeader next() { |
aoqi@0 | 250 | final Header hdr = ih.next(); |
aoqi@0 | 251 | return new AttachmentEx.MimeHeader() { |
aoqi@0 | 252 | @Override |
aoqi@0 | 253 | public String getValue() { |
aoqi@0 | 254 | return hdr.getValue(); |
aoqi@0 | 255 | } |
aoqi@0 | 256 | @Override |
aoqi@0 | 257 | public String getName() { |
aoqi@0 | 258 | return hdr.getName(); |
aoqi@0 | 259 | } |
aoqi@0 | 260 | }; |
aoqi@0 | 261 | } |
aoqi@0 | 262 | |
aoqi@0 | 263 | @Override |
aoqi@0 | 264 | public void remove() { |
aoqi@0 | 265 | throw new UnsupportedOperationException(); |
aoqi@0 | 266 | } |
aoqi@0 | 267 | }; |
aoqi@0 | 268 | } |
aoqi@0 | 269 | } |
aoqi@0 | 270 | |
aoqi@0 | 271 | public ContentTypeImpl getContentType() { |
aoqi@0 | 272 | return contentType; |
aoqi@0 | 273 | } |
aoqi@0 | 274 | |
aoqi@0 | 275 | } |