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