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