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
27
28 public HohServlet() {
29 super.setMessageHandler(new MessageHandlerImpl());
30 }
31
32
33
34
35
36
37
38
39
40 public void setApplication(ReceivingApplication<? extends Message> theApplication) {
41 myApplicationRouter = new ApplicationRouterImpl();
42 myApplicationRouter.bindApplication(new AppRoutingDataImpl("*", "*", "*", "*"), theApplication);
43 }
44
45
46
47
48
49
50
51
52
53
54 public void setApplicationRouter(ApplicationRouter theApplicationRouter) {
55 myApplicationRouter = theApplicationRouter;
56 }
57
58
59
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 }