View Javadoc

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  public class HohRawServlet extends HttpServlet {
27  
28  	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  		theResp.setStatus(400);
41  		theResp.setContentType("text/html");
42  
43  		String message = "GET method is not supported by this server";
44  		HTTPUtils.write400BadRequest(theResp.getOutputStream(), message, false);
45  
46  	}
47  
48  	/**
49  	 * {@inheritDoc}
50  	 */
51  	@Override
52  	protected void doPost(HttpServletRequest theReq, HttpServletResponse theResp) throws ServletException, IOException {
53  
54  		Hl7OverHttpRequestDecoder decoder = new Hl7OverHttpRequestDecoder();
55  		decoder.setHeaders(new LinkedHashMap<String, String>());
56  
57  		Enumeration<?> headerNames = theReq.getHeaderNames();
58  		while (headerNames.hasMoreElements()) {
59  			String nextName = (String) headerNames.nextElement();
60  			decoder.getHeaders().put(nextName, theReq.getHeader(nextName));
61  		}
62  
63  		decoder.setPath(theReq.getRequestURI());
64  		decoder.setAuthorizationCallback(myAuthorizationCallback);
65  		decoder.setSigner(mySigner);
66  
67  		try {
68  			decoder.readContentsFromInputStreamAndDecode(theReq.getInputStream());
69  		} catch (AuthorizationFailureException e) {
70  			ourLog.error("Authorization failed on request for {}", theReq.getRequestURI());
71  			theResp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
72  			HTTPUtils.write401Unauthorized(theResp.getOutputStream(), false);
73  			return;
74  		} catch (DecodeException e) {
75  			ourLog.error("Request failure for " + theReq.getRequestURI(), e);
76  			theResp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
77  			HTTPUtils.write400BadRequest(theResp.getOutputStream(), e.getMessage(), false);
78  			return;
79  		} catch (SignatureVerificationException e) {
80  			ourLog.error("Signature verification failed on request for {}", theReq.getRequestURI());
81  			theResp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
82  			HTTPUtils.write400SignatureVerificationFailed(theResp.getOutputStream(), false);
83  			return;
84  		}
85  
86  		Charset charset = decoder.getCharset();
87  		ourLog.debug("Message charset is {}", charset.displayName());
88  
89  		RawReceivable rawMessage = new RawReceivable(decoder.getMessage());
90  		rawMessage.addMetadata(MessageMetadataKeys.REMOTE_HOST_ADDRESS.name(), theReq.getRemoteAddr());
91  
92  		IResponseSendable<String> response;
93  		try {
94  			response = myMessageHandler.messageReceived(rawMessage);
95  		} catch (MessageProcessingException e) {
96  			ourLog.error("Processing problem for " + theReq.getRequestURI(), e);
97  			theResp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
98  			HTTPUtils.write500InternalServerError(theResp.getOutputStream(), e.getMessage(), false);
99  			return;
100 		}
101 
102 		theResp.setCharacterEncoding(charset.name());
103 		theResp.setContentType(response.getEncodingStyle().getContentType());
104 		theResp.setStatus(response.getResponseCode().getCode());
105 
106 		// n.b. don't ask for the writer until headers are set
107 		response.writeMessage(theResp.getWriter());
108 		theResp.flushBuffer();
109 
110 	}
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 		myAuthorizationCallback = theAuthorizationCallback;
118 	}
119 
120 	/**
121 	 * @param theMessageHandler
122 	 *            the messageHandler to set
123 	 */
124 	public void setMessageHandler(IMessageHandler<String> theMessageHandler) {
125 		myMessageHandler = theMessageHandler;
126 	}
127 
128 	/**
129 	 * Sets the message signer if signature profile is being used
130 	 */
131 	public void setSigner(ISigner theSigner) {
132 		mySigner = theSigner;
133 	}
134 
135 }