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/FastInfosetCodec.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,273 @@ 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.fastinfoset.vocab.ParserVocabulary; 1.34 +import com.sun.xml.internal.fastinfoset.vocab.SerializerVocabulary; 1.35 +import com.sun.xml.internal.ws.api.SOAPVersion; 1.36 +import com.sun.xml.internal.ws.api.message.Message; 1.37 +import com.sun.xml.internal.ws.api.message.Messages; 1.38 +import com.sun.xml.internal.ws.api.pipe.Codec; 1.39 +import com.sun.xml.internal.ws.api.pipe.ContentType; 1.40 +import com.sun.xml.internal.ws.api.message.Packet; 1.41 +import com.sun.xml.internal.ws.encoding.ContentTypeImpl; 1.42 +import java.io.BufferedInputStream; 1.43 + 1.44 +import javax.xml.stream.XMLStreamException; 1.45 +import javax.xml.stream.XMLStreamWriter; 1.46 +import javax.xml.ws.WebServiceException; 1.47 +import java.io.OutputStream; 1.48 +import java.io.InputStream; 1.49 +import java.io.IOException; 1.50 +import java.nio.channels.WritableByteChannel; 1.51 +import java.nio.channels.ReadableByteChannel; 1.52 +import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource; 1.53 + 1.54 +/** 1.55 + * A codec for encoding/decoding XML infosets to/from fast 1.56 + * infoset documents. 1.57 + * 1.58 + * @author Paul Sandoz 1.59 + */ 1.60 +public class FastInfosetCodec implements Codec { 1.61 + private static final int DEFAULT_INDEXED_STRING_SIZE_LIMIT = 32; 1.62 + private static final int DEFAULT_INDEXED_STRING_MEMORY_LIMIT = 4 * 1024 * 1024; //4M limit 1.63 + 1.64 + private StAXDocumentParser _parser; 1.65 + 1.66 + private StAXDocumentSerializer _serializer; 1.67 + 1.68 + private final boolean _retainState; 1.69 + 1.70 + private final ContentType _contentType; 1.71 + 1.72 + /* package */ FastInfosetCodec(boolean retainState) { 1.73 + _retainState = retainState; 1.74 + _contentType = (retainState) ? new ContentTypeImpl(FastInfosetMIMETypes.STATEFUL_INFOSET) : 1.75 + new ContentTypeImpl(FastInfosetMIMETypes.INFOSET); 1.76 + } 1.77 + 1.78 + public String getMimeType() { 1.79 + return _contentType.getContentType(); 1.80 + } 1.81 + 1.82 + public Codec copy() { 1.83 + return new FastInfosetCodec(_retainState); 1.84 + } 1.85 + 1.86 + public ContentType getStaticContentType(Packet packet) { 1.87 + return _contentType; 1.88 + } 1.89 + 1.90 + public ContentType encode(Packet packet, OutputStream out) { 1.91 + Message message = packet.getMessage(); 1.92 + if (message != null && message.hasPayload()) { 1.93 + final XMLStreamWriter writer = getXMLStreamWriter(out); 1.94 + try { 1.95 + writer.writeStartDocument(); 1.96 + packet.getMessage().writePayloadTo(writer); 1.97 + writer.writeEndDocument(); 1.98 + writer.flush(); 1.99 + } catch (XMLStreamException e) { 1.100 + throw new WebServiceException(e); 1.101 + } 1.102 + } 1.103 + 1.104 + return _contentType; 1.105 + } 1.106 + 1.107 + public ContentType encode(Packet packet, WritableByteChannel buffer) { 1.108 + //TODO: not yet implemented 1.109 + throw new UnsupportedOperationException(); 1.110 + } 1.111 + 1.112 + public void decode(InputStream in, String contentType, Packet packet) throws IOException { 1.113 + /* Implements similar logic as the XMLMessage.create(String, InputStream). 1.114 + * But it's faster, as we know the InputStream has FastInfoset content*/ 1.115 + Message message; 1.116 + in = hasSomeData(in); 1.117 + if (in != null) { 1.118 + message = Messages.createUsingPayload(new FastInfosetSource(in), 1.119 + SOAPVersion.SOAP_11); 1.120 + } else { 1.121 + message = Messages.createEmpty(SOAPVersion.SOAP_11); 1.122 + } 1.123 + 1.124 + packet.setMessage(message); 1.125 + } 1.126 + 1.127 + public void decode(ReadableByteChannel in, String contentType, Packet response) { 1.128 + throw new UnsupportedOperationException(); 1.129 + } 1.130 + 1.131 + private XMLStreamWriter getXMLStreamWriter(OutputStream out) { 1.132 + if (_serializer != null) { 1.133 + _serializer.setOutputStream(out); 1.134 + return _serializer; 1.135 + } else { 1.136 + return _serializer = createNewStreamWriter(out, _retainState); 1.137 + } 1.138 + } 1.139 + 1.140 + /** 1.141 + * Creates a new {@link FastInfosetCodec} instance. 1.142 + * 1.143 + * @return a new {@link FastInfosetCodec} instance. 1.144 + */ 1.145 + public static FastInfosetCodec create() { 1.146 + return create(false); 1.147 + } 1.148 + 1.149 + /** 1.150 + * Creates a new {@link FastInfosetCodec} instance. 1.151 + * 1.152 + * @param retainState if true the Codec should retain the state of 1.153 + * vocabulary tables for multiple encode/decode invocations. 1.154 + * @return a new {@link FastInfosetCodec} instance. 1.155 + */ 1.156 + public static FastInfosetCodec create(boolean retainState) { 1.157 + return new FastInfosetCodec(retainState); 1.158 + } 1.159 + 1.160 + /** 1.161 + * Create a new (@link StAXDocumentSerializer} instance. 1.162 + * 1.163 + * @param in the OutputStream to serialize to. 1.164 + * @param retainState if true the serializer should retain the state of 1.165 + * vocabulary tables for multiple serializations. 1.166 + * @return a new {@link StAXDocumentSerializer} instance. 1.167 + */ 1.168 + /* package */ static StAXDocumentSerializer createNewStreamWriter(OutputStream out, boolean retainState) { 1.169 + return createNewStreamWriter(out, retainState, DEFAULT_INDEXED_STRING_SIZE_LIMIT, DEFAULT_INDEXED_STRING_MEMORY_LIMIT); 1.170 + } 1.171 + 1.172 + /** 1.173 + * Create a new (@link StAXDocumentSerializer} instance. 1.174 + * 1.175 + * @param in the OutputStream to serialize to. 1.176 + * @param retainState if true the serializer should retain the state of 1.177 + * vocabulary tables for multiple serializations. 1.178 + * @return a new {@link StAXDocumentSerializer} instance. 1.179 + */ 1.180 + /* package */ static StAXDocumentSerializer createNewStreamWriter(OutputStream out, 1.181 + boolean retainState, int indexedStringSizeLimit, int stringsMemoryLimit) { 1.182 + StAXDocumentSerializer serializer = new StAXDocumentSerializer(out); 1.183 + if (retainState) { 1.184 + /** 1.185 + * Create a serializer vocabulary external to the serializer. 1.186 + * This will ensure that the vocabulary will never be cleared 1.187 + * for each serialization and will be retained (and will grow) 1.188 + * for each serialization 1.189 + */ 1.190 + SerializerVocabulary vocabulary = new SerializerVocabulary(); 1.191 + serializer.setVocabulary(vocabulary); 1.192 + serializer.setMinAttributeValueSize(0); 1.193 + serializer.setMaxAttributeValueSize(indexedStringSizeLimit); 1.194 + serializer.setMinCharacterContentChunkSize(0); 1.195 + serializer.setMaxCharacterContentChunkSize(indexedStringSizeLimit); 1.196 + serializer.setAttributeValueMapMemoryLimit(stringsMemoryLimit); 1.197 + serializer.setCharacterContentChunkMapMemoryLimit(stringsMemoryLimit); 1.198 + } 1.199 + return serializer; 1.200 + } 1.201 + 1.202 + /** 1.203 + * Create a new (@link StAXDocumentParser} instance. 1.204 + * 1.205 + * @param in the InputStream to parse from. 1.206 + * @param retainState if true the parser should retain the state of 1.207 + * vocabulary tables for multiple parses. 1.208 + * @return a new {@link StAXDocumentParser} instance. 1.209 + */ 1.210 + /* package */ static StAXDocumentParser createNewStreamReader(InputStream in, boolean retainState) { 1.211 + StAXDocumentParser parser = new StAXDocumentParser(in); 1.212 + parser.setStringInterning(true); 1.213 + if (retainState) { 1.214 + /** 1.215 + * Create a parser vocabulary external to the parser. 1.216 + * This will ensure that the vocabulary will never be cleared 1.217 + * for each parse and will be retained (and will grow) 1.218 + * for each parse. 1.219 + */ 1.220 + ParserVocabulary vocabulary = new ParserVocabulary(); 1.221 + parser.setVocabulary(vocabulary); 1.222 + } 1.223 + return parser; 1.224 + } 1.225 + 1.226 + /** 1.227 + * Create a new (@link StAXDocumentParser} recyclable instance. 1.228 + * 1.229 + * @param in the InputStream to parse from. 1.230 + * @param retainState if true the parser should retain the state of 1.231 + * vocabulary tables for multiple parses. 1.232 + * @return a new recyclable {@link StAXDocumentParser} instance. 1.233 + */ 1.234 + /* package */ static StAXDocumentParser createNewStreamReaderRecyclable(InputStream in, boolean retainState) { 1.235 + StAXDocumentParser parser = new FastInfosetStreamReaderRecyclable(in); 1.236 + parser.setStringInterning(true); 1.237 + parser.setForceStreamClose(true); 1.238 + if (retainState) { 1.239 + /** 1.240 + * Create a parser vocabulary external to the parser. 1.241 + * This will ensure that the vocabulary will never be cleared 1.242 + * for each parse and will be retained (and will grow) 1.243 + * for each parse. 1.244 + */ 1.245 + ParserVocabulary vocabulary = new ParserVocabulary(); 1.246 + parser.setVocabulary(vocabulary); 1.247 + } 1.248 + return parser; 1.249 + } 1.250 + 1.251 + /** 1.252 + * Method is copied from com.sun.xml.internal.ws.encoding.xml.XMLMessage 1.253 + * @TODO method should be public in some util package? 1.254 + * 1.255 + * Finds if the stream has some content or not 1.256 + * 1.257 + * @return null if there is no data 1.258 + * else stream to be used 1.259 + */ 1.260 + private static InputStream hasSomeData(InputStream in) throws IOException { 1.261 + if (in != null) { 1.262 + if (in.available() < 1) { 1.263 + if (!in.markSupported()) { 1.264 + in = new BufferedInputStream(in); 1.265 + } 1.266 + in.mark(1); 1.267 + if (in.read() != -1) { 1.268 + in.reset(); 1.269 + } else { 1.270 + in = null; // No data 1.271 + } 1.272 + } 1.273 + } 1.274 + return in; 1.275 + } 1.276 +}