View Javadoc

1   package ca.uhn.hl7v2.hoh.relay.sender;
2   
3   import java.io.IOException;
4   import java.util.Map;
5   
6   import org.springframework.beans.factory.BeanNameAware;
7   import org.springframework.beans.factory.InitializingBean;
8   
9   import ca.uhn.hl7v2.HL7Exception;
10  import ca.uhn.hl7v2.hoh.api.DecodeException;
11  import ca.uhn.hl7v2.hoh.api.EncodeException;
12  import ca.uhn.hl7v2.hoh.api.IReceivable;
13  import ca.uhn.hl7v2.hoh.hapi.api.MessageSendable;
14  import ca.uhn.hl7v2.hoh.hapi.client.HohClientMultithreaded;
15  import ca.uhn.hl7v2.hoh.util.Validate;
16  import ca.uhn.hl7v2.model.Message;
17  import ca.uhn.hl7v2.protocol.ApplicationRouter;
18  import ca.uhn.hl7v2.protocol.ReceivingApplicationException;
19  import ca.uhn.hl7v2.util.Terser;
20  
21  public class RelayHttpSender extends HohClientMultithreaded implements IRelaySender, BeanNameAware, InitializingBean {
22  
23  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RelayHttpSender.class);
24  	private String myBeanName;
25  	
26  	/**
27  	 * {@inheritDoc}
28  	 */
29  	public void afterPropertiesSet() throws Exception {
30  		Validate.propertySet(getUrl(), "Url");
31  		ourLog.info("Sender [{}] will transmit by HL7 over HTTP to {}", myBeanName, getUrl().toExternalForm());
32  	}
33  
34  	/**
35  	 * Returns true
36  	 * 
37  	 * {@inheritDoc}
38  	 */
39  	public boolean canProcess(Message theMessage) {
40  		return true;
41  	}
42  
43  	/**
44  	 * {@inheritDoc}
45  	 */
46  	public String getBeanName() {
47  		return myBeanName;
48  	}
49  
50  	/**
51  	 * {@inheritDoc}
52  	 */
53  	public Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws ReceivingApplicationException, HL7Exception {
54  		String sendingIp = (String) theMetadata.get(ApplicationRouter.METADATA_KEY_SENDING_IP);
55  		String controlId = (String) theMetadata.get(ApplicationRouter.METADATA_KEY_MESSAGE_CONTROL_ID);
56  
57  		ourLog.info("Relaying message with ID {} from {} to URL {}", new Object[] {controlId, sendingIp, getUrl()});
58  		
59  		IReceivable<Message> response;
60  		long delay = System.currentTimeMillis();
61  		try {
62  			response = sendAndReceiveMessage(new MessageSendable(theMessage));
63  			delay = System.currentTimeMillis() - delay;
64  		} catch (DecodeException e) {
65  			throw new HL7Exception(e);
66  		} catch (IOException e) {
67  			throw new HL7Exception(e);
68  		} catch (EncodeException e) {
69  			throw new HL7Exception(e);
70  		}
71  		
72  		String responseControlId = new Terser(response.getMessage()).get("/MSH-10");
73  		ourLog.info("Received response to ID {} with ID {} in {} ms", new Object[] {controlId, responseControlId, delay});
74  		
75  		return response.getMessage();
76  	}
77  
78  	/**
79  	 * <i>Automatically called by the container</i>
80  	 */
81  	public void setBeanName(String theBeanName) {
82  		myBeanName = theBeanName;
83  	}
84  
85  
86  }