Skip to content

Instantly share code, notes, and snippets.

@yamanyar
Created June 26, 2012 08:00
Show Gist options
  • Save yamanyar/2994268 to your computer and use it in GitHub Desktop.
Save yamanyar/2994268 to your computer and use it in GitHub Desktop.
Log and reply with AggregatedXmlMessageValidationException details
package com.yamanyar.mediation.esb.services;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.Message;
import org.springframework.integration.message.ErrorMessage;
import org.springframework.integration.xml.AggregatedXmlMessageValidationException;
import org.springframework.xml.xsd.XsdSchemaException;
import java.util.Iterator;
/**
* This class is designed to print details of AggregatedXmlMessageValidationException.
* It only prints top 10 (or settable) problems.
*/
public class ErrorDetail {
private final Log logger = LogFactory.getLog(getClass());
private int maxErrorLoggerRequest = 10; //set default value
public void setMaxErrorLoggerRequest(int maxErrorLoggerRequest) {
this.maxErrorLoggerRequest = maxErrorLoggerRequest;
}
public Message log(Message message) {
if (message instanceof ErrorMessage) {
ErrorMessage m = (ErrorMessage) message;
Throwable payload = m.getPayload();
if (payload != null) {
Throwable cause = payload.getCause();
if (cause != null) {
if (cause instanceof AggregatedXmlMessageValidationException) {
StringBuilder sb = new StringBuilder();
AggregatedXmlMessageValidationException aggregatedXmlMessageValidationException = (AggregatedXmlMessageValidationException) cause;
Iterator<Throwable> throwableIterator = aggregatedXmlMessageValidationException.exceptionIterator();
//max allow top 10 errors; do not log all!
int x = 0;
while (throwableIterator.hasNext() && x < maxErrorLoggerRequest) {
Throwable next = throwableIterator.next();
if (next != null) {
String itemMsg = next.getMessage();
if (itemMsg != null) {
sb.append(itemMsg).append("\n");
logger.error(itemMsg);
x++;
}
}
}
m = new ErrorMessage(new XsdSchemaException(sb.toString()), m.getHeaders());
return m;
}
}
}
}
logger.error("An error occured: " + message.getPayload());
return message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment