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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, 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.stream.XMLStreamReader;
ohair@286 44 import javax.xml.ws.WebServiceException;
ohair@286 45 import java.io.OutputStream;
ohair@286 46 import java.io.InputStream;
ohair@286 47 import java.io.IOException;
ohair@286 48 import java.nio.channels.WritableByteChannel;
ohair@286 49 import java.nio.channels.ReadableByteChannel;
ohair@286 50 import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource;
ohair@286 51
ohair@286 52 /**
ohair@286 53 * A codec for encoding/decoding XML infosets to/from fast
ohair@286 54 * infoset documents.
ohair@286 55 *
ohair@286 56 * @author Paul Sandoz
ohair@286 57 */
ohair@286 58 public class FastInfosetCodec implements Codec {
ohair@286 59 private static final int DEFAULT_INDEXED_STRING_SIZE_LIMIT = 32;
ohair@286 60 private static final int DEFAULT_INDEXED_STRING_MEMORY_LIMIT = 4 * 1024 * 1024; //4M limit
ohair@286 61
ohair@286 62 private StAXDocumentParser _parser;
ohair@286 63
ohair@286 64 private StAXDocumentSerializer _serializer;
ohair@286 65
ohair@286 66 private final boolean _retainState;
ohair@286 67
ohair@286 68 private final ContentType _contentType;
ohair@286 69
ohair@286 70 /* package */ FastInfosetCodec(boolean retainState) {
ohair@286 71 _retainState = retainState;
ohair@286 72 _contentType = (retainState) ? new ContentTypeImpl(FastInfosetMIMETypes.STATEFUL_INFOSET) :
ohair@286 73 new ContentTypeImpl(FastInfosetMIMETypes.INFOSET);
ohair@286 74 }
ohair@286 75
ohair@286 76 public String getMimeType() {
ohair@286 77 return _contentType.getContentType();
ohair@286 78 }
ohair@286 79
ohair@286 80 public Codec copy() {
ohair@286 81 return new FastInfosetCodec(_retainState);
ohair@286 82 }
ohair@286 83
ohair@286 84 public ContentType getStaticContentType(Packet packet) {
ohair@286 85 return _contentType;
ohair@286 86 }
ohair@286 87
ohair@286 88 public ContentType encode(Packet packet, OutputStream out) {
ohair@286 89 Message message = packet.getMessage();
ohair@286 90 if (message != null && message.hasPayload()) {
ohair@286 91 final XMLStreamWriter writer = getXMLStreamWriter(out);
ohair@286 92 try {
ohair@286 93 writer.writeStartDocument();
ohair@286 94 packet.getMessage().writePayloadTo(writer);
ohair@286 95 writer.writeEndDocument();
ohair@286 96 writer.flush();
ohair@286 97 } catch (XMLStreamException e) {
ohair@286 98 throw new WebServiceException(e);
ohair@286 99 }
ohair@286 100 }
ohair@286 101
ohair@286 102 return _contentType;
ohair@286 103 }
ohair@286 104
ohair@286 105 public ContentType encode(Packet packet, WritableByteChannel buffer) {
ohair@286 106 //TODO: not yet implemented
ohair@286 107 throw new UnsupportedOperationException();
ohair@286 108 }
ohair@286 109
ohair@286 110 public void decode(InputStream in, String contentType, Packet packet) throws IOException {
ohair@286 111 /* Implements similar logic as the XMLMessage.create(String, InputStream).
ohair@286 112 * But it's faster, as we know the InputStream has FastInfoset content*/
ohair@286 113 Message message = null;
ohair@286 114 in = hasSomeData(in);
ohair@286 115 if (in != null) {
ohair@286 116 message = Messages.createUsingPayload(new FastInfosetSource(in),
ohair@286 117 SOAPVersion.SOAP_11);
ohair@286 118 } else {
ohair@286 119 message = Messages.createEmpty(SOAPVersion.SOAP_11);
ohair@286 120 }
ohair@286 121
ohair@286 122 packet.setMessage(message);
ohair@286 123 }
ohair@286 124
ohair@286 125 public void decode(ReadableByteChannel in, String contentType, Packet response) {
ohair@286 126 throw new UnsupportedOperationException();
ohair@286 127 }
ohair@286 128
ohair@286 129 private XMLStreamWriter getXMLStreamWriter(OutputStream out) {
ohair@286 130 if (_serializer != null) {
ohair@286 131 _serializer.setOutputStream(out);
ohair@286 132 return _serializer;
ohair@286 133 } else {
ohair@286 134 return _serializer = createNewStreamWriter(out, _retainState);
ohair@286 135 }
ohair@286 136 }
ohair@286 137
ohair@286 138 private XMLStreamReader getXMLStreamReader(InputStream in) {
ohair@286 139 if (_parser != null) {
ohair@286 140 _parser.setInputStream(in);
ohair@286 141 return _parser;
ohair@286 142 } else {
ohair@286 143 return _parser = createNewStreamReader(in, _retainState);
ohair@286 144 }
ohair@286 145 }
ohair@286 146
ohair@286 147 /**
ohair@286 148 * Creates a new {@link FastInfosetCodec} instance.
ohair@286 149 *
ohair@286 150 * @return a new {@link FastInfosetCodec} instance.
ohair@286 151 */
ohair@286 152 public static FastInfosetCodec create() {
ohair@286 153 return create(false);
ohair@286 154 }
ohair@286 155
ohair@286 156 /**
ohair@286 157 * Creates a new {@link FastInfosetCodec} instance.
ohair@286 158 *
ohair@286 159 * @param retainState if true the Codec should retain the state of
ohair@286 160 * vocabulary tables for multiple encode/decode invocations.
ohair@286 161 * @return a new {@link FastInfosetCodec} instance.
ohair@286 162 */
ohair@286 163 public static FastInfosetCodec create(boolean retainState) {
ohair@286 164 return new FastInfosetCodec(retainState);
ohair@286 165 }
ohair@286 166
ohair@286 167 /**
ohair@286 168 * Create a new (@link StAXDocumentSerializer} instance.
ohair@286 169 *
ohair@286 170 * @param in the OutputStream to serialize to.
ohair@286 171 * @param retainState if true the serializer should retain the state of
ohair@286 172 * vocabulary tables for multiple serializations.
ohair@286 173 * @return a new {@link StAXDocumentSerializer} instance.
ohair@286 174 */
ohair@286 175 /* package */ static StAXDocumentSerializer createNewStreamWriter(OutputStream out, boolean retainState) {
ohair@286 176 return createNewStreamWriter(out, retainState, DEFAULT_INDEXED_STRING_SIZE_LIMIT, DEFAULT_INDEXED_STRING_MEMORY_LIMIT);
ohair@286 177 }
ohair@286 178
ohair@286 179 /**
ohair@286 180 * Create a new (@link StAXDocumentSerializer} instance.
ohair@286 181 *
ohair@286 182 * @param in the OutputStream to serialize to.
ohair@286 183 * @param retainState if true the serializer should retain the state of
ohair@286 184 * vocabulary tables for multiple serializations.
ohair@286 185 * @return a new {@link StAXDocumentSerializer} instance.
ohair@286 186 */
ohair@286 187 /* package */ static StAXDocumentSerializer createNewStreamWriter(OutputStream out,
ohair@286 188 boolean retainState, int indexedStringSizeLimit, int stringsMemoryLimit) {
ohair@286 189 StAXDocumentSerializer serializer = new StAXDocumentSerializer(out);
ohair@286 190 if (retainState) {
ohair@286 191 /**
ohair@286 192 * Create a serializer vocabulary external to the serializer.
ohair@286 193 * This will ensure that the vocabulary will never be cleared
ohair@286 194 * for each serialization and will be retained (and will grow)
ohair@286 195 * for each serialization
ohair@286 196 */
ohair@286 197 SerializerVocabulary vocabulary = new SerializerVocabulary();
ohair@286 198 serializer.setVocabulary(vocabulary);
ohair@286 199 serializer.setMinAttributeValueSize(0);
ohair@286 200 serializer.setMaxAttributeValueSize(indexedStringSizeLimit);
ohair@286 201 serializer.setMinCharacterContentChunkSize(0);
ohair@286 202 serializer.setMaxCharacterContentChunkSize(indexedStringSizeLimit);
ohair@286 203 serializer.setAttributeValueMapMemoryLimit(stringsMemoryLimit);
ohair@286 204 serializer.setCharacterContentChunkMapMemoryLimit(stringsMemoryLimit);
ohair@286 205 }
ohair@286 206 return serializer;
ohair@286 207 }
ohair@286 208
ohair@286 209 /**
ohair@286 210 * Create a new (@link StAXDocumentParser} instance.
ohair@286 211 *
ohair@286 212 * @param in the InputStream to parse from.
ohair@286 213 * @param retainState if true the parser should retain the state of
ohair@286 214 * vocabulary tables for multiple parses.
ohair@286 215 * @return a new {@link StAXDocumentParser} instance.
ohair@286 216 */
ohair@286 217 /* package */ static StAXDocumentParser createNewStreamReader(InputStream in, boolean retainState) {
ohair@286 218 StAXDocumentParser parser = new StAXDocumentParser(in);
ohair@286 219 parser.setStringInterning(true);
ohair@286 220 if (retainState) {
ohair@286 221 /**
ohair@286 222 * Create a parser vocabulary external to the parser.
ohair@286 223 * This will ensure that the vocabulary will never be cleared
ohair@286 224 * for each parse and will be retained (and will grow)
ohair@286 225 * for each parse.
ohair@286 226 */
ohair@286 227 ParserVocabulary vocabulary = new ParserVocabulary();
ohair@286 228 parser.setVocabulary(vocabulary);
ohair@286 229 }
ohair@286 230 return parser;
ohair@286 231 }
ohair@286 232
ohair@286 233 /**
ohair@286 234 * Create a new (@link StAXDocumentParser} recyclable instance.
ohair@286 235 *
ohair@286 236 * @param in the InputStream to parse from.
ohair@286 237 * @param retainState if true the parser should retain the state of
ohair@286 238 * vocabulary tables for multiple parses.
ohair@286 239 * @return a new recyclable {@link StAXDocumentParser} instance.
ohair@286 240 */
ohair@286 241 /* package */ static StAXDocumentParser createNewStreamReaderRecyclable(InputStream in, boolean retainState) {
ohair@286 242 StAXDocumentParser parser = new FastInfosetStreamReaderRecyclable(in);
ohair@286 243 parser.setStringInterning(true);
ohair@286 244 parser.setForceStreamClose(true);
ohair@286 245 if (retainState) {
ohair@286 246 /**
ohair@286 247 * Create a parser vocabulary external to the parser.
ohair@286 248 * This will ensure that the vocabulary will never be cleared
ohair@286 249 * for each parse and will be retained (and will grow)
ohair@286 250 * for each parse.
ohair@286 251 */
ohair@286 252 ParserVocabulary vocabulary = new ParserVocabulary();
ohair@286 253 parser.setVocabulary(vocabulary);
ohair@286 254 }
ohair@286 255 return parser;
ohair@286 256 }
ohair@286 257
ohair@286 258 /**
ohair@286 259 * Method is copied from com.sun.xml.internal.ws.encoding.xml.XMLMessage
ohair@286 260 * @TODO method should be public in some util package?
ohair@286 261 *
ohair@286 262 * Finds if the stream has some content or not
ohair@286 263 *
ohair@286 264 * @return null if there is no data
ohair@286 265 * else stream to be used
ohair@286 266 */
ohair@286 267 private static InputStream hasSomeData(InputStream in) throws IOException {
ohair@286 268 if (in != null) {
ohair@286 269 if (in.available() < 1) {
ohair@286 270 if (!in.markSupported()) {
ohair@286 271 in = new BufferedInputStream(in);
ohair@286 272 }
ohair@286 273 in.mark(1);
ohair@286 274 if (in.read() != -1) {
ohair@286 275 in.reset();
ohair@286 276 } else {
ohair@286 277 in = null; // No data
ohair@286 278 }
ohair@286 279 }
ohair@286 280 }
ohair@286 281 return in;
ohair@286 282 }
ohair@286 283 }

mercurial