View Javadoc

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