View Javadoc

1   /*
2    * Copyright 2002-2009 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      private final boolean langAttributeOnSoap11FaultString;
49  
50      AxiomSoap11Body(SOAPBody axiomBody,
51                      SOAPFactory axiomFactory,
52                      boolean payloadCaching,
53                      boolean langAttributeOnSoap11FaultString) {
54          super(axiomBody, axiomFactory, payloadCaching);
55          this.langAttributeOnSoap11FaultString = langAttributeOnSoap11FaultString;
56      }
57  
58      public SoapFault addMustUnderstandFault(String faultString, Locale locale) {
59          SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_MUST_UNDERSTAND, faultString, locale);
60          return new AxiomSoap11Fault(fault, getAxiomFactory());
61      }
62  
63      public SoapFault addClientOrSenderFault(String faultString, Locale locale) {
64          SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_SENDER, faultString, locale);
65          return new AxiomSoap11Fault(fault, getAxiomFactory());
66      }
67  
68      public SoapFault addServerOrReceiverFault(String faultString, Locale locale) {
69          SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_RECEIVER, faultString, locale);
70          return new AxiomSoap11Fault(fault, getAxiomFactory());
71      }
72  
73      public SoapFault addVersionMismatchFault(String faultString, Locale locale) {
74          SOAPFault fault = addStandardFault(SOAP11Constants.FAULT_CODE_VERSION_MISMATCH, faultString, locale);
75          return new AxiomSoap11Fault(fault, getAxiomFactory());
76      }
77  
78      public Soap11Fault addFault(QName code, String faultString, Locale faultStringLocale) {
79          Assert.notNull(code, "No faultCode given");
80          Assert.hasLength(faultString, "faultString cannot be empty");
81          if (!StringUtils.hasLength(code.getNamespaceURI())) {
82              throw new IllegalArgumentException(
83                      "A fault code with namespace and local part must be specific for a custom fault code");
84          }
85          if (!langAttributeOnSoap11FaultString) {
86              faultStringLocale = null;
87          }
88          try {
89              AxiomUtils.removeContents(getAxiomBody());
90              SOAPFault fault = getAxiomFactory().createSOAPFault(getAxiomBody());
91              SOAPFaultCode faultCode = getAxiomFactory().createSOAPFaultCode(fault);
92              setValueText(code, fault, faultCode);
93              SOAPFaultReason faultReason = getAxiomFactory().createSOAPFaultReason(fault);
94              if (faultStringLocale != null) {
95                  addLangAttribute(faultStringLocale, faultReason);
96              }
97              faultReason.setText(faultString);
98              return new AxiomSoap11Fault(fault, getAxiomFactory());
99  
100         }
101         catch (SOAPProcessingException ex) {
102             throw new AxiomSoapFaultException(ex);
103         }
104     }
105 
106     private void setValueText(QName code, SOAPFault fault, SOAPFaultCode faultCode) {
107         String prefix = QNameUtils.getPrefix(code);
108         if (StringUtils.hasLength(code.getNamespaceURI()) && StringUtils.hasLength(prefix)) {
109             OMNamespace namespace = fault.findNamespaceURI(prefix);
110             if (namespace == null) {
111                 fault.declareNamespace(code.getNamespaceURI(), prefix);
112             }
113         }
114         else if (StringUtils.hasLength(code.getNamespaceURI())) {
115             OMNamespace namespace = fault.findNamespace(code.getNamespaceURI(), null);
116             if (namespace == null) {
117                 namespace = fault.declareNamespace(code.getNamespaceURI(), "");
118             }
119             code = QNameUtils.createQName(code.getNamespaceURI(), code.getLocalPart(), namespace.getPrefix());
120         }
121         faultCode.setText(code);
122     }
123 
124     private SOAPFault addStandardFault(String localName, String faultString, Locale locale) {
125         Assert.notNull(faultString, "No faultString given");
126         try {
127             AxiomUtils.removeContents(getAxiomBody());
128             SOAPFault fault = getAxiomFactory().createSOAPFault(getAxiomBody());
129             SOAPFaultCode faultCode = getAxiomFactory().createSOAPFaultCode(fault);
130             faultCode.setText(QNameUtils.createQName(fault.getNamespace().getNamespaceURI(), localName,
131                     fault.getNamespace().getPrefix()));
132             SOAPFaultReason faultReason = getAxiomFactory().createSOAPFaultReason(fault);
133             if (locale != null) {
134                 addLangAttribute(locale, faultReason);
135             }
136             faultReason.setText(faultString);
137             return fault;
138         }
139         catch (SOAPProcessingException ex) {
140             throw new AxiomSoapFaultException(ex);
141         }
142     }
143 
144     private void addLangAttribute(Locale locale, SOAPFaultReason faultReason) {
145         OMNamespace xmlNamespace = getAxiomFactory().createOMNamespace("http://www.w3.org/XML/1998/namespace", "xml");
146         OMAttribute langAttribute =
147                 getAxiomFactory().createOMAttribute("lang", xmlNamespace, AxiomUtils.toLanguage(locale));
148         faultReason.addAttribute(langAttribute);
149     }
150 
151     public SoapFault getFault() {
152         SOAPFault axiomFault = getAxiomBody().getFault();
153         return axiomFault != null ? new AxiomSoap11Fault(axiomFault, getAxiomFactory()) : null;
154     }
155 
156 }