View Javadoc
1   package ca.uhn.hl7v2.examples.hoh;
2   
3   import java.io.IOException;
4   import java.util.Map;
5   
6   import ca.uhn.hl7v2.HL7Exception;
7   import ca.uhn.hl7v2.hoh.hapi.server.HohServlet;
8   import ca.uhn.hl7v2.model.Message;
9   import ca.uhn.hl7v2.protocol.ReceivingApplication;
10  import ca.uhn.hl7v2.protocol.ReceivingApplicationException;
11  import jakarta.servlet.ServletConfig;
12  
13  /**
14   * Example servlet implementation which receives HL7 messages
15   * and uses HAPI to process them.
16   */
17  public class ExampleHl7OverHttpServletWithOneApplication extends HohServlet {
18  
19  	/**
20  	 * Initialise the servlet
21  	 */
22  	@Override
23  	public void init(ServletConfig theConfig) {
24  		
25  		/* Servlet should be initialized with an instance of
26  		 * ReceivingApplication, which handles incoming messages 
27  		 */
28  		setApplication(new MyApplication());
29  		
30  	}
31  
32  	/**
33  	 * The application does the actual processing
34  	 */
35  	private static class MyApplication implements ReceivingApplication<Message>
36  	{
37  
38  		/**
39  		 * processMessage is fired each time a new message 
40  		 * arrives. 
41  		 * 
42  		 * @param theMessage The message which was received
43  		 * @param theMetadata A map containing additional information about
44  		 *                    the message, where it came from, etc.  
45  		 */
46  		@Override
47  		public Message../../../ca/uhn/hl7v2/model/Message.html#Message">Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws ReceivingApplicationException, HL7Exception {
48  			System.out.println("Received message:\n" + theMessage.encode());
49  
50  			// .. process the message ..
51  			
52  			/*
53  			 * Now reply to the message
54  			 */
55  			Message response;
56  			try {
57  				response = theMessage.generateACK();
58  			} catch (IOException e) {
59  				throw new ReceivingApplicationException(e);
60  			}
61  			
62  			/*
63  			 * If something goes horribly wrong, you can throw an 
64  			 * exception and an HTTP 500 error will be generated.
65  			 * However, it is preferable to return a normal HL7 ACK 
66  			 * message with an "AE" response code to note an error. 
67  			 */
68  			boolean somethingFailed = false;
69  
70  			return response;
71  		}
72  
73  		/**
74  		 * {@inheritDoc}
75  		 */
76  		@Override
77  		public boolean canProcess(Message theMessage) {
78  			return true;
79  		}
80  		
81  	}
82  	
83  }