View Javadoc
1   package ca.uhn.hl7v2.hoh.relay.sender;
2   
3   import java.io.IOException;
4   import java.net.ConnectException;
5   import java.util.Map;
6   
7   import org.springframework.beans.factory.BeanNameAware;
8   import org.springframework.beans.factory.InitializingBean;
9   
10  import ca.uhn.hl7v2.HL7Exception;
11  import ca.uhn.hl7v2.hoh.api.DecodeException;
12  import ca.uhn.hl7v2.hoh.api.EncodeException;
13  import ca.uhn.hl7v2.hoh.api.IReceivable;
14  import ca.uhn.hl7v2.hoh.hapi.api.MessageSendable;
15  import ca.uhn.hl7v2.hoh.hapi.client.HohClientMultithreaded;
16  import ca.uhn.hl7v2.hoh.relay.Binder;
17  import ca.uhn.hl7v2.hoh.util.Validate;
18  import ca.uhn.hl7v2.model.Message;
19  import ca.uhn.hl7v2.protocol.ApplicationRouter;
20  import ca.uhn.hl7v2.protocol.MetadataKeys;
21  import ca.uhn.hl7v2.util.Terser;
22  
23  public class RelayHttpSender extends HohClientMultithreaded implements IRelaySender<Message>, BeanNameAware, InitializingBean {
24  
25  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RelayHttpSender.class);
26  	private String myBeanName;
27  	private int myIoRetries = 0;
28  	
29  	/**
30  	 * {@inheritDoc}
31  	 */
32  	public void afterPropertiesSet() {
33  		Validate.propertySet(getUrl(), "Url");
34  		ourLog.info("Sender [{}] will transmit by HL7 over HTTP to {}", myBeanName, getUrl().toExternalForm());
35  	}
36  
37  	/**
38  	 * Returns true
39  	 * 
40  	 * {@inheritDoc}
41  	 */
42  	public boolean canProcess(Message theMessage) {
43  		return true;
44  	}
45  
46  	/**
47  	 * {@inheritDoc}
48  	 */
49  	public String getBeanName() {
50  		return myBeanName;
51  	}
52  
53  	/**
54  	 * @see #setIoRetries(int)
55  	 */
56  	public int getIoRetries() {
57  		return myIoRetries;
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	public Message../../../../ca/uhn/hl7v2/model/Message.html#Message">Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws HL7Exception {
64  		String sendingIp = (String) theMetadata.get(ApplicationRouter.METADATA_KEY_SENDING_IP);
65  		Object sendingPort = theMetadata.get(ApplicationRouter.METADATA_KEY_SENDING_PORT);
66  		String controlId = (String) theMetadata.get(ApplicationRouter.METADATA_KEY_MESSAGE_CONTROL_ID);
67  		String rawMessage = (String) theMetadata.get(MetadataKeys.IN_RAW_MESSAGE);
68  
69  		ourLog.info("Relaying message ({} bytes) with ID {} from {}:{} to URL {}", rawMessage.length(), controlId, sendingIp, sendingPort, getUrl());
70  		
71  		IReceivable<Message> response;
72  		long delay = System.currentTimeMillis();
73  		int attempt = -1;
74  		while(true) {
75  			attempt++;
76  			
77  			if (attempt > 0) {
78  				ourLog.info("This is attempt {}", attempt);
79  			}
80  			
81  			try {
82  				response = sendAndReceiveMessage(new MessageSendable(theMessage));
83  				delay = System.currentTimeMillis() - delay;
84  			} catch (DecodeException e) {
85  				ourLog.error("Failed to process HL7 over HTTP response from URL \"" + getUrl().toExternalForm() + "\"", e);
86  				throw new HL7Exception(Binder.getProductname() + " - Failed to process HL7 over HTTP response from URL \"" + getUrl().toExternalForm() + "\" - Error was: " + e.getMessage());
87  			} catch (ConnectException e) {
88  				ourLog.info("Failed to connect to URL \"" + getUrl().toExternalForm() + "\" - Error was: " + e.getMessage());
89  				throw new HL7Exception(Binder.getProductname() + " - Failed to connect to URL \"" + getUrl().toExternalForm() + "\" - Error was: " + e.getMessage());
90  			} catch (IOException e) {
91  				if (attempt < myIoRetries) {
92  					ourLog.warn("Got an IOException, going to retry transmission: " + e.toString());
93  					continue;
94  				}
95  				ourLog.error("IO Exception communicating with URL \"" + getUrl().toExternalForm() + "\"", e);
96  				throw new HL7Exception(Binder.getProductname() + " - IO Exception communicating with URL URL \"" + getUrl().toExternalForm() + "\" - Error was: " + e.getMessage());
97  			} catch (EncodeException e) {
98  				ourLog.error("Failed to create HTTP request", e);
99  				throw new HL7Exception(Binder.getProductname() + " - Failed to create HTTP request - Error was: " + e.getMessage());
100 			}
101 		
102 			break;
103 		}
104 		
105 		String responseControlId = new Terser(response.getMessage()).get("/MSH-10");
106 		ourLog.info("Received response to ID {} with ID {} in {} ms", controlId, responseControlId, delay);
107 		
108 		return response.getMessage();
109 	}
110 
111 	/**
112 	 * <i>Automatically called by the container</i>
113 	 */
114 	public void setBeanName(String theBeanName) {
115 		myBeanName = theBeanName;
116 	}
117 
118 	/**
119 	 * If set to a positive integer, the relay will attempt to redeliver a message up to the given
120 	 * number of times before giving up, if the transmission fails due to an IO exception. 
121 	 */
122 	public void setIoRetries(int theIoRetries) {
123 		myIoRetries = theIoRetries;
124 	}
125 
126 
127 }