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