View Javadoc

1   package ca.uhn.hl7v2.examples.hoh;
2   
3   import java.io.IOException;
4   
5   import ca.uhn.hl7v2.HL7Exception;
6   import ca.uhn.hl7v2.hoh.api.DecodeException;
7   import ca.uhn.hl7v2.hoh.api.EncodeException;
8   import ca.uhn.hl7v2.hoh.api.IAuthorizationClientCallback;
9   import ca.uhn.hl7v2.hoh.api.IReceivable;
10  import ca.uhn.hl7v2.hoh.api.ISendable;
11  import ca.uhn.hl7v2.hoh.api.MessageMetadataKeys;
12  import ca.uhn.hl7v2.hoh.auth.SingleCredentialClientCallback;
13  import ca.uhn.hl7v2.hoh.hapi.api.MessageSendable;
14  import ca.uhn.hl7v2.hoh.hapi.client.HohClientSimple;
15  import ca.uhn.hl7v2.hoh.sockets.TlsSocketFactory;
16  import ca.uhn.hl7v2.model.Message;
17  import ca.uhn.hl7v2.model.v25.message.ADT_A01;
18  import ca.uhn.hl7v2.parser.Parser;
19  import ca.uhn.hl7v2.parser.PipeParser;
20  
21  public class HohClientSimpleExample {
22  
23  	/**
24  	 * Example of an HL7-over-HTTP client for sending messages 
25  	 */
26  	public static void main(String[] args) throws HL7Exception, IOException {
27  		
28  		/*
29  		 * http://localhost:8080/AppContext
30  		 */
31  		String host = "localhost";
32  		int port = 8080;
33  		String uri = "/AppContext";
34  		
35  		// Create a parser
36  		Parser parser = PipeParser.getInstanceWithNoValidation();
37  		
38  		// Create a client
39  		HohClientSimple client = new HohClientSimple(host, port, uri, parser);
40  
41  		// Set the socket factory to use TLS (if this is desired)
42  		client.setSocketFactory(new TlsSocketFactory());
43  		
44  		// Optionally, if credentials should be sent, they 
45  		// can be provided using a credential callback
46  		IAuthorizationClientCallback authCalback = new SingleCredentialClientCallback("ausername", "somepassword");
47  		client.setAuthorizationCallback(authCalback);
48  		
49  		// The ISendable defines the object that provides the actual
50  		// message to send
51  		ADT_A01 adt = new ADT_A01();
52  		adt.initQuickstart("ADT", "A01", "T");
53  		// .. set other values on the message ..
54  		
55  		// The MessageSendable provides the message to send 
56  		ISendable<Message> sendable = new MessageSendable(adt);
57  		
58  		try {
59  			// sendAndReceive actually sends the message
60  			IReceivable<Message> receivable = client.sendAndReceiveMessage(sendable);
61  			
62  			// receivavle.getRawMessage() provides the response
63  			Message message = receivable.getMessage();
64  			System.out.println("Response was:\n" + message.encode());
65  			
66  			// IReceivable also stores metadata about the message
67  			String remoteHostIp = (String) receivable.getMetadata().get(MessageMetadataKeys.REMOTE_HOST_ADDRESS);
68  			System.out.println("From:\n" + remoteHostIp);
69  			
70  			/*
71  			 * Note that the client may be reused as many times as you like,
72  			 * by calling sendAndReceiveMessage repeatedly
73  			 */
74  			
75  		} catch (DecodeException e) {
76  			// Thrown if the response can't be read
77  			e.printStackTrace();
78  		} catch (IOException e) {
79  			// Thrown if communication fails
80  			e.printStackTrace();
81  		} catch (EncodeException e) {
82  			// Thrown if the message can't be encoded (generally a programming bug)
83  			e.printStackTrace();
84  		}
85  		
86  	}
87  
88  }