View Javadoc

1   /*
2    * Copyright 2005-2011 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.saaj;
18  
19  import java.util.Iterator;
20  import javax.xml.namespace.QName;
21  import javax.xml.soap.SOAPElement;
22  import javax.xml.soap.SOAPException;
23  import javax.xml.soap.SOAPHeader;
24  import javax.xml.soap.SOAPHeaderElement;
25  import javax.xml.transform.Result;
26  
27  import org.springframework.util.Assert;
28  import org.springframework.ws.soap.SoapHeader;
29  import org.springframework.ws.soap.SoapHeaderElement;
30  import org.springframework.ws.soap.SoapHeaderException;
31  
32  /**
33   * SAAJ-specific implementation of the <code>SoapHeader</code> interface. Wraps a [email protected] javax.xml.soap.SOAPHeader}.
34   *
35   * @author Arjen Poutsma
36   * @since 1.0.0
37   */
38  abstract class SaajSoapHeader extends SaajSoapElement<SOAPHeader> implements SoapHeader {
39  
40      SaajSoapHeader(SOAPHeader header) {
41          super(header);
42      }
43  
44      public Iterator<SoapHeaderElement> examineAllHeaderElements() throws SoapHeaderException {
45          Iterator<SOAPHeaderElement> iterator = getImplementation().examineAllHeaderElements(getSaajHeader());
46          return new SaajSoapHeaderElementIterator(iterator);
47      }
48  
49      @SuppressWarnings("unchecked")
50      public Iterator<SoapHeaderElement> examineHeaderElements(QName name) throws SoapHeaderException {
51          try {
52              Iterator iterator = getImplementation().getChildElements(getSaajHeader(), name);
53              return new SaajSoapHeaderElementIterator(iterator);
54          }
55          catch (SOAPException ex) {
56              throw new SaajSoapHeaderException(ex);
57          }
58      }
59  
60      public Iterator<SoapHeaderElement> examineMustUnderstandHeaderElements(String actorOrRole) throws SoapHeaderException {
61          Iterator<SOAPHeaderElement> iterator = getImplementation().examineMustUnderstandHeaderElements(getSaajHeader(), actorOrRole);
62          return new SaajSoapHeaderElementIterator(iterator);
63      }
64  
65      public SoapHeaderElement addHeaderElement(QName name) throws SoapHeaderException {
66          try {
67              SOAPHeaderElement headerElement = getImplementation().addHeaderElement(getSaajHeader(), name);
68              return new SaajSoapHeaderElement(headerElement);
69          }
70          catch (SOAPException ex) {
71              throw new SaajSoapHeaderException(ex);
72          }
73      }
74  
75      public void removeHeaderElement(QName name) throws SoapHeaderException {
76          try {
77              Iterator<SOAPElement> iterator = getImplementation().getChildElements(getSaajHeader(), name);
78              if (iterator.hasNext()) {
79                  SOAPElement element = iterator.next();
80                  element.detachNode();
81              }
82          }
83          catch (SOAPException ex) {
84              throw new SaajSoapHeaderException(ex);
85          }
86      }
87  
88      protected SOAPHeader getSaajHeader() {
89          return getSaajElement();
90      }
91  
92      public Result getResult() {
93          return getImplementation().getResult(getSaajHeader());
94      }
95  
96      protected static class SaajSoapHeaderElementIterator implements Iterator<SoapHeaderElement> {
97  
98          private final Iterator<SOAPHeaderElement> iterator;
99  
100         protected SaajSoapHeaderElementIterator(Iterator<SOAPHeaderElement> iterator) {
101             Assert.notNull(iterator, "iterator must not be null");
102             this.iterator = iterator;
103         }
104 
105         public boolean hasNext() {
106             return iterator.hasNext();
107         }
108 
109         public SoapHeaderElement next() {
110             SOAPHeaderElement saajHeaderElement = iterator.next();
111             return new SaajSoapHeaderElement(saajHeaderElement);
112         }
113 
114         public void remove() {
115             iterator.remove();
116         }
117     }
118 
119 }