Skip to content

Instantly share code, notes, and snippets.

@rahulsom
Created February 22, 2016 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulsom/4870d2a9ef3ebe92cadd to your computer and use it in GitHub Desktop.
Save rahulsom/4870d2a9ef3ebe92cadd to your computer and use it in GitHub Desktop.
Send HL7 Message from file to host:port
@Grab('ca.uhn.hapi:hapi-base:2.1')
@Grab('ca.uhn.hapi:hapi-structures-v26:2.1')
import ca.uhn.hl7v2.DefaultHapiContext
import ca.uhn.hl7v2.HapiContext
import ca.uhn.hl7v2.app.ConnectionData
import ca.uhn.hl7v2.llp.LowerLayerProtocol
import ca.uhn.hl7v2.llp.MinLowerLayerProtocol
import ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder
def cli = new CliBuilder()
cli.with {
f longOpt: 'file', args: 1, argName: 'filePath', required: true, "HL7 File to send"
h longOpt: 'host', args: 1, argName: 'hostname', "Host to send message to"
p longOpt: 'port', args: 1, argName: 'portNumber', required: true, "port to send message to"
}
def options = cli.parse(args)
def host = options.host ?: 'localhost'
def port = Integer.parseInt(options.port)
def fileName = options.file as String
println "host: $host"
println "port: $port"
println "fileName: $fileName"
if (!new File(fileName).exists()) {
println "File '$fileName' not found."
System.exit(1)
}
HapiContext ctx = new DefaultHapiContext()
ctx.setValidationRuleBuilder(new NoValidationBuilder())
println "Connecting to $host:$port"
def conn = ctx.connectionHub.attach(new ConnectionData(host, port, ctx.pipeParser, new MinLowerLayerProtocol()))
def start = System.nanoTime()
println "Sending contents of '$fileName' to $host:$port"
def text = new File(fileName).text
text.replace('\r', '\n').eachLine {
println "< $it"
}
def response = conn.initiator.sendAndReceive(ctx.pipeParser.parse(text.replace('\n', '\r')))
def stop = System.nanoTime()
println "Received response"
response.encode().replace('\r', '\n').eachLine {
println "> $it"
}
println "----"
println "Time taken: ${(stop-start)/1000000} ms"
System.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment