use of ca.uhn.hl7v2.model.Message in project nifi by apache.
the class ExtractHL7Attributes method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).evaluateAttributeExpressions(flowFile).getValue());
final Boolean useSegmentNames = context.getProperty(USE_SEGMENT_NAMES).asBoolean();
final Boolean parseSegmentFields = context.getProperty(PARSE_SEGMENT_FIELDS).asBoolean();
final Boolean skipValidation = context.getProperty(SKIP_VALIDATION).asBoolean();
final String inputVersion = context.getProperty(HL7_INPUT_VERSION).getValue();
final byte[] buffer = new byte[(int) flowFile.getSize()];
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(final InputStream in) throws IOException {
StreamUtils.fillBuffer(in, buffer);
}
});
@SuppressWarnings("resource") final HapiContext hapiContext = new DefaultHapiContext();
if (!inputVersion.equals("autodetect")) {
hapiContext.setModelClassFactory(new CanonicalModelClassFactory(inputVersion));
}
if (skipValidation) {
hapiContext.setValidationContext((ValidationContext) ValidationContextFactory.noValidation());
}
final PipeParser parser = hapiContext.getPipeParser();
final String hl7Text = new String(buffer, charset);
try {
final Message message = parser.parse(hl7Text);
final Map<String, String> attributes = getAttributes(message, useSegmentNames, parseSegmentFields);
flowFile = session.putAllAttributes(flowFile, attributes);
getLogger().debug("Added the following attributes for {}: {}", new Object[] { flowFile, attributes });
} catch (final HL7Exception e) {
getLogger().error("Failed to extract attributes from {} due to {}", new Object[] { flowFile, e });
session.transfer(flowFile, REL_FAILURE);
return;
}
session.transfer(flowFile, REL_SUCCESS);
}
use of ca.uhn.hl7v2.model.Message in project camel by apache.
the class AckExpression method evaluate.
@Override
public Object evaluate(Exchange exchange) {
Throwable t = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
Message msg = exchange.getIn().getBody(Message.class);
try {
HL7Exception hl7e = generateHL7Exception(t);
AcknowledgmentCode code = acknowledgementCode;
if (t != null && code == null) {
code = AcknowledgmentCode.AE;
}
return msg.generateACK(code == null ? AcknowledgmentCode.AA : code, hl7e);
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
}
use of ca.uhn.hl7v2.model.Message in project camel by apache.
the class HL7DataFormat method marshal.
public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {
Message message = ExchangeHelper.convertToMandatoryType(exchange, Message.class, body);
String charsetName = HL7Charset.getCharsetName(message, exchange);
String encoded = HL7Converter.encode(message, parser);
outputStream.write(encoded.getBytes(charsetName));
}
use of ca.uhn.hl7v2.model.Message in project camel by apache.
the class HL7DataFormat method unmarshal.
public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
byte[] body = ExchangeHelper.convertToMandatoryType(exchange, byte[].class, inputStream);
String charsetName = HL7Charset.getCharsetName(body, guessCharsetName(body, exchange));
String bodyAsString = new String(body, charsetName);
Message message = HL7Converter.parse(bodyAsString, parser);
// add MSH fields as message out headers
Terser terser = new Terser(message);
for (Map.Entry<String, String> entry : HEADER_MAP.entrySet()) {
exchange.getOut().setHeader(entry.getKey(), terser.get(entry.getValue()));
}
exchange.getOut().setHeader(HL7_CONTEXT, hapiContext);
exchange.getOut().setHeader(Exchange.CHARSET_NAME, charsetName);
return message;
}
use of ca.uhn.hl7v2.model.Message in project camel by apache.
the class HL7MLLPEncoder method encode.
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
if (message == null) {
throw new IllegalArgumentException("Message to be encoded is null");
} else if (message instanceof Exception) {
// we cannot handle exceptions
throw (Exception) message;
}
byte[] body;
if (message instanceof Message) {
body = ((Message) message).encode().getBytes(config.getCharset());
} else if (message instanceof String) {
body = ((String) message).getBytes(config.getCharset());
} else if (message instanceof byte[]) {
body = (byte[]) message;
} else {
throw new IllegalArgumentException("The message to encode is not a supported type: " + message.getClass().getCanonicalName());
}
// put the data into the byte buffer
IoBuffer buf = IoBuffer.allocate(body.length + 3).setAutoExpand(true);
buf.put((byte) config.getStartByte());
buf.put(body);
buf.put((byte) config.getEndByte1());
buf.put((byte) config.getEndByte2());
// flip the buffer so we can use it to write to the out stream
buf.flip();
LOG.debug("Encoded HL7 from {} to byte stream", message.getClass().getCanonicalName());
out.write(buf);
}
Aggregations