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: package com.sun.xml.ws.encoding.fastinfoset;
037:
038: import com.sun.xml.fastinfoset.stax.StAXDocumentSerializer;
039: import com.sun.xml.fastinfoset.stax.StAXDocumentParser;
040: import com.sun.xml.ws.api.pipe.Codec;
041: import com.sun.xml.ws.api.pipe.ContentType;
042: import com.sun.xml.ws.api.message.Packet;
043: import com.sun.xml.ws.api.SOAPVersion;
044: import com.sun.xml.ws.api.pipe.StreamSOAPCodec;
045: import com.sun.xml.ws.message.stream.StreamHeader;
046: import com.sun.xml.stream.buffer.XMLStreamBuffer;
047: import com.sun.xml.ws.encoding.ContentTypeImpl;
048:
049: import javax.xml.stream.XMLStreamException;
050: import javax.xml.stream.XMLStreamWriter;
051: import javax.xml.stream.XMLStreamReader;
052: import javax.xml.ws.WebServiceException;
053: import java.io.OutputStream;
054: import java.io.InputStream;
055: import java.io.IOException;
056: import java.nio.channels.WritableByteChannel;
057: import java.nio.channels.ReadableByteChannel;
058:
059: /**
060: * A stream SOAP codec for handling SOAP message infosets to fast
061: * infoset documents.
062: *
063: * <p>
064: * This implementation currently defers to {@link StreamSOAPCodec} for the decoding
065: * using {@link XMLStreamReader}.
066: *
067: * @author Paul Sandoz
068: */
069: public abstract class FastInfosetStreamSOAPCodec implements Codec {
070: private static final FastInfosetStreamReaderFactory READER_FACTORY = FastInfosetStreamReaderFactory
071: .getInstance();
072:
073: private StAXDocumentParser _statefulParser;
074: private StAXDocumentSerializer _serializer;
075:
076: private final StreamSOAPCodec _soapCodec;
077:
078: private final boolean _retainState;
079:
080: protected final ContentType _defaultContentType;
081:
082: /* package */FastInfosetStreamSOAPCodec(StreamSOAPCodec soapCodec,
083: SOAPVersion soapVersion, boolean retainState,
084: String mimeType) {
085: // _soapCodec = StreamSOAPCodec.create(soapVersion);
086: _soapCodec = soapCodec;
087: _retainState = retainState;
088: _defaultContentType = new ContentTypeImpl(mimeType);
089: }
090:
091: /* package */FastInfosetStreamSOAPCodec(
092: FastInfosetStreamSOAPCodec that) {
093: this ._soapCodec = (StreamSOAPCodec) that._soapCodec.copy();
094: this ._retainState = that._retainState;
095: this ._defaultContentType = that._defaultContentType;
096: }
097:
098: public String getMimeType() {
099: return _defaultContentType.getContentType();
100: }
101:
102: public ContentType getStaticContentType(Packet packet) {
103: return getContentType(packet.soapAction);
104: }
105:
106: public ContentType encode(Packet packet, OutputStream out) {
107: if (packet.getMessage() != null) {
108: final XMLStreamWriter writer = getXMLStreamWriter(out);
109: try {
110: packet.getMessage().writeTo(writer);
111: writer.flush();
112: } catch (XMLStreamException e) {
113: throw new WebServiceException(e);
114: }
115: }
116: return getContentType(packet.soapAction);
117: }
118:
119: public ContentType encode(Packet packet, WritableByteChannel buffer) {
120: //TODO: not yet implemented
121: throw new UnsupportedOperationException();
122: }
123:
124: public void decode(InputStream in, String contentType,
125: Packet response) throws IOException {
126: response.setMessage(_soapCodec.decode(getXMLStreamReader(in)));
127: }
128:
129: public void decode(ReadableByteChannel in, String contentType,
130: Packet response) {
131: throw new UnsupportedOperationException();
132: }
133:
134: protected abstract StreamHeader createHeader(
135: XMLStreamReader reader, XMLStreamBuffer mark);
136:
137: protected abstract ContentType getContentType(String soapAction);
138:
139: private XMLStreamWriter getXMLStreamWriter(OutputStream out) {
140: if (_serializer != null) {
141: _serializer.setOutputStream(out);
142: return _serializer;
143: } else {
144: return _serializer = FastInfosetCodec
145: .createNewStreamWriter(out, _retainState);
146: }
147: }
148:
149: private XMLStreamReader getXMLStreamReader(InputStream in) {
150: // If the _retainState is true (FI stateful) then pick up Codec assiciated XMLStreamReader
151: if (_retainState) {
152: if (_statefulParser != null) {
153: _statefulParser.setInputStream(in);
154: return _statefulParser;
155: } else {
156: return _statefulParser = FastInfosetCodec
157: .createNewStreamReader(in, _retainState);
158: }
159: }
160:
161: // Otherwise thread assiciated XMLStreamReader
162: return READER_FACTORY.doCreate(null, in, false);
163: }
164:
165: /**
166: * Creates a new {@link FastInfosetStreamSOAPCodec} instance.
167: *
168: * @param version the SOAP version of the codec.
169: * @return a new {@link FastInfosetStreamSOAPCodec} instance.
170: */
171: public static FastInfosetStreamSOAPCodec create(
172: StreamSOAPCodec soapCodec, SOAPVersion version) {
173: return create(soapCodec, version, false);
174: }
175:
176: /**
177: * Creates a new {@link FastInfosetStreamSOAPCodec} instance.
178: *
179: * @param version the SOAP version of the codec.
180: * @param retainState if true the Codec should retain the state of
181: * vocabulary tables for multiple encode/decode invocations.
182: * @return a new {@link FastInfosetStreamSOAPCodec} instance.
183: */
184: public static FastInfosetStreamSOAPCodec create(
185: StreamSOAPCodec soapCodec, SOAPVersion version,
186: boolean retainState) {
187: if (version == null)
188: // this decoder is for SOAP, not for XML/HTTP
189: throw new IllegalArgumentException();
190: switch (version) {
191: case SOAP_11:
192: return new FastInfosetStreamSOAP11Codec(soapCodec,
193: retainState);
194: case SOAP_12:
195: return new FastInfosetStreamSOAP12Codec(soapCodec,
196: retainState);
197: default:
198: throw new AssertionError();
199: }
200: }
201: }
|