View Javadoc

1   package ca.uhn.hl7v2.examples.hoh;
2   
3   import java.io.FileNotFoundException;
4   import java.io.IOException;
5   import java.security.KeyStoreException;
6   import java.security.NoSuchAlgorithmException;
7   import java.security.cert.CertificateException;
8   
9   import ca.uhn.hl7v2.DefaultHapiContext;
10  import ca.uhn.hl7v2.HL7Exception;
11  import ca.uhn.hl7v2.app.Connection;
12  import ca.uhn.hl7v2.hoh.api.DecodeException;
13  import ca.uhn.hl7v2.hoh.api.EncodeException;
14  import ca.uhn.hl7v2.hoh.api.IReceivable;
15  import ca.uhn.hl7v2.hoh.api.ISendable;
16  import ca.uhn.hl7v2.hoh.llp.Hl7OverHttpLowerLayerProtocol;
17  import ca.uhn.hl7v2.hoh.raw.client.HohRawClientSimple;
18  import ca.uhn.hl7v2.hoh.sign.BouncyCastleCmsMessageSigner;
19  import ca.uhn.hl7v2.hoh.util.KeystoreUtils;
20  import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
21  
22  public class SignatureClient {
23  
24  	/**
25  	 * @param args
26  	 * @throws IOException 
27  	 * @throws FileNotFoundException 
28  	 * @throws CertificateException 
29  	 * @throws NoSuchAlgorithmException 
30  	 * @throws KeyStoreException 
31  	 * @throws EncodeException 
32  	 * @throws DecodeException 
33  	 * @throws HL7Exception 
34  	 */
35  	@SuppressWarnings("unused")
36     public static void main(String[] args) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, DecodeException, EncodeException, HL7Exception {
37  
38  		ISendable<?> sendable=null;
39  		{
40  // START SNIPPET: client 
41  // Create a client
42  HohRawClientSimple client = new HohRawClientSimple("remotehost", 8080, "/");
43  
44  // Create a message signer
45  BouncyCastleCmsMessageSigner signer = new BouncyCastleCmsMessageSigner();
46  signer.setKeyStore(KeystoreUtils.loadKeystore("/path/to/keystore/keystore.jks", "store_password"));
47  signer.setKeyAlias("keyalias");
48  signer.setAliasPassword("key_password");
49  
50  client.setSigner(signer);
51  
52  // Send a message
53  IReceivable<String> response = client.sendAndReceive(sendable);
54  // END SNIPPET: client 
55  	}
56  
57  // START SNIPPET: llp
58  Hl7OverHttpLowerLayerProtocol llp;
59  llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT);
60  
61  //Create a message signer
62  BouncyCastleCmsMessageSigner signer = new BouncyCastleCmsMessageSigner();
63  signer.setKeyStore(KeystoreUtils.loadKeystore("/path/to/keystore/keystore.jks", "store_password"));
64  signer.setKeyAlias("keyalias");
65  signer.setAliasPassword("key_password");
66  llp.setSigner(signer);
67  
68  // Create the ConnectionHub
69  String host = "localhost";
70  int port = 8080;
71  boolean tls = false;
72  
73  DefaultHapiContext ctx = new DefaultHapiContext();
74  ctx.setLowerLayerProtocol(llp);
75  
76  // Connect
77  Connection connection = ctx.newClient(host, port, tls);
78  //END SNIPPET: llp
79  
80  
81  	}
82  
83  }