View Javadoc

1   /*
2    * Copyright 2005 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.Iterator;
20  import java.util.Properties;
21  
22  import org.springframework.util.CollectionUtils;
23  
24  /**
25   * Exception resolver that allows for mapping exception class names to SOAP Faults. The mappings are set using the
26   * <code>exceptionMappings</code> property, the format of which is documented in {@link SoapFaultDefinitionEditor}.
27   *
28   * @author Arjen Poutsma
29   * @since 1.0.0
30   */
31  public class SoapFaultMappingExceptionResolver extends AbstractSoapFaultDefinitionExceptionResolver {
32  
33      private Properties exceptionMappings;
34  
35      /**
36       * Set the mappings between exception class names and SOAP Faults. The exception class name can be a substring, with
37       * no wildcard support at present.
38       * <p/>
39       * The values of the given properties object should use the format described in
40       * <code>SoapFaultDefinitionEditor</code>.
41       * <p/>
42       * Follows the same matching algorithm as <code>SimpleMappingExceptionResolver</code>.
43       *
44       * @param mappings exception patterns (can also be fully qualified class names) as keys, fault definition texts as
45       *                 values
46       * @see SoapFaultDefinitionEditor
47       */
48      public void setExceptionMappings(Properties mappings) {
49          exceptionMappings = mappings;
50      }
51  
52      protected SoapFaultDefinition getFaultDefinition(Object endpoint, Exception ex) {
53          if (!CollectionUtils.isEmpty(exceptionMappings)) {
54              String definitionText = null;
55              int deepest = Integer.MAX_VALUE;
56              for (Iterator iterator = exceptionMappings.keySet().iterator(); iterator.hasNext();) {
57                  String exceptionMapping = (String) iterator.next();
58                  int depth = getDepth(exceptionMapping, ex);
59                  if (depth >= 0 && depth < deepest) {
60                      deepest = depth;
61                      definitionText = exceptionMappings.getProperty(exceptionMapping);
62                  }
63              }
64              if (definitionText != null) {
65                  SoapFaultDefinitionEditor editor = new SoapFaultDefinitionEditor();
66                  editor.setAsText(definitionText);
67                  return (SoapFaultDefinition) editor.getValue();
68              }
69          }
70          return null;
71      }
72  
73      /**
74       * Return the depth to the superclass matching. <code>0</code> means ex matches exactly. Returns <code>-1</code> if
75       * there's no match. Otherwise, returns depth. Lowest depth wins.
76       * <p/>
77       * Follows the same algorithm as RollbackRuleAttribute, and SimpleMappingExceptionResolver
78       */
79      protected int getDepth(String exceptionMapping, Exception ex) {
80          return getDepth(exceptionMapping, ex.getClass(), 0);
81      }
82  
83      private int getDepth(String exceptionMapping, Class exceptionClass, int depth) {
84          if (exceptionClass.getName().indexOf(exceptionMapping) != -1) {
85              return depth;
86          }
87          if (exceptionClass.equals(Throwable.class)) {
88              return -1;
89          }
90          return getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1);
91      }
92  
93  }