325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.encoding.fastinfoset;
325N/A
325N/Aimport com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer;
325N/Aimport com.sun.xml.internal.fastinfoset.stax.StAXDocumentParser;
325N/Aimport com.sun.xml.internal.ws.api.pipe.Codec;
325N/Aimport com.sun.xml.internal.ws.api.pipe.ContentType;
325N/Aimport com.sun.xml.internal.ws.api.message.Packet;
325N/Aimport com.sun.xml.internal.ws.api.SOAPVersion;
325N/Aimport com.sun.xml.internal.ws.api.pipe.StreamSOAPCodec;
325N/Aimport com.sun.xml.internal.ws.message.stream.StreamHeader;
325N/Aimport com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
325N/Aimport com.sun.xml.internal.ws.encoding.ContentTypeImpl;
325N/A
325N/Aimport javax.xml.stream.XMLStreamException;
325N/Aimport javax.xml.stream.XMLStreamWriter;
325N/Aimport javax.xml.stream.XMLStreamReader;
325N/Aimport javax.xml.ws.WebServiceException;
325N/Aimport java.io.OutputStream;
325N/Aimport java.io.InputStream;
325N/Aimport java.io.IOException;
325N/Aimport java.nio.channels.WritableByteChannel;
325N/Aimport java.nio.channels.ReadableByteChannel;
325N/A
325N/A/**
325N/A * A stream SOAP codec for handling SOAP message infosets to fast
325N/A * infoset documents.
325N/A *
325N/A * <p>
325N/A * This implementation currently defers to {@link StreamSOAPCodec} for the decoding
325N/A * using {@link XMLStreamReader}.
325N/A *
325N/A * @author Paul Sandoz
325N/A */
325N/Apublic abstract class FastInfosetStreamSOAPCodec implements Codec {
325N/A private static final FastInfosetStreamReaderFactory READER_FACTORY = FastInfosetStreamReaderFactory.getInstance();
325N/A
325N/A private StAXDocumentParser _statefulParser;
325N/A private StAXDocumentSerializer _serializer;
325N/A
325N/A private final StreamSOAPCodec _soapCodec;
325N/A
325N/A private final boolean _retainState;
325N/A
325N/A protected final ContentType _defaultContentType;
325N/A
325N/A /* package */ FastInfosetStreamSOAPCodec(StreamSOAPCodec soapCodec, SOAPVersion soapVersion, boolean retainState, String mimeType) {
325N/A// _soapCodec = StreamSOAPCodec.create(soapVersion);
325N/A _soapCodec = soapCodec;
325N/A _retainState = retainState;
325N/A _defaultContentType = new ContentTypeImpl(mimeType);
325N/A }
325N/A
325N/A /* package */ FastInfosetStreamSOAPCodec(FastInfosetStreamSOAPCodec that) {
325N/A this._soapCodec = (StreamSOAPCodec) that._soapCodec.copy();
325N/A this._retainState = that._retainState;
325N/A this._defaultContentType = that._defaultContentType;
325N/A }
325N/A
325N/A public String getMimeType() {
325N/A return _defaultContentType.getContentType();
325N/A }
325N/A
325N/A public ContentType getStaticContentType(Packet packet) {
325N/A return getContentType(packet.soapAction);
325N/A }
325N/A
325N/A public ContentType encode(Packet packet, OutputStream out) {
325N/A if (packet.getMessage() != null) {
325N/A final XMLStreamWriter writer = getXMLStreamWriter(out);
325N/A try {
325N/A packet.getMessage().writeTo(writer);
325N/A writer.flush();
325N/A } catch (XMLStreamException e) {
325N/A throw new WebServiceException(e);
325N/A }
325N/A }
325N/A return getContentType(packet.soapAction);
325N/A }
325N/A
325N/A public ContentType encode(Packet packet, WritableByteChannel buffer) {
325N/A //TODO: not yet implemented
325N/A throw new UnsupportedOperationException();
325N/A }
325N/A
325N/A public void decode(InputStream in, String contentType, Packet response) throws IOException {
325N/A response.setMessage(
325N/A _soapCodec.decode(getXMLStreamReader(in)));
325N/A }
325N/A
325N/A public void decode(ReadableByteChannel in, String contentType, Packet response) {
325N/A throw new UnsupportedOperationException();
325N/A }
325N/A
325N/A protected abstract StreamHeader createHeader(XMLStreamReader reader, XMLStreamBuffer mark);
325N/A
325N/A protected abstract ContentType getContentType(String soapAction);
325N/A
325N/A private XMLStreamWriter getXMLStreamWriter(OutputStream out) {
325N/A if (_serializer != null) {
325N/A _serializer.setOutputStream(out);
325N/A return _serializer;
325N/A } else {
325N/A return _serializer = FastInfosetCodec.createNewStreamWriter(out, _retainState);
325N/A }
325N/A }
325N/A
325N/A private XMLStreamReader getXMLStreamReader(InputStream in) {
325N/A // If the _retainState is true (FI stateful) then pick up Codec assiciated XMLStreamReader
325N/A if (_retainState) {
325N/A if (_statefulParser != null) {
325N/A _statefulParser.setInputStream(in);
325N/A return _statefulParser;
325N/A } else {
325N/A return _statefulParser = FastInfosetCodec.createNewStreamReader(in, _retainState);
325N/A }
325N/A }
325N/A
325N/A // Otherwise thread assiciated XMLStreamReader
325N/A return READER_FACTORY.doCreate(null, in, false);
325N/A }
325N/A
325N/A /**
325N/A * Creates a new {@link FastInfosetStreamSOAPCodec} instance.
325N/A *
325N/A * @param version the SOAP version of the codec.
325N/A * @return a new {@link FastInfosetStreamSOAPCodec} instance.
325N/A */
325N/A public static FastInfosetStreamSOAPCodec create(StreamSOAPCodec soapCodec, SOAPVersion version) {
325N/A return create(soapCodec, version, false);
325N/A }
325N/A
325N/A /**
325N/A * Creates a new {@link FastInfosetStreamSOAPCodec} instance.
325N/A *
325N/A * @param version the SOAP version of the codec.
325N/A * @param retainState if true the Codec should retain the state of
325N/A * vocabulary tables for multiple encode/decode invocations.
325N/A * @return a new {@link FastInfosetStreamSOAPCodec} instance.
325N/A */
325N/A public static FastInfosetStreamSOAPCodec create(StreamSOAPCodec soapCodec,
325N/A SOAPVersion version, boolean retainState) {
325N/A if(version==null)
325N/A // this decoder is for SOAP, not for XML/HTTP
325N/A throw new IllegalArgumentException();
325N/A switch(version) {
325N/A case SOAP_11:
325N/A return new FastInfosetStreamSOAP11Codec(soapCodec, retainState);
325N/A case SOAP_12:
325N/A return new FastInfosetStreamSOAP12Codec(soapCodec, retainState);
325N/A default:
325N/A throw new AssertionError();
325N/A }
325N/A }
325N/A}