Copyright © infotec016 . Powered by Blogger.

Friday, February 22, 2019

Hl7 Java Client with Tls


HL7 Sender Java Code


package hl7senderwithTls;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.app.Connection;
import ca.uhn.hl7v2.app.Initiator;
import ca.uhn.hl7v2.hoh.sockets.CustomCertificateTlsSocketFactory;
import ca.uhn.hl7v2.hoh.util.HapiSocketTlsFactoryWrapper;
import ca.uhn.hl7v2.hoh.util.KeystoreUtils;
import ca.uhn.hl7v2.llp.LLPException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.Parser;

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

public class hl7senderwithTls {

    public static void main(String[] args) throws HL7Exception, 
     LLPException, IOException {

        final int PORT_NUMBER = 9014;
        final String tlsKeystoreFilepath = "src/main/resources/keystore.jks";
        final String tlsKeystorePassphrase = "changeit";
        final String tlsKeystoreType = "JKS";

        // InHAPI almost all things revolve around a context object
        HapiContext context = new DefaultHapiContext();

        String adtMessage = "MSH|^~\\&|NES|NINTENDO|TESTSYSTEM|TESTFACILITY" + 
                "|20010101000000||ADT^A01|Q123456789T123456789X123456|P|2.3\r" +
                "EVN|A01|20010101000000|||^KOOPA^BOWSER\r";
        try {
            //To validate the keystore
            KeyStore keyStore = KeystoreUtils.loadKeystore(tlsKeystoreFilepath, 
             tlsKeystorePassphrase);
            KeyStore.getInstance(tlsKeystoreType);
            KeystoreUtils.validateKeystoreForTlsSending(keyStore);
            CustomCertificateTlsSocketFactory tlsFac = new 
             CustomCertificateTlsSocketFactory(tlsKeystoreType,
                    tlsKeystoreFilepath, tlsKeystorePassphrase);
            context.setSocketFactory(new HapiSocketTlsFactoryWrapper(tlsFac));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        // create a new MLLP client over the specified port
        Connection connection = context.newClient("localhost", PORT_NUMBER, true);

        // The initiator which will be used to transmit our message
        Initiator initiator = connection.getInitiator();

        // send the created HL7 message over the connection established
        Parser parser = context.getPipeParser();
        System.out.println("Sending message:" + "\n" + adtMessage);
        Message response = initiator.sendAndReceive(parser.parse(adtMessage));

        // display the message response received from the remote party
        String responseString = parser.encode(response);
        System.out.println("Received response:\n" + responseString.replaceAll("\r", "\n"));

    }
}

HL7 Receiver Java Code

This main class is used to


package hl7receiverwithTls;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.app.HL7Service;
import ca.uhn.hl7v2.hoh.sockets.CustomCertificateTlsSocketFactory;
import ca.uhn.hl7v2.hoh.util.HapiSocketTlsFactoryWrapper;
import ca.uhn.hl7v2.hoh.util.KeystoreUtils;
import ca.uhn.hl7v2.parser.Parser;
import ca.uhn.hl7v2.parser.PipeParser;

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

public class hl7receiverwithTls {
    public static void main(String[] args) throws InterruptedException {

        final int PORT_NUMBER = 9014;
        final String tlsKeystoreFilepath = "src/main/resources/keystore.jks";
        final String tlsKeystorePassphrase = "changeit";
        final String tlsKeystoreType = "JKS";
        // In HAPI, almost all things revolve around a context object
        HapiContext context = new DefaultHapiContext();
        try {
            //To validate the keystore
            KeyStore keyStore = KeystoreUtils.loadKeystore(tlsKeystoreFilepath, tlsKeystorePassphrase);
            KeyStore.getInstance(tlsKeystoreType);
            KeystoreUtils.validateKeystoreForTlsSending(keyStore);
            CustomCertificateTlsSocketFactory tlsFac = new CustomCertificateTlsSocketFactory(tlsKeystoreType,
                    tlsKeystoreFilepath, tlsKeystorePassphrase);
            context.setSocketFactory(new HapiSocketTlsFactoryWrapper(tlsFac));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
            Parser parser = new PipeParser();
            HL7Service ourHl7Server = context.newServer(PORT_NUMBER, true);
            // You can set up routing rules for your HL7 listener by extending the AppRoutingData class
            ourHl7Server.registerApplication(new RegisterEventRouter(), new ourSimpleApplication());
            ourHl7Server.setShutdownTimeout(10000);
            ourHl7Server.startAndWait(); 
    }
}


Simple Application class that is used to receive the message and create and send the acknowledgement

package hl7receiverwithTls;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.protocol.ReceivingApplication;
import ca.uhn.hl7v2.protocol.ReceivingApplicationException;

import java.io.IOException;
import java.util.Map;

public class ourSimpleApplication implements ReceivingApplication {

    PipeParser pipeParser = new PipeParser();

    public Message processMessage(Message message, Map map) throws ReceivingApplicationException, HL7Exception {

        System.out.println("Received Message\n" + pipeParser.encode(message));
        Message response = null;
        try {
            response = message.generateACK();
            System.out.println("Sent Response\n" + pipeParser.encode(response));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    public boolean canProcess(Message message) {

        return true;
    }
}


Event Router class that is used to rout the message to the appropriate versions and types.


package hl7receiverwithTls;

import ca.uhn.hl7v2.protocol.ApplicationRouter;

public class RegisterEventRouter implements ApplicationRouter.AppRoutingData {

    public String getMessageType() {

        return "*";
    }

    public String getTriggerEvent() {

        return "*";
    }

    public String getProcessingId() {

        return "*";
    }

    public String getVersion() {

        return "*";
    }

}


0 comments:

Post a Comment