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.soap.SOAP12Constants;
23  import org.apache.axiom.soap.SOAPBody;
24  import org.apache.axiom.soap.SOAPFactory;
25  import org.apache.axiom.soap.SOAPFault;
26  import org.apache.axiom.soap.SOAPFaultCode;
27  import org.apache.axiom.soap.SOAPFaultReason;
28  import org.apache.axiom.soap.SOAPFaultText;
29  import org.apache.axiom.soap.SOAPFaultValue;
30  import org.apache.axiom.soap.SOAPProcessingException;
31  
32  import org.springframework.util.Assert;
33  import org.springframework.ws.soap.SoapFault;
34  import org.springframework.ws.soap.axiom.support.AxiomUtils;
35  import org.springframework.ws.soap.soap12.Soap12Body;
36  import org.springframework.ws.soap.soap12.Soap12Fault;
37  
38  /**
39   * Axiom-specific version of <code>org.springframework.ws.soap.Soap12Body</code>.
40   *
41   * @author Arjen Poutsma
42   * @since 1.0.0
43   */
44  class AxiomSoap12Body extends AxiomSoapBody implements Soap12Body {
45  
46      AxiomSoap12Body(SOAPBody axiomBody, SOAPFactory axiomFactory, boolean payloadCaching) {
47          super(axiomBody, axiomFactory, payloadCaching);
48      }
49  
50      public SoapFault addMustUnderstandFault(String reason, Locale locale) {
51          Assert.notNull(locale, "No locale given");
52          SOAPFault fault = addStandardFault(SOAP12Constants.FAULT_CODE_MUST_UNDERSTAND, reason, locale);
53          return new AxiomSoap12Fault(fault, getAxiomFactory());
54      }
55  
56      public SoapFault addClientOrSenderFault(String reason, Locale locale) {
57          Assert.notNull(locale, "No locale given");
58          SOAPFault fault = addStandardFault(SOAP12Constants.FAULT_CODE_SENDER, reason, locale);
59          return new AxiomSoap12Fault(fault, getAxiomFactory());
60      }
61  
62      public SoapFault addServerOrReceiverFault(String reason, Locale locale) {
63          Assert.notNull(locale, "No locale given");
64          SOAPFault fault = addStandardFault(SOAP12Constants.FAULT_CODE_RECEIVER, reason, locale);
65          return new AxiomSoap12Fault(fault, getAxiomFactory());
66      }
67  
68      public SoapFault addVersionMismatchFault(String reason, Locale locale) {
69          Assert.notNull(locale, "No locale given");
70          SOAPFault fault = addStandardFault(SOAP12Constants.FAULT_CODE_VERSION_MISMATCH, reason, locale);
71          return new AxiomSoap12Fault(fault, getAxiomFactory());
72      }
73  
74      public Soap12Fault addDataEncodingUnknownFault(QName[] subcodes, String reason, Locale locale) {
75          Assert.notNull(locale, "No locale given");
76          SOAPFault fault = addStandardFault(SOAP12Constants.FAULT_CODE_DATA_ENCODING_UNKNOWN, reason, locale);
77          return new AxiomSoap12Fault(fault, getAxiomFactory());
78      }
79  
80      private SOAPFault addStandardFault(String localName, String faultStringOrReason, Locale locale) {
81          Assert.notNull(faultStringOrReason, "No faultStringOrReason given");
82          try {
83              AxiomUtils.removeContents(getAxiomBody());
84              SOAPFault fault = getAxiomFactory().createSOAPFault(getAxiomBody());
85              SOAPFaultCode code = getAxiomFactory().createSOAPFaultCode(fault);
86              SOAPFaultValue value = getAxiomFactory().createSOAPFaultValue(code);
87              value.setText(fault.getNamespace().getPrefix() + ":" + localName);
88              SOAPFaultReason reason = getAxiomFactory().createSOAPFaultReason(fault);
89              SOAPFaultText text = getAxiomFactory().createSOAPFaultText(reason);
90              if (locale != null) {
91                  text.setLang(AxiomUtils.toLanguage(locale));
92              }
93              text.setText(faultStringOrReason);
94              return fault;
95          }
96          catch (SOAPProcessingException ex) {
97              throw new AxiomSoapFaultException(ex);
98          }
99      }
100 
101     public SoapFault getFault() {
102         SOAPFault axiomFault = getAxiomBody().getFault();
103         return axiomFault != null ? new AxiomSoap12Fault(axiomFault, getAxiomFactory()) : null;
104     }
105 
106 }