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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.encoding.fastinfoset;
ohair@286 27
ohair@286 28 import com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer;
ohair@286 29 import com.sun.xml.internal.fastinfoset.stax.StAXDocumentParser;
ohair@286 30 import com.sun.xml.internal.fastinfoset.vocab.ParserVocabulary;
ohair@286 31 import com.sun.xml.internal.fastinfoset.vocab.SerializerVocabulary;
ohair@286 32 import com.sun.xml.internal.ws.api.SOAPVersion;
ohair@286 33 import com.sun.xml.internal.ws.api.message.Message;
ohair@286 34 import com.sun.xml.internal.ws.api.message.Messages;
ohair@286 35 import com.sun.xml.internal.ws.api.pipe.Codec;
ohair@286 36 import com.sun.xml.internal.ws.api.pipe.ContentType;
ohair@286 37 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 38 import com.sun.xml.internal.ws.encoding.ContentTypeImpl;
ohair@286 39 import java.io.BufferedInputStream;
ohair@286 40
ohair@286 41 import javax.xml.stream.XMLStreamException;
ohair@286 42 import javax.xml.stream.XMLStreamWriter;
ohair@286 43 import javax.xml.ws.WebServiceException;
ohair@286 44 import java.io.OutputStream;
ohair@286 45 import java.io.InputStream;
ohair@286 46 import java.io.IOException;
ohair@286 47 import java.nio.channels.WritableByteChannel;
ohair@286 48 import java.nio.channels.ReadableByteChannel;
ohair@286 49 import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource;
ohair@286 50
ohair@286 51 /**
ohair@286 52 * A codec for encoding/decoding XML infosets to/from fast
ohair@286 53 * infoset documents.
ohair@286 54 *
ohair@286 55 * @author Paul Sandoz
ohair@286 56 */
ohair@286 57 public class FastInfosetCodec implements Codec {
ohair@286 58 private static final int DEFAULT_INDEXED_STRING_SIZE_LIMIT = 32;
ohair@286 59 private static final int DEFAULT_INDEXED_STRING_MEMORY_LIMIT = 4 * 1024 * 1024; //4M limit
ohair@286 60
ohair@286 61 private StAXDocumentParser _parser;
ohair@286 62
ohair@286 63 private StAXDocumentSerializer _serializer;
ohair@286 64
ohair@286 65 private final boolean _retainState;
ohair@286 66
ohair@286 67 private final ContentType _contentType;
ohair@286 68
ohair@286 69 /* package */ FastInfosetCodec(boolean retainState) {
ohair@286 70 _retainState = retainState;
ohair@286 71 _contentType = (retainState) ? new ContentTypeImpl(FastInfosetMIMETypes.STATEFUL_INFOSET) :
ohair@286 72 new ContentTypeImpl(FastInfosetMIMETypes.INFOSET);
ohair@286 73 }
ohair@286 74
ohair@286 75 public String getMimeType() {
ohair@286 76 return _contentType.getContentType();
ohair@286 77 }
ohair@286 78
ohair@286 79 public Codec copy() {
ohair@286 80 return new FastInfosetCodec(_retainState);
ohair@286 81 }
ohair@286 82
ohair@286 83 public ContentType getStaticContentType(Packet packet) {
ohair@286 84 return _contentType;
ohair@286 85 }
ohair@286 86
ohair@286 87 public ContentType encode(Packet packet, OutputStream out) {
ohair@286 88 Message message = packet.getMessage();
ohair@286 89 if (message != null && message.hasPayload()) {
ohair@286 90 final XMLStreamWriter writer = getXMLStreamWriter(out);
ohair@286 91 try {
ohair@286 92 writer.writeStartDocument();
ohair@286 93 packet.getMessage().writePayloadTo(writer);
ohair@286 94 writer.writeEndDocument();
ohair@286 95 writer.flush();
ohair@286 96 } catch (XMLStreamException e) {
ohair@286 97 throw new WebServiceException(e);
ohair@286 98 }
ohair@286 99 }
ohair@286 100
ohair@286 101 return _contentType;
ohair@286 102 }
ohair@286 103
ohair@286 104 public ContentType encode(Packet packet, WritableByteChannel buffer) {
ohair@286 105 //TODO: not yet implemented
ohair@286 106 throw new UnsupportedOperationException();
ohair@286 107 }
ohair@286 108
ohair@286 109 public void decode(InputStream in, String contentType, Packet packet) throws IOException {
ohair@286 110 /* Implements similar logic as the XMLMessage.create(String, InputStream).
ohair@286 111 * But it's faster, as we know the InputStream has FastInfoset content*/
alanb@368 112 Message message;
ohair@286 113 in = hasSomeData(in);
ohair@286 114 if (in != null) {
ohair@286 115 message = Messages.createUsingPayload(new FastInfosetSource(in),
ohair@286 116 SOAPVersion.SOAP_11);
ohair@286 117 } else {
ohair@286 118 message = Messages.createEmpty(SOAPVersion.SOAP_11);
ohair@286 119 }
ohair@286 120
ohair@286 121 packet.setMessage(message);
ohair@286 122 }
ohair@286 123
ohair@286 124 public void decode(ReadableByteChannel in, String contentType, Packet response) {
ohair@286 125 throw new UnsupportedOperationException();
ohair@286 126 }
ohair@286 127
ohair@286 128 private XMLStreamWriter getXMLStreamWriter(OutputStream out) {
ohair@286 129 if (_serializer != null) {
ohair@286 130 _serializer.setOutputStream(out);
ohair@286 131 return _serializer;
ohair@286 132 } else {
ohair@286 133 return _serializer = createNewStreamWriter(out, _retainState);
ohair@286 134 }
ohair@286 135 }
ohair@286 136
ohair@286 137 /**
ohair@286 138 * Creates a new {@link FastInfosetCodec} instance.
ohair@286 139 *
ohair@286 140 * @return a new {@link FastInfosetCodec} instance.
ohair@286 141 */
ohair@286 142 public static FastInfosetCodec create() {
ohair@286 143 return create(false);
ohair@286 144 }
ohair@286 145
ohair@286 146 /**
ohair@286 147 * Creates a new {@link FastInfosetCodec} instance.
ohair@286 148 *
ohair@286 149 * @param retainState if true the Codec should retain the state of
ohair@286 150 * vocabulary tables for multiple encode/decode invocations.
ohair@286 151 * @return a new {@link FastInfosetCodec} instance.
ohair@286 152 */
ohair@286 153 public static FastInfosetCodec create(boolean retainState) {
ohair@286 154 return new FastInfosetCodec(retainState);
ohair@286 155 }
ohair@286 156
ohair@286 157 /**
ohair@286 158 * Create a new (@link StAXDocumentSerializer} instance.
ohair@286 159 *
ohair@286 160 * @param in the OutputStream to serialize to.
ohair@286 161 * @param retainState if true the serializer should retain the state of
ohair@286 162 * vocabulary tables for multiple serializations.
ohair@286 163 * @return a new {@link StAXDocumentSerializer} instance.
ohair@286 164 */
ohair@286 165 /* package */ static StAXDocumentSerializer createNewStreamWriter(OutputStream out, boolean retainState) {
ohair@286 166 return createNewStreamWriter(out, retainState, DEFAULT_INDEXED_STRING_SIZE_LIMIT, DEFAULT_INDEXED_STRING_MEMORY_LIMIT);
ohair@286 167 }
ohair@286 168
ohair@286 169 /**
ohair@286 170 * Create a new (@link StAXDocumentSerializer} instance.
ohair@286 171 *
ohair@286 172 * @param in the OutputStream to serialize to.
ohair@286 173 * @param retainState if true the serializer should retain the state of
ohair@286 174 * vocabulary tables for multiple serializations.
ohair@286 175 * @return a new {@link StAXDocumentSerializer} instance.
ohair@286 176 */
ohair@286 177 /* package */ static StAXDocumentSerializer createNewStreamWriter(OutputStream out,
ohair@286 178 boolean retainState, int indexedStringSizeLimit, int stringsMemoryLimit) {
ohair@286 179 StAXDocumentSerializer serializer = new StAXDocumentSerializer(out);
ohair@286 180 if (retainState) {
ohair@286 181 /**
ohair@286 182 * Create a serializer vocabulary external to the serializer.
ohair@286 183 * This will ensure that the vocabulary will never be cleared
ohair@286 184 * for each serialization and will be retained (and will grow)
ohair@286 185 * for each serialization
ohair@286 186 */
ohair@286 187 SerializerVocabulary vocabulary = new SerializerVocabulary();
ohair@286 188 serializer.setVocabulary(vocabulary);
ohair@286 189 serializer.setMinAttributeValueSize(0);
ohair@286 190 serializer.setMaxAttributeValueSize(indexedStringSizeLimit);
ohair@286 191 serializer.setMinCharacterContentChunkSize(0);
ohair@286 192 serializer.setMaxCharacterContentChunkSize(indexedStringSizeLimit);
ohair@286 193 serializer.setAttributeValueMapMemoryLimit(stringsMemoryLimit);
ohair@286 194 serializer.setCharacterContentChunkMapMemoryLimit(stringsMemoryLimit);
ohair@286 195 }
ohair@286 196 return serializer;
ohair@286 197 }
ohair@286 198
ohair@286 199 /**
ohair@286 200 * Create a new (@link StAXDocumentParser} instance.
ohair@286 201 *
ohair@286 202 * @param in the InputStream to parse from.
ohair@286 203 * @param retainState if true the parser should retain the state of
ohair@286 204 * vocabulary tables for multiple parses.
ohair@286 205 * @return a new {@link StAXDocumentParser} instance.
ohair@286 206 */
ohair@286 207 /* package */ static StAXDocumentParser createNewStreamReader(InputStream in, boolean retainState) {
ohair@286 208 StAXDocumentParser parser = new StAXDocumentParser(in);
ohair@286 209 parser.setStringInterning(true);
ohair@286 210 if (retainState) {
ohair@286 211 /**
ohair@286 212 * Create a parser vocabulary external to the parser.
ohair@286 213 * This will ensure that the vocabulary will never be cleared
ohair@286 214 * for each parse and will be retained (and will grow)
ohair@286 215 * for each parse.
ohair@286 216 */
ohair@286 217 ParserVocabulary vocabulary = new ParserVocabulary();
ohair@286 218 parser.setVocabulary(vocabulary);
ohair@286 219 }
ohair@286 220 return parser;
ohair@286 221 }
ohair@286 222
ohair@286 223 /**
ohair@286 224 * Create a new (@link StAXDocumentParser} recyclable instance.
ohair@286 225 *
ohair@286 226 * @param in the InputStream to parse from.
ohair@286 227 * @param retainState if true the parser should retain the state of
ohair@286 228 * vocabulary tables for multiple parses.
ohair@286 229 * @return a new recyclable {@link StAXDocumentParser} instance.
ohair@286 230 */
ohair@286 231 /* package */ static StAXDocumentParser createNewStreamReaderRecyclable(InputStream in, boolean retainState) {
ohair@286 232 StAXDocumentParser parser = new FastInfosetStreamReaderRecyclable(in);
ohair@286 233 parser.setStringInterning(true);
ohair@286 234 parser.setForceStreamClose(true);
ohair@286 235 if (retainState) {
ohair@286 236 /**
ohair@286 237 * Create a parser vocabulary external to the parser.
ohair@286 238 * This will ensure that the vocabulary will never be cleared
ohair@286 239 * for each parse and will be retained (and will grow)
ohair@286 240 * for each parse.
ohair@286 241 */
ohair@286 242 ParserVocabulary vocabulary = new ParserVocabulary();
ohair@286 243 parser.setVocabulary(vocabulary);
ohair@286 244 }
ohair@286 245 return parser;
ohair@286 246 }
ohair@286 247
ohair@286 248 /**
ohair@286 249 * Method is copied from com.sun.xml.internal.ws.encoding.xml.XMLMessage
ohair@286 250 * @TODO method should be public in some util package?
ohair@286 251 *
ohair@286 252 * Finds if the stream has some content or not
ohair@286 253 *
ohair@286 254 * @return null if there is no data
ohair@286 255 * else stream to be used
ohair@286 256 */
ohair@286 257 private static InputStream hasSomeData(InputStream in) throws IOException {
ohair@286 258 if (in != null) {
ohair@286 259 if (in.available() < 1) {
ohair@286 260 if (!in.markSupported()) {
ohair@286 261 in = new BufferedInputStream(in);
ohair@286 262 }
ohair@286 263 in.mark(1);
ohair@286 264 if (in.read() != -1) {
ohair@286 265 in.reset();
ohair@286 266 } else {
ohair@286 267 in = null; // No data
ohair@286 268 }
ohair@286 269 }
ohair@286 270 }
ohair@286 271 return in;
ohair@286 272 }
ohair@286 273 }

mercurial