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.Arrays;
20  import java.util.Iterator;
21  import java.util.List;
22  import javax.xml.namespace.QName;
23  
24  import org.apache.axiom.om.OMElement;
25  import org.apache.axiom.om.OMException;
26  import org.apache.axiom.om.OMNamespace;
27  import org.apache.axiom.soap.RolePlayer;
28  import org.apache.axiom.soap.SOAPFactory;
29  import org.apache.axiom.soap.SOAPHeader;
30  import org.apache.axiom.soap.SOAPHeaderBlock;
31  import org.apache.axiom.soap.SOAPProcessingException;
32  import org.springframework.util.ObjectUtils;
33  import org.springframework.ws.soap.SoapHeaderElement;
34  import org.springframework.ws.soap.SoapHeaderException;
35  import org.springframework.ws.soap.soap12.Soap12Header;
36  import org.springframework.xml.namespace.QNameUtils;
37  
38  /**
39   * Axiom-specific version of <code>org.springframework.ws.soap.Soap12Header</code>.
40   *
41   * @author Arjen Poutsma
42   * @since 1.0.0
43   */
44  class AxiomSoap12Header extends AxiomSoapHeader implements Soap12Header {
45  
46      AxiomSoap12Header(SOAPHeader axiomHeader, SOAPFactory axiomFactory) {
47          super(axiomHeader, axiomFactory);
48      }
49  
50      public SoapHeaderElement addNotUnderstoodHeaderElement(QName headerName) {
51          try {
52              SOAPHeaderBlock notUnderstood =
53                      getAxiomHeader().addHeaderBlock("NotUnderstood", getAxiomHeader().getNamespace());
54              OMNamespace headerNamespace =
55                      notUnderstood.declareNamespace(headerName.getNamespaceURI(), QNameUtils.getPrefix(headerName));
56              notUnderstood.addAttribute("qname", headerNamespace.getPrefix() + ":" + headerName.getLocalPart(), null);
57              return new AxiomSoapHeaderElement(notUnderstood, getAxiomFactory());
58          }
59          catch (SOAPProcessingException ex) {
60              throw new AxiomSoapHeaderException(ex);
61          }
62      }
63  
64      public SoapHeaderElement addUpgradeHeaderElement(String[] supportedSoapUris) {
65          try {
66              SOAPHeaderBlock upgrade = getAxiomHeader().addHeaderBlock("Upgrade", getAxiomHeader().getNamespace());
67              for (int i = 0; i < supportedSoapUris.length; i++) {
68                  OMElement supportedEnvelope = getAxiomFactory()
69                          .createOMElement("SupportedEnvelope", getAxiomHeader().getNamespace(), upgrade);
70                  OMNamespace namespace = supportedEnvelope.declareNamespace(supportedSoapUris[i], "");
71                  supportedEnvelope.addAttribute("qname", namespace.getPrefix() + ":Envelope", null);
72              }
73              return new AxiomSoapHeaderElement(upgrade, getAxiomFactory());
74          }
75          catch (OMException ex) {
76              throw new AxiomSoapHeaderException(ex);
77          }
78      }
79  
80      public Iterator examineHeaderElementsToProcess(final String[] roles, final boolean isUltimateDestination)
81              throws SoapHeaderException {
82          RolePlayer rolePlayer = null;
83          if (!ObjectUtils.isEmpty(roles)) {
84              rolePlayer = new RolePlayer() {
85  
86                  public List getRoles() {
87                      return Arrays.asList(roles);
88                  }
89  
90                  public boolean isUltimateDestination() {
91                      return isUltimateDestination;
92                  }
93              };
94          }
95          Iterator result = getAxiomHeader().getHeadersToProcess(rolePlayer);
96          return new AxiomSoapHeaderElementIterator(result);
97      }
98  }