View Javadoc
1   package ca.uhn.hl7v2.hoh.hapi.api;
2   
3   import java.io.IOException;
4   import java.io.Writer;
5   
6   import ca.uhn.hl7v2.HL7Exception;
7   import ca.uhn.hl7v2.hoh.api.IResponseSendable;
8   import ca.uhn.hl7v2.hoh.encoder.EncodingStyle;
9   import ca.uhn.hl7v2.hoh.encoder.ResponseCode;
10  import ca.uhn.hl7v2.model.Message;
11  import ca.uhn.hl7v2.parser.GenericParser;
12  import ca.uhn.hl7v2.parser.Parser;
13  import ca.uhn.hl7v2.parser.XMLParser;
14  import ca.uhn.hl7v2.util.Terser;
15  
16  public class MessageSendable implements IResponseSendable<Message> {
17  	
18  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(MessageSendable.class);
19  
20  	private final Message myMessage;
21  	private final EncodingStyle myEncodingStyle;
22  	private ResponseCode myResponseCode;
23  
24  	private final String myRawMessage;
25  
26  	/**
27  	 * Constructor
28  	 * 
29  	 * @param theMessage
30  	 *            The message to return
31  	 * @throws HL7Exception
32  	 *             If message could not be encoded
33  	 */
34  	public MessageSendable(Message theMessage) throws HL7Exception {
35  		this(theMessage, theMessage.getParser());
36  	}
37  
38  	/**
39  	 * Constructor
40  	 * 
41  	 * @param theMessage
42  	 *            The message to return
43  	 * @param theParser
44  	 *            The parser to use to encode the message
45  	 * @throws HL7Exception
46  	 *             If message could not be encoded
47  	 */
48  	public MessageSendable(Message theMessage, Parser theParser) throws HL7Exception {
49  		if (theMessage == null) {
50  			throw new NullPointerException("Raw Message may not be null");
51  		}
52  		myMessage = theMessage;
53  		myEncodingStyle = detectEncodingStyle(theParser);
54  		myRawMessage = theParser.encode(myMessage);
55  	}
56  
57  	private EncodingStyle detectEncodingStyle(Parser theParser) {
58  		if (theParser instanceof XMLParser) {
59  			return EncodingStyle.XML;
60  		} else if (theParser instanceof GenericParser/../../ca/uhn/hl7v2/parser/GenericParser.html#GenericParser">GenericParser && !((GenericParser) theParser).isPipeParserPrimary()) {
61  			return EncodingStyle.XML;
62  		} else {
63  			return EncodingStyle.ER7;
64  		}
65  	}
66  
67  	public void writeMessage(Writer theWriter) throws IOException {
68  		theWriter.append(myRawMessage);
69  		theWriter.flush();
70  	}
71  
72  	public EncodingStyle getEncodingStyle() {
73  		return myEncodingStyle;
74  	}
75  
76  	public ResponseCode getResponseCode() {
77  		if (myResponseCode == null) {
78  			try {
79  				myResponseCode = ResponseCode.detect(new Terser(myMessage).get("/MSA-2"));
80  				if (myResponseCode == null) {
81  					throw new HL7Exception("No response code in message, this is probably an error");
82  				}
83  			} catch (HL7Exception e) {
84  				ourLog.info("Could not detect response code in message", e);
85  				myResponseCode = ResponseCode.HTTP_500_INTERNAL_SERVER_ERROR;
86  			}
87  		}
88  		return myResponseCode;
89  	}
90  
91  	public Message getMessage() {
92  		return myMessage;
93  	}
94  
95  }