View Javadoc
1   package ca.uhn.hl7v2.examples;
2   
3   import ca.uhn.hl7v2.DefaultHapiContext;
4   import ca.uhn.hl7v2.HL7Exception;
5   import ca.uhn.hl7v2.HapiContext;
6   import ca.uhn.hl7v2.app.Connection;
7   import ca.uhn.hl7v2.app.HL7Service;
8   import ca.uhn.hl7v2.hoh.sockets.CustomCertificateTlsSocketFactory;
9   import ca.uhn.hl7v2.hoh.util.HapiSocketTlsFactoryWrapper;
10  
11  public class UsingTls {
12  
13  	/**
14  	 * Example for how to use TLS/SSL
15  	 */
16  	@SuppressWarnings("unused")
17  	public static void main(String[] args) throws HL7Exception {
18  		// http://example.com/ewre
19  
20  		/*
21  		 * http://example.com/ewre
22  		 */
23  		/**
24  		 * http://example.com/ewre
25  		 */
26  
27  		/*
28  		 * To create a client or a server which uses TLS/SSL, you simply pass
29  		 * "true" to the factory methods in question on the context.
30  		 */
31  		HapiContext ctx = new DefaultHapiContext();
32  
33  		boolean useTls = true;
34  		int port = 8888;
35  		HL7Service server = ctx.newServer(port, useTls); // server
36  		Connection client = ctx.newClient("localhost", port, useTls); // client
37  
38  		/*
39  		 * Note that in most cases you will need to import certificates in order
40  		 * to make this work.
41  		 * 
42  		 * (TODO: this could use more information)
43  		 */
44  
45  		/*
46  		 * Another option is to use a custom certificate socket factory, which
47  		 * allows you to use different keystores for different connections. This
48  		 * is a more flexible option if you are running multiple applications on
49  		 * one JVM.
50  		 * 
51  		 * Note that CustomCertificateTlsSocketFactory comes from the
52  		 * HL7-over-HTTP library, so that library must be added to your
53  		 * classpath in order for this class to be visible. See here:
54  		 * http://hl7api.sourceforge.net/getting_started.html
55  		 */
56  		CustomCertificateTlsSocketFactory sfac = new CustomCertificateTlsSocketFactory();
57  		sfac.setKeystoreFilename("file://C:/keystores/client.jks");
58  		sfac.setKeystorePassphrase("keystorepass");
59  
60  		// Use the following adapter to pass the socket factory to the context
61  		ctx.setSocketFactory(new HapiSocketTlsFactoryWrapper(sfac));
62  
63  		// Your client will now use the socket factory
64  		client = ctx.newClient("localhost", port, useTls);
65  	}
66  
67  }