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.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      protected final boolean resolveExceptionInternal(MessageContext messageContext, Object endpoint, Exception ex) {
60          Assert.isInstanceOf(SoapMessage.class, messageContext.getResponse(),
61                  "AbstractSoapFaultDefinitionExceptionResolver requires a SoapMessage");
62  
63          SoapFaultDefinition definition = getFaultDefinition(endpoint, ex);
64          if (definition == null) {
65              definition = defaultFault;
66          }
67          if (definition == null) {
68              return false;
69          }
70  
71          String faultStringOrReason = definition.getFaultStringOrReason();
72          if (!StringUtils.hasLength(faultStringOrReason)) {
73              faultStringOrReason = StringUtils.hasLength(ex.getMessage()) ? ex.getMessage() : ex.toString();
74          }
75          SoapBody soapBody = ((SoapMessage) messageContext.getResponse()).getSoapBody();
76          SoapFault fault;
77  
78          if (SoapFaultDefinition.SERVER.equals(definition.getFaultCode()) ||
79                  SoapFaultDefinition.RECEIVER.equals(definition.getFaultCode())) {
80              fault = soapBody.addServerOrReceiverFault(faultStringOrReason, definition.getLocale());
81          }
82          else if (SoapFaultDefinition.CLIENT.equals(definition.getFaultCode()) ||
83                  SoapFaultDefinition.SENDER.equals(definition.getFaultCode())) {
84              fault = soapBody.addClientOrSenderFault(faultStringOrReason, definition.getLocale());
85          }
86          else {
87              if (soapBody instanceof Soap11Body) {
88                  Soap11Body soap11Body = (Soap11Body) soapBody;
89                  fault = soap11Body.addFault(definition.getFaultCode(), faultStringOrReason, definition.getLocale());
90              }
91              else if (soapBody instanceof Soap12Body) {
92                  Soap12Body soap12Body = (Soap12Body) soapBody;
93                  Soap12Fault soap12Fault =
94                          (Soap12Fault) soap12Body.addServerOrReceiverFault(faultStringOrReason, definition
95                                  .getLocale());
96                  soap12Fault.addFaultSubcode(definition.getFaultCode());
97                  fault = soap12Fault;
98              }
99              else {
100                 throw new IllegalStateException("This class only supports SOAP 1.1 and SOAP 1.2.");
101             }
102         }
103         if (fault != null) {
104             customizeFault(endpoint, ex, fault);
105         }
106         return true;
107     }
108 
109     /**
110      * Customize the {@link SoapFault} created by this resolver. Called for each created fault
111      * <p/>
112      * The default implementation is empty. Can be overridden in subclasses to customize the properties of the fault,
113      * such as adding details, etc.
114      *
115      * @param endpoint the executed endpoint, or <code>null</code>  if none chosen at the time of the exception
116      * @param ex       the exception to be handled
117      * @param fault    the created fault
118      */
119     protected void customizeFault(Object endpoint, Exception ex, SoapFault fault) {
120     }
121 
122 }