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.server.endpoint;
18  
19  import org.springframework.util.Assert;
20  import org.springframework.util.StringUtils;
21  import org.springframework.ws.context.MessageContext;
22  import org.springframework.ws.server.EndpointExceptionResolver;
23  import org.springframework.ws.server.endpoint.AbstractEndpointExceptionResolver;
24  import org.springframework.ws.soap.SoapBody;
25  import org.springframework.ws.soap.SoapFault;
26  import org.springframework.ws.soap.SoapMessage;
27  import org.springframework.ws.soap.soap11.Soap11Body;
28  import org.springframework.ws.soap.soap12.Soap12Body;
29  import org.springframework.ws.soap.soap12.Soap12Fault;
30  
31  /**
32   * Abstract base class for SOAP-based {@link EndpointExceptionResolver} implementations that depend on {@link
33   * SoapFaultDefinition}. Provides a default endpoint property, and a template method that provides the definition for a
34   * given exception.
35   *
36   * @author Arjen Poutsma
37   * @see #setDefaultFault(SoapFaultDefinition)
38   * @see #getFaultDefinition(Object,Exception)
39   * @since 1.0.0
40   */
41  public abstract class AbstractSoapFaultDefinitionExceptionResolver extends AbstractEndpointExceptionResolver {
42  
43      private SoapFaultDefinition defaultFault;
44  
45      /** Set the default fault. This fault will be returned if no specific mapping was found. */
46      public void setDefaultFault(SoapFaultDefinition defaultFault) {
47          this.defaultFault = defaultFault;
48      }
49  
50      /**
51       * Template method that returns the {@link SoapFaultDefinition} for the given exception.
52       *
53       * @param endpoint the executed endpoint, or <code>null</code>  if none chosen at the time of the exception
54       * @param ex       the exception to be handled
55       * @return the definition mapped to the exception, or <code>null</code> if none is found.
56       */
57      protected abstract SoapFaultDefinition getFaultDefinition(Object endpoint, Exception ex);
58  
59      @Override
60      protected final boolean resolveExceptionInternal(MessageContext messageContext, Object endpoint, Exception ex) {
61          Assert.isInstanceOf(SoapMessage.class, messageContext.getResponse(),
62                  "AbstractSoapFaultDefinitionExceptionResolver requires a SoapMessage");
63  
64          SoapFaultDefinition definition = getFaultDefinition(endpoint, ex);
65          if (definition == null) {
66              definition = defaultFault;
67          }
68          if (definition == null) {
69              return false;
70          }
71  
72          String faultStringOrReason = definition.getFaultStringOrReason();
73          if (!StringUtils.hasLength(faultStringOrReason)) {
74              faultStringOrReason = StringUtils.hasLength(ex.getMessage()) ? ex.getMessage() : ex.toString();
75          }
76          SoapBody soapBody = ((SoapMessage) messageContext.getResponse()).getSoapBody();
77          SoapFault fault;
78  
79          if (SoapFaultDefinition.SERVER.equals(definition.getFaultCode()) ||
80                  SoapFaultDefinition.RECEIVER.equals(definition.getFaultCode())) {
81              fault = soapBody.addServerOrReceiverFault(faultStringOrReason, definition.getLocale());
82          }
83          else if (SoapFaultDefinition.CLIENT.equals(definition.getFaultCode()) ||
84                  SoapFaultDefinition.SENDER.equals(definition.getFaultCode())) {
85              fault = soapBody.addClientOrSenderFault(faultStringOrReason, definition.getLocale());
86          }
87          else {
88              if (soapBody instanceof Soap11Body) {
89                  Soap11Body soap11Body = (Soap11Body) soapBody;
90                  fault = soap11Body.addFault(definition.getFaultCode(), faultStringOrReason, definition.getLocale());
91              }
92              else if (soapBody instanceof Soap12Body) {
93                  Soap12Body soap12Body = (Soap12Body) soapBody;
94                  Soap12Fault soap12Fault =
95                          (Soap12Fault) soap12Body.addServerOrReceiverFault(faultStringOrReason, definition
96                                  .getLocale());
97                  soap12Fault.addFaultSubcode(definition.getFaultCode());
98                  fault = soap12Fault;
99              }
100             else {
101                 throw new IllegalStateException("This class only supports SOAP 1.1 and SOAP 1.2.");
102             }
103         }
104         if (fault != null) {
105             customizeFault(endpoint, ex, fault);
106         }
107         return true;
108     }
109 
110     /**
111      * Customize the {@link SoapFault} created by this resolver. Called for each created fault
112      * <p/>
113      * The default implementation is empty. Can be overridden in subclasses to customize the properties of the fault,
114      * such as adding details, etc.
115      *
116      * @param endpoint the executed endpoint, or <code>null</code>  if none chosen at the time of the exception
117      * @param ex       the exception to be handled
118      * @param fault    the created fault
119      */
120     protected void customizeFault(Object endpoint, Exception ex, SoapFault fault) {
121     }
122 
123 }