001package ca.uhn.hl7v2.hoh.hapi.api;
002
003import java.io.IOException;
004import java.io.Writer;
005
006import ca.uhn.hl7v2.HL7Exception;
007import ca.uhn.hl7v2.hoh.api.IResponseSendable;
008import ca.uhn.hl7v2.hoh.encoder.EncodingStyle;
009import ca.uhn.hl7v2.hoh.encoder.ResponseCode;
010import ca.uhn.hl7v2.model.Message;
011import ca.uhn.hl7v2.parser.GenericParser;
012import ca.uhn.hl7v2.parser.Parser;
013import ca.uhn.hl7v2.parser.XMLParser;
014import ca.uhn.hl7v2.util.Terser;
015
016public class MessageSendable implements IResponseSendable<Message> {
017        
018        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(MessageSendable.class);
019
020        private final Message myMessage;
021        private final EncodingStyle myEncodingStyle;
022        private ResponseCode myResponseCode;
023
024        private String myRawMessage;
025
026        /**
027         * Constructor
028         * 
029         * @param theMessage
030         *            The message to return
031         * @throws HL7Exception
032         *             If message could not be encoded
033         */
034        public MessageSendable(Message theMessage) throws HL7Exception {
035                this(theMessage, theMessage.getParser());
036        }
037
038        /**
039         * Constructor
040         * 
041         * @param theMessage
042         *            The message to return
043         * @param theParser
044         *            The parser to use to encode the message
045         * @throws HL7Exception
046         *             If message could not be encoded
047         */
048        public MessageSendable(Message theMessage, Parser theParser) throws HL7Exception {
049                if (theMessage == null) {
050                        throw new NullPointerException("Raw Message may not be null");
051                }
052                myMessage = theMessage;
053                myEncodingStyle = detectEncodingStyle(theParser);
054                myRawMessage = theParser.encode(myMessage);
055        }
056
057        private EncodingStyle detectEncodingStyle(Parser theParser) {
058                if (theParser instanceof XMLParser) {
059                        return EncodingStyle.XML;
060                } else if (theParser instanceof GenericParser && ((GenericParser) theParser).isPipeParserPrimary() == false) {
061                        return EncodingStyle.XML;
062                } else {
063                        return EncodingStyle.ER7;
064                }
065        }
066
067        public void writeMessage(Writer theWriter) throws IOException {
068                theWriter.append(myRawMessage);
069                theWriter.flush();
070        }
071
072        public EncodingStyle getEncodingStyle() {
073                return myEncodingStyle;
074        }
075
076        public ResponseCode getResponseCode() {
077                if (myResponseCode == null) {
078                        try {
079                                myResponseCode = ResponseCode.detect(new Terser(myMessage).get("/MSA-2"));
080                                if (myResponseCode == null) {
081                                        throw new HL7Exception("No response code in message, this is probably an error");
082                                }
083                        } catch (HL7Exception e) {
084                                ourLog.info("Could not detect response code in message", e);
085                                myResponseCode = ResponseCode.HTTP_500_INTERNAL_SERVER_ERROR;
086                        }
087                }
088                return myResponseCode;
089        }
090
091        public Message getMessage() {
092                return myMessage;
093        }
094
095}