View Javadoc

1   /*
2    * Copyright 2007 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.saaj;
18  
19  import java.util.Locale;
20  import javax.xml.namespace.QName;
21  import javax.xml.soap.SOAPBody;
22  import javax.xml.soap.SOAPException;
23  import javax.xml.soap.SOAPFault;
24  
25  import org.springframework.util.Assert;
26  import org.springframework.ws.soap.SoapFault;
27  import org.springframework.ws.soap.SoapVersion;
28  import org.springframework.ws.soap.soap12.Soap12Body;
29  import org.springframework.ws.soap.soap12.Soap12Fault;
30  
31  /**
32   * SAAJ-specific implementation of the <code>Soap12Body</code> interface. Wraps a {@link javax.xml.soap.SOAPBody}.
33   *
34   * @author Arjen Poutsma
35   * @since 1.0.0
36   */
37  class SaajSoap12Body extends SaajSoapBody implements Soap12Body {
38  
39      SaajSoap12Body(SOAPBody body) {
40          super(body);
41      }
42  
43      public SoapFault getFault() {
44          SOAPFault fault = getImplementation().getFault(getSaajBody());
45          return fault != null ? new SaajSoap12Fault(fault) : null;
46      }
47  
48      public SoapFault addClientOrSenderFault(String faultString, Locale locale) {
49          return addFault(SoapVersion.SOAP_12.getClientOrSenderFaultName(), faultString, locale);
50      }
51  
52      public SoapFault addMustUnderstandFault(String faultString, Locale locale) {
53          return addFault(SoapVersion.SOAP_12.getMustUnderstandFaultName(), faultString, locale);
54      }
55  
56      public SoapFault addServerOrReceiverFault(String faultString, Locale locale) {
57          return addFault(SoapVersion.SOAP_12.getServerOrReceiverFaultName(), faultString, locale);
58      }
59  
60      public SoapFault addVersionMismatchFault(String faultString, Locale locale) {
61          return addFault(SoapVersion.SOAP_12.getVersionMismatchFaultName(), faultString, locale);
62      }
63  
64      public Soap12Fault addDataEncodingUnknownFault(QName[] subcodes, String reason, Locale locale) {
65          QName name = new QName(SoapVersion.SOAP_12.getEnvelopeNamespaceUri(), "DataEncodingUnknown");
66          Soap12Fault fault = addFault(name, reason, locale);
67          for (int i = 0; i < subcodes.length; i++) {
68              fault.addFaultSubcode(subcodes[i]);
69          }
70          return fault;
71      }
72  
73      protected Soap12Fault addFault(QName faultCode, String faultString, Locale faultStringLocale) {
74          Assert.notNull(faultCode, "No faultCode given");
75          Assert.hasLength(faultString, "faultString cannot be empty");
76          Assert.hasLength(faultCode.getLocalPart(), "faultCode's localPart cannot be empty");
77          Assert.hasLength(faultCode.getNamespaceURI(), "faultCode's namespaceUri cannot be empty");
78          try {
79              getImplementation().removeContents(getSaajBody());
80              SOAPFault saajFault =
81                      getImplementation().addFault(getSaajBody(), faultCode, faultString, faultStringLocale);
82              return new SaajSoap12Fault(saajFault);
83          }
84          catch (SOAPException ex) {
85              throw new SaajSoapFaultException(ex);
86          }
87      }
88  
89  }