Coverage Report - ca.uhn.hl7v2.hoh.raw.server.HohRawServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
HohRawServlet
52%
30/57
100%
2/2
2.8
 
 1  
 package ca.uhn.hl7v2.hoh.raw.server;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.nio.charset.Charset;
 5  
 import java.util.Enumeration;
 6  
 import java.util.LinkedHashMap;
 7  
 
 8  
 import javax.servlet.ServletException;
 9  
 import javax.servlet.http.HttpServlet;
 10  
 import javax.servlet.http.HttpServletRequest;
 11  
 import javax.servlet.http.HttpServletResponse;
 12  
 
 13  
 import ca.uhn.hl7v2.hoh.api.DecodeException;
 14  
 import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
 15  
 import ca.uhn.hl7v2.hoh.api.IMessageHandler;
 16  
 import ca.uhn.hl7v2.hoh.api.IResponseSendable;
 17  
 import ca.uhn.hl7v2.hoh.api.MessageMetadataKeys;
 18  
 import ca.uhn.hl7v2.hoh.api.MessageProcessingException;
 19  
 import ca.uhn.hl7v2.hoh.encoder.AuthorizationFailureException;
 20  
 import ca.uhn.hl7v2.hoh.encoder.Hl7OverHttpRequestDecoder;
 21  
 import ca.uhn.hl7v2.hoh.raw.api.RawReceivable;
 22  
 import ca.uhn.hl7v2.hoh.sign.ISigner;
 23  
 import ca.uhn.hl7v2.hoh.sign.SignatureVerificationException;
 24  
 import ca.uhn.hl7v2.hoh.util.HTTPUtils;
 25  
 
 26  50
 public class HohRawServlet extends HttpServlet {
 27  
 
 28  5
         private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(HohRawServlet.class);
 29  
         private static final long serialVersionUID = 1L;
 30  
         private IAuthorizationServerCallback myAuthorizationCallback;
 31  
         private IMessageHandler<String> myMessageHandler;
 32  
         private ISigner mySigner;
 33  
 
 34  
         /**
 35  
          * {@inheritDoc}
 36  
          */
 37  
         @Override
 38  
         protected void doGet(HttpServletRequest theReq, HttpServletResponse theResp) throws ServletException, IOException {
 39  
 
 40  0
                 theResp.setStatus(400);
 41  0
                 theResp.setContentType("text/html");
 42  
 
 43  0
                 String message = "GET method is not supported by this server";
 44  0
                 HTTPUtils.write400BadRequest(theResp.getOutputStream(), message, false);
 45  
 
 46  0
         }
 47  
 
 48  
         /**
 49  
          * {@inheritDoc}
 50  
          */
 51  
         @Override
 52  
         protected void doPost(HttpServletRequest theReq, HttpServletResponse theResp) throws ServletException, IOException {
 53  
 
 54  50
                 Hl7OverHttpRequestDecoder decoder = new Hl7OverHttpRequestDecoder();
 55  50
                 decoder.setHeaders(new LinkedHashMap<String, String>());
 56  
 
 57  50
                 Enumeration<?> headerNames = theReq.getHeaderNames();
 58  450
                 while (headerNames.hasMoreElements()) {
 59  400
                         String nextName = (String) headerNames.nextElement();
 60  400
                         decoder.getHeaders().put(nextName, theReq.getHeader(nextName));
 61  400
                 }
 62  
 
 63  50
                 decoder.setPath(theReq.getRequestURI());
 64  50
                 decoder.setAuthorizationCallback(myAuthorizationCallback);
 65  50
                 decoder.setSigner(mySigner);
 66  
 
 67  
                 try {
 68  50
                         decoder.readContentsFromInputStreamAndDecode(theReq.getInputStream());
 69  0
                 } catch (AuthorizationFailureException e) {
 70  0
                         ourLog.error("Authorization failed on request for {}", theReq.getRequestURI());
 71  0
                         theResp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
 72  0
                         HTTPUtils.write401Unauthorized(theResp.getOutputStream(), false);
 73  0
                         return;
 74  0
                 } catch (DecodeException e) {
 75  0
                         ourLog.error("Request failure for " + theReq.getRequestURI(), e);
 76  0
                         theResp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
 77  0
                         HTTPUtils.write400BadRequest(theResp.getOutputStream(), e.getMessage(), false);
 78  0
                         return;
 79  0
                 } catch (SignatureVerificationException e) {
 80  0
                         ourLog.error("Signature verification failed on request for {}", theReq.getRequestURI());
 81  0
                         theResp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
 82  0
                         HTTPUtils.write400SignatureVerificationFailed(theResp.getOutputStream(), false);
 83  0
                         return;
 84  50
                 }
 85  
 
 86  50
                 Charset charset = decoder.getCharset();
 87  50
                 ourLog.debug("Message charset is {}", charset.displayName());
 88  
 
 89  50
                 RawReceivable rawMessage = new RawReceivable(decoder.getMessage());
 90  50
                 rawMessage.addMetadata(MessageMetadataKeys.REMOTE_HOST_ADDRESS.name(), theReq.getRemoteAddr());
 91  
 
 92  
                 IResponseSendable<String> response;
 93  
                 try {
 94  50
                         response = myMessageHandler.messageReceived(rawMessage);
 95  0
                 } catch (MessageProcessingException e) {
 96  0
                         ourLog.error("Processing problem for " + theReq.getRequestURI(), e);
 97  0
                         theResp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
 98  0
                         HTTPUtils.write500InternalServerError(theResp.getOutputStream(), e.getMessage(), false);
 99  0
                         return;
 100  50
                 }
 101  
 
 102  50
                 theResp.setCharacterEncoding(charset.name());
 103  50
                 theResp.setContentType(response.getEncodingStyle().getContentType());
 104  50
                 theResp.setStatus(response.getResponseCode().getCode());
 105  
 
 106  
                 // n.b. don't ask for the writer until headers are set
 107  50
                 response.writeMessage(theResp.getWriter());
 108  50
                 theResp.flushBuffer();
 109  
 
 110  50
         }
 111  
 
 112  
         /**
 113  
          * If set, provides a callback which will be used to validate incoming
 114  
          * credentials
 115  
          */
 116  
         public void setAuthorizationCallback(IAuthorizationServerCallback theAuthorizationCallback) {
 117  30
                 myAuthorizationCallback = theAuthorizationCallback;
 118  30
         }
 119  
 
 120  
         /**
 121  
          * @param theMessageHandler
 122  
          *            the messageHandler to set
 123  
          */
 124  
         public void setMessageHandler(IMessageHandler<String> theMessageHandler) {
 125  50
                 myMessageHandler = theMessageHandler;
 126  50
         }
 127  
 
 128  
         /**
 129  
          * Sets the message signer if signature profile is being used
 130  
          */
 131  
         public void setSigner(ISigner theSigner) {
 132  0
                 mySigner = theSigner;
 133  0
         }
 134  
 
 135  
 }