Skip to content

Commit

Permalink
MIRTH-2970
Browse files Browse the repository at this point in the history
The AutoResponder.getResponse method now throws an exception if it fails to generate an ack. This exception is now being caught in relevant classes. If an error occurs, the stack trace is retrieved and a response error is set on the resulting message.
Issue: MIRTH-2970


git-svn-id: https://svn.mirthcorp.com/connect/trunk@8076 c801cdd1-710f-0410-a44a-8d9edceeb29b
  • Loading branch information
jaysenp committed Jul 1, 2016
1 parent cfd4c03 commit ccf510e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

Expand Down Expand Up @@ -1165,6 +1166,7 @@ protected DispatchResult dispatchRawMessage(RawMessage rawMessage, boolean batch
DonkeyDao dao = null;
Message processedMessage = null;
Response response = null;
String responseErrorMessage = null;
DispatchResult dispatchResult = null;

try {
Expand Down Expand Up @@ -1200,7 +1202,11 @@ protected DispatchResult dispatchRawMessage(RawMessage rawMessage, boolean batch
}

if (responseSelector.canRespond()) {
response = responseSelector.getResponse(sourceMessage, processedMessage);
try {
response = responseSelector.getResponse(sourceMessage, processedMessage);
} catch (Exception e) {
responseErrorMessage = ExceptionUtils.getStackTrace(e);
}
}
} catch (RuntimeException e) {
// TODO determine behavior if this occurs.
Expand All @@ -1220,6 +1226,10 @@ protected DispatchResult dispatchRawMessage(RawMessage rawMessage, boolean batch
// Create the DispatchResult at the very end because lockAcquired might have changed
if (persistedMessageId != null) {
dispatchResult = new DispatchResult(persistedMessageId, processedMessage, response, sourceConnector.isRespondAfterProcessing(), lockAcquired);

if (StringUtils.isNotBlank(responseErrorMessage)) {
dispatchResult.setResponseError(responseErrorMessage);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.concurrent.Callable;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.mirth.connect.donkey.model.message.ConnectorMessage;
Expand Down Expand Up @@ -298,16 +299,26 @@ private void recoverUnfinishedMessage(Message unfinishedMessage) throws Interrup

if (responseSelector.canRespond()) {
Response response = null;
String responseErrorMessage = null;

/*
* only put a response in the dispatchResult if a response was not already stored in the
* source message (which happens when the source queue is enabled)
*/
if (sourceMessage.getResponse() == null) {
response = responseSelector.getResponse(sourceMessage, unfinishedMessage);
try {
response = responseSelector.getResponse(sourceMessage, unfinishedMessage);
} catch (Exception e) {
responseErrorMessage = ExceptionUtils.getStackTrace(e);
}
}

DispatchResult dispatchResult = new DispatchResult(unfinishedMessage.getMessageId(), unfinishedMessage, response, true, false);

if (StringUtils.isNotBlank(responseErrorMessage)) {
dispatchResult.setResponseError(responseErrorMessage);
}

channel.getSourceConnector().handleRecoveredResponse(dispatchResult);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean canRespond() {
* @param message
* The composite message
*/
public Response getResponse(ConnectorMessage sourceMessage, Message message) {
public Response getResponse(ConnectorMessage sourceMessage, Message message) throws Exception {
if (respondFromName.equals(SourceConnectorProperties.RESPONSE_AUTO_BEFORE)) {
// Assume a successful status since we're responding before the message has been processed
return dataType.getAutoResponder().getResponse(Status.RECEIVED, sourceMessage.getRaw().getContent(), sourceMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface AutoResponder {
* statuses).
* @return The response that should be sent back to the originating system.
*/
public Response getResponse(Status status, String message, ConnectorMessage connectorMessage);
public Response getResponse(Status status, String message, ConnectorMessage connectorMessage) throws Exception;

/**
* Returns a response message generated based on the passed inbound message and properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public HL7v2AutoResponder(SerializationProperties serializationProperties, Respo
}

@Override
public Response getResponse(Status status, String message, ConnectorMessage connectorMessage) {
public Response getResponse(Status status, String message, ConnectorMessage connectorMessage) throws Exception {
HL7v2ResponseGenerationProperties hl7Properties = getReplacedHL7Properties(connectorMessage);
return generateACK(status, message, hl7Properties);
}
Expand All @@ -88,7 +88,7 @@ private HL7v2ResponseGenerationProperties getReplacedHL7Properties(ConnectorMess
return hl7v2Properties;
}

private Response generateACK(Status status, String hl7Message, HL7v2ResponseGenerationProperties hl7v2Properties) {
private Response generateACK(Status status, String hl7Message, HL7v2ResponseGenerationProperties hl7v2Properties) throws Exception {
boolean errorOnly = false;
boolean always = false;
boolean successOnly = false;
Expand Down Expand Up @@ -185,8 +185,7 @@ private Response generateACK(Status status, String hl7Message, HL7v2ResponseGene
logger.debug("HL7v2 " + (nack ? "N" : "") + "ACK successfully generated: " + ACK);
} catch (Exception e) {
logger.warn("Error generating HL7v2 ACK.", e);
statusMessage = "Error generating HL7v2 ACK.";
error = e.getMessage();
throw new Exception("Error generating HL7v2 ACK.", e);
}

return new Response(status, ACK, statusMessage, error);
Expand Down

0 comments on commit ccf510e

Please sign in to comment.