Search in sources :

Example 1 with ValidationContext

use of nl.nn.adapterframework.validation.ValidationContext in project iaf by ibissource.

the class Json2XmlValidator method alignXml2Json.

protected PipeRunResult alignXml2Json(String messageToValidate, IPipeLineSession session, boolean responseMode) throws XmlValidatorException, PipeRunException, ConfigurationException {
    ValidationContext context = validator.createValidationContext(session, getRootValidations(responseMode), getInvalidRootNamespaces());
    XMLReader parser = validator.getValidatingParser(session, context);
    XmlAligner aligner = new XmlAligner((PSVIProvider) parser);
    Xml2Json xml2json = new Xml2Json(aligner, isCompactJsonArrays(), !isJsonWithRootElements());
    parser.setContentHandler(aligner);
    aligner.setContentHandler(xml2json);
    aligner.setErrorHandler(context.getErrorHandler());
    String resultEvent = validator.validate(messageToValidate, session, getLogPrefix(session), parser, xml2json, context);
    String out = xml2json.toString();
    PipeForward forward = determineForward(resultEvent, session, responseMode);
    PipeRunResult result = new PipeRunResult(forward, out);
    return result;
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) XmlAligner(nl.nn.adapterframework.align.XmlAligner) Xml2Json(nl.nn.adapterframework.align.Xml2Json) PipeForward(nl.nn.adapterframework.core.PipeForward) XMLReader(org.xml.sax.XMLReader) ValidationContext(nl.nn.adapterframework.validation.ValidationContext)

Example 2 with ValidationContext

use of nl.nn.adapterframework.validation.ValidationContext in project iaf by ibissource.

the class Json2XmlValidator method alignJson.

protected PipeRunResult alignJson(String messageToValidate, IPipeLineSession session, boolean responseMode) throws PipeRunException, XmlValidatorException {
    ValidationContext context;
    ValidatorHandler validatorHandler;
    try {
        context = validator.createValidationContext(session, getRootValidations(responseMode), getInvalidRootNamespaces());
        validatorHandler = validator.getValidatorHandler(session, context);
    } catch (ConfigurationException e) {
        throw new PipeRunException(this, "Cannot create ValidationContext", e);
    }
    String resultEvent;
    String out = null;
    try {
        Json2Xml aligner = new Json2Xml(validatorHandler, context.getXsModels(), isCompactJsonArrays(), getMessageRoot(responseMode), isStrictJsonArraySyntax());
        if (StringUtils.isNotEmpty(getTargetNamespace())) {
            aligner.setTargetNamespace(getTargetNamespace());
        }
        aligner.setDeepSearch(isDeepSearch());
        aligner.setErrorHandler(context.getErrorHandler());
        aligner.setFailOnWildcards(isFailOnWildcards());
        ParameterList parameterList = getParameterList();
        if (parameterList != null) {
            ParameterResolutionContext prc = new ParameterResolutionContext(messageToValidate, session, isNamespaceAware(), false);
            Map<String, Object> parametervalues = null;
            parametervalues = prc.getValueMap(parameterList);
            aligner.setOverrideValues(parametervalues);
        }
        JsonStructure jsonStructure = Json.createReader(new StringReader(messageToValidate)).read();
        if (getOutputFormat(session, responseMode).equalsIgnoreCase(FORMAT_JSON)) {
            Xml2Json xml2json = new Xml2Json(aligner, isCompactJsonArrays(), !isJsonWithRootElements());
            aligner.setContentHandler(xml2json);
            aligner.startParse(jsonStructure);
            out = xml2json.toString();
        } else {
            Source source = aligner.asSource(jsonStructure);
            out = XmlUtils.source2String(source, isProduceNamespaceLessXml());
        }
    } catch (Exception e) {
        resultEvent = validator.finalizeValidation(context, session, e);
    }
    resultEvent = validator.finalizeValidation(context, session, null);
    PipeForward forward = determineForward(resultEvent, session, responseMode);
    PipeRunResult result = new PipeRunResult(forward, out);
    return result;
}
Also used : ValidatorHandler(javax.xml.validation.ValidatorHandler) PipeForward(nl.nn.adapterframework.core.PipeForward) Source(javax.xml.transform.Source) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException) ValidationContext(nl.nn.adapterframework.validation.ValidationContext) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) StringReader(java.io.StringReader) ParameterList(nl.nn.adapterframework.parameters.ParameterList) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) Xml2Json(nl.nn.adapterframework.align.Xml2Json) Json2Xml(nl.nn.adapterframework.align.Json2Xml) JsonStructure(javax.json.JsonStructure)

Example 3 with ValidationContext

