View Javadoc

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