Coverage Report - ca.uhn.hl7v2.hoh.llp.HohLlpWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
HohLlpWriter
85%
30/35
100%
10/10
1.875
 
 1  
 package ca.uhn.hl7v2.hoh.llp;
 2  
 
 3  
 import java.io.DataOutputStream;
 4  
 import java.io.IOException;
 5  
 import java.io.OutputStream;
 6  
 import java.nio.charset.Charset;
 7  
 
 8  
 import ca.uhn.hl7v2.hoh.api.EncodeException;
 9  
 import ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpEncoder;
 10  
 import ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpRequestEncoder;
 11  
 import ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpResponseEncoder;
 12  
 import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
 13  
 import ca.uhn.hl7v2.llp.HL7Writer;
 14  
 import ca.uhn.hl7v2.llp.LLPException;
 15  
 
 16  
 class HohLlpWriter implements HL7Writer {
 17  
 
 18  
         private OutputStream myOutputStream;
 19  
         private Charset myPreferredCharset;
 20  
         private Hl7OverHttpLowerLayerProtocol myProtocol;
 21  
         private Charset myCharsetForNextMessage;
 22  
 
 23  
         /**
 24  
          * Constructor
 25  
          */
 26  65
         public HohLlpWriter(Hl7OverHttpLowerLayerProtocol theProtocol) {
 27  65
                 myProtocol = theProtocol;
 28  65
         }
 29  
 
 30  
         /**
 31  
          * {@inheritDoc}
 32  
          */
 33  
         public void close() throws IOException {
 34  0
                 myOutputStream.close();
 35  0
         }
 36  
 
 37  
         OutputStream getOutputStream() {
 38  0
                 return myOutputStream;
 39  
         }
 40  
 
 41  
         /**
 42  
          * @return the preferredCharset
 43  
          */
 44  
         public Charset getPreferredCharset() {
 45  55
                 return myPreferredCharset;
 46  
         }
 47  
 
 48  
         /**
 49  
          * {@inheritDoc}
 50  
          */
 51  
         public void setOutputStream(OutputStream theOutputStream) throws IOException {
 52  65
                 myOutputStream = theOutputStream;
 53  65
         }
 54  
 
 55  
         /**
 56  
          * @param thePreferredCharset
 57  
          *            the preferredCharset to set
 58  
          */
 59  
         public void setPreferredCharset(Charset thePreferredCharset) {
 60  65
                 myPreferredCharset = thePreferredCharset;
 61  65
         }
 62  
 
 63  
         /**
 64  
          * {@inheritDoc}
 65  
          */
 66  
         public void writeMessage(String theRawMessage) throws LLPException, IOException {
 67  
 
 68  
                 AbstractHl7OverHttpEncoder e;
 69  70
                 if (myProtocol.getRole() == ServerRoleEnum.CLIENT) {
 70  35
                         e = new Hl7OverHttpRequestEncoder();
 71  35
                         if (myProtocol.getAuthorizationClientCallback() != null) {
 72  15
                                 e.setUsername(myProtocol.getAuthorizationClientCallback().provideUsername(myProtocol.getUriPath()));
 73  15
                                 e.setPassword(myProtocol.getAuthorizationClientCallback().providePassword(myProtocol.getUriPath()));
 74  
                         }
 75  
                 } else {
 76  35
                         e = new Hl7OverHttpResponseEncoder();
 77  
                 }
 78  
 
 79  70
                 if (myProtocol.getRole() == ServerRoleEnum.CLIENT) {
 80  35
                         e.setSigner(myProtocol.getSigner());
 81  
                 }
 82  
                 
 83  70
                 e.setMessage(theRawMessage);
 84  70
                 if (myCharsetForNextMessage != null) {
 85  35
                         e.setCharset(myCharsetForNextMessage);
 86  35
                         myCharsetForNextMessage = null;
 87  35
                 } else if (getPreferredCharset() != null) {
 88  20
                         e.setCharset(getPreferredCharset());
 89  
                 }
 90  
 
 91  70
                 e.setPath(myProtocol.getUriPath());
 92  70
                 DataOutputStream dos = new DataOutputStream(myOutputStream);
 93  
                 try {
 94  70
                         e.encodeToOutputStream(dos);
 95  0
                 } catch (EncodeException e1) {
 96  0
                         throw new LLPException("Failed to encode message", e1);
 97  70
                 }
 98  
 
 99  70
                 dos.flush();
 100  
 
 101  70
         }
 102  
 
 103  
         void setCharsetForNextMessage(Charset theCharset) {
 104  35
                 myCharsetForNextMessage = theCharset;
 105  35
         }
 106  
 
 107  
 }