View Javadoc

1   package ca.uhn.hl7v2.examples.hoh;
2   
3   import java.io.IOException;
4   import java.util.Map;
5   
6   import javax.servlet.ServletConfig;
7   import javax.servlet.ServletException;
8   
9   import ca.uhn.hl7v2.AcknowledgmentCode;
10  import ca.uhn.hl7v2.HL7Exception;
11  import ca.uhn.hl7v2.hoh.hapi.server.HohServlet;
12  import ca.uhn.hl7v2.model.Message;
13  import ca.uhn.hl7v2.protocol.ReceivingApplication;
14  import ca.uhn.hl7v2.protocol.ReceivingApplicationException;
15  
16  /**
17   * Example servlet implementation which receives HL7 messages
18   * and uses HAPI to process them.
19   */
20  public class ExampleHl7OverHttpServletWithOneApplication extends HohServlet {
21  
22  	/**
23  	 * Initialise the servlet
24  	 */
25  	@Override
26  	public void init(ServletConfig theConfig) throws ServletException {
27  		
28  		/* Servlet should be initialized with an instance of
29  		 * ReceivingApplication, which handles incoming messages 
30  		 */
31  		setApplication(new MyApplication());
32  		
33  	}
34  
35  	/**
36  	 * The application does the actual processing
37  	 */
38  	private class MyApplication implements ReceivingApplication
39  	{
40  
41  		/**
42  		 * processMessage is fired each time a new message 
43  		 * arrives. 
44  		 * 
45  		 * @param theMessage The message which was received
46  		 * @param theMetadata A map containing additional information about
47  		 *                    the message, where it came from, etc.  
48  		 */
49  		public Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws ReceivingApplicationException, HL7Exception {
50  			System.out.println("Received message:\n" + theMessage.encode());
51  
52  			// .. process the message ..
53  			
54  			/*
55  			 * Now reply to the message
56  			 */
57  			Message response;
58  			try {
59  				response = theMessage.generateACK();
60  			} catch (IOException e) {
61  				throw new ReceivingApplicationException(e);
62  			}
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  			if (somethingFailed) {
72  				throw new ReceivingApplicationException("");
73  			}
74  
75  			/*
76  			 * It is better to return an HL7 message with an AE response
77  			 * code. This will still be returned by the transport with
78  			 * an HTTP 500 status code, but an HL7 message will still 
79  			 * be propagated up. 
80  			 */
81  			if (somethingFailed) {
82  				try {
83  					response = theMessage.generateACK(AcknowledgmentCode.AE, 
84  							new HL7Exception("There was a problem!!"));
85  				} catch (IOException e) {
86  					throw new ReceivingApplicationException(e);
87  				}
88  			}
89  			
90  			return response;
91  		}
92  
93  		/**
94  		 * {@inheritDoc}
95  		 */
96  		public boolean canProcess(Message theMessage) {
97  			return true;
98  		}
99  		
100 	}
101 	
102 }