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/fastinfoset/FastInfosetStreamSOAPCodec.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,179 @@ 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.fastinfoset; 1.30 + 1.31 +import com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer; 1.32 +import com.sun.xml.internal.fastinfoset.stax.StAXDocumentParser; 1.33 +import com.sun.xml.internal.ws.api.pipe.Codec; 1.34 +import com.sun.xml.internal.ws.api.pipe.ContentType; 1.35 +import com.sun.xml.internal.ws.api.message.Packet; 1.36 +import com.sun.xml.internal.ws.api.SOAPVersion; 1.37 +import com.sun.xml.internal.ws.api.pipe.StreamSOAPCodec; 1.38 +import com.sun.xml.internal.ws.message.stream.StreamHeader; 1.39 +import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; 1.40 +import com.sun.xml.internal.ws.encoding.ContentTypeImpl; 1.41 + 1.42 +import javax.xml.stream.XMLStreamException; 1.43 +import javax.xml.stream.XMLStreamWriter; 1.44 +import javax.xml.stream.XMLStreamReader; 1.45 +import javax.xml.ws.WebServiceException; 1.46 +import java.io.OutputStream; 1.47 +import java.io.InputStream; 1.48 +import java.io.IOException; 1.49 +import java.nio.channels.WritableByteChannel; 1.50 +import java.nio.channels.ReadableByteChannel; 1.51 + 1.52 +/** 1.53 + * A stream SOAP codec for handling SOAP message infosets to fast 1.54 + * infoset documents. 1.55 + * 1.56 + * <p> 1.57 + * This implementation currently defers to {@link StreamSOAPCodec} for the decoding 1.58 + * using {@link XMLStreamReader}. 1.59 + * 1.60 + * @author Paul Sandoz 1.61 + */ 1.62 +public abstract class FastInfosetStreamSOAPCodec implements Codec { 1.63 + private static final FastInfosetStreamReaderFactory READER_FACTORY = FastInfosetStreamReaderFactory.getInstance(); 1.64 + 1.65 + private StAXDocumentParser _statefulParser; 1.66 + private StAXDocumentSerializer _serializer; 1.67 + 1.68 + private final StreamSOAPCodec _soapCodec; 1.69 + 1.70 + private final boolean _retainState; 1.71 + 1.72 + protected final ContentType _defaultContentType; 1.73 + 1.74 + /* package */ FastInfosetStreamSOAPCodec(StreamSOAPCodec soapCodec, SOAPVersion soapVersion, boolean retainState, String mimeType) { 1.75 +// _soapCodec = StreamSOAPCodec.create(soapVersion); 1.76 + _soapCodec = soapCodec; 1.77 + _retainState = retainState; 1.78 + _defaultContentType = new ContentTypeImpl(mimeType); 1.79 + } 1.80 + 1.81 + /* package */ FastInfosetStreamSOAPCodec(FastInfosetStreamSOAPCodec that) { 1.82 + this._soapCodec = (StreamSOAPCodec) that._soapCodec.copy(); 1.83 + this._retainState = that._retainState; 1.84 + this._defaultContentType = that._defaultContentType; 1.85 + } 1.86 + 1.87 + public String getMimeType() { 1.88 + return _defaultContentType.getContentType(); 1.89 + } 1.90 + 1.91 + public ContentType getStaticContentType(Packet packet) { 1.92 + return getContentType(packet.soapAction); 1.93 + } 1.94 + 1.95 + public ContentType encode(Packet packet, OutputStream out) { 1.96 + if (packet.getMessage() != null) { 1.97 + final XMLStreamWriter writer = getXMLStreamWriter(out); 1.98 + try { 1.99 + packet.getMessage().writeTo(writer); 1.100 + writer.flush(); 1.101 + } catch (XMLStreamException e) { 1.102 + throw new WebServiceException(e); 1.103 + } 1.104 + } 1.105 + return getContentType(packet.soapAction); 1.106 + } 1.107 + 1.108 + public ContentType encode(Packet packet, WritableByteChannel buffer) { 1.109 + //TODO: not yet implemented 1.110 + throw new UnsupportedOperationException(); 1.111 + } 1.112 + 1.113 + public void decode(InputStream in, String contentType, Packet response) throws IOException { 1.114 + response.setMessage( 1.115 + _soapCodec.decode(getXMLStreamReader(in))); 1.116 + } 1.117 + 1.118 + public void decode(ReadableByteChannel in, String contentType, Packet response) { 1.119 + throw new UnsupportedOperationException(); 1.120 + } 1.121 + 1.122 + protected abstract StreamHeader createHeader(XMLStreamReader reader, XMLStreamBuffer mark); 1.123 + 1.124 + protected abstract ContentType getContentType(String soapAction); 1.125 + 1.126 + private XMLStreamWriter getXMLStreamWriter(OutputStream out) { 1.127 + if (_serializer != null) { 1.128 + _serializer.setOutputStream(out); 1.129 + return _serializer; 1.130 + } else { 1.131 + return _serializer = FastInfosetCodec.createNewStreamWriter(out, _retainState); 1.132 + } 1.133 + } 1.134 + 1.135 + private XMLStreamReader getXMLStreamReader(InputStream in) { 1.136 + // If the _retainState is true (FI stateful) then pick up Codec assiciated XMLStreamReader 1.137 + if (_retainState) { 1.138 + if (_statefulParser != null) { 1.139 + _statefulParser.setInputStream(in); 1.140 + return _statefulParser; 1.141 + } else { 1.142 + return _statefulParser = FastInfosetCodec.createNewStreamReader(in, _retainState); 1.143 + } 1.144 + } 1.145 + 1.146 + // Otherwise thread assiciated XMLStreamReader 1.147 + return READER_FACTORY.doCreate(null, in, false); 1.148 + } 1.149 + 1.150 + /** 1.151 + * Creates a new {@link FastInfosetStreamSOAPCodec} instance. 1.152 + * 1.153 + * @param version the SOAP version of the codec. 1.154 + * @return a new {@link FastInfosetStreamSOAPCodec} instance. 1.155 + */ 1.156 + public static FastInfosetStreamSOAPCodec create(StreamSOAPCodec soapCodec, SOAPVersion version) { 1.157 + return create(soapCodec, version, false); 1.158 + } 1.159 + 1.160 + /** 1.161 + * Creates a new {@link FastInfosetStreamSOAPCodec} instance. 1.162 + * 1.163 + * @param version the SOAP version of the codec. 1.164 + * @param retainState if true the Codec should retain the state of 1.165 + * vocabulary tables for multiple encode/decode invocations. 1.166 + * @return a new {@link FastInfosetStreamSOAPCodec} instance. 1.167 + */ 1.168 + public static FastInfosetStreamSOAPCodec create(StreamSOAPCodec soapCodec, 1.169 + SOAPVersion version, boolean retainState) { 1.170 + if(version==null) 1.171 + // this decoder is for SOAP, not for XML/HTTP 1.172 + throw new IllegalArgumentException(); 1.173 + switch(version) { 1.174 + case SOAP_11: 1.175 + return new FastInfosetStreamSOAP11Codec(soapCodec, retainState); 1.176 + case SOAP_12: 1.177 + return new FastInfosetStreamSOAP12Codec(soapCodec, retainState); 1.178 + default: 1.179 + throw new AssertionError(); 1.180 + } 1.181 + } 1.182 +}