/**
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
* specific language governing rights and limitations under the License.
*
* The Original Code is "Responder.java". Description:
* "Performs the responding role in a message exchange according to HL7's original mode
* processing rules."
*
* The Initial Developer of the Original Code is University Health Network. Copyright (C)
* 2002. All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* Alternatively, the contents of this file may be used under the terms of the
* GNU General Public License (the �GPL�), in which case the provisions of the GPL are
* applicable instead of those above. If you wish to allow use of your version of this
* file only under the terms of the GPL and not to allow others to use your version
* of this file under the MPL, indicate your decision by deleting the provisions above
* and replace them with the notice and other provisions required by the GPL License.
* If you do not delete the provisions above, a recipient may use your version of
* this file under either the MPL or the GPL.
*
*/
package ca.uhn.hl7v2.app;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.llp.HL7Reader;
import ca.uhn.hl7v2.llp.HL7Writer;
import ca.uhn.hl7v2.llp.LLPException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.Segment;
import ca.uhn.hl7v2.parser.Parser;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.util.MessageIDGenerator;
import ca.uhn.hl7v2.util.Terser;
import ca.uhn.log.HapiLog;
import ca.uhn.log.HapiLogFactory;
//import ca.uhn.hl7v2.model.v24.datatype.ValidNM;
/**
*
Performs the responding role in a message exchange (i.e receiver of the first message,
* sender of the response; analagous to the server in a client-server interaction), according
* to HL7's original mode processing rules.
*
At the time of writing, enhanced mode, two-phase reply, continuation messages, and
* batch processing are unsupported.
* @author Bryan Tripp
*/
public class Responder {
private static final HapiLog log = HapiLogFactory.getHapiLog(Responder.class);
//private LowerLayerProtocol llp;
private Parser parser;
private ArrayList apps;
private HL7Reader in;
private HL7Writer out;
private BufferedWriter checkWriter = null;
/**
* Creates a new instance of Responder that optionally validates parsing of incoming
* messages using a system property. If the system property
* ca.uhn.hl7v2.app.checkparse equals "true", parse integrity is checked,
* i.e. each message is re-encoded and differences between the received message text
* and the re-encoded text are written to the file /parse_check.txt.
*/
public Responder(Parser parser) throws LLPException {
String checkParse = System.getProperty("ca.uhn.hl7v2.app.checkparse");
if (checkParse != null && checkParse.equals("true")) {
init(parser, true);
}
else {
init(parser, false);
}
}
/**
* Creates a new instance of Responder that optionally validates parsing of incoming messages.
* @param validate if true, encodes each incoming message after parsing it, compares
* the result to the original message string, and prints differences to the file
* "/parse_check.txt" in the working directory. This process is slow and should
* only be used during testing.
*/
public Responder(Parser parser, boolean checkParse) {
init(parser, checkParse);
}
/**
* Performs common constructor tasks.
*/
private void init(Parser parser, boolean checkParse) {
this.parser = parser;
apps = new ArrayList(10);
try {
if (checkParse)
checkWriter = new BufferedWriter(
new FileWriter(
ca.uhn.hl7v2.util.Home.getHomeDirectory().getAbsolutePath() + "/parse_check.txt", true));
}
catch (IOException e) {
log.error( "Unable to open file to write parse check results. Parse integrity checks will not proceed", e );
}
}
/**
* Processes an incoming message string and returns the response message string.
* Message processing consists of parsing the message, finding an appropriate
* Application and processing the message with it, and encoding the response.
* Applications are chosen from among those registered using
* registerApplication. The Parser is obtained from the Connection
* associated with this Responder.
*/
protected String processMessage(String incomingMessageString) throws HL7Exception {
HapiLog rawOutbound = HapiLogFactory.getHapiLog("ca.uhn.hl7v2.raw.outbound");
HapiLog rawInbound = HapiLogFactory.getHapiLog("ca.uhn.hl7v2.raw.inbound");
log.info( "Responder got message: " + incomingMessageString );
rawInbound.info(incomingMessageString);
Message incomingMessageObject = null;
String outgoingMessageString = null;
try {
incomingMessageObject = parser.parse(incomingMessageString);
}
catch (HL7Exception e) {
boolean processed = false;
for (Object app : apps) {
if (!processed) {
if (app instanceof ApplicationExceptionHandler) {
ApplicationExceptionHandler aeh = (ApplicationExceptionHandler) app;
outgoingMessageString = aeh.processException(incomingMessageString, e);
processed = true;
}
if (app instanceof MessageTypeRouter) {
HashMap routerApps = ((MessageTypeRouter) app).apps;
Set> entrySet = routerApps.entrySet();
for (Map.Entry