Coverage Report - ca.uhn.hl7v2.hoh.llp.HohLlpReader
 
Classes in this File Line Coverage Branch Coverage Complexity
HohLlpReader
60%
24/40
50%
5/10
3.6
 
 1  
 package ca.uhn.hl7v2.hoh.llp;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.InputStream;
 5  
 import java.nio.charset.Charset;
 6  
 
 7  
 import ca.uhn.hl7v2.hoh.api.DecodeException;
 8  
 import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
 9  
 import ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder;
 10  
 import ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpRequestDecoder;
 11  
 import ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpResponseDecoder;
 12  
 import ca.uhn.hl7v2.hoh.encoder.NoMessageReceivedException;
 13  
 import ca.uhn.hl7v2.hoh.sign.SignatureVerificationException;
 14  
 import ca.uhn.hl7v2.hoh.util.HTTPUtils;
 15  
 import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
 16  
 import ca.uhn.hl7v2.llp.HL7Reader;
 17  
 import ca.uhn.hl7v2.llp.LLPException;
 18  
 
 19  
 class HohLlpReader implements HL7Reader {
 20  5
         private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HohLlpReader.class);
 21  
 
 22  
         private InputStream myInputStream;
 23  
         private Hl7OverHttpLowerLayerProtocol myProtocol;
 24  
         private HohLlpWriter myWriter;
 25  
 
 26  
         /**
 27  
          * Constructor
 28  
          */
 29  65
         public HohLlpReader(Hl7OverHttpLowerLayerProtocol theProtocol) {
 30  65
                 myProtocol = theProtocol;
 31  65
         }
 32  
 
 33  
         /**
 34  
          * {@inheritDoc}
 35  
          */
 36  
         public void close() throws IOException {
 37  0
                 myInputStream.close();
 38  0
         }
 39  
 
 40  
         public String getMessage() throws LLPException, IOException {
 41  
                 AbstractHl7OverHttpDecoder decoder;
 42  
 
 43  165
                 if (myProtocol.getRole() == ServerRoleEnum.CLIENT) {
 44  70
                         decoder = new Hl7OverHttpResponseDecoder();
 45  
                 } else {
 46  95
                         Hl7OverHttpRequestDecoder requestDecoder = new Hl7OverHttpRequestDecoder();
 47  95
                         requestDecoder.setAuthorizationCallback(myProtocol.getAuthorizationServerCallback());
 48  95
                         decoder = requestDecoder;
 49  95
                         decoder.setSigner(myProtocol.getSigner());
 50  
                 }
 51  
 
 52  
                 
 53  
                 try {
 54  165
                         decoder.readHeadersAndContentsFromInputStreamAndDecode(myInputStream);
 55  0
                 } catch (DecodeException e) {
 56  0
                         if (myProtocol.getRole() == ServerRoleEnum.CLIENT) {
 57  0
                                 throw new LLPException("Failed to process response", e);
 58  
                         } else {
 59  0
                                 ourLog.info("Failed to read contents", e);
 60  0
                                 HTTPUtils.write400BadRequest(myWriter.getOutputStream(), e.getMessage());
 61  
 
 62  0
                                 myInputStream.close();
 63  0
                                 myWriter.getOutputStream().close();
 64  
 
 65  0
                                 throw new LLPException("Failed to read message", e);
 66  
                         }
 67  30
                 } catch (NoMessageReceivedException e) {
 68  30
                         return null;
 69  0
                 } catch (SignatureVerificationException e) {
 70  0
                         throw new LLPException("Failed to verify message signature", e);
 71  70
                 }
 72  
                 
 73  70
                 if (myProtocol.getRole() == ServerRoleEnum.SERVER) {
 74  35
                         Charset charset = decoder.getCharset();
 75  35
                         myWriter.setCharsetForNextMessage(charset);
 76  
                         
 77  35
                         IAuthorizationServerCallback authorizationCallback = myProtocol.getAuthorizationServerCallback();
 78  35
                         if (authorizationCallback != null) {
 79  0
                                 boolean auth = authorizationCallback.authorize(decoder.getPath(), decoder.getUsername(), decoder.getPassword());
 80  0
                                 if (!auth) {
 81  0
                                         HTTPUtils.write401Unauthorized(myWriter.getOutputStream());
 82  0
                                         throw new LLPException("Authorization at URI[" + decoder.getPath() + "] failed for user[" + decoder.getUsername() + "]");
 83  
                                 }
 84  
                         }
 85  
                 }
 86  
 
 87  70
                 return decoder.getMessage();
 88  
 
 89  
         }
 90  
 
 91  
         public void setInputStream(InputStream theInputStream) throws IOException {
 92  65
                 myInputStream = theInputStream;
 93  65
         }
 94  
 
 95  
         void setWriter(HohLlpWriter theWriter) {
 96  65
                 myWriter = theWriter;
 97  65
         }
 98  
 
 99  
 }