Coverage Report - ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpResponseDecoder
 
Classes in this File Line Coverage Branch Coverage Complexity
Hl7OverHttpResponseDecoder
76%
20/26
50%
5/10
4.333
 
 1  
 package ca.uhn.hl7v2.hoh.encoder;
 2  
 
 3  
 import static ca.uhn.hl7v2.hoh.util.StringUtils.*;
 4  
 
 5  
 import java.io.IOException;
 6  
 import java.io.InputStream;
 7  
 
 8  
 import ca.uhn.hl7v2.hoh.api.DecodeException;
 9  
 
 10  180
 public class Hl7OverHttpResponseDecoder extends AbstractHl7OverHttpDecoder {
 11  
 
 12  5
         private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Hl7OverHttpResponseDecoder.class);
 13  
         private String myActionLine;
 14  
 
 15  
         @Override
 16  
         protected String readActionLineAndDecode(InputStream theInputStream) throws IOException, NoMessageReceivedException, DecodeException {
 17  180
                 ourLog.trace("Entering readActionLineAndDecode(InputStream)");
 18  
 
 19  180
                 if (myActionLine == null) {
 20  180
                         String firstLine = readFirstLine(theInputStream);
 21  145
                         if (firstLine == null || isBlank(firstLine)) {
 22  0
                                 throw new NoMessageReceivedException();
 23  
                         }
 24  
 
 25  145
                         if (!firstLine.startsWith("HTTP/1.1 ")) {
 26  0
                                 throw new DecodeException("HTTP response begins with unknown HTTP version. Line is: " + firstLine);
 27  
                         }
 28  
 
 29  145
                         String statusPart = firstLine.substring(9);
 30  145
                         int spaceIdx = statusPart.indexOf(' ');
 31  145
                         if (spaceIdx == -1) {
 32  0
                                 throw new DecodeException("Invalid response line, no space after status code. Line is: " + firstLine);
 33  
                         }
 34  
 
 35  145
                         String statusCode = statusPart.substring(0, spaceIdx);
 36  
                         try {
 37  145
                                 setResponseStatus(Integer.parseInt(statusCode));
 38  0
                         } catch (NumberFormatException e) {
 39  0
                                 throw new DecodeException("Invalid response line. Bad status code: " + statusCode);
 40  145
                         }
 41  
 
 42  145
                         setResponseName(statusPart.substring(spaceIdx).trim());
 43  145
                         myActionLine = firstLine;
 44  145
                         ourLog.trace("Action line is {}", myActionLine);
 45  
 
 46  145
                 } else {
 47  0
                         ourLog.trace("Already have an action line");
 48  
                 }
 49  
                 
 50  145
                 return myActionLine;
 51  
         }
 52  
 
 53  
         @Override
 54  
         protected void authorize() throws AuthorizationFailureException {
 55  
                 // responses do not need authorization
 56  145
         }
 57  
 
 58  
         /**
 59  
          * Returns true if the connection=close header was present in the response
 60  
          */
 61  
         @Override
 62  
         public boolean isConnectionCloseHeaderPresent() {
 63  110
                 return super.isConnectionCloseHeaderPresent();
 64  
         }
 65  
 
 66  
 }