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.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.SOAPConstants;
24  import javax.xml.soap.SOAPException;
25  import javax.xml.soap.SOAPHeader;
26  import javax.xml.soap.SOAPHeaderElement;
27  
28  import org.springframework.util.ObjectUtils;
29  import org.springframework.util.StringUtils;
30  import org.springframework.ws.soap.SoapHeaderElement;
31  import org.springframework.ws.soap.SoapHeaderException;
32  import org.springframework.ws.soap.soap12.Soap12Header;
33  
34  /**
35   * SAAJ-specific implementation of the <code>Soap12Header</code> interface. Wraps a [email protected] javax.xml.soap.SOAPHeader}.
36   *
37   * @author Arjen Poutsma
38   * @since 1.0.0
39   */
40  class SaajSoap12Header extends SaajSoapHeader implements Soap12Header {
41  
42      SaajSoap12Header(SOAPHeader header) {
43          super(header);
44      }
45  
46      public SoapHeaderElement addNotUnderstoodHeaderElement(QName headerName) {
47          try {
48              SOAPHeaderElement headerElement =
49                      getImplementation().addNotUnderstoodHeaderElement(getSaajHeader(), headerName);
50              return new SaajSoapHeaderElement(headerElement);
51          }
52          catch (SOAPException ex) {
53              throw new SaajSoapHeaderException(ex);
54          }
55      }
56  
57      public SoapHeaderElement addUpgradeHeaderElement(String[] supportedSoapUris) {
58          try {
59              SOAPHeaderElement headerElement =
60                      getImplementation().addUpgradeHeaderElement(getSaajHeader(), supportedSoapUris);
61              return new SaajSoapHeaderElement(headerElement);
62          }
63          catch (SOAPException ex) {
64              throw new SaajSoapHeaderException(ex);
65          }
66      }
67  
68      public Iterator<SoapHeaderElement> examineHeaderElementsToProcess(String[] roles, boolean isUltimateDestination)
69              throws SoapHeaderException {
70          List<SOAPHeaderElement> result = new ArrayList<SOAPHeaderElement>();
71          Iterator<SOAPHeaderElement> iterator = getImplementation().examineAllHeaderElements(getSaajHeader());
72          while (iterator.hasNext()) {
73              SOAPHeaderElement saajHeaderElement = iterator.next();
74              String headerRole = saajHeaderElement.getRole();
75              if (shouldProcess(headerRole, roles, isUltimateDestination)) {
76                  result.add(saajHeaderElement);
77              }
78          }
79          return new SaajSoapHeaderElementIterator(result.iterator());
80  
81      }
82  
83      private boolean shouldProcess(String headerRole, String[] roles, boolean isUltimateDestination) {
84          if (!StringUtils.hasLength(headerRole)) {
85              return true;
86          }
87          if (SOAPConstants.URI_SOAP_1_2_ROLE_NEXT.equals(headerRole)) {
88              return true;
89          }
90          if (SOAPConstants.URI_SOAP_1_2_ROLE_ULTIMATE_RECEIVER.equals(headerRole)) {
91              return isUltimateDestination;
92          }
93          if (SOAPConstants.URI_SOAP_1_2_ROLE_NONE.equals(headerRole)) {
94              return false;
95          }
96          if (!ObjectUtils.isEmpty(roles)) {
97              for (String role : roles) {
98                  if (role.equals(headerRole)) {
99                      return true;
100                 }
101             }
102         }
103         return false;
104     }
105 }