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.xml.internal.ws.api.SOAPVersion; |
aoqi@0 | 29 | import com.sun.xml.internal.ws.api.WSFeatureList; |
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.api.message.Message; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.api.message.Packet; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.api.pipe.Codec; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.api.pipe.ContentType; |
aoqi@0 | 36 | import com.sun.xml.internal.ws.developer.StreamingAttachmentFeature; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import java.io.IOException; |
aoqi@0 | 39 | import java.io.InputStream; |
aoqi@0 | 40 | import java.io.OutputStream; |
aoqi@0 | 41 | import java.nio.channels.ReadableByteChannel; |
aoqi@0 | 42 | import java.util.Iterator; |
aoqi@0 | 43 | import java.util.UUID; |
aoqi@0 | 44 | |
aoqi@0 | 45 | /** |
aoqi@0 | 46 | * {@link Codec}s that uses the MIME multipart as the underlying format. |
aoqi@0 | 47 | * |
aoqi@0 | 48 | * <p> |
aoqi@0 | 49 | * When the runtime needs to dynamically choose a {@link Codec}, and |
aoqi@0 | 50 | * when there are more than one {@link Codec}s that use MIME multipart, |
aoqi@0 | 51 | * it is often impossible to determine the right {@link Codec} unless |
aoqi@0 | 52 | * you parse the multipart message to some extent. |
aoqi@0 | 53 | * |
aoqi@0 | 54 | * <p> |
aoqi@0 | 55 | * By having all such {@link Codec}s extending from this class, |
aoqi@0 | 56 | * the "sniffer" can decode a multipart message partially, and then |
aoqi@0 | 57 | * pass the partial parse result to the ultimately-responsible {@link Codec}. |
aoqi@0 | 58 | * This improves the performance. |
aoqi@0 | 59 | * |
aoqi@0 | 60 | * @author Kohsuke Kawaguchi |
aoqi@0 | 61 | */ |
aoqi@0 | 62 | abstract class MimeCodec implements Codec { |
aoqi@0 | 63 | |
aoqi@0 | 64 | public static final String MULTIPART_RELATED_MIME_TYPE = "multipart/related"; |
aoqi@0 | 65 | |
aoqi@0 | 66 | protected Codec mimeRootCodec; |
aoqi@0 | 67 | protected final SOAPVersion version; |
aoqi@0 | 68 | protected final WSFeatureList features; |
aoqi@0 | 69 | |
aoqi@0 | 70 | protected MimeCodec(SOAPVersion version, WSFeatureList f) { |
aoqi@0 | 71 | this.version = version; |
aoqi@0 | 72 | this.features = f; |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | public String getMimeType() { |
aoqi@0 | 76 | return MULTIPART_RELATED_MIME_TYPE; |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | protected Codec getMimeRootCodec(Packet packet) { |
aoqi@0 | 80 | return mimeRootCodec; |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | // TODO: preencode String literals to byte[] so that they don't have to |
aoqi@0 | 84 | // go through char[]->byte[] conversion at runtime. |
aoqi@0 | 85 | public ContentType encode(Packet packet, OutputStream out) throws IOException { |
aoqi@0 | 86 | Message msg = packet.getMessage(); |
aoqi@0 | 87 | if (msg == null) { |
aoqi@0 | 88 | return null; |
aoqi@0 | 89 | } |
aoqi@0 | 90 | ContentTypeImpl ctImpl = (ContentTypeImpl)getStaticContentType(packet); |
aoqi@0 | 91 | String boundary = ctImpl.getBoundary(); |
aoqi@0 | 92 | boolean hasAttachments = (boundary != null); |
aoqi@0 | 93 | Codec rootCodec = getMimeRootCodec(packet); |
aoqi@0 | 94 | if (hasAttachments) { |
aoqi@0 | 95 | writeln("--"+boundary, out); |
aoqi@0 | 96 | ContentType ct = rootCodec.getStaticContentType(packet); |
aoqi@0 | 97 | String ctStr = (ct != null) ? ct.getContentType() : rootCodec.getMimeType(); |
aoqi@0 | 98 | writeln("Content-Type: " + ctStr, out); |
aoqi@0 | 99 | writeln(out); |
aoqi@0 | 100 | } |
aoqi@0 | 101 | ContentType primaryCt = rootCodec.encode(packet, out); |
aoqi@0 | 102 | |
aoqi@0 | 103 | if (hasAttachments) { |
aoqi@0 | 104 | writeln(out); |
aoqi@0 | 105 | // Encode all the attchments |
aoqi@0 | 106 | for (Attachment att : msg.getAttachments()) { |
aoqi@0 | 107 | writeln("--"+boundary, out); |
aoqi@0 | 108 | //SAAJ's AttachmentPart.getContentId() returns content id already enclosed with |
aoqi@0 | 109 | //angle brackets. For now put angle bracket only if its not there |
aoqi@0 | 110 | String cid = att.getContentId(); |
aoqi@0 | 111 | if(cid != null && cid.length() >0 && cid.charAt(0) != '<') |
aoqi@0 | 112 | cid = '<' + cid + '>'; |
aoqi@0 | 113 | writeln("Content-Id:" + cid, out); |
aoqi@0 | 114 | writeln("Content-Type: " + att.getContentType(), out); |
aoqi@0 | 115 | writeCustomMimeHeaders(att, out); |
aoqi@0 | 116 | writeln("Content-Transfer-Encoding: binary", out); |
aoqi@0 | 117 | writeln(out); // write \r\n |
aoqi@0 | 118 | att.writeTo(out); |
aoqi@0 | 119 | writeln(out); // write \r\n |
aoqi@0 | 120 | } |
aoqi@0 | 121 | writeAsAscii("--"+boundary, out); |
aoqi@0 | 122 | writeAsAscii("--", out); |
aoqi@0 | 123 | } |
aoqi@0 | 124 | // TODO not returing correct multipart/related type(no boundary) |
aoqi@0 | 125 | return hasAttachments ? ctImpl : primaryCt; |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | private void writeCustomMimeHeaders(Attachment att, OutputStream out) throws IOException { |
aoqi@0 | 129 | if (att instanceof AttachmentEx) { |
aoqi@0 | 130 | Iterator<AttachmentEx.MimeHeader> allMimeHeaders = ((AttachmentEx) att).getMimeHeaders(); |
aoqi@0 | 131 | while (allMimeHeaders.hasNext()) { |
aoqi@0 | 132 | AttachmentEx.MimeHeader mh = allMimeHeaders.next(); |
aoqi@0 | 133 | String name = mh.getName(); |
aoqi@0 | 134 | |
aoqi@0 | 135 | if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Id".equalsIgnoreCase(name)) { |
aoqi@0 | 136 | writeln(name +": " + mh.getValue(), out); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | } |
aoqi@0 | 139 | } |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | public ContentType getStaticContentType(Packet packet) { |
aoqi@0 | 143 | ContentType ct = (ContentType) packet.getInternalContentType(); |
aoqi@0 | 144 | if ( ct != null ) return ct; |
aoqi@0 | 145 | Message msg = packet.getMessage(); |
aoqi@0 | 146 | boolean hasAttachments = !msg.getAttachments().isEmpty(); |
aoqi@0 | 147 | Codec rootCodec = getMimeRootCodec(packet); |
aoqi@0 | 148 | |
aoqi@0 | 149 | if (hasAttachments) { |
aoqi@0 | 150 | String boundary = "uuid:" + UUID.randomUUID().toString(); |
aoqi@0 | 151 | String boundaryParameter = "boundary=\"" + boundary + "\""; |
aoqi@0 | 152 | // TODO use primaryEncoder to get type |
aoqi@0 | 153 | String messageContentType = MULTIPART_RELATED_MIME_TYPE + |
aoqi@0 | 154 | "; type=\"" + rootCodec.getMimeType() + "\"; " + |
aoqi@0 | 155 | boundaryParameter; |
aoqi@0 | 156 | ContentTypeImpl impl = new ContentTypeImpl(messageContentType, packet.soapAction, null); |
aoqi@0 | 157 | impl.setBoundary(boundary); |
aoqi@0 | 158 | impl.setBoundaryParameter(boundaryParameter); |
aoqi@0 | 159 | packet.setContentType(impl); |
aoqi@0 | 160 | return impl; |
aoqi@0 | 161 | } else { |
aoqi@0 | 162 | ct = rootCodec.getStaticContentType(packet); |
aoqi@0 | 163 | packet.setContentType(ct); |
aoqi@0 | 164 | return ct; |
aoqi@0 | 165 | } |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | /** |
aoqi@0 | 169 | * Copy constructor. |
aoqi@0 | 170 | */ |
aoqi@0 | 171 | protected MimeCodec(MimeCodec that) { |
aoqi@0 | 172 | this.version = that.version; |
aoqi@0 | 173 | this.features = that.features; |
aoqi@0 | 174 | } |
aoqi@0 | 175 | |
aoqi@0 | 176 | public void decode(InputStream in, String contentType, Packet packet) throws IOException { |
aoqi@0 | 177 | MimeMultipartParser parser = new MimeMultipartParser(in, contentType, features.get(StreamingAttachmentFeature.class)); |
aoqi@0 | 178 | decode(parser,packet); |
aoqi@0 | 179 | } |
aoqi@0 | 180 | |
aoqi@0 | 181 | public void decode(ReadableByteChannel in, String contentType, Packet packet) { |
aoqi@0 | 182 | throw new UnsupportedOperationException(); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | /** |
aoqi@0 | 186 | * Parses a {@link Packet} from a {@link MimeMultipartParser}. |
aoqi@0 | 187 | */ |
aoqi@0 | 188 | protected abstract void decode(MimeMultipartParser mpp, Packet packet) throws IOException; |
aoqi@0 | 189 | |
aoqi@0 | 190 | public abstract MimeCodec copy(); |
aoqi@0 | 191 | |
aoqi@0 | 192 | |
aoqi@0 | 193 | public static void writeln(String s,OutputStream out) throws IOException { |
aoqi@0 | 194 | writeAsAscii(s,out); |
aoqi@0 | 195 | writeln(out); |
aoqi@0 | 196 | } |
aoqi@0 | 197 | |
aoqi@0 | 198 | /** |
aoqi@0 | 199 | * Writes a string as ASCII string. |
aoqi@0 | 200 | */ |
aoqi@0 | 201 | public static void writeAsAscii(String s,OutputStream out) throws IOException { |
aoqi@0 | 202 | int len = s.length(); |
aoqi@0 | 203 | for( int i=0; i<len; i++ ) |
aoqi@0 | 204 | out.write((byte)s.charAt(i)); |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | public static void writeln(OutputStream out) throws IOException { |
aoqi@0 | 208 | out.write('\r'); |
aoqi@0 | 209 | out.write('\n'); |
aoqi@0 | 210 | } |
aoqi@0 | 211 | } |