use of nl.nn.adapterframework.validation.ValidationContext in project iaf by ibissource.

the class XmlValidator method validate.

protected PipeForward validate(Message messageToValidate, PipeLineSession session, boolean responseMode, String messageRoot) throws XmlValidatorException, PipeRunException, ConfigurationException {
    ValidationContext context = null;
    if (StringUtils.isNotEmpty(messageRoot)) {
        context = validator.createValidationContext(session, createRootValidation(messageRoot), getInvalidRootNamespaces());
    } else {
        context = validator.createValidationContext(session, getRootValidations(responseMode), getInvalidRootNamespaces());
    }
    ValidatorHandler validatorHandler = validator.getValidatorHandler(session, context);
    XMLFilterImpl storeRootFilter = StringUtils.isNotEmpty(getRootElementSessionKey()) ? new RootElementToSessionKeyFilter(session, getRootElementSessionKey(), getRootNamespaceSessionKey(), null) : null;
    if (storeRootFilter != null) {
        validatorHandler.setContentHandler(storeRootFilter);
    }
    String resultEvent = validator.validate(messageToValidate, session, getLogPrefix(session), validatorHandler, storeRootFilter, context);
    return determineForward(resultEvent, session, responseMode);
}
Also used : RootElementToSessionKeyFilter(nl.nn.adapterframework.xml.RootElementToSessionKeyFilter) ValidatorHandler(javax.xml.validation.ValidatorHandler) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) ValidationContext(nl.nn.adapterframework.validation.ValidationContext)

Example 4 with ValidationContext

use of nl.nn.adapterframework.validation.ValidationContext in project iaf by ibissource.

the class Json2XmlValidator method alignJson.

protected PipeRunResult alignJson(String messageToValidate, PipeLineSession session, boolean responseMode) throws PipeRunException, XmlValidatorException {
    ValidationContext context;
    ValidatorHandler validatorHandler;
    try {
        context = validator.createValidationContext(session, getJsonRootValidations(responseMode), getInvalidRootNamespaces());
        validatorHandler = validator.getValidatorHandler(session, context);
    } catch (ConfigurationException e) {
        throw new PipeRunException(this, "Cannot create ValidationContext", e);
    }
    String resultEvent;
    String out = null;
    try {
        Json2Xml aligner = new Json2Xml(validatorHandler, context.getXsModels(), isCompactJsonArrays(), getMessageRoot(responseMode), isStrictJsonArraySyntax());
        if (StringUtils.isNotEmpty(getTargetNamespace())) {
            aligner.setTargetNamespace(getTargetNamespace());
        }
        aligner.setDeepSearch(isDeepSearch());
        aligner.setErrorHandler(context.getErrorHandler());
        aligner.setFailOnWildcards(isFailOnWildcards());
        aligner.setIgnoreUndeclaredElements(isIgnoreUndeclaredElements());
        ParameterList parameterList = getParameterList();
        if (parameterList != null) {
            Map<String, Object> parametervalues = null;
            parametervalues = parameterList.getValues(new Message(messageToValidate), session).getValueMap();
            // remove parameters with null values, to support optional request parameters
            for (Iterator<String> it = parametervalues.keySet().iterator(); it.hasNext(); ) {
                String key = it.next();
                if (parametervalues.get(key) == null) {
                    it.remove();
                }
            }
            aligner.setOverrideValues(parametervalues);
        }
        JsonStructure jsonStructure = Json.createReader(new StringReader(messageToValidate)).read();
        // cannot build filter chain as usual backwardly, because it ends differently.
        // This will be fixed once an OutputStream can be provided to Xml2Json
        XMLFilterImpl sourceFilter = aligner;
        if (StringUtils.isNotEmpty(getRootElementSessionKey())) {
            XMLFilterImpl storeRootFilter = new RootElementToSessionKeyFilter(session, getRootElementSessionKey(), getRootNamespaceSessionKey(), null);
            aligner.setContentHandler(storeRootFilter);
            sourceFilter = storeRootFilter;
        }
        if (getOutputFormat(session, responseMode) == DocumentFormat.JSON) {
            Xml2Json xml2json = new Xml2Json(aligner, isCompactJsonArrays(), !isJsonWithRootElements());
            sourceFilter.setContentHandler(xml2json);
            aligner.startParse(jsonStructure);
            out = xml2json.toString();
        } else {
            XmlWriter xmlWriter = new XmlWriter();
            xmlWriter.setIncludeXmlDeclaration(true);
            ContentHandler handler = xmlWriter;
            if (isProduceNamespaceLessXml()) {
                handler = new NamespaceRemovingFilter(handler);
            }
            sourceFilter.setContentHandler(handler);
            aligner.startParse(jsonStructure);
            out = xmlWriter.toString();
        }
    } catch (Exception e) {
        resultEvent = validator.finalizeValidation(context, session, e);
    }
    resultEvent = validator.finalizeValidation(context, session, null);
    PipeForward forward = determineForward(resultEvent, session, responseMode);
    PipeRunResult result = new PipeRunResult(forward, out);
    return result;
}
Also used : RootElementToSessionKeyFilter(nl.nn.adapterframework.xml.RootElementToSessionKeyFilter) ValidatorHandler(javax.xml.validation.ValidatorHandler) Message(nl.nn.adapterframework.stream.Message) PipeForward(nl.nn.adapterframework.core.PipeForward) ContentHandler(org.xml.sax.ContentHandler) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException) ValidationContext(nl.nn.adapterframework.validation.ValidationContext) NamespaceRemovingFilter(nl.nn.adapterframework.xml.NamespaceRemovingFilter) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) PipeRunException(nl.nn.adapterframework.core.PipeRunException) StringReader(java.io.StringReader) ParameterList(nl.nn.adapterframework.parameters.ParameterList) JsonObject(javax.json.JsonObject) Xml2Json(nl.nn.adapterframework.align.Xml2Json) Json2Xml(nl.nn.adapterframework.align.Json2Xml) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) JsonStructure(javax.json.JsonStructure)

