HAPI HL7验证会引发异常

时间:2018-06-04 20:20:35

标签: java hl7 hapi

我正在开发一个我打算用于HL7消息验证的Java端点。我有一个运行的基本应用程序,它使用标准HAPI HL7验证示例的变体。如果我传递了有效的信息,我会得到"成功"响应。如果我传递了无效的消息,我仍然会得到一个"成功"响应。

我得到错误响应的唯一方法是HL7格式错误并且PipeParser抛出异常。在这种情况下,它会被捕获阻塞。

我想看到的是,如果我传入一条无效的消息,它实际上已经验证并返回所有验证错误。但我实际上并没有看到任何验证。它解析或崩溃试图解析。

我在这里缺少什么?

    HapiContext context = new DefaultHapiContext();

    ValidationContext validationContext =  ValidationContextFactory.defaultValidation();
    context.setValidationContext(validationContext);


    try
    {
        context.getParserConfiguration().setUnexpectedSegmentBehaviour(UnexpectedSegmentBehaviourEnum.THROW_HL7_EXCEPTION);

        Message messageValidationResults = context.getPipeParser().parse(hl7Message);

        SimpleValidationExceptionHandler handler = new SimpleValidationExceptionHandler(context);
        handler.setMinimumSeverityToCollect(Severity.INFO);

        Validator<Boolean> validator = context.getMessageValidator();

        if (!validator.validate(messageValidationResults, handler))
        {
            if (handler.getExceptions().size() == 0)
            {
                hl7ValidationResult = "SUCCESS - Message Validated Successfully";
            }
            else
            {
                hl7ValidationResult = "ERROR - Found " + handler.getExceptions().size() + " problems\n\n";
                for (Exception e : handler.getExceptions())
                {
                    hl7ValidationResult += (e.getClass().getSimpleName() + " - " + e.getMessage()) + "\n";
                }
            }

        }
    }
    catch (Exception e)
    {
        hl7ValidationResult = "ERROR - " + e.getMessage();

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String sStackTrace = sw.toString();

        hl7ValidationResult += "\n\n" + sStackTrace;
    }

1 个答案:

答案 0 :(得分:1)

如果您认为不正确,请忽略答案,我停止使用HL7,但是,看看我的旧项目,我找到了这个,也许它可以帮助您找到问题的解决方案:

{

    DefaultValidationBuilder builder = new DefaultValidationBuilder() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void configure() {
            super.configure();
            forVersion(Version.V26);
        }

    };

    HapiContext context = new DefaultHapiContext();
    context.setValidationRuleBuilder(builder);
    PipeParser hapiParser = context.getPipeParser();

    try {

        hapiParser.parse(hl7Message);

    } catch (ca.uhn.hl7v2.HL7Exception e) {

        // String error, String language, String requisitionNumber, String controlId, String processinId, String senderApplication, String senderFacility
        errors.add(new HL7ValidationError(
            "HAPI Validator error found: " + e.getMessage(), 
            extractor.accessPatientDirectly().getLanguage(), 
            extractor.accessPatientDirectly().getRequisitionNumber(), 
            extractor.accessPatientDirectly().getControlID(), 
            "", 
            extractor.accessPatientDirectly().getSenderApplication(), 
            extractor.accessPatientDirectly().getSenderFacility())
        );  
        log.debug("HAPI Validator error found: " + e.getMessage()); 
    }

    try {
        context.close();
    }
    catch (Exception ex) {
        log.debug("Unable to close HapiContext(): " + ex.getMessage());
    }

}

基本上我使用 hapiParser.parse(hl7Message); 并捕获HL7Exception