001: /*
002: * Copyright 2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.server.endpoint;
018:
019: import java.beans.PropertyEditorSupport;
020: import java.util.Locale;
021: import javax.xml.namespace.QName;
022:
023: import org.springframework.beans.propertyeditors.LocaleEditor;
024: import org.springframework.util.StringUtils;
025: import org.springframework.xml.namespace.QNameEditor;
026:
027: /**
028: * PropertyEditor for <code>SoapFaultDefinition</code> objects. Takes strings of form
029: * <pre>
030: * faultCode,faultString,locale
031: * </pre>
032: * where <code>faultCode</code> is the string representation of a <code>QName</code>, <code>faultStringOrReason</code>
033: * is the optional fault string, and <code>locale</code> is the optional string representations for the
034: * <code>faultStringOrReason</code>language. By default, the language is set to English, and the fault string set to the
035: * exception message.
036: * <p/>
037: * Instead of supplying a custom fault code, you can use the constants <code>SERVER</code> or <code>RECEIVER</code>
038: * indicate a <code>Server</code>/<code>Receiver</code> fault, or <code>CLIENT</code> or <code>SENDER</code>
039: * to<code>Client</code>/<code>Sender</code> fault respectivaly.
040: * <p/>
041: * For example:
042: * <pre>
043: * RECEIVER,Server error
044: * </pre>
045: * or
046: * <pre>
047: * CLIENT,Client error
048: * </pre>
049: * or
050: * <pre>
051: * {http://springframework.org/spring-ws}spring-ws:FatalError},A fatal error has occurred
052: * </pre>
053: *
054: * @author Arjen Poutsma
055: * @see javax.xml.namespace.QName#toString()
056: * @see org.springframework.xml.namespace.QNameEditor
057: * @see SoapFaultDefinition#RECEIVER
058: * @see SoapFaultDefinition#SERVER
059: * @see SoapFaultDefinition#SENDER
060: * @see SoapFaultDefinition#CLIENT
061: * @see org.springframework.ws.soap.SoapFault#getFaultCode()
062: * @since 1.0.0
063: */
064: public class SoapFaultDefinitionEditor extends PropertyEditorSupport {
065:
066: private static final int FAULT_CODE_INDEX = 0;
067:
068: private static final int FAULT_STRING_INDEX = 1;
069:
070: private static final int FAULT_STRING_LOCALE_INDEX = 2;
071:
072: public void setAsText(String text) throws IllegalArgumentException {
073: if (!StringUtils.hasLength(text)) {
074: setValue(null);
075: } else {
076: String[] tokens = StringUtils
077: .commaDelimitedListToStringArray(text);
078: if (tokens.length < FAULT_STRING_INDEX) {
079: throw new IllegalArgumentException(
080: "Invalid amount of comma delimited values in ["
081: + text
082: + "]: SoapFaultDefinitionEditor requires at least 1");
083: }
084: SoapFaultDefinition definition = new SoapFaultDefinition();
085: QNameEditor qNameEditor = new QNameEditor();
086: qNameEditor.setAsText(tokens[FAULT_CODE_INDEX].trim());
087: definition.setFaultCode((QName) qNameEditor.getValue());
088: if (tokens.length > 1) {
089: definition
090: .setFaultStringOrReason(tokens[FAULT_STRING_INDEX]
091: .trim());
092: if (tokens.length > 2) {
093: LocaleEditor localeEditor = new LocaleEditor();
094: localeEditor
095: .setAsText(tokens[FAULT_STRING_LOCALE_INDEX]
096: .trim());
097: definition.setLocale((Locale) localeEditor
098: .getValue());
099: }
100: }
101: setValue(definition);
102: }
103: }
104:
105: }
|