Coverage Report - ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpRequestDecoder
 
Classes in this File Line Coverage Branch Coverage Complexity
Hl7OverHttpRequestDecoder
75%
27/36
54%
13/24
7.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  
 import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
 10  
 import ca.uhn.hl7v2.hoh.util.StringUtils;
 11  
 
 12  330
 public class Hl7OverHttpRequestDecoder extends AbstractHl7OverHttpDecoder {
 13  
 
 14  
         private IAuthorizationServerCallback myAuthorizationCallback;
 15  
         private String myActionLine;
 16  5
         private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Hl7OverHttpRequestDecoder.class);
 17  
 
 18  
         protected void authorize() throws AuthorizationFailureException {
 19  270
                 if (myAuthorizationCallback != null) {
 20  155
                         String username = getUsername();
 21  155
                         if (!myAuthorizationCallback.authorize(getPath(), username, getPassword())) {
 22  0
                                 if (StringUtils.isBlank(username)) {
 23  0
                                         throw new AuthorizationFailureException("Authorization failed: No username provided");
 24  
                                 } else {
 25  0
                                         throw new AuthorizationFailureException("Authorization failed for user: " + getUsername());
 26  
                                 }
 27  
                         }
 28  155
                 } else {
 29  115
                         if (isNotBlank(getUsername()) || isNotBlank(getPassword())) {
 30  95
                                 ourLog.warn("Request contains username and/or password, but no authorization callback has been set so credentials can not be validated");
 31  
                         }
 32  
                 }
 33  270
         }
 34  
 
 35  
         @Override
 36  
         protected String readActionLineAndDecode(InputStream theInputStream) throws DecodeException, IOException, NoMessageReceivedException {
 37  280
                 ourLog.trace("Entering readActionLineAndDecode(InputStream)");
 38  
                 
 39  280
                 if (myActionLine == null) {
 40  280
                         String firstLine = readFirstLine(theInputStream);
 41  220
                         if (firstLine == null || isBlank(firstLine)) {
 42  0
                                 throw new NoMessageReceivedException();
 43  
                         }
 44  
 
 45  220
                         if (!firstLine.startsWith("POST ")) {
 46  0
                                 throw new DecodeException("HTTP request line message is not valid. Only POST action is supported. Request line was: " + firstLine);
 47  
                         }
 48  
 
 49  220
                         firstLine = firstLine.substring(5);
 50  220
                         int nextSpace = firstLine.indexOf(' ');
 51  220
                         if (nextSpace == -1) {
 52  0
                                 throw new DecodeException("HTTP request line message is not valid. Not HTTP version found. Request line was: " + firstLine);
 53  
                         }
 54  
 
 55  220
                         setPath(firstLine.substring(0, nextSpace));
 56  220
                         if (isBlank(getPath())) {
 57  0
                                 throw new DecodeException("HTTP request line message is not valid. No request URI found. Request line was: " + firstLine);
 58  
                         }
 59  
 
 60  220
                         String protocolVersion = firstLine.substring(nextSpace + 1);
 61  220
                         if (!"HTTP/1.1".equals(protocolVersion)) {
 62  0
                                 throw new DecodeException("HTTP request line message is not valid. HTTP version not supported. Request line was: " + firstLine);
 63  
                         }
 64  
 
 65  220
                         myActionLine = firstLine;
 66  220
                         ourLog.trace("Action line is {}", myActionLine);
 67  
 
 68  220
                 } else {
 69  0
                         ourLog.trace("Already have an action line");
 70  
                 }
 71  
                 
 72  220
                 return myActionLine;
 73  
         }
 74  
 
 75  
         /**
 76  
          * @param theAuthorizationCallback
 77  
          *            the authorizationCallback to set
 78  
          */
 79  
         public void setAuthorizationCallback(IAuthorizationServerCallback theAuthorizationCallback) {
 80  270
                 myAuthorizationCallback = theAuthorizationCallback;
 81  270
         }
 82  
 
 83  
 }