View Javadoc
1   package ca.uhn.hl7v2.hoh.hapi.server;
2   
3   import java.util.HashMap;
4   
5   import ca.uhn.hl7v2.HL7Exception;
6   import ca.uhn.hl7v2.hoh.api.IMessageHandler;
7   import ca.uhn.hl7v2.hoh.api.IReceivable;
8   import ca.uhn.hl7v2.hoh.api.IResponseSendable;
9   import ca.uhn.hl7v2.hoh.api.MessageProcessingException;
10  import ca.uhn.hl7v2.hoh.raw.api.RawSendable;
11  import ca.uhn.hl7v2.hoh.raw.server.HohRawServlet;
12  import ca.uhn.hl7v2.model.Message;
13  import ca.uhn.hl7v2.protocol.ApplicationRouter;
14  import ca.uhn.hl7v2.protocol.ReceivingApplication;
15  import ca.uhn.hl7v2.protocol.Transportable;
16  import ca.uhn.hl7v2.protocol.impl.AppRoutingDataImpl;
17  import ca.uhn.hl7v2.protocol.impl.ApplicationRouterImpl;
18  import ca.uhn.hl7v2.protocol.impl.TransportableImpl;
19  
20  public class HohServlet extends HohRawServlet {
21  
22  	private static final long serialVersionUID = 1L;
23  	private ApplicationRouter myApplicationRouter;
24  
25  	/**
26  	 * Constructor
27  	 */
28  	public HohServlet() {
29  		super.setMessageHandler(new MessageHandlerImpl());
30  	}
31  
32  	/**
33  	 * <p>
34  	 * Route all messages to a single application
35  	 * </p>
36  	 * <p>
37  	 * This method should not be called if {@link #setApplicationRouter(ApplicationRouter)} has been called
38  	 * </p>
39  	 */
40  	public void setApplication(ReceivingApplication<? extends Message> theApplication) {
41  		myApplicationRouter = new ApplicationRouterImpl();
42  		myApplicationRouter.bindApplication(new AppRoutingDataImpl("*", "*", "*", "*"), theApplication);
43  	}
44  
45  	/**
46  	 * <p>
47  	 * Constructor which accepts an ApplicationRouter which may direct different
48  	 * types of messages to different applications
49  	 * </p>
50  	 * <p>
51  	 * Does not need to be provided if {@link #setApplication(ReceivingApplication)} has been called.
52  	 * </p>
53  	 */
54  	public void setApplicationRouter(ApplicationRouter theApplicationRouter) {
55  		myApplicationRouter = theApplicationRouter;
56  	}
57  
58  	/**
59  	 * Must not be called
60  	 */
61  	@Override
62  	public void setMessageHandler(IMessageHandler<String> theMessageHandler) {
63  		throw new UnsupportedOperationException();
64  	}
65  
66  	private class MessageHandlerImpl implements IMessageHandler<String> {
67  
68  		public IResponseSendable<String> messageReceived(IReceivable<String> theMessage) throws MessageProcessingException {
69  
70  			Transportable received = new TransportableImpl(theMessage.getMessage(), new HashMap<>(theMessage.getMetadata()));
71  			Transportable response;
72  			try {
73  				response = myApplicationRouter.processMessage(received);
74  			} catch (HL7Exception e) {
75  				throw new MessageProcessingException(e);
76  			}
77  
78  			return new RawSendable(response.getMessage());
79  		}
80  
81  	}
82  
83  }