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.beans.PropertyEditorSupport;
20  import java.util.Locale;
21  import javax.xml.namespace.QName;
22  
23  import org.springframework.beans.propertyeditors.LocaleEditor;
24  import org.springframework.util.StringUtils;
25  import org.springframework.xml.namespace.QNameEditor;
26  
27  /**
28   * PropertyEditor for <code>SoapFaultDefinition</code> objects. Takes strings of form
29   * <pre>
30   * faultCode,faultString,locale
31   * </pre>
32   * where <code>faultCode</code> is the string representation of a <code>QName</code>, <code>faultStringOrReason</code>
33   * is the optional fault string, and <code>locale</code> is the optional string representations for the
34   * <code>faultStringOrReason</code>language. By default, the language is set to English, and the fault string set to the
35   * exception message.
36   * <p/>
37   * Instead of supplying a custom fault code, you can use the constants <code>SERVER</code> or <code>RECEIVER</code>
38   * indicate a <code>Server</code>/<code>Receiver</code> fault, or <code>CLIENT</code> or <code>SENDER</code>
39   * to<code>Client</code>/<code>Sender</code> fault respectively.
40   * <p/>
41   * For example:
42   * <pre>
43   * RECEIVER,Server error
44   * </pre>
45   * or
46   * <pre>
47   * CLIENT,Client error
48   * </pre>
49   * or
50   * <pre>
51   * {http://springframework.org/spring-ws}spring-ws:FatalError},A fatal error has occurred
52   * </pre>
53   *
54   * @author Arjen Poutsma
55   * @see javax.xml.namespace.QName#toString()
56   * @see org.springframework.xml.namespace.QNameEditor
57   * @see SoapFaultDefinition#RECEIVER
58   * @see SoapFaultDefinition#SERVER
59   * @see SoapFaultDefinition#SENDER
60   * @see SoapFaultDefinition#CLIENT
61   * @see org.springframework.ws.soap.SoapFault#getFaultCode()
62   * @since 1.0.0
63   */
64  public class SoapFaultDefinitionEditor extends PropertyEditorSupport {
65  
66      private static final int FAULT_CODE_INDEX = 0;
67  
68      private static final int FAULT_STRING_INDEX = 1;
69  
70      private static final int FAULT_STRING_LOCALE_INDEX = 2;
71  
72      @Override
73      public void setAsText(String text) throws IllegalArgumentException {
74          if (!StringUtils.hasLength(text)) {
75              setValue(null);
76          }
77          else {
78              String[] tokens = StringUtils.commaDelimitedListToStringArray(text);
79              if (tokens.length < FAULT_STRING_INDEX) {
80                  throw new IllegalArgumentException("Invalid amount of comma delimited values in [" + text +
81                          "]: SoapFaultDefinitionEditor requires at least 1");
82              }
83              SoapFaultDefinition definition = new SoapFaultDefinition();
84              QNameEditor qNameEditor = new QNameEditor();
85              qNameEditor.setAsText(tokens[FAULT_CODE_INDEX].trim());
86              definition.setFaultCode((QName) qNameEditor.getValue());
87              if (tokens.length > 1) {
88                  definition.setFaultStringOrReason(tokens[FAULT_STRING_INDEX].trim());
89                  if (tokens.length > 2) {
90                      LocaleEditor localeEditor = new LocaleEditor();
91                      localeEditor.setAsText(tokens[FAULT_STRING_LOCALE_INDEX].trim());
92                      definition.setLocale((Locale) localeEditor.getValue());
93                  }
94              }
95              setValue(definition);
96          }
97      }
98  
99  }