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