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 java.util.Locale;
20  
21  import org.springframework.util.Assert;
22  import org.springframework.util.StringUtils;
23  import org.springframework.ws.context.MessageContext;
24  import org.springframework.ws.server.endpoint.AbstractEndpointExceptionResolver;
25  import org.springframework.ws.soap.SoapBody;
26  import org.springframework.ws.soap.SoapFault;
27  import org.springframework.ws.soap.SoapMessage;
28  
29  /**
30   * Simple, SOAP-specific [email protected] org.springframework.ws.server.EndpointExceptionResolver EndpointExceptionResolver}
31   * implementation that stores the exception's message as the fault string.
32   * <p/>
33   * The fault code is always set to a Server (in SOAP 1.1) or Receiver (SOAP 1.2).
34   *
35   * @author Arjen Poutsma
36   * @since 1.0.0
37   */
38  public class SimpleSoapExceptionResolver extends AbstractEndpointExceptionResolver {
39  
40      private Locale locale = Locale.ENGLISH;
41  
42      /**
43       * Returns the locale for the faultstring or reason of the SOAP Fault.
44       * <p/>
45       * Defaults to [email protected] Locale#ENGLISH}.
46       */
47      public Locale getLocale() {
48          return locale;
49      }
50  
51      /**
52       * Sets the locale for the faultstring or reason of the SOAP Fault.
53       * <p/>
54       * Defaults to [email protected] Locale#ENGLISH}.
55       */
56      public void setLocale(Locale locale) {
57          Assert.notNull(locale, "locale must not be null");
58          this.locale = locale;
59      }
60  
61      @Override
62      protected final boolean resolveExceptionInternal(MessageContext messageContext, Object endpoint, Exception ex) {
63          Assert.isInstanceOf(SoapMessage.class, messageContext.getResponse(),
64                  "SimpleSoapExceptionResolver requires a SoapMessage");
65          SoapMessage response = (SoapMessage) messageContext.getResponse();
66          String faultString = StringUtils.hasLength(ex.getMessage()) ? ex.getMessage() : ex.toString();
67          SoapBody body = response.getSoapBody();
68          SoapFault fault = body.addServerOrReceiverFault(faultString, getLocale());
69          customizeFault(messageContext, endpoint, ex, fault);
70          return true;
71      }
72  
73      /**
74       * Empty template method to allow subclasses an opportunity to customize the given [email protected] SoapFault}. Called from
75       * [email protected] #resolveExceptionInternal(MessageContext,Object,Exception)}.
76       *
77       * @param messageContext current message context
78       * @param endpoint       the executed endpoint, or <code>null</code> if none chosen at the time of the exception
79       * @param ex             the exception that got thrown during endpoint execution
80       * @param fault          the SOAP fault to be customized.
81       */
82      protected void customizeFault(MessageContext messageContext, Object endpoint, Exception ex, SoapFault fault) {
83      }
84  }