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.OMNamespace;
022: import org.apache.axiom.om.OMNode;
023: import org.apache.axiom.om.impl.dom.ElementImpl;
024: import org.apache.axiom.om.impl.dom.NamespaceImpl;
025: import org.apache.axiom.om.impl.dom.NodeImpl;
026: import org.apache.axiom.soap.SOAPFactory;
027: import org.apache.axiom.soap.SOAPHeaderBlock;
028: import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;
029: import org.apache.axiom.soap.impl.dom.soap11.SOAP11HeaderBlockImpl;
030: import org.apache.axiom.soap.impl.dom.soap12.SOAP12Factory;
031: import org.apache.axiom.soap.impl.dom.soap12.SOAP12HeaderBlockImpl;
032: import org.apache.axis2.namespace.Constants;
033:
034: import javax.xml.namespace.QName;
035: import javax.xml.soap.Name;
036: import javax.xml.soap.Node;
037: import javax.xml.soap.SOAPElement;
038: import javax.xml.soap.SOAPException;
039: import javax.xml.soap.SOAPHeader;
040: import javax.xml.soap.SOAPHeaderElement;
041: import java.util.ArrayList;
042: import java.util.Collection;
043: import java.util.Iterator;
044:
045: public class SOAPHeaderImpl extends SOAPElementImpl implements
046: SOAPHeader {
047:
048: private org.apache.axiom.soap.SOAPHeader omSOAPHeader;
049:
050: /**
051: * Constructor
052: *
053: * @param header
054: */
055: public SOAPHeaderImpl(org.apache.axiom.soap.SOAPHeader header) {
056: super ((ElementImpl) header);
057: omSOAPHeader = header;
058: }
059:
060: /* (non-Javadoc)
061: * @see javax.xml.soap.SOAPElement#addChildElement(java.lang.String)
062: */
063: public SOAPElement addChildElement(String localName)
064: throws SOAPException {
065: return addHeaderElement(new PrefixedQName(null, localName, null));
066: }
067:
068: /* (non-Javadoc)
069: * @see javax.xml.soap.SOAPElement#addChildElement(java.lang.String, java.lang.String)
070: */
071: public SOAPElement addChildElement(String localName, String prefix)
072: throws SOAPException {
073: String namespaceURI = getNamespaceURI(prefix);
074:
075: if (namespaceURI == null) {
076: throw new SOAPException(
077: "Namespace not declared for the give prefix: "
078: + prefix);
079: }
080: return addChildElement(localName, prefix, namespaceURI);
081: }
082:
083: /* (non-Javadoc)
084: * @see javax.xml.soap.SOAPElement#addChildElement(java.lang.String, java.lang.String, java.lang.String)
085: */
086: public SOAPElement addChildElement(String localName, String prefix,
087: String uri) throws SOAPException {
088: OMNamespace ns = new NamespaceImpl(uri, prefix);
089: SOAPHeaderBlock headerBlock = null;
090: if (this .element.getOMFactory() instanceof SOAP11Factory) {
091: headerBlock = new SOAP11HeaderBlockImpl(localName, ns,
092: omSOAPHeader, (SOAPFactory) this .element
093: .getOMFactory());
094: } else {
095: headerBlock = new SOAP12HeaderBlockImpl(localName, ns,
096: omSOAPHeader, (SOAPFactory) this .element
097: .getOMFactory());
098: }
099: SOAPHeaderElementImpl soapHeaderElement = new SOAPHeaderElementImpl(
100: headerBlock);
101: element.setUserData(SAAJ_NODE, this , null);
102: soapHeaderElement.element.setUserData(SAAJ_NODE,
103: soapHeaderElement, null);
104: soapHeaderElement.setParentElement(this );
105: return soapHeaderElement;
106: }
107:
108: /* (non-Javadoc)
109: * @see javax.xml.soap.SOAPElement#addChildElement(javax.xml.soap.Name)
110: */
111: public SOAPElement addChildElement(Name name) throws SOAPException {
112: return addHeaderElement(name);
113: }
114:
115: /* (non-Javadoc)
116: * @see javax.xml.soap.SOAPElement#addChildElement(javax.xml.soap.SOAPElement)
117: */
118: public SOAPElement addChildElement(SOAPElement soapElement)
119: throws SOAPException {
120: OMNamespace ns = new NamespaceImpl(soapElement
121: .getNamespaceURI(), soapElement.getPrefix());
122: SOAPHeaderBlock headerBlock = null;
123: if (this .element.getOMFactory() instanceof SOAP11Factory) {
124: headerBlock = new SOAP11HeaderBlockImpl(soapElement
125: .getLocalName(), ns, omSOAPHeader,
126: (SOAPFactory) this .element.getOMFactory());
127: } else {
128: headerBlock = new SOAP12HeaderBlockImpl(soapElement
129: .getLocalName(), ns, omSOAPHeader,
130: (SOAPFactory) this .element.getOMFactory());
131:
132: }
133: SOAPHeaderElementImpl soapHeaderElement = new SOAPHeaderElementImpl(
134: headerBlock);
135: element.setUserData(SAAJ_NODE, this , null);
136: soapHeaderElement.element.setUserData(SAAJ_NODE,
137: soapHeaderElement, null);
138: soapHeaderElement.setParentElement(this );
139: return soapHeaderElement;
140: }
141:
142: /**
143: * Creates a new <CODE>SOAPHeaderElement</CODE> object initialized with the specified name and
144: * adds it to this <CODE>SOAPHeader</CODE> object.
145: *
146: * @param name a <CODE>Name</CODE> object with the name of the new <CODE>SOAPHeaderElement</CODE>
147: * object
148: * @return the new <CODE>SOAPHeaderElement</CODE> object that was inserted into this
149: * <CODE>SOAPHeader</CODE> object
150: * @throws SOAPException if a SOAP error occurs
151: */
152: public SOAPHeaderElement addHeaderElement(Name name)
153: throws SOAPException {
154: if (name.getURI() == null || name.getURI().trim().length() == 0
155: || name.getPrefix() == null
156: || name.getPrefix().trim().length() == 0) {
157: throw new SOAPException(
158: "SOAP1.1 and SOAP1.2 requires all HeaderElements to have "
159: + "qualified namespace.");
160: }
161: OMNamespace ns = new NamespaceImpl(name.getURI(), name
162: .getPrefix());
163:
164: SOAPHeaderBlock headerBlock = null;
165: if (this .element.getOMFactory() instanceof SOAP11Factory) {
166: headerBlock = new SOAP11HeaderBlockImpl(
167: name.getLocalName(), ns, omSOAPHeader,
168: (SOAPFactory) this .element.getOMFactory());
169: } else {
170: headerBlock = new SOAP12HeaderBlockImpl(
171: name.getLocalName(), ns, omSOAPHeader,
172: (SOAPFactory) this .element.getOMFactory());
173: }
174:
175: SOAPHeaderElementImpl soapHeaderElement = new SOAPHeaderElementImpl(
176: headerBlock);
177: element.setUserData(SAAJ_NODE, this , null);
178: soapHeaderElement.element.setUserData(SAAJ_NODE,
179: soapHeaderElement, null);
180: soapHeaderElement.setParentElement(this );
181: return soapHeaderElement;
182: }
183:
184: /**
185: * Returns a list of all the <CODE>SOAPHeaderElement</CODE> objects in this
186: * <CODE>SOAPHeader</CODE> object that have the the specified actor. An actor is a global
187: * attribute that indicates the intermediate parties to whom the message should be sent. An
188: * actor receives the message and then sends it to the next actor. The default actor is the
189: * ultimate intended recipient for the message, so if no actor attribute is included in a
190: * <CODE>SOAPHeader</CODE> object, the message is sent to its ultimate destination.
191: *
192: * @param actor a <CODE>String</CODE> giving the URI of the actor for which to search
193: * @return an <CODE>Iterator</CODE> object over all the <CODE> SOAPHeaderElement</CODE> objects
194: * that contain the specified actor
195: * @see #extractHeaderElements(String) extractHeaderElements(java.lang.String)
196: */
197: public Iterator examineHeaderElements(String actor) {
198: Collection elements = new ArrayList();
199: for (Iterator iterator = omSOAPHeader
200: .examineHeaderBlocks(actor); iterator.hasNext();) {
201: elements.add(toSAAJNode((NodeImpl) iterator.next()));
202: }
203: return elements.iterator();
204: }
205:
206: /**
207: * Returns a list of all the <CODE>SOAPHeaderElement</CODE> objects in this
208: * <CODE>SOAPHeader</CODE> object that have the the specified actor and detaches them from this
209: * <CODE> SOAPHeader</CODE> object.
210: * <p/>
211: * <P>This method allows an actor to process only the parts of the <CODE>SOAPHeader</CODE>
212: * object that apply to it and to remove them before passing the message on to the next actor.
213: *
214: * @param actor a <CODE>String</CODE> giving the URI of the actor for which to search
215: * @return an <CODE>Iterator</CODE> object over all the <CODE> SOAPHeaderElement</CODE> objects
216: * that contain the specified actor
217: * @see #examineHeaderElements(String) examineHeaderElements(java.lang.String)
218: */
219: public Iterator extractHeaderElements(String actor) {
220: Collection elements = new ArrayList();
221: for (Iterator iterator = omSOAPHeader
222: .extractHeaderBlocks(actor); iterator.hasNext();) {
223: elements.add(toSAAJNode((NodeImpl) iterator.next()));
224: }
225: return elements.iterator();
226: }
227:
228: /**
229: * Returns an <code>Iterator</code> over all the <code>SOAPHeaderElement</code> objects in this
230: * <code>SOAPHeader</code> object that have the specified actor and that have a MustUnderstand
231: * attribute whose value is equivalent to <code>true</code>.
232: *
233: * @param actor a <code>String</code> giving the URI of the actor for which to search
234: * @return an <code>Iterator</code> object over all the <code>SOAPHeaderElement</code> objects
235: * that contain the specified actor and are marked as MustUnderstand
236: */
237: public Iterator examineMustUnderstandHeaderElements(String actor) {
238: Collection elements = new ArrayList();
239: for (Iterator iterator = omSOAPHeader
240: .examineMustUnderstandHeaderBlocks(actor); iterator
241: .hasNext();) {
242: elements.add(toSAAJNode((NodeImpl) iterator.next()));
243: }
244: return elements.iterator();
245: }
246:
247: /**
248: * Returns an <code>Iterator</code> over all the <code>SOAPHeaderElement</code> objects in this
249: * <code>SOAPHeader</code> object.
250: *
251: * @return an <code>Iterator</code> object over all the <code>SOAPHeaderElement</code> objects
252: * contained by this <code>SOAPHeader</code>
253: */
254: public Iterator examineAllHeaderElements() {
255: Collection elements = new ArrayList();
256: for (Iterator iterator = omSOAPHeader.examineAllHeaderBlocks(); iterator
257: .hasNext();) {
258: elements.add(toSAAJNode((NodeImpl) iterator.next()));
259: }
260: return elements.iterator();
261: }
262:
263: /**
264: * Returns an <code>Iterator</code> over all the <code>SOAPHeaderElement</code> objects in this
265: * <code>SOAPHeader </code> object and detaches them from this <code>SOAPHeader</code> object.
266: *
267: * @return an <code>Iterator</code> object over all the <code>SOAPHeaderElement</code> objects
268: * contained by this <code>SOAPHeader</code>
269: */
270: public Iterator extractAllHeaderElements() {
271: Collection elements = new ArrayList();
272: for (Iterator iterator = omSOAPHeader.extractAllHeaderBlocks(); iterator
273: .hasNext();) {
274: elements.add(toSAAJNode((NodeImpl) iterator.next()));
275: }
276: return elements.iterator();
277: }
278:
279: public SOAPHeaderElement addHeaderElement(QName qname)
280: throws SOAPException {
281: return (SOAPHeaderElement) addChildElement(
282: qname.getLocalPart(), qname.getPrefix(), qname
283: .getNamespaceURI());
284: }
285:
286: /**
287: * Creates a new NotUnderstood SOAPHeaderElement object initialized with the specified name and
288: * adds it to this SOAPHeader object. This operation is supported only by SOAP 1.2
289: *
290: * @param name - a QName object with the name of the SOAPHeaderElement object that was not
291: * understood.
292: * @return the new SOAPHeaderElement object that was inserted into this SOAPHeader object
293: * @throws SOAPException- if a SOAP error occurs. java.lang.UnsupportedOperationException - if
294: * this is a SOAP 1.1 Header.
295: */
296:
297: public SOAPHeaderElement addNotUnderstoodHeaderElement(QName qname)
298: throws SOAPException {
299: SOAPHeaderBlock soapHeaderBlock = null;
300: OMNamespace ns = new NamespaceImpl(qname.getNamespaceURI(),
301: qname.getPrefix());
302: if (this .element.getOMFactory() instanceof SOAP11Factory) {
303: throw new UnsupportedOperationException();
304: } else {
305: soapHeaderBlock = this .omSOAPHeader.addHeaderBlock(
306: Constants.ELEM_NOTUNDERSTOOD, this .element
307: .getNamespace());
308: soapHeaderBlock.addAttribute(qname.getLocalPart(), qname
309: .getPrefix(), ns);
310: }
311: SOAPHeaderElementImpl soapHeaderElementImpl = new SOAPHeaderElementImpl(
312: soapHeaderBlock);
313: return soapHeaderElementImpl;
314: }
315:
316: /**
317: * Creates a new Upgrade SOAPHeaderElement object initialized with the specified List of
318: * supported SOAP URIs and adds it to this SOAPHeader object. This operation is supported on
319: * both SOAP 1.1 and SOAP 1.2 header.
320: *
321: * @param supportedSOAPURIs - an Iterator object with the URIs of SOAP versions supported.
322: * @return the new SOAPHeaderElement object that was inserted into this SOAPHeader object
323: * @throws SOAPException - if a SOAP error occurs.
324: */
325: public SOAPHeaderElement addUpgradeHeaderElement(Iterator iterator)
326: throws SOAPException {
327: SOAPHeaderBlock upgrade = this .omSOAPHeader.addHeaderBlock(
328: Constants.ELEM_UPGRADE, this .element.getNamespace());
329:
330: int index = 0;
331: String prefix = "ns";
332: while (iterator.hasNext()) {
333: index++;
334: String supported = (String) iterator.next();
335:
336: OMNamespace namespace = new NamespaceImpl(supported, prefix
337: + index);
338:
339: if (this .element.getOMFactory() instanceof SOAP11Factory) {
340: SOAP11HeaderBlockImpl supportedEnvelop = new SOAP11HeaderBlockImpl(
341: Constants.ELEM_SUPPORTEDENVELOPE, namespace,
342: (SOAPFactory) this .element.getOMFactory());
343: supportedEnvelop.addAttribute(Constants.ATTR_QNAME,
344: prefix + index + ":" + Constants.ELEM_ENVELOPE,
345: null);
346: upgrade.addChild(supportedEnvelop);
347: } else {
348: SOAP12HeaderBlockImpl supportedEnvelop = new SOAP12HeaderBlockImpl(
349: Constants.ELEM_SUPPORTEDENVELOPE, namespace,
350: (SOAPFactory) this .element.getOMFactory());
351: supportedEnvelop.addAttribute(Constants.ATTR_QNAME,
352: prefix + index + ":" + Constants.ELEM_ENVELOPE,
353: null);
354: upgrade.addChild(supportedEnvelop);
355: }
356: }
357: SOAPHeaderElementImpl soapHeaderElementImpl = new SOAPHeaderElementImpl(
358: upgrade);
359: return soapHeaderElementImpl;
360: }
361:
362: public SOAPHeaderElement addUpgradeHeaderElement(String[] as)
363: throws SOAPException {
364: ArrayList supportedEnvelops = new ArrayList();
365: for (int a = 0; a < as.length; a++) {
366: String supported = (String) as[a];
367: supportedEnvelops.add(supported);
368: }
369: if (supportedEnvelops.size() > 0) {
370: return addUpgradeHeaderElement(supportedEnvelops.iterator());
371: }
372: return null;
373: }
374:
375: public SOAPHeaderElement addUpgradeHeaderElement(String s)
376: throws SOAPException {
377: if (s == null && s.trim().length() > 0) {
378: return null;
379: }
380: ArrayList supportedEnvelops = new ArrayList();
381: supportedEnvelops.add(s);
382: return addUpgradeHeaderElement(supportedEnvelops.iterator());
383: }
384:
385: public SOAPElement addTextNode(String text) throws SOAPException {
386: if (this .element.getOMFactory() instanceof SOAP11Factory) {
387: return super .addTextNode(text);
388: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
389: throw new SOAPException(
390: "Cannot add text node to SOAPHeader");
391: } else {
392: return null;
393: }
394: }
395:
396: public Iterator getChildElements(Name name) {
397: QName qName = new QName(name.getURI(), name.getLocalName());
398: return getChildren(element.getChildrenWithName(qName));
399: }
400:
401: public Iterator getChildElements() {
402: return getChildren(element.getChildren());
403: }
404:
405: private Iterator getChildren(Iterator childIter) {
406: Collection childElements = new ArrayList();
407: while (childIter.hasNext()) {
408: org.w3c.dom.Node domNode = (org.w3c.dom.Node) childIter
409: .next();
410: Node saajNode = toSAAJNode(domNode);
411: if (saajNode instanceof javax.xml.soap.Text) {
412: childElements.add(saajNode);
413: } else if (!(saajNode instanceof SOAPHeaderElement)) {
414: // silently replace node, as per saaj 1.2 spec
415: SOAPHeaderElement headerEle = new SOAPHeaderElementImpl(
416: (SOAPHeaderBlock) domNode);
417: ((NodeImpl) domNode).setUserData(SAAJ_NODE, headerEle,
418: null);
419: childElements.add(headerEle);
420: } else {
421: childElements.add(saajNode);
422: }
423: }
424: return childElements.iterator();
425: }
426:
427: public void detachNode() {
428: this .detach();
429: }
430:
431: public OMNode detach() {
432: OMNode omNode = omSOAPHeader.detach();
433: parentElement = null;
434: return omNode;
435: }
436: }
|