001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.ws.encoding.jaxb;
027:
028: import com.sun.xml.internal.ws.pept.ept.MessageInfo;
029: import com.sun.xml.internal.bind.api.BridgeContext;
030: import static com.sun.xml.internal.ws.client.BindingProviderProperties.JAXB_OUTPUTSTREAM;
031: import com.sun.xml.internal.ws.encoding.soap.DeserializationException;
032: import com.sun.xml.internal.ws.encoding.soap.SerializationException;
033: import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
034:
035: import javax.xml.namespace.NamespaceContext;
036: import javax.xml.namespace.QName;
037: import javax.xml.stream.XMLStreamConstants;
038: import javax.xml.stream.XMLStreamException;
039: import javax.xml.stream.XMLStreamReader;
040: import javax.xml.stream.XMLStreamWriter;
041: import java.io.IOException;
042: import java.io.OutputStream;
043:
044: public class RpcLitPayloadSerializer {
045:
046: /*
047: * Uses BridgeContext to serialize the rpc/lit payload. First it writes
048: * the operation, and then it serializes each parameter
049: */
050: public static void serialize(RpcLitPayload obj,
051: BridgeContext bridgeContext, MessageInfo messageInfo,
052: XMLStreamWriter writer) {
053: try {
054: QName op = obj.getOperation();
055: String opURI = op.getNamespaceURI();
056:
057: writer.writeStartElement("ans", op.getLocalPart(), opURI);
058: writer.setPrefix("ans", opURI);
059: writer.writeNamespace("ans", opURI);
060:
061: // Pass output stream directly to JAXB when available
062: OutputStream os = (OutputStream) messageInfo
063: .getMetaData(JAXB_OUTPUTSTREAM);
064: if (os != null) {
065: /*
066: * Make sure that current element is closed before passing the
067: * output stream to JAXB. Using Zephyr, it suffices to write
068: * an empty string (TODO: other StAX impls?).
069: */
070: writer.writeCharacters("");
071:
072: // Flush output of StAX serializer
073: writer.flush();
074:
075: NamespaceContext nsc = writer.getNamespaceContext();
076:
077: // Let JAXB serialize each param to the output stream
078: for (JAXBBridgeInfo param : obj.getBridgeParameters()) {
079: param.serialize(bridgeContext, os, nsc);
080: }
081: } else {
082: // Otherwise, use a StAX writer
083: for (JAXBBridgeInfo param : obj.getBridgeParameters()) {
084: param.serialize(bridgeContext, writer);
085: }
086: }
087:
088: writer.writeEndElement(); // </ans:operation>
089: } catch (XMLStreamException e) {
090: throw new SerializationException(e);
091: }
092: }
093:
094: public static void serialize(RpcLitPayload obj,
095: BridgeContext bridgeContext, OutputStream writer) {
096: QName op = obj.getOperation();
097: String opURI = op.getNamespaceURI();
098: String startElm = "<ans:" + op.getLocalPart() + " xmlns:ans=\""
099: + opURI + "\">";
100: String endElm = "</ans:" + op.getLocalPart() + ">";
101: try {
102: writer.write(startElm.getBytes());
103: for (JAXBBridgeInfo param : obj.getBridgeParameters()) {
104: param.serialize(bridgeContext, writer, null);
105: }
106: writer.write(endElm.getBytes());
107: } catch (IOException e) {
108: throw new SerializationException(e);
109: }
110: }
111:
112: /*
113: * Uses BridgeContext to deserialize the rpc/lit payload. First it reads
114: * the operation element, and then it deserializes each parameter. If the
115: * expected parameter is not found, it throws an exception
116: */
117: public static void deserialize(XMLStreamReader reader,
118: RpcLitPayload payload, BridgeContext bridgeContext) {
119: XMLStreamReaderUtil.nextElementContent(reader); // </operation> or <partName>
120: for (JAXBBridgeInfo param : payload.getBridgeParameters()) {
121: // throw exception if the part accessor name is not what we expect
122: QName partName = reader.getName();
123: if (!partName.equals(param.getName())) {
124: throw new DeserializationException(
125: "xsd.unexpectedElementName", new Object[] {
126: param.getName(), partName });
127: }
128: param.deserialize(reader, bridgeContext);
129:
130: // reader could be left on CHARS token rather than <partName>
131: if (reader.getEventType() == XMLStreamConstants.CHARACTERS
132: && reader.isWhiteSpace()) {
133: XMLStreamReaderUtil.nextContent(reader);
134: }
135: }
136: XMLStreamReaderUtil.nextElementContent(reader); // </env:body>
137: }
138: }
|