View Javadoc
1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.axiom;
18  
19  import java.util.Iterator;
20  import javax.xml.namespace.QName;
21  import javax.xml.transform.Result;
22  
23  import org.springframework.ws.soap.SoapHeader;
24  import org.springframework.ws.soap.SoapHeaderElement;
25  import org.springframework.ws.soap.SoapHeaderException;
26  import org.springframework.xml.namespace.QNameUtils;
27  
28  import org.apache.axiom.om.OMElement;
29  import org.apache.axiom.om.OMException;
30  import org.apache.axiom.om.OMNamespace;
31  import org.apache.axiom.soap.SOAPFactory;
32  import org.apache.axiom.soap.SOAPHeader;
33  import org.apache.axiom.soap.SOAPHeaderBlock;
34  
35  /**
36   * Axiom-specific version of <code>org.springframework.ws.soap.SoapHeader</code>.
37   *
38   * @author Arjen Poutsma
39   * @since 1.0.0
40   */
41  abstract class AxiomSoapHeader extends AxiomSoapElement implements SoapHeader {
42  
43      AxiomSoapHeader(SOAPHeader axiomHeader, SOAPFactory axiomFactory) {
44          super(axiomHeader, axiomFactory);
45      }
46  
47      public Result getResult() {
48          return new AxiomResult(getAxiomHeader(), getAxiomFactory());
49      }
50  
51      public SoapHeaderElement addHeaderElement(QName name) {
52          try {
53              OMNamespace namespace =
54                      getAxiomFactory().createOMNamespace(name.getNamespaceURI(), QNameUtils.getPrefix(name));
55              SOAPHeaderBlock axiomHeaderBlock = getAxiomHeader().addHeaderBlock(name.getLocalPart(), namespace);
56              return new AxiomSoapHeaderElement(axiomHeaderBlock, getAxiomFactory());
57          }
58          catch (OMException ex) {
59              throw new AxiomSoapHeaderException(ex);
60          }
61      }
62  
63      public void removeHeaderElement(QName name) throws SoapHeaderException {
64          try {
65              OMElement element = getAxiomHeader().getFirstChildWithName(name);
66              if (element != null) {
67                  element.detach();
68              }
69          }
70          catch (OMException ex) {
71              throw new AxiomSoapHeaderException(ex);
72          }
73      }
74  
75      @SuppressWarnings("unchecked")
76      public Iterator<SoapHeaderElement> examineMustUnderstandHeaderElements(String role) {
77          try {
78              return new AxiomSoapHeaderElementIterator(getAxiomHeader().examineMustUnderstandHeaderBlocks(role));
79          }
80          catch (OMException ex) {
81              throw new AxiomSoapHeaderException(ex);
82          }
83      }
84  
85      @SuppressWarnings("unchecked")
86      public Iterator<SoapHeaderElement> examineAllHeaderElements() {
87          try {
88              return new AxiomSoapHeaderElementIterator(getAxiomHeader().examineAllHeaderBlocks());
89          }
90          catch (OMException ex) {
91              throw new AxiomSoapHeaderException(ex);
92          }
93      }
94  
95      @SuppressWarnings("unchecked")
96      public Iterator<SoapHeaderElement> examineHeaderElements(QName name) throws SoapHeaderException {
97          try {
98              return new AxiomSoapHeaderElementIterator(getAxiomHeader().getChildrenWithName(name));
99          }
100         catch (OMException ex) {
101             throw new AxiomSoapHeaderException(ex);
102         }
103     }
104 
105     protected SOAPHeader getAxiomHeader() {
106         return (SOAPHeader) getAxiomElement();
107     }
108 
109     protected class AxiomSoapHeaderElementIterator implements Iterator<SoapHeaderElement> {
110 
111         private final Iterator<SOAPHeaderBlock> axiomIterator;
112 
113         protected AxiomSoapHeaderElementIterator(Iterator<SOAPHeaderBlock> axiomIterator) {
114             this.axiomIterator = axiomIterator;
115         }
116 
117         public boolean hasNext() {
118             return axiomIterator.hasNext();
119         }
120 
121         public SoapHeaderElement next() {
122             try {
123                 SOAPHeaderBlock axiomHeaderBlock = axiomIterator.next();
124                 return new AxiomSoapHeaderElement(axiomHeaderBlock, getAxiomFactory());
125             }
126             catch (OMException ex) {
127                 throw new AxiomSoapHeaderException(ex);
128             }
129         }
130 
131         public void remove() {
132             axiomIterator.remove();
133         }
134     }
135 }