Search in sources :

Example 41 with Message

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));
}
Also used : Message(ca.uhn.hl7v2.model.Message)

Example 42 with Message

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;
}
Also used : Message(ca.uhn.hl7v2.model.Message) Terser(ca.uhn.hl7v2.util.Terser) HashMap(java.util.HashMap) Map(java.util.Map)

Example 43 with 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);
}
Also used : Message(ca.uhn.hl7v2.model.Message) IoBuffer(org.apache.mina.core.buffer.IoBuffer)

Example 44 with Message

use of ca.uhn.hl7v2.model.Message in project camel by apache.

the class HL7RouteTest method createADR19Message.

// END SNIPPET: e2
private static Message createADR19Message() throws Exception {
    ADR_A19 adr = new ADR_A19();
    // Populate the MSH Segment
    MSH mshSegment = adr.getMSH();
    mshSegment.getFieldSeparator().setValue("|");
    mshSegment.getEncodingCharacters().setValue("^~\\&");
    mshSegment.getDateTimeOfMessage().getTimeOfAnEvent().setValue("200701011539");
    mshSegment.getSendingApplication().getNamespaceID().setValue("MYSENDER");
    mshSegment.getSequenceNumber().setValue("123");
    mshSegment.getMessageType().getMessageType().setValue("ADR");
    mshSegment.getMessageType().getTriggerEvent().setValue("A19");
    // Populate the PID Segment
    MSA msa = adr.getMSA();
    msa.getAcknowledgementCode().setValue("AA");
    msa.getMessageControlID().setValue("123");
    QRD qrd = adr.getQRD();
    qrd.getQueryDateTime().getTimeOfAnEvent().setValue("20080805120000");
    return adr.getMessage();
}
Also used : QRD(ca.uhn.hl7v2.model.v24.segment.QRD) MSH(ca.uhn.hl7v2.model.v24.segment.MSH) ADR_A19(ca.uhn.hl7v2.model.v24.message.ADR_A19) MSA(ca.uhn.hl7v2.model.v24.segment.MSA)

Example 45 with Message

use of ca.uhn.hl7v2.model.Message in project camel by apache.

the class HL7ValidateTest method testMarshalWithoutValidation.

@Test
public void testMarshalWithoutValidation() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:end");
    mock.expectedMessageCount(1);
    Message message = createADT01Message();
    template.sendBody("direct:start2", message);
    assertMockEndpointsSatisfied();
}
Also used : Message(ca.uhn.hl7v2.model.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Aggregations

Message (ca.uhn.hl7v2.model.Message)44 QRD (ca.uhn.hl7v2.model.v24.segment.QRD)19 Test (org.junit.Test)19 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)18 MSH (ca.uhn.hl7v2.model.v24.segment.MSH)13 ArrayList (java.util.ArrayList)12 HL7Exception (ca.uhn.hl7v2.HL7Exception)11 ADR_A19 (ca.uhn.hl7v2.model.v24.message.ADR_A19)9 MSA (ca.uhn.hl7v2.model.v24.segment.MSA)9 ADT_A01 (ca.uhn.hl7v2.model.v24.message.ADT_A01)7 PID (ca.uhn.hl7v2.model.v24.segment.PID)7 Exchange (org.apache.camel.Exchange)7 Processor (org.apache.camel.Processor)7 RouteBuilder (org.apache.camel.builder.RouteBuilder)7 Structure (ca.uhn.hl7v2.model.Structure)5 IOException (java.io.IOException)5 Group (ca.uhn.hl7v2.model.Group)4 Segment (ca.uhn.hl7v2.model.Segment)4 File (java.io.File)4 SegmentModel (org.talend.designer.hl7.model.SegmentModel)4