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.Locale;
20  import javax.xml.namespace.QName;
21  
22  import org.apache.axiom.om.OMAttribute;
23  import org.apache.axiom.om.OMNamespace;
24  import org.apache.axiom.soap.SOAP11Constants;
25  import org.apache.axiom.soap.SOAPBody;
26  import org.apache.axiom.soap.SOAPFactory;
27  import org.apache.axiom.soap.SOAPFault;
28  import org.apache.axiom.soap.SOAPFaultCode;
29  import org.apache.axiom.soap.SOAPFaultReason;
30  import org.apache.axiom.soap.SOAPProcessingException;
31  
32  import org.springframework.util.Assert;
33  import org.springframework.util.StringUtils;
34  import org.springframework.ws.soap.SoapFault;
35  import org.springframework.ws.soap.axiom.support.AxiomUtils;
36  import org.springframework.ws.soap.soap11.Soap11Body;
37  import org.springframework.ws.soap.soap11.Soap11Fault;
38  import org.springframework.xml.namespace.QNameUtils;
39  
40  /**
41   * Axiom-specific version of <code>org.springframework.ws.soap.Soap11Body</code>.
42   *
43   * @author Arjen Poutsma
44   * @since 1.0.0
45   */
46  class AxiomSoap11Body extends AxiomSoapBody implements Soap11Body {
47  
48      AxiomSoap11Body(SOAPBody axiomBody, SOAPFactory axiomFactory, boolean payloadCaching) {
49          super(axiomBody, axiomFactory, payloadCaching);
50      }
51  
52      public SoapFault addMustUnderstandFault(String faultString, Locale locale) {
53          SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_MUST_UNDERSTAND, faultString, locale);
54          return new AxiomSoap11Fault(fault, getAxiomFactory());
55      }
56  
57      public SoapFault addClientOrSenderFault(String faultString, Locale locale) {
58          SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_SENDER, faultString, locale);
59          return new AxiomSoap11Fault(fault, getAxiomFactory());
60      }
61  
62      public SoapFault addServerOrReceiverFault(String faultString, Locale locale) {
63          SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_RECEIVER, faultString, locale);
64          return new AxiomSoap11Fault(fault, getAxiomFactory());
65      }
66  
67      public SoapFault addVersionMismatchFault(String faultString, Locale locale) {
68          SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_VERSION_MISMATCH, faultString, locale);
69          return new AxiomSoap11Fault(fault, getAxiomFactory());
70      }
71  
72      public Soap11Fault addFault(QName code, String faultString, Locale locale) {
73          Assert.notNull(code, "No faultCode given");
74          Assert.hasLength(faultString, "faultString cannot be empty");
75          if (!StringUtils.hasLength(code.getNamespaceURI())) {
76              throw new IllegalArgumentException(
77                      "A fault code with namespace and local part must be specific for a custom fault code");
78          }
79          try {
80              AxiomUtils.removeContents(getAxiomBody());
81              SOAPFault fault = getAxiomFactory().createSOAPFault(getAxiomBody());
82              SOAPFaultCode faultCode = getAxiomFactory().createSOAPFaultCode(fault);
83              setValueText(code, fault, faultCode);
84              SOAPFaultReason faultReason = getAxiomFactory().createSOAPFaultReason(fault);
85              if (locale != null) {
86                  addLangAttribute(locale, faultReason);
87              }
88              faultReason.setText(faultString);
89              return new AxiomSoap11Fault(fault, getAxiomFactory());
90  
91          }
92          catch (SOAPProcessingException ex) {
93              throw new AxiomSoapFaultException(ex);
94          }
95  
96      }
97  
98      private void setValueText(QName code, SOAPFault fault, SOAPFaultCode faultCode) {
99          String prefix = QNameUtils.getPrefix(code);
100         if (StringUtils.hasLength(code.getNamespaceURI()) && StringUtils.hasLength(prefix)) {
101             OMNamespace namespace = fault.findNamespaceURI(prefix);
102             if (namespace == null) {
103                 fault.declareNamespace(code.getNamespaceURI(), prefix);
104             }
105         }
106         else if (StringUtils.hasLength(code.getNamespaceURI())) {
107             OMNamespace namespace = fault.findNamespace(code.getNamespaceURI(), null);
108             if (namespace == null) {
109                 throw new IllegalArgumentException("Could not resolve namespace of code [" + code + "]");
110             }
111             code = QNameUtils.createQName(code.getNamespaceURI(), code.getLocalPart(), namespace.getPrefix());
112         }
113         faultCode.setText(code);
114     }
115 
116     private SOAPFault addStandardFault(String localName, String faultString, Locale locale) {
117         Assert.notNull(faultString, "No faultString given");
118         try {
119             AxiomUtils.removeContents(getAxiomBody());
120             SOAPFault fault = getAxiomFactory().createSOAPFault(getAxiomBody());
121             SOAPFaultCode faultCode = getAxiomFactory().createSOAPFaultCode(fault);
122             faultCode.setText(QNameUtils.createQName(fault.getNamespace().getNamespaceURI(), localName,
123                     fault.getNamespace().getPrefix()));
124             SOAPFaultReason faultReason = getAxiomFactory().createSOAPFaultReason(fault);
125             if (locale != null) {
126                 addLangAttribute(locale, faultReason);
127             }
128             faultReason.setText(faultString);
129             return fault;
130         }
131         catch (SOAPProcessingException ex) {
132             throw new AxiomSoapFaultException(ex);
133         }
134     }
135 
136     private void addLangAttribute(Locale locale, SOAPFaultReason faultReason) {
137         OMNamespace xmlNamespace = getAxiomFactory().createOMNamespace("http://www.w3.org/XML/1998/namespace", "xml");
138         OMAttribute langAttribute =
139                 getAxiomFactory().createOMAttribute("lang", xmlNamespace, AxiomUtils.toLanguage(locale));
140         faultReason.addAttribute(langAttribute);
141     }
142 
143     public SoapFault getFault() {
144         SOAPFault axiomFault = getAxiomBody().getFault();
145         return axiomFault != null ? new AxiomSoap11Fault(axiomFault, getAxiomFactory()) : null;
146     }
147 
148 }