001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.tcp.encoding;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.xml.ws.api.pipe.Codecs;
042: import com.sun.xml.ws.api.pipe.StreamSOAPCodec;
043: import com.sun.xml.ws.transport.tcp.encoding.WSTCPFastInfosetStreamReaderRecyclable.RecycleAwareListener;
044: import com.sun.xml.fastinfoset.stax.StAXDocumentParser;
045: import com.sun.xml.fastinfoset.stax.StAXDocumentSerializer;
046: import com.sun.xml.fastinfoset.vocab.ParserVocabulary;
047: import com.sun.xml.fastinfoset.vocab.SerializerVocabulary;
048: import com.sun.xml.stream.buffer.XMLStreamBuffer;
049: import com.sun.xml.ws.api.SOAPVersion;
050: import com.sun.xml.ws.api.message.Packet;
051: import com.sun.xml.ws.api.pipe.Codec;
052: import com.sun.xml.ws.api.pipe.ContentType;
053: import com.sun.xml.ws.encoding.ContentTypeImpl;
054: import com.sun.xml.ws.encoding.fastinfoset.FastInfosetStreamSOAPCodec;
055: import com.sun.xml.ws.message.stream.StreamHeader;
056: import java.io.IOException;
057: import java.io.InputStream;
058: import java.io.OutputStream;
059: import java.nio.channels.ReadableByteChannel;
060: import java.nio.channels.WritableByteChannel;
061: import javax.xml.stream.XMLStreamException;
062: import javax.xml.stream.XMLStreamReader;
063: import javax.xml.stream.XMLStreamWriter;
064: import javax.xml.ws.WebServiceException;
065:
066: /**
067: * @author Alexey Stashok
068: */
069: public abstract class WSTCPFastInfosetStreamCodec implements Codec {
070: private static final int DEFAULT_INDEXED_STRING_SIZE_LIMIT = 32;
071: private static final int DEFAULT_INDEXED_STRING_MEMORY_LIMIT = 4 * 1024 * 1024; //4M limit
072:
073: private StAXDocumentParser _statefulParser;
074: private StAXDocumentSerializer _serializer;
075:
076: private final StreamSOAPCodec _soapCodec;
077: private final boolean _retainState;
078:
079: protected final ContentType _defaultContentType;
080:
081: private final RecycleAwareListener _readerRecycleListener;
082:
083: /* package */WSTCPFastInfosetStreamCodec(@Nullable
084: StreamSOAPCodec soapCodec, @NotNull
085: SOAPVersion soapVersion, @NotNull
086: RecycleAwareListener readerRecycleListener, boolean retainState,
087: String mimeType) {
088: _soapCodec = soapCodec != null ? soapCodec : Codecs
089: .createSOAPEnvelopeXmlCodec(soapVersion);
090: _readerRecycleListener = readerRecycleListener;
091: _retainState = retainState;
092: _defaultContentType = new ContentTypeImpl(mimeType);
093: }
094:
095: /* package */WSTCPFastInfosetStreamCodec(
096: WSTCPFastInfosetStreamCodec that) {
097: this ._soapCodec = (StreamSOAPCodec) that._soapCodec.copy();
098: this ._readerRecycleListener = that._readerRecycleListener;
099: this ._retainState = that._retainState;
100: this ._defaultContentType = that._defaultContentType;
101: }
102:
103: public String getMimeType() {
104: return _defaultContentType.getContentType();
105: }
106:
107: public ContentType getStaticContentType(Packet packet) {
108: return getContentType(packet.soapAction);
109: }
110:
111: public ContentType encode(Packet packet, OutputStream out) {
112: if (packet.getMessage() != null) {
113: final XMLStreamWriter writer = getXMLStreamWriter(out);
114: try {
115: packet.getMessage().writeTo(writer);
116: writer.flush();
117: } catch (XMLStreamException e) {
118: throw new WebServiceException(e);
119: }
120: }
121: return getContentType(packet.soapAction);
122: }
123:
124: public ContentType encode(Packet packet, WritableByteChannel buffer) {
125: //TODO: not yet implemented
126: throw new UnsupportedOperationException();
127: }
128:
129: public void decode(InputStream in, String contentType,
130: Packet response) throws IOException {
131: response.setMessage(_soapCodec.decode(getXMLStreamReader(in)));
132: }
133:
134: public void decode(ReadableByteChannel in, String contentType,
135: Packet response) {
136: throw new UnsupportedOperationException();
137: }
138:
139: protected abstract StreamHeader createHeader(
140: XMLStreamReader reader, XMLStreamBuffer mark);
141:
142: protected abstract ContentType getContentType(String soapAction);
143:
144: private XMLStreamWriter getXMLStreamWriter(OutputStream out) {
145: if (_serializer != null) {
146: _serializer.setOutputStream(out);
147: return _serializer;
148: } else {
149: StAXDocumentSerializer serializer = new StAXDocumentSerializer(
150: out);
151: if (_retainState) {
152: SerializerVocabulary vocabulary = new SerializerVocabulary();
153: serializer.setVocabulary(vocabulary);
154: serializer
155: .setAttributeValueSizeLimit(DEFAULT_INDEXED_STRING_SIZE_LIMIT);
156: serializer
157: .setCharacterContentChunkSizeLimit(DEFAULT_INDEXED_STRING_SIZE_LIMIT);
158: serializer
159: .setAttributeValueMapMemoryLimit(DEFAULT_INDEXED_STRING_MEMORY_LIMIT);
160: serializer
161: .setCharacterContentChunkMapMemoryLimit(DEFAULT_INDEXED_STRING_MEMORY_LIMIT);
162: }
163: _serializer = serializer;
164: return serializer;
165: }
166: }
167:
168: private XMLStreamReader getXMLStreamReader(InputStream in) {
169: if (_statefulParser != null) {
170: _statefulParser.setInputStream(in);
171: return _statefulParser;
172: } else {
173: StAXDocumentParser parser = new WSTCPFastInfosetStreamReaderRecyclable(
174: in, _readerRecycleListener);
175: parser.setStringInterning(true);
176: if (_retainState) {
177: ParserVocabulary vocabulary = new ParserVocabulary();
178: parser.setVocabulary(vocabulary);
179: }
180: _statefulParser = parser;
181: return _statefulParser;
182: }
183: }
184:
185: /**
186: * Creates a new {@link FastInfosetStreamSOAPCodec} instance.
187: *
188: * @param version the SOAP version of the codec.
189: * @return a new {@link WSTCPFastInfosetStreamCodec} instance.
190: */
191: public static WSTCPFastInfosetStreamCodec create(
192: StreamSOAPCodec soapCodec, SOAPVersion version,
193: RecycleAwareListener readerRecycleListener,
194: boolean retainState) {
195: if (version == null)
196: // this decoder is for SOAP, not for XML/HTTP
197: throw new IllegalArgumentException();
198: switch (version) {
199: case SOAP_11:
200: return new WSTCPFastInfosetStreamSOAP11Codec(soapCodec,
201: readerRecycleListener, retainState);
202: case SOAP_12:
203: return new WSTCPFastInfosetStreamSOAP12Codec(soapCodec,
204: readerRecycleListener, retainState);
205: default:
206: throw new AssertionError();
207: }
208: }
209: }
|