View Javadoc
1   package ca.uhn.hl7v2.examples.hoh;
2   
3   import ca.uhn.hl7v2.hoh.api.IAuthorizationServerCallback;
4   import ca.uhn.hl7v2.hoh.api.IMessageHandler;
5   import ca.uhn.hl7v2.hoh.api.IReceivable;
6   import ca.uhn.hl7v2.hoh.api.IResponseSendable;
7   import ca.uhn.hl7v2.hoh.api.MessageProcessingException;
8   import ca.uhn.hl7v2.hoh.auth.SingleCredentialServerCallback;
9   import ca.uhn.hl7v2.hoh.raw.api.RawSendable;
10  import ca.uhn.hl7v2.hoh.raw.server.HohRawServlet;
11  
12  /**
13   * This class illustrates an example HL7 over HTTP
14   * raw message servlet for receiving and responding to messages
15   */
16  public class ExampleRawHl7OverHttpServlet extends HohRawServlet {
17  
18  	/**
19  	 * Constructor
20  	 */
21  	public ExampleRawHl7OverHttpServlet() {
22  		
23  		/*
24  		 * The servlet must be provided an implementation
25  		 * of IMessageHandler<String>, such as the one which
26  		 * is nested below
27  		 */
28  		setMessageHandler(new ExampleMessageHandler());
29  		
30  		/* 
31  		 * Optionally, if we want to verify HTTP authentication,
32  		 * we can specify an authorization callback
33  		 */
34  		IAuthorizationServerCallback callback = 
35  			  new SingleCredentialServerCallback("someusername", "apassword");
36  		setAuthorizationCallback(callback);
37  		
38  	}
39  
40  	/**
41  	 * IMessageHandler defines the interface for the class which receives
42  	 * and processes messages which come in
43  	 */
44  	private static class ExampleMessageHandler implements IMessageHandler<String> {
45  
46  		/**
47  		 * This method is fired every time a message is received. The return value
48  		 * contains the HL7 response value 
49  		 */
50  		public IResponseSendable<String> messageReceived(IReceivable<String> theReceived) 
51  				   throws MessageProcessingException {
52  			
53  			String incomingRawMsg = theReceived.getMessage();
54  			System.out.println("Received message:\n" + incomingRawMsg);
55  			
56  			// ... do some processing ...
57  			
58  			/*
59  			 * Your application should generate an appropriate 
60  			 * HL7 ACK message here
61  			 */
62  			String ack = "MSH|....."; 
63  			
64  			/*
65  			 * If something goes horribly wrong, you can throw an 
66  			 * exception and an HTTP 500 error will be generated.
67  			 * However, it is preferable to return a normal HL7 ACK 
68  			 * message with an "AE" response code to note an error. 
69  			 */
70  			boolean somethingFailed = false;
71  
72              // Return the raw response message
73  			return new RawSendable(ack);
74  		}
75  		
76  	}
77  	
78  }