Coverage Report - ca.uhn.hl7v2.hoh.hapi.server.HohServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
HohServlet
70%
7/10
N/A
1.6
HohServlet$1
N/A
N/A
1.6
HohServlet$MessageHandlerImpl
71%
5/7
N/A
1.6
 
 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  20
 public class HohServlet extends HohRawServlet {
 21  
 
 22  
         private static final long serialVersionUID = 1L;
 23  
         private ApplicationRouter myApplicationRouter;
 24  
 
 25  
         /**
 26  
          * Constructor
 27  
          */
 28  20
         public HohServlet() {
 29  20
                 super.setMessageHandler(new MessageHandlerImpl());
 30  20
         }
 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  20
                 myApplicationRouter = new ApplicationRouterImpl();
 42  20
                 myApplicationRouter.bindApplication(new AppRoutingDataImpl("*", "*", "*", "*"), theApplication);
 43  20
         }
 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  0
                 myApplicationRouter = theApplicationRouter;
 56  0
         }
 57  
 
 58  
         /**
 59  
          * Must not be called
 60  
          */
 61  
         @Override
 62  
         public void setMessageHandler(IMessageHandler<String> theMessageHandler) {
 63  0
                 throw new UnsupportedOperationException();
 64  
         }
 65  
 
 66  40
         private class MessageHandlerImpl implements IMessageHandler<String> {
 67  
 
 68  
                 public IResponseSendable<String> messageReceived(IReceivable<String> theMessage) throws MessageProcessingException {
 69  
 
 70  20
                         Transportable received = new TransportableImpl(theMessage.getMessage(), new HashMap<String, Object>(theMessage.getMetadata()));
 71  
                         Transportable response;
 72  
                         try {
 73  20
                                 response = myApplicationRouter.processMessage(received);
 74  0
                         } catch (HL7Exception e) {
 75  0
                                 throw new MessageProcessingException(e);
 76  20
                         }
 77  
 
 78  20
                         return new RawSendable(response.getMessage());
 79  
                 }
 80  
 
 81  
         }
 82  
 
 83  
 }