Example 5 with ValidationContext

use of nl.nn.adapterframework.validation.ValidationContext in project iaf by ibissource.

the class Json2XmlValidator method alignXml2Json.

protected PipeRunResult alignXml2Json(String messageToValidate, PipeLineSession session, boolean responseMode) throws XmlValidatorException, PipeRunException, ConfigurationException {
    ValidationContext context = validator.createValidationContext(session, getJsonRootValidations(responseMode), getInvalidRootNamespaces());
    ValidatorHandler validatorHandler = validator.getValidatorHandler(session, context);
    // Make sure to use Xerces' ValidatorHandlerImpl, otherwise casting below will fail.
    XmlAligner aligner = new XmlAligner(validatorHandler);
    Xml2Json xml2json = new Xml2Json(aligner, isCompactJsonArrays(), !isJsonWithRootElements());
    XMLFilterImpl handler = xml2json;
    if (StringUtils.isNotEmpty(getRootElementSessionKey())) {
        handler = new RootElementToSessionKeyFilter(session, getRootElementSessionKey(), getRootNamespaceSessionKey(), handler);
    }
    aligner.setContentHandler(handler);
    aligner.setErrorHandler(context.getErrorHandler());
    String resultEvent = validator.validate(messageToValidate, session, getLogPrefix(session), validatorHandler, xml2json, context);
    String out = xml2json.toString();
    PipeForward forward = determineForward(resultEvent, session, responseMode);
    PipeRunResult result = new PipeRunResult(forward, out);
    return result;
}
Also used : RootElementToSessionKeyFilter(nl.nn.adapterframework.xml.RootElementToSessionKeyFilter) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) XmlAligner(nl.nn.adapterframework.align.XmlAligner) ValidatorHandler(javax.xml.validation.ValidatorHandler) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) Xml2Json(nl.nn.adapterframework.align.Xml2Json) PipeForward(nl.nn.adapterframework.core.PipeForward) ValidationContext(nl.nn.adapterframework.validation.ValidationContext)

Aggregations

ValidationContext (nl.nn.adapterframework.validation.ValidationContext)5 ValidatorHandler (javax.xml.validation.ValidatorHandler)4 Xml2Json (nl.nn.adapterframework.align.Xml2Json)4 PipeForward (nl.nn.adapterframework.core.PipeForward)4 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)4 RootElementToSessionKeyFilter (nl.nn.adapterframework.xml.RootElementToSessionKeyFilter)3 XMLFilterImpl (org.xml.sax.helpers.XMLFilterImpl)3 StringReader (java.io.StringReader)2 JsonStructure (javax.json.JsonStructure)2 Json2Xml (nl.nn.adapterframework.align.Json2Xml)2 XmlAligner (nl.nn.adapterframework.align.XmlAligner)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 PipeRunException (nl.nn.adapterframework.core.PipeRunException)2 ParameterList (nl.nn.adapterframework.parameters.ParameterList)2 XmlValidatorException (nl.nn.adapterframework.validation.XmlValidatorException)2 IOException (java.io.IOException)1 JsonObject (javax.json.JsonObject)1 Source (javax.xml.transform.Source)1 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)1 Message (nl.nn.adapterframework.stream.Message)1