aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.encoding.fastinfoset; aoqi@0: aoqi@0: import com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer; aoqi@0: import com.sun.xml.internal.fastinfoset.stax.StAXDocumentParser; aoqi@0: import com.sun.xml.internal.ws.api.pipe.Codec; aoqi@0: import com.sun.xml.internal.ws.api.pipe.ContentType; aoqi@0: import com.sun.xml.internal.ws.api.message.Packet; aoqi@0: import com.sun.xml.internal.ws.api.SOAPVersion; aoqi@0: import com.sun.xml.internal.ws.api.pipe.StreamSOAPCodec; aoqi@0: import com.sun.xml.internal.ws.message.stream.StreamHeader; aoqi@0: import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; aoqi@0: import com.sun.xml.internal.ws.encoding.ContentTypeImpl; aoqi@0: aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: import javax.xml.stream.XMLStreamReader; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: import java.io.OutputStream; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.IOException; aoqi@0: import java.nio.channels.WritableByteChannel; aoqi@0: import java.nio.channels.ReadableByteChannel; aoqi@0: aoqi@0: /** aoqi@0: * A stream SOAP codec for handling SOAP message infosets to fast aoqi@0: * infoset documents. aoqi@0: * aoqi@0: *

aoqi@0: * This implementation currently defers to {@link StreamSOAPCodec} for the decoding aoqi@0: * using {@link XMLStreamReader}. aoqi@0: * aoqi@0: * @author Paul Sandoz aoqi@0: */ aoqi@0: public abstract class FastInfosetStreamSOAPCodec implements Codec { aoqi@0: private static final FastInfosetStreamReaderFactory READER_FACTORY = FastInfosetStreamReaderFactory.getInstance(); aoqi@0: aoqi@0: private StAXDocumentParser _statefulParser; aoqi@0: private StAXDocumentSerializer _serializer; aoqi@0: aoqi@0: private final StreamSOAPCodec _soapCodec; aoqi@0: aoqi@0: private final boolean _retainState; aoqi@0: aoqi@0: protected final ContentType _defaultContentType; aoqi@0: aoqi@0: /* package */ FastInfosetStreamSOAPCodec(StreamSOAPCodec soapCodec, SOAPVersion soapVersion, boolean retainState, String mimeType) { aoqi@0: // _soapCodec = StreamSOAPCodec.create(soapVersion); aoqi@0: _soapCodec = soapCodec; aoqi@0: _retainState = retainState; aoqi@0: _defaultContentType = new ContentTypeImpl(mimeType); aoqi@0: } aoqi@0: aoqi@0: /* package */ FastInfosetStreamSOAPCodec(FastInfosetStreamSOAPCodec that) { aoqi@0: this._soapCodec = (StreamSOAPCodec) that._soapCodec.copy(); aoqi@0: this._retainState = that._retainState; aoqi@0: this._defaultContentType = that._defaultContentType; aoqi@0: } aoqi@0: aoqi@0: public String getMimeType() { aoqi@0: return _defaultContentType.getContentType(); aoqi@0: } aoqi@0: aoqi@0: public ContentType getStaticContentType(Packet packet) { aoqi@0: return getContentType(packet.soapAction); aoqi@0: } aoqi@0: aoqi@0: public ContentType encode(Packet packet, OutputStream out) { aoqi@0: if (packet.getMessage() != null) { aoqi@0: final XMLStreamWriter writer = getXMLStreamWriter(out); aoqi@0: try { aoqi@0: packet.getMessage().writeTo(writer); aoqi@0: writer.flush(); aoqi@0: } catch (XMLStreamException e) { aoqi@0: throw new WebServiceException(e); aoqi@0: } aoqi@0: } aoqi@0: return getContentType(packet.soapAction); aoqi@0: } aoqi@0: aoqi@0: public ContentType encode(Packet packet, WritableByteChannel buffer) { aoqi@0: //TODO: not yet implemented aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: aoqi@0: public void decode(InputStream in, String contentType, Packet response) throws IOException { aoqi@0: response.setMessage( aoqi@0: _soapCodec.decode(getXMLStreamReader(in))); aoqi@0: } aoqi@0: aoqi@0: public void decode(ReadableByteChannel in, String contentType, Packet response) { aoqi@0: throw new UnsupportedOperationException(); aoqi@0: } aoqi@0: aoqi@0: protected abstract StreamHeader createHeader(XMLStreamReader reader, XMLStreamBuffer mark); aoqi@0: aoqi@0: protected abstract ContentType getContentType(String soapAction); aoqi@0: aoqi@0: private XMLStreamWriter getXMLStreamWriter(OutputStream out) { aoqi@0: if (_serializer != null) { aoqi@0: _serializer.setOutputStream(out); aoqi@0: return _serializer; aoqi@0: } else { aoqi@0: return _serializer = FastInfosetCodec.createNewStreamWriter(out, _retainState); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private XMLStreamReader getXMLStreamReader(InputStream in) { aoqi@0: // If the _retainState is true (FI stateful) then pick up Codec assiciated XMLStreamReader aoqi@0: if (_retainState) { aoqi@0: if (_statefulParser != null) { aoqi@0: _statefulParser.setInputStream(in); aoqi@0: return _statefulParser; aoqi@0: } else { aoqi@0: return _statefulParser = FastInfosetCodec.createNewStreamReader(in, _retainState); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Otherwise thread assiciated XMLStreamReader aoqi@0: return READER_FACTORY.doCreate(null, in, false); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a new {@link FastInfosetStreamSOAPCodec} instance. aoqi@0: * aoqi@0: * @param version the SOAP version of the codec. aoqi@0: * @return a new {@link FastInfosetStreamSOAPCodec} instance. aoqi@0: */ aoqi@0: public static FastInfosetStreamSOAPCodec create(StreamSOAPCodec soapCodec, SOAPVersion version) { aoqi@0: return create(soapCodec, version, false); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a new {@link FastInfosetStreamSOAPCodec} instance. aoqi@0: * aoqi@0: * @param version the SOAP version of the codec. aoqi@0: * @param retainState if true the Codec should retain the state of aoqi@0: * vocabulary tables for multiple encode/decode invocations. aoqi@0: * @return a new {@link FastInfosetStreamSOAPCodec} instance. aoqi@0: */ aoqi@0: public static FastInfosetStreamSOAPCodec create(StreamSOAPCodec soapCodec, aoqi@0: SOAPVersion version, boolean retainState) { aoqi@0: if(version==null) aoqi@0: // this decoder is for SOAP, not for XML/HTTP aoqi@0: throw new IllegalArgumentException(); aoqi@0: switch(version) { aoqi@0: case SOAP_11: aoqi@0: return new FastInfosetStreamSOAP11Codec(soapCodec, retainState); aoqi@0: case SOAP_12: aoqi@0: return new FastInfosetStreamSOAP12Codec(soapCodec, retainState); aoqi@0: default: aoqi@0: throw new AssertionError(); aoqi@0: } aoqi@0: } aoqi@0: }