001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.impl.message;
024:
025: import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
026: import com.sun.xml.stream.buffer.stax.StreamWriterBufferCreator;
027: import com.sun.xml.ws.api.SOAPVersion;
028: import com.sun.xml.ws.api.message.Message;
029: import com.sun.xml.ws.message.source.PayloadSourceMessage;
030: import com.sun.xml.ws.message.stream.StreamMessage;
031: import com.sun.xml.ws.security.opt.api.SecurityElement;
032: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
033: import com.sun.xml.ws.security.opt.impl.util.NamespaceContextEx;
034: import com.sun.xml.ws.security.opt.impl.util.XMLStreamFilterWithId;
035: import javax.xml.stream.XMLStreamException;
036: import javax.xml.stream.XMLStreamWriter;
037: import com.sun.xml.wss.impl.MessageConstants;
038: import javax.xml.stream.XMLStreamReader;
039:
040: /**
041: *
042: * @author K.Venugopal@sun.com
043: */
044: public class SOAPBody {
045: private static final String BODY = "Body";
046: private static final String BODY_PREFIX = "S";
047:
048: private Message message;
049: private SOAPVersion soapVersion;
050: private byte[] byteStream;
051: private SecurityElement bodyContent;
052: private String wsuId;
053: private String contentId;
054: private MutableXMLStreamBuffer buffer = null;
055:
056: public SOAPBody(Message message) {
057: this .message = message;
058: this .soapVersion = SOAPVersion.SOAP_11;
059: }
060:
061: /**
062: *
063: * Creates a new instance of SOAPBody
064: *
065: */
066:
067: public SOAPBody(Message message, SOAPVersion soapVersion) {
068: this .message = message;
069: this .soapVersion = soapVersion;
070: }
071:
072: public SOAPBody(byte[] payLoad, SOAPVersion soapVersion) {
073: byteStream = payLoad;
074: this .soapVersion = soapVersion;
075: }
076:
077: public SOAPBody(SecurityElement se, SOAPVersion soapVersion) {
078: bodyContent = se;
079: this .soapVersion = soapVersion;
080: }
081:
082: public SOAPVersion getSOAPVersion() {
083: return soapVersion;
084: }
085:
086: public String getId() {
087: return wsuId;
088: }
089:
090: public void setId(String id) {
091: wsuId = id;
092: }
093:
094: public String getBodyContentId() {
095: if (contentId != null)
096: return contentId;
097: else if (bodyContent != null)
098: return bodyContent.getId();
099: return null;
100: }
101:
102: public void setBodyContentId(String id) {
103: this .contentId = id;
104: }
105:
106: public void writePayload(XMLStreamWriter writer)
107: throws XMLStreamException {
108: if (this .message != null) {
109: if (getBodyContentId() == null)
110: this .message.writePayloadTo(writer);
111: else {
112: boolean isSOAP12 = (this .soapVersion == SOAPVersion.SOAP_12) ? true
113: : false;
114: XMLStreamFilterWithId xmlStreamFilterWithId = new XMLStreamFilterWithId(
115: writer, new NamespaceContextEx(isSOAP12),
116: getBodyContentId());
117: this .message.writePayloadTo(xmlStreamFilterWithId);
118: }
119: } else if (bodyContent != null) {
120: ((SecurityElementWriter) bodyContent).writeTo(writer);
121: } else if (buffer != null) {
122: buffer.writeToXMLStreamWriter(writer);
123: } else {
124: throw new UnsupportedOperationException();
125: //TODO
126: }
127: }
128:
129: public void writeTo(XMLStreamWriter writer)
130: throws XMLStreamException {
131: writer.writeStartElement(BODY_PREFIX, BODY,
132: this .soapVersion.nsUri);
133: if (wsuId != null) {
134: writer.writeAttribute("wsu", MessageConstants.WSU_NS, "Id",
135: wsuId);
136: }
137: writePayload(writer);
138: writer.writeEndElement();
139: //writer.flush();
140: }
141:
142: public String getPayloadNamespaceURI() {
143: if (message != null) {
144: return message.getPayloadNamespaceURI();
145: }
146: if (bodyContent != null) {
147: return bodyContent.getNamespaceURI();
148: }
149: return null;
150: }
151:
152: public String getPayloadLocalPart() {
153: if (message != null) {
154: return message.getPayloadLocalPart();
155: }
156: if (bodyContent != null) {
157: return bodyContent.getLocalPart();
158: }
159: return null;
160: }
161:
162: public XMLStreamReader read() throws XMLStreamException {
163: if (message != null) {
164: return message.readPayload();
165: } else if (bodyContent != null) {
166: return bodyContent.readHeader();
167: }
168: throw new XMLStreamException("Invalid SOAPBody");
169: }
170:
171: public void cachePayLoad() throws XMLStreamException {
172: if (message != null) {
173: if (message instanceof StreamMessage
174: || message instanceof PayloadSourceMessage
175: || message instanceof com.sun.xml.ws.message.jaxb.JAXBMessage) {
176: if (buffer == null) {
177: buffer = new MutableXMLStreamBuffer();
178: StreamWriterBufferCreator creator = new StreamWriterBufferCreator(
179: buffer);
180: this.writePayload(creator);
181: this.message = null;
182: }
183: }
184: }
185: }
186: }
|