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