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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/encoding/MimeCodec.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,211 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.encoding;
    1.30 +
    1.31 +import com.sun.xml.internal.ws.api.SOAPVersion;
    1.32 +import com.sun.xml.internal.ws.api.WSFeatureList;
    1.33 +import com.sun.xml.internal.ws.api.message.Attachment;
    1.34 +import com.sun.xml.internal.ws.api.message.AttachmentEx;
    1.35 +import com.sun.xml.internal.ws.api.message.Message;
    1.36 +import com.sun.xml.internal.ws.api.message.Packet;
    1.37 +import com.sun.xml.internal.ws.api.pipe.Codec;
    1.38 +import com.sun.xml.internal.ws.api.pipe.ContentType;
    1.39 +import com.sun.xml.internal.ws.developer.StreamingAttachmentFeature;
    1.40 +
    1.41 +import java.io.IOException;
    1.42 +import java.io.InputStream;
    1.43 +import java.io.OutputStream;
    1.44 +import java.nio.channels.ReadableByteChannel;
    1.45 +import java.util.Iterator;
    1.46 +import java.util.UUID;
    1.47 +
    1.48 +/**
    1.49 + * {@link Codec}s that uses the MIME multipart as the underlying format.
    1.50 + *
    1.51 + * <p>
    1.52 + * When the runtime needs to dynamically choose a {@link Codec}, and
    1.53 + * when there are more than one {@link Codec}s that use MIME multipart,
    1.54 + * it is often impossible to determine the right {@link Codec} unless
    1.55 + * you parse the multipart message to some extent.
    1.56 + *
    1.57 + * <p>
    1.58 + * By having all such {@link Codec}s extending from this class,
    1.59 + * the "sniffer" can decode a multipart message partially, and then
    1.60 + * pass the partial parse result to the ultimately-responsible {@link Codec}.
    1.61 + * This improves the performance.
    1.62 + *
    1.63 + * @author Kohsuke Kawaguchi
    1.64 + */
    1.65 +abstract class MimeCodec implements Codec {
    1.66 +
    1.67 +    public static final String MULTIPART_RELATED_MIME_TYPE = "multipart/related";
    1.68 +
    1.69 +    protected Codec mimeRootCodec;
    1.70 +    protected final SOAPVersion version;
    1.71 +    protected final WSFeatureList features;
    1.72 +
    1.73 +    protected MimeCodec(SOAPVersion version, WSFeatureList f) {
    1.74 +        this.version = version;
    1.75 +        this.features = f;
    1.76 +    }
    1.77 +
    1.78 +    public String getMimeType() {
    1.79 +        return MULTIPART_RELATED_MIME_TYPE;
    1.80 +    }
    1.81 +
    1.82 +    protected Codec getMimeRootCodec(Packet packet) {
    1.83 +        return mimeRootCodec;
    1.84 +    }
    1.85 +
    1.86 +    // TODO: preencode String literals to byte[] so that they don't have to
    1.87 +    // go through char[]->byte[] conversion at runtime.
    1.88 +    public ContentType encode(Packet packet, OutputStream out) throws IOException {
    1.89 +        Message msg = packet.getMessage();
    1.90 +        if (msg == null) {
    1.91 +            return null;
    1.92 +        }
    1.93 +        ContentTypeImpl ctImpl = (ContentTypeImpl)getStaticContentType(packet);
    1.94 +        String boundary = ctImpl.getBoundary();
    1.95 +        boolean hasAttachments = (boundary != null);
    1.96 +        Codec rootCodec = getMimeRootCodec(packet);
    1.97 +        if (hasAttachments) {
    1.98 +            writeln("--"+boundary, out);
    1.99 +            ContentType ct = rootCodec.getStaticContentType(packet);
   1.100 +            String ctStr = (ct != null) ? ct.getContentType() : rootCodec.getMimeType();
   1.101 +            writeln("Content-Type: " + ctStr, out);
   1.102 +            writeln(out);
   1.103 +        }
   1.104 +        ContentType primaryCt = rootCodec.encode(packet, out);
   1.105 +
   1.106 +        if (hasAttachments) {
   1.107 +            writeln(out);
   1.108 +            // Encode all the attchments
   1.109 +            for (Attachment att : msg.getAttachments()) {
   1.110 +                writeln("--"+boundary, out);
   1.111 +                //SAAJ's AttachmentPart.getContentId() returns content id already enclosed with
   1.112 +                //angle brackets. For now put angle bracket only if its not there
   1.113 +                String cid = att.getContentId();
   1.114 +                if(cid != null && cid.length() >0 && cid.charAt(0) != '<')
   1.115 +                    cid = '<' + cid + '>';
   1.116 +                writeln("Content-Id:" + cid, out);
   1.117 +                writeln("Content-Type: " + att.getContentType(), out);
   1.118 +                writeCustomMimeHeaders(att, out);
   1.119 +                writeln("Content-Transfer-Encoding: binary", out);
   1.120 +                writeln(out);                    // write \r\n
   1.121 +                att.writeTo(out);
   1.122 +                writeln(out);                    // write \r\n
   1.123 +            }
   1.124 +            writeAsAscii("--"+boundary, out);
   1.125 +            writeAsAscii("--", out);
   1.126 +        }
   1.127 +        // TODO not returing correct multipart/related type(no boundary)
   1.128 +        return hasAttachments ? ctImpl : primaryCt;
   1.129 +    }
   1.130 +
   1.131 +    private void writeCustomMimeHeaders(Attachment att, OutputStream out) throws IOException {
   1.132 +        if (att instanceof AttachmentEx) {
   1.133 +            Iterator<AttachmentEx.MimeHeader> allMimeHeaders = ((AttachmentEx) att).getMimeHeaders();
   1.134 +            while (allMimeHeaders.hasNext()) {
   1.135 +                AttachmentEx.MimeHeader mh = allMimeHeaders.next();
   1.136 +                String name = mh.getName();
   1.137 +
   1.138 +                if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Id".equalsIgnoreCase(name)) {
   1.139 +                    writeln(name +": " + mh.getValue(), out);
   1.140 +                }
   1.141 +            }
   1.142 +        }
   1.143 +    }
   1.144 +
   1.145 +    public ContentType getStaticContentType(Packet packet) {
   1.146 +        ContentType ct = (ContentType) packet.getInternalContentType();
   1.147 +        if ( ct != null ) return ct;
   1.148 +        Message msg = packet.getMessage();
   1.149 +        boolean hasAttachments = !msg.getAttachments().isEmpty();
   1.150 +        Codec rootCodec = getMimeRootCodec(packet);
   1.151 +
   1.152 +        if (hasAttachments) {
   1.153 +            String boundary = "uuid:" + UUID.randomUUID().toString();
   1.154 +            String boundaryParameter = "boundary=\"" + boundary + "\"";
   1.155 +            // TODO use primaryEncoder to get type
   1.156 +            String messageContentType =  MULTIPART_RELATED_MIME_TYPE +
   1.157 +                    "; type=\"" + rootCodec.getMimeType() + "\"; " +
   1.158 +                    boundaryParameter;
   1.159 +            ContentTypeImpl impl = new ContentTypeImpl(messageContentType, packet.soapAction, null);
   1.160 +            impl.setBoundary(boundary);
   1.161 +            impl.setBoundaryParameter(boundaryParameter);
   1.162 +            packet.setContentType(impl);
   1.163 +            return impl;
   1.164 +        } else {
   1.165 +            ct = rootCodec.getStaticContentType(packet);
   1.166 +            packet.setContentType(ct);
   1.167 +            return ct;
   1.168 +        }
   1.169 +    }
   1.170 +
   1.171 +    /**
   1.172 +     * Copy constructor.
   1.173 +     */
   1.174 +    protected MimeCodec(MimeCodec that) {
   1.175 +        this.version = that.version;
   1.176 +        this.features = that.features;
   1.177 +    }
   1.178 +
   1.179 +    public void decode(InputStream in, String contentType, Packet packet) throws IOException {
   1.180 +        MimeMultipartParser parser = new MimeMultipartParser(in, contentType, features.get(StreamingAttachmentFeature.class));
   1.181 +        decode(parser,packet);
   1.182 +    }
   1.183 +
   1.184 +    public void decode(ReadableByteChannel in, String contentType, Packet packet) {
   1.185 +        throw new UnsupportedOperationException();
   1.186 +    }
   1.187 +
   1.188 +    /**
   1.189 +     * Parses a {@link Packet} from a {@link MimeMultipartParser}.
   1.190 +     */
   1.191 +    protected abstract void decode(MimeMultipartParser mpp, Packet packet) throws IOException;
   1.192 +
   1.193 +    public abstract MimeCodec copy();
   1.194 +
   1.195 +
   1.196 +    public static void writeln(String s,OutputStream out) throws IOException {
   1.197 +        writeAsAscii(s,out);
   1.198 +        writeln(out);
   1.199 +    }
   1.200 +
   1.201 +    /**
   1.202 +     * Writes a string as ASCII string.
   1.203 +     */
   1.204 +    public static void writeAsAscii(String s,OutputStream out) throws IOException {
   1.205 +        int len = s.length();
   1.206 +        for( int i=0; i<len; i++ )
   1.207 +            out.write((byte)s.charAt(i));
   1.208 +    }
   1.209 +
   1.210 +    public static void writeln(OutputStream out) throws IOException {
   1.211 +        out.write('\r');
   1.212 +        out.write('\n');
   1.213 +    }
   1.214 +}

mercurial