001package ca.uhn.hl7v2.hoh.hapi.server;
002
003import java.util.HashMap;
004
005import ca.uhn.hl7v2.HL7Exception;
006import ca.uhn.hl7v2.hoh.api.IMessageHandler;
007import ca.uhn.hl7v2.hoh.api.IReceivable;
008import ca.uhn.hl7v2.hoh.api.IResponseSendable;
009import ca.uhn.hl7v2.hoh.api.MessageProcessingException;
010import ca.uhn.hl7v2.hoh.raw.api.RawSendable;
011import ca.uhn.hl7v2.hoh.raw.server.HohRawServlet;
012import ca.uhn.hl7v2.model.Message;
013import ca.uhn.hl7v2.protocol.ApplicationRouter;
014import ca.uhn.hl7v2.protocol.ReceivingApplication;
015import ca.uhn.hl7v2.protocol.Transportable;
016import ca.uhn.hl7v2.protocol.impl.AppRoutingDataImpl;
017import ca.uhn.hl7v2.protocol.impl.ApplicationRouterImpl;
018import ca.uhn.hl7v2.protocol.impl.TransportableImpl;
019
020public class HohServlet extends HohRawServlet {
021
022        private static final long serialVersionUID = 1L;
023        private ApplicationRouter myApplicationRouter;
024
025        /**
026         * Constructor
027         */
028        public HohServlet() {
029                super.setMessageHandler(new MessageHandlerImpl());
030        }
031
032        /**
033         * <p>
034         * Route all messages to a single application
035         * </p>
036         * <p>
037         * This method should not be called if {@link #setApplicationRouter(ApplicationRouter)} has been called
038         * </p>
039         */
040        public void setApplication(ReceivingApplication<? extends Message> theApplication) {
041                myApplicationRouter = new ApplicationRouterImpl();
042                myApplicationRouter.bindApplication(new AppRoutingDataImpl("*", "*", "*", "*"), theApplication);
043        }
044
045        /**
046         * <p>
047         * Constructor which accepts an ApplicationRouter which may direct different
048         * types of messages to different applications
049         * </p>
050         * <p>
051         * Does not need to be provided if {@link #setApplication(ReceivingApplication)} has been called.
052         * </p>
053         */
054        public void setApplicationRouter(ApplicationRouter theApplicationRouter) {
055                myApplicationRouter = theApplicationRouter;
056        }
057
058        /**
059         * Must not be called
060         */
061        @Override
062        public void setMessageHandler(IMessageHandler<String> theMessageHandler) {
063                throw new UnsupportedOperationException();
064        }
065
066        private class MessageHandlerImpl implements IMessageHandler<String> {
067
068                public IResponseSendable<String> messageReceived(IReceivable<String> theMessage) throws MessageProcessingException {
069
070                        Transportable received = new TransportableImpl(theMessage.getMessage(), new HashMap<String, Object>(theMessage.getMetadata()));
071                        Transportable response;
072                        try {
073                                response = myApplicationRouter.processMessage(received);
074                        } catch (HL7Exception e) {
075                                throw new MessageProcessingException(e);
076                        }
077
078                        return new RawSendable(response.getMessage());
079                }
080
081        }
082
083}