001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.saaj;
020:
021: import org.apache.axiom.om.impl.dom.DocumentImpl;
022: import org.apache.axiom.om.impl.dom.NodeImpl;
023: import org.apache.axiom.om.impl.dom.TextImpl;
024: import org.apache.axiom.soap.SOAPFactory;
025: import org.apache.axiom.soap.impl.dom.soap11.SOAP11BodyImpl;
026: import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;
027: import org.apache.axiom.soap.impl.dom.soap11.SOAP11HeaderImpl;
028: import org.apache.axiom.soap.impl.dom.soap12.SOAP12Factory;
029: import org.apache.axiom.soap.impl.dom.soap12.SOAP12HeaderImpl;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Node;
032:
033: import javax.xml.soap.Name;
034: import javax.xml.soap.SOAPBody;
035: import javax.xml.soap.SOAPElement;
036: import javax.xml.soap.SOAPException;
037: import javax.xml.soap.SOAPHeader;
038:
039: /**
040: *
041: */
042: public class SOAPEnvelopeImpl extends SOAPElementImpl implements
043: javax.xml.soap.SOAPEnvelope {
044:
045: private org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl omSOAPEnvelope;
046:
047: public SOAPEnvelopeImpl(
048: final org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl envelope) {
049: super (envelope);
050: omSOAPEnvelope = envelope;
051: }
052:
053: public void setOwnerDocument(Document document) {
054: super .setOwnerDocument((DocumentImpl) document);
055: }
056:
057: public org.apache.axiom.soap.SOAPEnvelope getOMEnvelope() {
058: return omSOAPEnvelope;
059: }
060:
061: /**
062: * Creates a new <CODE>Name</CODE> object initialized with the given local name, namespace
063: * prefix, and namespace URI.
064: * <p/>
065: * <P>This factory method creates <CODE>Name</CODE> objects for use in the SOAP/XML document.
066: *
067: * @param localName a <CODE>String</CODE> giving the local name
068: * @param prefix a <CODE>String</CODE> giving the prefix of the namespace
069: * @param uri a <CODE>String</CODE> giving the URI of the namespace
070: * @return a <CODE>Name</CODE> object initialized with the given local name, namespace prefix,
071: * and namespace URI
072: * @throws javax.xml.soap.SOAPException if there is a SOAP error
073: */
074: public Name createName(String localName, String prefix, String uri)
075: throws SOAPException {
076: try {
077: return new PrefixedQName(uri, localName, prefix);
078: } catch (Exception e) {
079: throw new SOAPException(e);
080: }
081: }
082:
083: /**
084: * Creates a new <CODE>Name</CODE> object initialized with the given local name.
085: * <p/>
086: * <P>This factory method creates <CODE>Name</CODE> objects for use in the SOAP/XML document.
087: *
088: * @param localName a <CODE>String</CODE> giving the local name
089: * @return a <CODE>Name</CODE> object initialized with the given local name
090: * @throws javax.xml.soap.SOAPException if there is a SOAP error
091: */
092: public Name createName(String localName) throws SOAPException {
093: try {
094: return new PrefixedQName(null, localName, null);
095: } catch (Exception e) {
096: throw new SOAPException(e);
097: }
098: }
099:
100: /**
101: * Returns the <CODE>SOAPHeader</CODE> object for this <CODE> SOAPEnvelope</CODE> object.
102: * <p/>
103: * <P>A new <CODE>SOAPMessage</CODE> object is by default created with a
104: * <CODE>SOAPEnvelope</CODE> object that contains an empty <CODE>SOAPHeader</CODE> object. As a
105: * result, the method <CODE>getHeader</CODE> will always return a <CODE>SOAPHeader</CODE> object
106: * unless the header has been removed and a new one has not been added.
107: *
108: * @return the <CODE>SOAPHeader</CODE> object or <CODE> null</CODE> if there is none
109: * @throws javax.xml.soap.SOAPException if there is a problem obtaining the <CODE>SOAPHeader</CODE>
110: * object
111: */
112: public SOAPHeader getHeader() throws SOAPException {
113: return (SOAPHeader) toSAAJNode((org.w3c.dom.Node) omSOAPEnvelope
114: .getHeader());
115: }
116:
117: /**
118: * Returns the <CODE>SOAPBody</CODE> object associated with this <CODE>SOAPEnvelope</CODE>
119: * object.
120: * <p/>
121: * <P>A new <CODE>SOAPMessage</CODE> object is by default created with a
122: * <CODE>SOAPEnvelope</CODE> object that contains an empty <CODE>SOAPBody</CODE> object. As a
123: * result, the method <CODE>getBody</CODE> will always return a <CODE>SOAPBody</CODE> object
124: * unless the body has been removed and a new one has not been added.
125: *
126: * @return the <CODE>SOAPBody</CODE> object for this <CODE> SOAPEnvelope</CODE> object or
127: * <CODE>null</CODE> if there is none
128: * @throws javax.xml.soap.SOAPException if there is a problem obtaining the <CODE>SOAPBody</CODE>
129: * object
130: */
131: public SOAPBody getBody() throws SOAPException {
132: return (SOAPBody) toSAAJNode((org.w3c.dom.Node) omSOAPEnvelope
133: .getBody());
134: }
135:
136: /**
137: * Creates a <CODE>SOAPHeader</CODE> object and sets it as the <CODE>SOAPHeader</CODE> object
138: * for this <CODE> SOAPEnvelope</CODE> object.
139: * <p/>
140: * <P>It is illegal to add a header when the envelope already contains a header. Therefore, this
141: * method should be called only after the existing header has been removed.
142: *
143: * @return the new <CODE>SOAPHeader</CODE> object
144: * @throws javax.xml.soap.SOAPException if this <CODE> SOAPEnvelope</CODE> object already
145: * contains a valid <CODE>SOAPHeader</CODE> object
146: */
147: public SOAPHeader addHeader() throws SOAPException {
148: org.apache.axiom.soap.SOAPHeader header = omSOAPEnvelope
149: .getHeader();
150: if (header == null) {
151: SOAPHeaderImpl saajSOAPHeader;
152: if (this .element.getOMFactory() instanceof SOAP11Factory) {
153: header = new SOAP11HeaderImpl(omSOAPEnvelope,
154: (SOAPFactory) this .element.getOMFactory());
155: saajSOAPHeader = new SOAPHeaderImpl(header);
156: saajSOAPHeader.setParentElement(this );
157: } else {
158: header = new SOAP12HeaderImpl(omSOAPEnvelope,
159: (SOAPFactory) this .element.getOMFactory());
160: saajSOAPHeader = new SOAPHeaderImpl(header);
161: saajSOAPHeader.setParentElement(this );
162: }
163: ((NodeImpl) omSOAPEnvelope.getHeader()).setUserData(
164: SAAJ_NODE, saajSOAPHeader, null);
165: return saajSOAPHeader;
166: } else {
167: throw new SOAPException(
168: "Header already present, can't set header again without "
169: + "deleting the existing header. "
170: + "Use getHeader() method and detach the header instead.");
171: }
172: }
173:
174: /**
175: * Creates a <CODE>SOAPBody</CODE> object and sets it as the <CODE>SOAPBody</CODE> object for
176: * this <CODE> SOAPEnvelope</CODE> object.
177: * <p/>
178: * <P>It is illegal to add a body when the envelope already contains a body. Therefore, this
179: * method should be called only after the existing body has been removed.
180: *
181: * @return the new <CODE>SOAPBody</CODE> object
182: * @throws javax.xml.soap.SOAPException if this <CODE> SOAPEnvelope</CODE> object already
183: * contains a valid <CODE>SOAPBody</CODE> object
184: */
185: public SOAPBody addBody() throws SOAPException {
186: org.apache.axiom.soap.SOAPBody body = omSOAPEnvelope.getBody();
187: if (body == null) {
188: body = new SOAP11BodyImpl(omSOAPEnvelope,
189: (SOAPFactory) this .element.getOMFactory());
190: SOAPBodyImpl saajSOAPBody = new SOAPBodyImpl(body);
191: saajSOAPBody.setParentElement(this );
192: ((NodeImpl) omSOAPEnvelope.getBody()).setUserData(
193: SAAJ_NODE, saajSOAPBody, null);
194: return saajSOAPBody;
195: } else {
196: throw new SOAPException(
197: "Body already present, can't set body again without "
198: + "deleting the existing body. Use getBody() method instead.");
199: }
200: }
201:
202: public SOAPElement addTextNode(String text) throws SOAPException {
203: Node firstChild = element.getFirstChild();
204: if (firstChild instanceof org.w3c.dom.Text) {
205: ((org.w3c.dom.Text) firstChild).setData(text);
206: } else {
207: // Else this is a header
208: TextImpl doomText = new TextImpl(text, this .element
209: .getOMFactory());
210: doomText.setNextOMSibling((NodeImpl) firstChild);
211: doomText.setPreviousOMSibling(null);
212: element.setFirstChild(doomText);
213: ((NodeImpl) firstChild).setPreviousOMSibling(doomText);
214: }
215: return this ;
216: }
217:
218: /**
219: * Override SOAPElement.addAttribute SOAP1.2 should not allow encodingStyle attribute to be set
220: * on Envelop
221: */
222: public SOAPElement addAttribute(Name name, String value)
223: throws SOAPException {
224: if (this .element.getOMFactory() instanceof SOAP12Factory) {
225: if ("encodingStyle".equals(name.getLocalName())) {
226: throw new SOAPException(
227: "SOAP1.2 does not allow encodingStyle attribute to be set "
228: + "on Envelope");
229: }
230: }
231: return super .addAttribute(name, value);
232: }
233:
234: /**
235: * Override SOAPElement.addChildElement SOAP 1.2 should not allow element to be added after body
236: * element
237: */
238: public SOAPElement addChildElement(Name name) throws SOAPException {
239: if (this .element.getOMFactory() instanceof SOAP12Factory) {
240: throw new SOAPException(
241: "Cannot add elements after body element");
242: } else if (this .element.getOMFactory() instanceof SOAP11Factory) {
243: //Let elements to be added any where.
244: return super.addChildElement(name);
245: }
246: return null;
247: }
248: }
|