View Javadoc

1   /*
2    * Copyright 2006 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.apache.axiom.om.OMElement;
24  import org.apache.axiom.om.OMException;
25  import org.apache.axiom.om.OMNamespace;
26  import org.apache.axiom.soap.SOAPFactory;
27  import org.apache.axiom.soap.SOAPHeader;
28  import org.apache.axiom.soap.SOAPHeaderBlock;
29  
30  import org.springframework.ws.soap.SoapHeader;
31  import org.springframework.ws.soap.SoapHeaderElement;
32  import org.springframework.ws.soap.SoapHeaderException;
33  import org.springframework.xml.namespace.QNameUtils;
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      public Iterator examineMustUnderstandHeaderElements(String role) {
76          try {
77              return new AxiomSoapHeaderElementIterator(getAxiomHeader().examineMustUnderstandHeaderBlocks(role));
78          }
79          catch (OMException ex) {
80              throw new AxiomSoapHeaderException(ex);
81          }
82      }
83  
84      public Iterator examineAllHeaderElements() {
85          try {
86              return new AxiomSoapHeaderElementIterator(getAxiomHeader().examineAllHeaderBlocks());
87          }
88          catch (OMException ex) {
89              throw new AxiomSoapHeaderException(ex);
90          }
91      }
92  
93      public Iterator examineHeaderElements(QName name) throws SoapHeaderException {
94          try {
95              return new AxiomSoapHeaderElementIterator(getAxiomHeader().getChildrenWithName(name));
96          }
97          catch (OMException ex) {
98              throw new AxiomSoapHeaderException(ex);
99          }
100     }
101 
102     protected SOAPHeader getAxiomHeader() {
103         return (SOAPHeader) getAxiomElement();
104     }
105 
106     protected class AxiomSoapHeaderElementIterator implements Iterator {
107 
108         private final Iterator axiomIterator;
109 
110         protected AxiomSoapHeaderElementIterator(Iterator axiomIterator) {
111             this.axiomIterator = axiomIterator;
112         }
113 
114         public boolean hasNext() {
115             return axiomIterator.hasNext();
116         }
117 
118         public Object next() {
119             try {
120                 SOAPHeaderBlock axiomHeaderBlock = (SOAPHeaderBlock) axiomIterator.next();
121                 return new AxiomSoapHeaderElement(axiomHeaderBlock, getAxiomFactory());
122             }
123             catch (OMException ex) {
124                 throw new AxiomSoapHeaderException(ex);
125             }
126         }
127 
128         public void remove() {
129             axiomIterator.remove();
130         }
131     }
132 }