Search in sources :

Example 1 with HL7ProcessingContext

use of org.wso2.carbon.business.messaging.hl7.common.HL7ProcessingContext in project carbon-mediation by wso2.

the class HL7MessageBuilder method processDocument.

/**
 * {@inheritdoc }*
 */
public OMElement processDocument(InputStream inputStream, String contentType, MessageContext msgCtx) throws AxisFault {
    try {
        HL7ProcessingContext processingCtx = new HL7ProcessingContext(msgCtx.getAxisService());
        String charset = getCharsetEncoding(contentType);
        msgCtx.setProperty(HL7Constants.HL7_MESSAGE_CHARSET, charset);
        String hl7String = HL7Utils.streamToString(inputStream, charset);
        if (log.isDebugEnabled()) {
            log.debug("HL7 String: " + hl7String);
        }
        Message message = processingCtx.parseMessage(hl7String);
        processingCtx.initMessageContext(message, msgCtx);
        processingCtx.checkConformanceProfile(message);
        String hl7Xml = serializeHL7toXML(message);
        return HL7Utils.generateHL7MessageElement(hl7Xml);
    } catch (AxisFault e) {
        throw e;
    } catch (Exception e) {
        throw new AxisFault(e.getMessage(), e);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) Message(ca.uhn.hl7v2.model.Message) HL7ProcessingContext(org.wso2.carbon.business.messaging.hl7.common.HL7ProcessingContext) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Example 2 with HL7ProcessingContext

use of org.wso2.carbon.business.messaging.hl7.common.HL7ProcessingContext in project carbon-mediation by wso2.

the class HL7MessageFormatter method writeTo.

/**
 * {@inheritdoc }*
 */
public void writeTo(MessageContext msgCtx, OMOutputFormat omOutputFormat, OutputStream outputStream, boolean b) throws AxisFault {
    OMElement omElement = msgCtx.getEnvelope().getBody().getFirstElement().getFirstElement();
    if (omElement == null) {
        omElement = msgCtx.getEnvelope().getBody().getFirstElement();
    }
    HL7ProcessingContext processingCtx;
    try {
        /* The AxisService retrieved at this point from the 
        	 * message context is not the correct one */
        processingCtx = new HL7ProcessingContext(true, false, false, null, null, null, null, null, null, true, true);
    } catch (Exception e) {
        throw new AxisFault(e.getMessage(), e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Inside the HL7 formatter: " + omElement.toString());
    }
    if (msgCtx.getFLOW() == MessageContext.OUT_FAULT_FLOW || msgCtx.getEnvelope().hasFault()) {
        SOAPFault soapFault = msgCtx.getEnvelope().getBody().getFault();
        try {
            Message message = processingCtx.createNack((Message) msgCtx.getProperty(HL7Constants.HL7_MESSAGE_OBJECT), soapFault.getReason().getText());
            if (log.isDebugEnabled()) {
                log.debug("Generate HL7 error: " + message);
            }
            this.writeMessageOut(message, outputStream, msgCtx);
        } catch (HL7Exception e) {
            throw new AxisFault("Error on creating HL7 Error segment: " + e.getMessage(), e);
        } catch (IOException e) {
            throw new AxisFault("Error on writing HL7 Error to output stream: " + e.getMessage(), e);
        }
    } else {
        try {
            String xmlFormat;
            Message message;
            Boolean isValid = (Boolean) msgCtx.getProperty(HL7Constants.HL7_VALIDATION_PASSED);
            if ((isValid != null && !isValid) && msgCtx.getProperty(HL7Constants.HL7_PASS_THROUGH_INVALID_MESSAGES).equals(true)) {
                // pass through invalid messages
                message = (Message) msgCtx.getProperty(HL7Constants.HL7_MESSAGE_OBJECT);
            } else {
                xmlFormat = omElement.toString();
                message = this.xmlParser.parse(xmlFormat);
            }
            if (log.isDebugEnabled()) {
                log.debug("Message inside the formatter: " + message);
            }
            if (this.isGenerateMessageAck(msgCtx)) {
                message = this.createGeneratedMessageAck(processingCtx, message, msgCtx);
            }
            this.writeMessageOut(message, outputStream, msgCtx);
        } catch (Exception e) {
            throw new AxisFault("Error occurred during HL7 message creation: " + e.getMessage(), e);
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) Message(ca.uhn.hl7v2.model.Message) HL7ProcessingContext(org.wso2.carbon.business.messaging.hl7.common.HL7ProcessingContext) HL7Exception(ca.uhn.hl7v2.HL7Exception) OMElement(org.apache.axiom.om.OMElement) SOAPFault(org.apache.axiom.soap.SOAPFault) IOException(java.io.IOException) IOException(java.io.IOException) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Example 3 with HL7ProcessingContext

use of org.wso2.carbon.business.messaging.hl7.common.HL7ProcessingContext in project carbon-mediation by wso2.

the class HL7TransportSender method sendApplicationACKResponse.

/**
 * Send application ack message from the incoming response  to the client
 * @param messageContext
 * @param outTransportInfo
 * @throws AxisFault
 */
private void sendApplicationACKResponse(MessageContext messageContext, OutTransportInfo outTransportInfo) throws AxisFault {
    HL7ProcessingContext processingContext = ((HL7TransportOutInfo) outTransportInfo).getProcessingContext();
    try {
        Message message = xmlPayloadToHL7Message(messageContext);
        processingContext.offerApplicationResponses(message, messageContext);
    } catch (HL7Exception e) {
        handleException("Error while sending an custom ack message", e);
    }
}
Also used : Message(ca.uhn.hl7v2.model.Message) HL7ProcessingContext(org.wso2.carbon.business.messaging.hl7.common.HL7ProcessingContext) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Example 4 with HL7ProcessingContext

use of org.wso2.carbon.business.messaging.hl7.common.HL7ProcessingContext in project carbon-mediation by wso2.

the class HL7MessageProcessor method processMessage.

public Message processMessage(Message message) throws ApplicationException, HL7Exception {
    if (log.isDebugEnabled()) {
        log.debug("Received HL7 message: " + message.toString());
    }
    Terser terser = new Terser(message);
    String messageControlerID = terser.get("/MSH-10");
    if (messageControlerID == null || messageControlerID.length() == 0) {
        throw new HL7Exception("MSH segment missing required field: Control ID (MSH-10)", HL7Exception.REQUIRED_FIELD_MISSING);
    }
    HL7ProcessingContext procCtx = this.endpoint.getProcessingContext();
    try {
        MessageContext messageContext = endpoint.createMessageContext();
        // Message validation if transport.hl7.ValidateMessages is true
        if (procCtx.isValidateMessage()) {
            procCtx.getPipeParser().setValidationContext(new DefaultValidation());
            try {
                procCtx.getPipeParser().parse(message.encode());
                messageContext.setProperty(HL7Constants.HL7_DEFAULT_VALIDATION_PASSED, "success");
            } catch (HL7Exception e) {
                messageContext.setProperty(HL7Constants.HL7_DEFAULT_VALIDATION_PASSED, "fault");
                log.error("Validation of HL7 message failed. " + e.getMessage());
            }
        }
        // We set the validation context to NoValidation() or else subsequent messages may validate inside
        // hapi server.
        procCtx.getPipeParser().setValidationContext(new NoValidation());
        HL7TransportOutInfo outinfo = new HL7TransportOutInfo();
        outinfo.setMessageControllerID(messageControlerID);
        outinfo.setProcessingContext(procCtx);
        messageContext.setProperty(Constants.OUT_TRANSPORT_INFO, outinfo);
        messageContext.setIncomingTransportName(HL7Constants.TRANSPORT_NAME);
        procCtx.initMessageContext(message, messageContext);
        procCtx.checkConformanceProfile(message);
        messageContext.setEnvelope(createEnvelope(messageContext, message));
        AxisEngine.receive(messageContext);
        return procCtx.handleHL7Result(messageContext, message);
    } catch (AxisFault axisFault) {
        return procCtx.createNack(message, "Error while processing the HL7 message through the engine: " + axisFault.getMessage());
    } catch (XMLStreamException e) {
        return procCtx.createNack(message, "IO error while processing the HL7 content: " + e.getMessage());
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) Terser(ca.uhn.hl7v2.util.Terser) XMLStreamException(javax.xml.stream.XMLStreamException) DefaultValidation(ca.uhn.hl7v2.validation.impl.DefaultValidation) HL7ProcessingContext(org.wso2.carbon.business.messaging.hl7.common.HL7ProcessingContext) HL7Exception(ca.uhn.hl7v2.HL7Exception) NoValidation(ca.uhn.hl7v2.validation.impl.NoValidation) HL7TransportOutInfo(org.wso2.carbon.business.messaging.hl7.transport.HL7TransportOutInfo) MessageContext(org.apache.axis2.context.MessageContext)

Aggregations

HL7Exception (ca.uhn.hl7v2.HL7Exception)4 HL7ProcessingContext (org.wso2.carbon.business.messaging.hl7.common.HL7ProcessingContext)4 Message (ca.uhn.hl7v2.model.Message)3 AxisFault (org.apache.axis2.AxisFault)3 Terser (ca.uhn.hl7v2.util.Terser)1 DefaultValidation (ca.uhn.hl7v2.validation.impl.DefaultValidation)1 NoValidation (ca.uhn.hl7v2.validation.impl.NoValidation)1 IOException (java.io.IOException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMElement (org.apache.axiom.om.OMElement)1 SOAPFault (org.apache.axiom.soap.SOAPFault)1 MessageContext (org.apache.axis2.context.MessageContext)1 HL7TransportOutInfo (org.wso2.carbon.business.messaging.hl7.transport.HL7TransportOutInfo)1