// invoke it any time something goes wrong
log.debug("ApplicationRouterImpl got message: {}", incomingMessageString);
rawInbound.debug(incomingMessageString);
Message incomingMessageObject = null;
String outgoingMessageString = null;
String outgoingMessageCharset = null;
try {
incomingMessageObject = myParser.parse(incomingMessageString);
Terser inTerser = new Terser(incomingMessageObject);
theMetadata.put(MetadataKeys.IN_MESSAGE_CONTROL_ID, inTerser.get("/.MSH-10"));
} catch (HL7Exception e) {
try {
outgoingMessageString = logAndMakeErrorMessage(e, myParser.getCriticalResponseData(incomingMessageString), myParser, myParser.getEncoding(incomingMessageString));
} catch (HL7Exception e2) {
outgoingMessageString = null;
}
if (myExceptionHandler != null) {
outgoingMessageString = myExceptionHandler.processException(incomingMessageString, theMetadata, outgoingMessageString, e);
if (outgoingMessageString == null) {
throw new HL7Exception("Application exception handler may not return null");
}
}
}
// At this point, no exception has occurred and the message is processed normally
if (outgoingMessageString == null) {
try {
//optionally check integrity of parse
String check = System.getProperty("ca.uhn.hl7v2.protocol.impl.check_parse");
if (check != null && check.equals("TRUE")) {
ParseChecker.checkParse(incomingMessageString, incomingMessageObject, myParser);
}
//message validation (in terms of optionality, cardinality) would go here ***
ReceivingApplication app = findApplication(incomingMessageObject);
theMetadata.put(RAW_MESSAGE_KEY, incomingMessageString);
log.debug("Sending message to application: {}", app.toString());
Message response = app.processMessage(incomingMessageObject, theMetadata);
//Here we explicitly use the same encoding as that of the inbound message - this is important with GenericParser, which might use a different encoding by default
outgoingMessageString = myParser.encode(response, myParser.getEncoding(incomingMessageString));
Terser t = new Terser(response);