Implemented #SWS-117: Allow SoapFaultMappingExceptionResolver to use strategy to obtain SoapFaultDefinition
This commit is contained in:
@@ -64,7 +64,7 @@ public abstract class AbstractEndpointExceptionResolver implements EndpointExcep
|
||||
* Template method for resolving exceptions. Gets called after <code>resolveException</code>.
|
||||
*
|
||||
* @param messageContext current message context
|
||||
* @param endpoint the executed endpoint, or null if none chosen at the time of the exception
|
||||
* @param endpoint the executed endpoint, or <code>null</code> if none chosen at the time of the exception
|
||||
* @param ex the exception that got thrown during endpoint execution
|
||||
* @return <code>true</code> if resolved; <code>false</code> otherwise
|
||||
* @see #resolveException(org.springframework.ws.context.MessageContext,Object,Exception)
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.soap.server.endpoint;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.server.EndpointExceptionResolver;
|
||||
import org.springframework.ws.server.endpoint.AbstractEndpointExceptionResolver;
|
||||
import org.springframework.ws.soap.SoapBody;
|
||||
import org.springframework.ws.soap.SoapFault;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.soap11.Soap11Body;
|
||||
|
||||
/**
|
||||
* Abstract base class for SOAP-based {@link EndpointExceptionResolver} implementations that depend on {@link
|
||||
* SoapFaultDefinition}. Provides a default endpoint property, and a template method that provides the definition for a
|
||||
* given exception.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #setDefaultFault(SoapFaultDefinition)
|
||||
* @see #getFaultDefinition(Object,Exception)
|
||||
*/
|
||||
public abstract class AbstractSoapFaultDefinitionExceptionResolver extends AbstractEndpointExceptionResolver {
|
||||
|
||||
private SoapFaultDefinition defaultFault;
|
||||
|
||||
/** Set the default fault. This fault will be returned if no specific mapping was found. */
|
||||
public void setDefaultFault(SoapFaultDefinition defaultFault) {
|
||||
this.defaultFault = defaultFault;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method that returns the {@link SoapFaultDefinition} for the given exception.
|
||||
*
|
||||
* @param endpoint the executed endpoint, or <code>null</code> if none chosen at the time of the exception
|
||||
* @param ex the exception to be handled
|
||||
* @return the definition mapped to the exception, or <code>null</code> if none is found.
|
||||
*/
|
||||
protected abstract SoapFaultDefinition getFaultDefinition(Object endpoint, Exception ex);
|
||||
|
||||
protected final boolean resolveExceptionInternal(MessageContext messageContext, Object endpoint, Exception ex) {
|
||||
Assert.isTrue(messageContext.getResponse() instanceof SoapMessage,
|
||||
"AbstractSoapFaultDefinitionExceptionResolver requires a SoapMessage");
|
||||
|
||||
SoapFaultDefinition definition = getFaultDefinition(endpoint, ex);
|
||||
if (definition == null) {
|
||||
definition = defaultFault;
|
||||
}
|
||||
if (definition == null) {
|
||||
return false;
|
||||
}
|
||||
if (!StringUtils.hasLength(definition.getFaultStringOrReason())) {
|
||||
definition.setFaultStringOrReason(ex.getMessage());
|
||||
}
|
||||
SoapBody soapBody = ((SoapMessage) messageContext.getResponse()).getSoapBody();
|
||||
SoapFault fault = null;
|
||||
|
||||
if (SoapFaultDefinition.SERVER.equals(definition.getFaultCode()) ||
|
||||
SoapFaultDefinition.RECEIVER.equals(definition.getFaultCode())) {
|
||||
fault = soapBody.addServerOrReceiverFault(definition.getFaultStringOrReason(), definition.getLocale());
|
||||
}
|
||||
else if (SoapFaultDefinition.CLIENT.equals(definition.getFaultCode()) ||
|
||||
SoapFaultDefinition.SENDER.equals(definition.getFaultCode())) {
|
||||
fault = soapBody.addClientOrSenderFault(definition.getFaultStringOrReason(), definition.getLocale());
|
||||
}
|
||||
else {
|
||||
// custom code, only supported for SOAP 1.1
|
||||
if (soapBody instanceof Soap11Body) {
|
||||
Soap11Body soap11Body = (Soap11Body) soapBody;
|
||||
fault = soap11Body.addFault(definition.getFaultCode(), definition.getFaultStringOrReason(),
|
||||
definition.getLocale());
|
||||
}
|
||||
else {
|
||||
logger.warn("SOAP 1.2 does not allow custom FaultCodes, only SENDER or RECEIVER.");
|
||||
}
|
||||
}
|
||||
if (fault != null) {
|
||||
customizeFault(endpoint, ex, fault);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize the {@link SoapFault} created by this resolver. Called for each created fault
|
||||
* <p/>
|
||||
* The default implementation is empty. Can be overridden in subclasses to customize the properties of the fault,
|
||||
* such as adding details, etc.
|
||||
*
|
||||
* @param endpoint the executed endpoint, or <code>null</code> if none chosen at the time of the exception
|
||||
* @param ex the exception to be handled
|
||||
* @param fault the created fault
|
||||
*/
|
||||
protected void customizeFault(Object endpoint, Exception ex, SoapFault fault) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -63,30 +63,22 @@ public class SoapFaultDefinition {
|
||||
|
||||
private Locale locale = Locale.ENGLISH;
|
||||
|
||||
/**
|
||||
* Returns the fault code.
|
||||
*/
|
||||
/** Returns the fault code. */
|
||||
public QName getFaultCode() {
|
||||
return faultCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the fault code.
|
||||
*/
|
||||
/** Sets the fault code. */
|
||||
public void setFaultCode(QName faultCode) {
|
||||
this.faultCode = faultCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fault string or reason text.
|
||||
*/
|
||||
/** Returns the fault string or reason text. By default, it is set to the exception message. */
|
||||
public String getFaultStringOrReason() {
|
||||
return faultStringOrReason;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the fault string or reason text.
|
||||
*/
|
||||
/** Sets the fault string or reason text. By default, it is set to the exception message. */
|
||||
public void setFaultStringOrReason(String faultStringOrReason) {
|
||||
this.faultStringOrReason = faultStringOrReason;
|
||||
}
|
||||
|
||||
@@ -30,11 +30,12 @@ import org.springframework.xml.namespace.QNameEditor;
|
||||
* faultCode,faultString,locale
|
||||
* </pre>
|
||||
* where <code>faultCode</code> is the string representation of a <code>QName</code>, <code>faultStringOrReason</code>
|
||||
* is the fault string, and <code>locale</code> is the optional string representations for the
|
||||
* <code>faultStringOrReason</code>language. By default, the language is set to English.
|
||||
* is the optional fault string, and <code>locale</code> is the optional string representations for the
|
||||
* <code>faultStringOrReason</code>language. By default, the language is set to English, and the fault string set to the
|
||||
* exception message.
|
||||
* <p/>
|
||||
* Instead of supplying a custom fault code, you can use the constants <code>SERVER</code> or <code>RECEIVER</code>
|
||||
* indicate a <code>Server</code>/<code>Receiver</code> fault, or or <code>CLIENT</code> or <code>SENDER</code>
|
||||
* indicate a <code>Server</code>/<code>Receiver</code> fault, or <code>CLIENT</code> or <code>SENDER</code>
|
||||
* to<code>Client</code>/<code>Sender</code> fault respectivaly.
|
||||
* <p/>
|
||||
* For example:
|
||||
@@ -54,7 +55,9 @@ import org.springframework.xml.namespace.QNameEditor;
|
||||
* @see javax.xml.namespace.QName#toString()
|
||||
* @see org.springframework.xml.namespace.QNameEditor
|
||||
* @see SoapFaultDefinition#RECEIVER
|
||||
* @see SoapFaultDefinition#SERVER
|
||||
* @see SoapFaultDefinition#SENDER
|
||||
* @see SoapFaultDefinition#CLIENT
|
||||
* @see org.springframework.ws.soap.SoapFault#getFaultCode()
|
||||
*/
|
||||
public class SoapFaultDefinitionEditor extends PropertyEditorSupport {
|
||||
@@ -71,19 +74,21 @@ public class SoapFaultDefinitionEditor extends PropertyEditorSupport {
|
||||
}
|
||||
else {
|
||||
String[] tokens = StringUtils.commaDelimitedListToStringArray(text);
|
||||
if (tokens.length < 2) {
|
||||
if (tokens.length < FAULT_STRING_INDEX) {
|
||||
throw new IllegalArgumentException("Invalid amount of comma delimited values in [" + text +
|
||||
"]: SoapFaultDefinitionEditor requires at least 2");
|
||||
"]: SoapFaultDefinitionEditor requires at least 1");
|
||||
}
|
||||
SoapFaultDefinition definition = new SoapFaultDefinition();
|
||||
QNameEditor qNameEditor = new QNameEditor();
|
||||
qNameEditor.setAsText(tokens[FAULT_CODE_INDEX].trim());
|
||||
definition.setFaultCode((QName) qNameEditor.getValue());
|
||||
definition.setFaultStringOrReason(tokens[FAULT_STRING_INDEX].trim());
|
||||
if (tokens.length > 2) {
|
||||
LocaleEditor localeEditor = new LocaleEditor();
|
||||
localeEditor.setAsText(tokens[FAULT_STRING_LOCALE_INDEX].trim());
|
||||
definition.setLocale((Locale) localeEditor.getValue());
|
||||
if (tokens.length > 1) {
|
||||
definition.setFaultStringOrReason(tokens[FAULT_STRING_INDEX].trim());
|
||||
if (tokens.length > 2) {
|
||||
LocaleEditor localeEditor = new LocaleEditor();
|
||||
localeEditor.setAsText(tokens[FAULT_STRING_LOCALE_INDEX].trim());
|
||||
definition.setLocale((Locale) localeEditor.getValue());
|
||||
}
|
||||
}
|
||||
setValue(definition);
|
||||
}
|
||||
|
||||
@@ -19,12 +19,7 @@ package org.springframework.ws.soap.server.endpoint;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.server.endpoint.AbstractEndpointExceptionResolver;
|
||||
import org.springframework.ws.soap.SoapBody;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.soap11.Soap11Body;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Exception resolver that allows for mapping exception class names to SOAP Faults. The mappings are set using the
|
||||
@@ -32,12 +27,10 @@ import org.springframework.ws.soap.soap11.Soap11Body;
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class SoapFaultMappingExceptionResolver extends AbstractEndpointExceptionResolver {
|
||||
public class SoapFaultMappingExceptionResolver extends AbstractSoapFaultDefinitionExceptionResolver {
|
||||
|
||||
private Properties exceptionMappings;
|
||||
|
||||
private SoapFaultDefinition defaultFault;
|
||||
|
||||
/**
|
||||
* Set the mappings between exception class names and SOAP Faults. The exception class name can be a substring, with
|
||||
* no wildcard support at present.
|
||||
@@ -45,60 +38,19 @@ public class SoapFaultMappingExceptionResolver extends AbstractEndpointException
|
||||
* The values of the given properties object should use the format described in
|
||||
* <code>SoapFaultDefinitionEditor</code>.
|
||||
* <p/>
|
||||
* Follows the same matching algorithm as <code>RuleBasedTransactionAttribute</code> and
|
||||
* <code>RollbackRuleAttribute</code>.
|
||||
* Follows the same matching algorithm as <code>SimpleMappingExceptionResolver</code>.
|
||||
*
|
||||
* @param mappings exception patterns (can also be fully qualified class names) as keys, fault definition texts as
|
||||
* values
|
||||
* @see SoapFaultDefinitionEditor
|
||||
* @see org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
|
||||
* @see org.springframework.transaction.interceptor.RuleBasedTransactionAttribute
|
||||
* @see org.springframework.transaction.interceptor.RollbackRuleAttribute
|
||||
*/
|
||||
public void setExceptionMappings(Properties mappings) {
|
||||
exceptionMappings = mappings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default fault. This fault will be returned if no specific mapping was found.
|
||||
*/
|
||||
public void setDefaultFault(SoapFaultDefinition defaultFault) {
|
||||
this.defaultFault = defaultFault;
|
||||
}
|
||||
|
||||
protected boolean resolveExceptionInternal(MessageContext messageContext, Object endpoint, Exception ex) {
|
||||
Assert.isTrue(messageContext.getResponse() instanceof SoapMessage,
|
||||
"SimpleSoapExceptionResolver requires a SoapMessage");
|
||||
|
||||
SoapFaultDefinition definition = getFaultDefinition(ex);
|
||||
if (definition == null) {
|
||||
return false;
|
||||
}
|
||||
SoapMessage soapResponse = (SoapMessage) messageContext.getResponse();
|
||||
SoapBody soapBody = soapResponse.getSoapBody();
|
||||
|
||||
if (SoapFaultDefinition.SERVER.equals(definition.getFaultCode()) ||
|
||||
SoapFaultDefinition.RECEIVER.equals(definition.getFaultCode())) {
|
||||
soapBody.addServerOrReceiverFault(definition.getFaultStringOrReason(), definition.getLocale());
|
||||
}
|
||||
else if (SoapFaultDefinition.CLIENT.equals(definition.getFaultCode()) ||
|
||||
SoapFaultDefinition.SENDER.equals(definition.getFaultCode())) {
|
||||
soapBody.addClientOrSenderFault(definition.getFaultStringOrReason(), definition.getLocale());
|
||||
}
|
||||
else {
|
||||
// custom code, only supported for SOAP 1.1
|
||||
if (soapBody instanceof Soap11Body) {
|
||||
Soap11Body soap11Body = (Soap11Body) soapBody;
|
||||
soap11Body.addFault(definition.getFaultCode(), definition.getFaultStringOrReason(),
|
||||
definition.getLocale());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private SoapFaultDefinition getFaultDefinition(Exception ex) {
|
||||
SoapFaultDefinition definition = null;
|
||||
if (exceptionMappings != null) {
|
||||
protected SoapFaultDefinition getFaultDefinition(Object endpoint, Exception ex) {
|
||||
if (!CollectionUtils.isEmpty(exceptionMappings)) {
|
||||
String definitionText = null;
|
||||
int deepest = Integer.MAX_VALUE;
|
||||
for (Enumeration names = exceptionMappings.propertyNames(); names.hasMoreElements();) {
|
||||
@@ -112,14 +64,10 @@ public class SoapFaultMappingExceptionResolver extends AbstractEndpointException
|
||||
if (definitionText != null) {
|
||||
SoapFaultDefinitionEditor editor = new SoapFaultDefinitionEditor();
|
||||
editor.setAsText(definitionText);
|
||||
definition = (SoapFaultDefinition) editor.getValue();
|
||||
return (SoapFaultDefinition) editor.getValue();
|
||||
}
|
||||
}
|
||||
if (definition != null || defaultFault == null) {
|
||||
return definition;
|
||||
}
|
||||
definition = defaultFault;
|
||||
return definition;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,7 +78,7 @@ public class SoapFaultMappingExceptionResolver extends AbstractEndpointException
|
||||
*
|
||||
* @see org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
|
||||
*/
|
||||
public int getDepth(String exceptionMapping, Exception ex) {
|
||||
protected int getDepth(String exceptionMapping, Exception ex) {
|
||||
return getDepth(exceptionMapping, ex.getClass(), 0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user