
net.sf.itcb.common.client.exceptions.resolver.ItcbFaultMessageResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-client Show documentation
Show all versions of common-client Show documentation
This module is the common client module for calling webservices (server module). It defines all generic treatements that allows calling webservices
The newest version!
package net.sf.itcb.common.client.exceptions.resolver;
import java.io.IOException;
import java.lang.reflect.Constructor;
import javax.annotation.Resource;
import javax.xml.transform.dom.DOMResult;
import net.sf.itcb.common.business.exceptions.BusinessItcbException;
import net.sf.itcb.common.client.exceptions.ClientItcbException;
import net.sf.itcb.common.client.exceptions.ClientItcbExceptionMappingErrors;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.FaultMessageResolver;
import org.springframework.ws.soap.SoapFaultDetailElement;
import org.springframework.ws.soap.client.SoapFaultClientException;
import org.springframework.ws.soap.client.core.SoapFaultMessageResolver;
/**
* Resolves webservices exceptions.
* Currently, only those exceptions are managed :
*
* - BusinessItcbException or children
* - Spring XSD Validation Error : validation error is never logged in client side in Spring WS base
*
*
* TODO In future versions, it has to be more modular in order to add other kind of exceptions
*
* @author Pierre Le Roux
* @since 0.0.3
*/
public class ItcbFaultMessageResolver extends SoapFaultMessageResolver implements FaultMessageResolver {
private static final String SPRING_WS_VALIDATION_ERROR = "ValidationError";
private static final Object SPRING_WS_URI = "http://springframework.org/spring-ws";
@Resource(name="common-clientResources")
protected org.springframework.context.MessageSource messageSource;
@Override
public void resolveFault(WebServiceMessage message) throws IOException {
// The upper class automatically resolve a SoapFaultClientException
try {
super.resolveFault(message);
}
catch(SoapFaultClientException sfce) {
// This call will throw specific known exceptions
resolvesSpecificException(sfce);
// If no specific exception management is done, the SoapFaultClientException is thrown
throw sfce;
}
}
private void resolvesSpecificException(SoapFaultClientException sfce) {
if(sfce.getFaultCode().getNamespaceURI().equals(BusinessItcbException.ITCB_NAMESPACE_URI) ) {
makeBusinessItcbException(sfce);
}
manageOtherSpecificSoapFaultClientException(sfce);
}
private void manageOtherSpecificSoapFaultClientException(SoapFaultClientException sfce) {
//ValidationError
if(sfce.getSoapFault().getFaultDetail() != null) {
try {
while(sfce.getSoapFault().getFaultDetail().getDetailEntries().hasNext()) {
SoapFaultDetailElement saajSoapFaultDetailElement = sfce.getSoapFault().getFaultDetail().getDetailEntries().next();
if(saajSoapFaultDetailElement.getName().getNamespaceURI().equals(SPRING_WS_URI) && saajSoapFaultDetailElement.getName().getLocalPart().equals(SPRING_WS_VALIDATION_ERROR)) {
if(saajSoapFaultDetailElement.getResult() instanceof DOMResult) {
String detailedMessage = ((DOMResult)saajSoapFaultDetailElement.getResult()).getNode().getTextContent();
throw new ClientItcbException(ClientItcbExceptionMappingErrors.COMMON_CLIENT_VALIDATION_ERROR, messageSource.getMessage("common-client.exception.validation_error", new Object[] {detailedMessage}, LocaleContextHolder.getLocale()));
}
}
}
}
catch(ClientItcbException cie) {
throw cie;
}
}
}
private void makeBusinessItcbException(SoapFaultClientException sfce) {
try {
while(sfce.getSoapFault().getFaultDetail().getDetailEntries().hasNext()) {
SoapFaultDetailElement saajSoapFaultDetailElement = sfce.getSoapFault().getFaultDetail().getDetailEntries().next();
if(saajSoapFaultDetailElement.getName().getLocalPart().equals(BusinessItcbException.EXCEPTION_CLASS.getLocalPart())) {
if(saajSoapFaultDetailElement.getResult() instanceof DOMResult) {
String exceptionClassName = ((DOMResult)saajSoapFaultDetailElement.getResult()).getNode().getTextContent();
Class> exceptionClass = Class.forName(exceptionClassName);
Constructor>[] constructors= exceptionClass.getConstructors();
for (Constructor> constructor : constructors) {
if(constructor.getParameterTypes().length == 2) {
Class> classErrorMapping = constructor.getParameterTypes()[0];
BusinessItcbException bie = (BusinessItcbException)constructor.newInstance( classErrorMapping.getMethod("valueOf", String.class).invoke(classErrorMapping, sfce.getFaultCode().getLocalPart()) , sfce.getLocalizedMessage());
throw bie;
}
}
}
}
}
}
catch(BusinessItcbException bie) {
throw bie;
}
// No mapping found
catch(Throwable t) {
t.printStackTrace();
throw new ClientItcbException(ClientItcbExceptionMappingErrors.COMMON_CLIENT_BUSINESS_TRANSFORMATION_ERROR, messageSource.getMessage("common-client.exception.resolver.transformation_error", new Object[] {t.getMessage()}, LocaleContextHolder.getLocale()));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy