Coverage Report - ca.uhn.hl7v2.hoh.hapi.api.MessageSendable
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageSendable
60%
18/30
25%
3/12
2.571
 
 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  15
 public class MessageSendable implements IResponseSendable<Message> {
 17  
         
 18  5
         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 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  25
                 this(theMessage, theMessage.getParser());
 36  25
         }
 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  25
         public MessageSendable(Message theMessage, Parser theParser) throws HL7Exception {
 49  25
                 if (theMessage == null) {
 50  0
                         throw new NullPointerException("Raw Message may not be null");
 51  
                 }
 52  25
                 myMessage = theMessage;
 53  25
                 myEncodingStyle = detectEncodingStyle(theParser);
 54  25
                 myRawMessage = theParser.encode(myMessage);
 55  25
         }
 56  
 
 57  
         private EncodingStyle detectEncodingStyle(Parser theParser) {
 58  25
                 if (theParser instanceof XMLParser) {
 59  0
                         return EncodingStyle.XML;
 60  25
                 } else if (theParser instanceof GenericParser && ((GenericParser) theParser).isPipeParserPrimary() == false) {
 61  0
                         return EncodingStyle.XML;
 62  
                 } else {
 63  25
                         return EncodingStyle.ER7;
 64  
                 }
 65  
         }
 66  
 
 67  
         public void writeMessage(Writer theWriter) throws IOException {
 68  20
                 theWriter.append(myRawMessage);
 69  20
                 theWriter.flush();
 70  20
         }
 71  
 
 72  
         public EncodingStyle getEncodingStyle() {
 73  20
                 return myEncodingStyle;
 74  
         }
 75  
 
 76  
         public ResponseCode getResponseCode() {
 77  0
                 if (myResponseCode == null) {
 78  
                         try {
 79  0
                                 myResponseCode = ResponseCode.detect(new Terser(myMessage).get("/MSA-2"));
 80  0
                                 if (myResponseCode == null) {
 81  0
                                         throw new HL7Exception("No response code in message, this is probably an error");
 82  
                                 }
 83  0
                         } catch (HL7Exception e) {
 84  0
                                 ourLog.info("Could not detect response code in message", e);
 85  0
                                 myResponseCode = ResponseCode.HTTP_500_INTERNAL_SERVER_ERROR;
 86  0
                         }
 87  
                 }
 88  0
                 return myResponseCode;
 89  
         }
 90  
 
 91  
         public Message getMessage() {
 92  15
                 return myMessage;
 93  
         }
 94  
 
 95  
 }