Skip to content

Commit

Permalink
Impl frankframework#1340 ErrorMessageFormatter returning Message inst…
Browse files Browse the repository at this point in the history
…ead of String (frankframework#1359)
  • Loading branch information
Gerrit van Brakel authored Jan 4, 2021
1 parent a3ed363 commit b9c7013
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 74 deletions.
23 changes: 8 additions & 15 deletions core/src/main/java/nl/nn/adapterframework/core/Adapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class Adapter implements IAdapter, NamedBean {
private String targetDesignDocument;
private boolean active=true;

private ArrayList<Receiver> receivers = new ArrayList<>();
private ArrayList<Receiver<?>> receivers = new ArrayList<>();
private long lastMessageDate = 0;
private String lastMessageProcessingState; //"OK" or "ERROR"
private PipeLine pipeline;
Expand Down Expand Up @@ -326,32 +326,25 @@ public long getLastExitIsTimeoutDate(String pipeName) {
}

@Override
public synchronized String formatErrorMessage(String errorMessage, Throwable t, Message originalMessage, String messageID, INamedObject objectInError, long receivedTime) {
public Message formatErrorMessage(String errorMessage, Throwable t, Message originalMessage, String messageID, INamedObject objectInError, long receivedTime) {
if (errorMessageFormatter == null) {
errorMessageFormatter = new ErrorMessageFormatter();
}
// you never can trust an implementation, so try/catch!
try {
String formattedErrorMessage= errorMessageFormatter.format(
errorMessage,
t,
objectInError,
originalMessage,
messageID,
receivedTime);
Message formattedErrorMessage= errorMessageFormatter.format(errorMessage, t, objectInError, originalMessage, messageID, receivedTime);

if(msgLog.isEnabled(MSGLOG_LEVEL_TERSE)) {
String resultOrSize = (isMsgLogHidden()) ? "SIZE="+getFileSizeAsBytes(new Message(formattedErrorMessage)) : formattedErrorMessage;
String resultOrSize = (isMsgLogHidden()) ? "SIZE="+getFileSizeAsBytes(formattedErrorMessage) : formattedErrorMessage.toString();
msgLog.log(MSGLOG_LEVEL_TERSE, String.format("Adapter [%s] messageId [%s] formatted errormessage, result [%s]", getName(), messageID, resultOrSize));
}

return formattedErrorMessage;
}
catch (Exception e) {
} catch (Exception e) {
String msg = "got error while formatting errormessage, original errorMessage [" + errorMessage + "]";
msg = msg + " from [" + (objectInError == null ? "unknown-null" : objectInError.getName()) + "]";
error(false, "got error while formatting errormessage", e);
return errorMessage;
return new Message(errorMessage);
}
}
/**
Expand Down Expand Up @@ -523,7 +516,7 @@ public Receiver getReceiverByNameAndListener(String receiverName, Class<?> liste
}

@Override
public Iterable<Receiver> getReceivers() {
public Iterable<Receiver<?>> getReceivers() {
return receivers;
}

Expand Down Expand Up @@ -590,7 +583,7 @@ public PipeLineResult processMessage(String messageId, Message message, IPipeLin
objectInError = this;
}
}
result.setResult(new Message(formatErrorMessage(msg, t, message, messageId, objectInError, startTime)));
result.setResult(formatErrorMessage(msg, t, message, messageId, objectInError, startTime));
//if (isRequestReplyLogging()) {

String format = "Adapter [%s] messageId [%s] got exit-state [%s] and result [%s] from PipeLine";
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/nl/nn/adapterframework/core/IAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public interface IAdapter extends IManagable {
* the web-functions.
*/
public MessageKeeper getMessageKeeper();
public Receiver getReceiverByName(String receiverName);
public Iterable<Receiver> getReceivers();
public Receiver<?> getReceiverByName(String receiverName);
public Iterable<Receiver<?>> getReceivers();
public PipeLineResult processMessage(String messageId, Message message, IPipeLineSession pipeLineSession);
public PipeLineResult processMessageWithExceptions(String messageId, Message message, IPipeLineSession pipeLineSession) throws ListenerException;

Expand All @@ -59,7 +59,7 @@ public interface IAdapter extends IManagable {
public Configuration getConfiguration();
public boolean isAutoStart();

public String formatErrorMessage(String errorMessage, Throwable t, Message originalMessage, String messageID, INamedObject objectInError, long receivedTime);
public Message formatErrorMessage(String errorMessage, Throwable t, Message originalMessage, String messageID, INamedObject objectInError, long receivedTime);

public void forEachStatisticsKeeperBody(StatisticsKeeperIterationHandler hski, Object data, int action) throws SenderException ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
*/
public interface IErrorMessageFormatter {

public String format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime);
public Message format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public interface IMessageHandler<M> {
* Formats any exception thrown by any of the above methods to a message that can be returned.
* Can be used if the calling system has no other way of returnin the exception to the caller.
*/
public String formatException(String extrainfo, String correlationId, Message message, Throwable t);
public Message formatException(String extrainfo, String correlationId, Message message, Throwable t);

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class ErrorMessageFormatter implements IErrorMessageFormatter {
* Override this method in descender-classes to obtain the required behaviour.
*/
@Override
public String format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime) {
public Message format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime) {

String details = null;
errorMessage = getErrorMessage(errorMessage, t);
Expand Down Expand Up @@ -102,7 +102,7 @@ public String format(String errorMessage, Throwable t, INamedObject location, Me
}
errorXml.addSubElement(originalMessageXml);

return errorXml.toXML();
return new Message(errorXml.toXML());
}

protected String getErrorMessage(String message, Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package nl.nn.adapterframework.errormessageformatters;

import java.io.IOException;
import java.net.URL;

import nl.nn.adapterframework.core.INamedObject;
Expand Down Expand Up @@ -44,25 +45,29 @@ public class FixedErrorMessage extends ErrorMessageFormatter {
private String styleSheetName = null;

@Override
public String format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime) {
public Message format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime) {

String stringToReturn = getReturnString();
if (stringToReturn==null) {
stringToReturn="";
Message messageToReturn = new Message(getReturnString());
if (messageToReturn.isEmpty()) {
messageToReturn=new Message("");
}
if (StringUtils.isNotEmpty(getFileName())) {
try {
stringToReturn += Misc.resourceToString(ClassUtils.getResourceURL(classLoader, getFileName()), SystemUtils.LINE_SEPARATOR);
messageToReturn = new Message(messageToReturn.asString() + Misc.resourceToString(ClassUtils.getResourceURL(classLoader, getFileName()), SystemUtils.LINE_SEPARATOR));
} catch (Throwable e) {
log.error("got exception loading error message file [" + getFileName() + "]", e);
}
}
if (StringUtils.isEmpty(stringToReturn)) {
stringToReturn = super.format(errorMessage, t, location, originalMessage, messageId, receivedTime);
}
if (messageToReturn.isEmpty()) {
messageToReturn = super.format(errorMessage, t, location, originalMessage, messageId, receivedTime);
}

if (StringUtils.isNotEmpty(getReplaceFrom())) {
stringToReturn = Misc.replace(stringToReturn, getReplaceFrom(), getReplaceTo() );
try {
messageToReturn = new Message(Misc.replace(messageToReturn.asString(), getReplaceFrom(), getReplaceTo()));
} catch (IOException e) {
log.error("got error formatting errorMessage", e);
}
}

if (StringUtils.isNotEmpty(styleSheetName)) {
Expand All @@ -71,15 +76,15 @@ public String format(String errorMessage, Throwable t, INamedObject location, Me
try{
String xsltResult = null;
Transformer transformer = XmlUtils.createTransformer(xsltSource);
xsltResult = XmlUtils.transformXml(transformer, stringToReturn);
stringToReturn = xsltResult;
xsltResult = XmlUtils.transformXml(transformer, messageToReturn.asSource());
messageToReturn = new Message(xsltResult);
} catch (Throwable e) {
log.error("got error transforming resource [" + xsltSource.toString() + "] from [" + styleSheetName + "]", e);
}
}
}

return stringToReturn;
return messageToReturn;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
public class SoapErrorMessage extends ErrorMessageFormatter {

@Override
public String format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime) {
public Message format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime) {

try {
return SoapWrapper.getInstance().createSoapFaultMessage(getErrorMessage(errorMessage, t)).asString();
return SoapWrapper.getInstance().createSoapFaultMessage(getErrorMessage(errorMessage, t));
} catch (Exception e) {
log.error("got error getting soapWrapper instance", e);
return super.format(errorMessage, t, location, originalMessage, messageId, receivedTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public class XslErrorMessageFormatter extends ErrorMessageFormatter {
private String xpathExpression;

@Override
public String format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime) {
public Message format(String errorMessage, Throwable t, INamedObject location, Message originalMessage, String messageId, long receivedTime) {

String result = super.format(errorMessage, t, location, originalMessage, messageId, receivedTime);
Message result = super.format(errorMessage, t, location, originalMessage, messageId, receivedTime);

if (StringUtils.isNotEmpty(getStyleSheet()) || StringUtils.isNotEmpty(getXpathExpression())) {
try {
Expand Down Expand Up @@ -94,7 +94,7 @@ public String format(String errorMessage, Throwable t, INamedObject location, Me

XmlUtils.setTransformerParameters(errorTransformer, parametervalues );
}
result = XmlUtils.transformXml(errorTransformer, result);
result = new Message(XmlUtils.transformXml(errorTransformer, result.asSource()));
} catch (IOException e) {
log.error(" cannot retrieve [" + styleSheet + "]", e);
} catch (javax.xml.transform.TransformerConfigurationException te) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
public class Y01ErrorMessageFormatter extends ErrorMessageFormatter {

public String format(
public Message format(
String message,
Throwable t,
INamedObject location,
Expand Down Expand Up @@ -58,6 +58,6 @@ public String format(



return result;
return new Message(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Message processRequest(String correlationId, Message rawMessage, Map<Stri
throw e;
}
log.debug("PushingListenerAdapter.processRequest() formats ListenerException to errormessage");
return new Message(handler.formatException(null,correlationId, message, e));
return handler.formatException(null,correlationId, message, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ public PipeRunResult doPipe(Message message, IPipeLineSession session) throws Pi
PipeForward exceptionForward = findForward(EXCEPTIONFORWARD);
if (exceptionForward!=null) {
log.warn(getLogPrefix(session) + "exception occured, forwarded to ["+exceptionForward.getPath()+"]", e);
String resultmsg=new ErrorMessageFormatter().format(getLogPrefix(session),e,this,message,session.getMessageId(),0);
return new PipeRunResult(exceptionForward,resultmsg);
return new PipeRunResult(exceptionForward, new ErrorMessageFormatter().format(getLogPrefix(session),e,this,message,session.getMessageId(),0));
}
throw new PipeRunException(this, getLogPrefix(session) + "Unexpected exception during compression", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,9 @@ public PipeRunResult doPipe(Message input, IPipeLineSession session) throws Pipe
}
}
if (timeoutForward!=null) {
String resultmsg;
Message resultmsg;
if (StringUtils.isNotEmpty(getResultOnTimeOut())) {
resultmsg =getResultOnTimeOut();
resultmsg =new Message(getResultOnTimeOut());
} else {
resultmsg=new ErrorMessageFormatter().format(getLogPrefix(session),toe,this,input,session.getMessageId(),0);
}
Expand All @@ -752,9 +752,7 @@ public PipeRunResult doPipe(Message input, IPipeLineSession session) throws Pipe
PipeForward exceptionForward = findForward(EXCEPTION_FORWARD);
if (exceptionForward!=null) {
log.warn(getLogPrefix(session) + "exception occured, forwarding to exception-forward ["+exceptionForward.getPath()+"], exception:\n", t);
String resultmsg;
resultmsg=new ErrorMessageFormatter().format(getLogPrefix(session),t,this,input,session.getMessageId(),0);
return new PipeRunResult(exceptionForward,resultmsg);
return new PipeRunResult(exceptionForward, new ErrorMessageFormatter().format(getLogPrefix(session),t,this,input,session.getMessageId(),0));
}
throw new PipeRunException(this, getLogPrefix(session) + "caught exception", t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public PipeRunResult processPipe(PipeLine pipeLine, IPipe pipe, Message message,
}

ErrorMessageFormatter emf = new ErrorMessageFormatter();
String errorMessage = emf.format(e.getMessage(), e.getCause(), pipeLine.getOwner(), message, pipeLineSession.getMessageId(), tsReceivedLong);
Message errorMessage = emf.format(e.getMessage(), e.getCause(), pipeLine.getOwner(), message, pipeLineSession.getMessageId(), tsReceivedLong);
return new PipeRunResult(pipe.getForwards().get("exception"), errorMessage);
}
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ public String processRequest(String correlationId, String rawMessage, HashMap co
} else {
try {
return handler.processRequest(this, correlationId, rawMessage, message, context).asString();
}
catch (ListenerException | IOException e) {
return handler.formatException(null,correlationId, message, e);
} catch (ListenerException | IOException e) {
try {
return handler.formatException(null,correlationId, message, e).asString();
} catch (IOException e1) {
throw new ListenerException(e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1297,8 +1297,7 @@ private Message processMessageInAdapter(Object rawMessageOrWrapper, Message mess
pipeLineResult=new PipeLineResult();
}
if (Message.isEmpty(pipeLineResult.getResult())) {
String formattedErrorMessage=adapter.formatErrorMessage("exception caught",t,message,messageId,this,startProcessingTimestamp);
pipeLineResult.setResult(new Message(formattedErrorMessage));
pipeLineResult.setResult(adapter.formatErrorMessage("exception caught",t,message,messageId,this,startProcessingTimestamp));
}
throw wrapExceptionAsListenerException(t);
} finally {
Expand Down Expand Up @@ -1684,7 +1683,7 @@ private String sendResultToSender(String correlationId, Message result) {
}

@Override
public String formatException(String extrainfo, String correlationId, Message message, Throwable t) {
public Message formatException(String extrainfo, String correlationId, Message message, Throwable t) {
return getAdapter().formatErrorMessage(extrainfo,t,message,correlationId,null,0);
}

Expand Down
4 changes: 0 additions & 4 deletions core/src/main/java/nl/nn/adapterframework/util/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1643,10 +1643,6 @@ public static void setTransformerParameters(Transformer t, Map<String,Object> pa
}
}

public static String transformXml(Transformer t, Document d) throws TransformerException, IOException {
return transformXml(t, new DOMSource(d));
}

public static String transformXml(Transformer t, String s) throws TransformerException, IOException, SAXException {
return transformXml(t, s, isNamespaceAwareByDefault());
}
Expand Down
Loading

0 comments on commit b9c7013

Please sign in to comment.