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