Search in sources :

Example 1 with ValidateMediator

use of org.apache.synapse.mediators.builtin.ValidateMediator in project wso2-synapse by wso2.

the class ValidateMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    ValidateMediator validateMediator = new ValidateMediator();
    // process schema element definitions and create DynamicProperties
    List<Value> schemaKeys = new ArrayList<Value>();
    Iterator schemas = elem.getChildrenWithName(SCHEMA_Q);
    while (schemas.hasNext()) {
        Object o = schemas.next();
        if (o instanceof OMElement) {
            OMElement omElem = (OMElement) o;
            OMAttribute keyAtt = omElem.getAttribute(ATT_KEY);
            if (keyAtt != null) {
                // ValueFactory for creating dynamic or static Value
                ValueFactory keyFac = new ValueFactory();
                // create dynamic or static key based on OMElement
                Value generatedKey = keyFac.createValue(XMLConfigConstants.KEY, omElem);
                schemaKeys.add(generatedKey);
            } else {
                handleException("A 'schema' definition must contain a local property 'key'");
            }
        } else {
            handleException("Invalid 'schema' declaration for validate mediator");
        }
    }
    if (schemaKeys.size() == 0) {
        handleException("No schema specified for the validate mediator");
    } else {
        validateMediator.setSchemaKeys(schemaKeys);
    }
    // process source XPath attribute if present
    OMAttribute attSource = elem.getAttribute(ATT_SOURCE);
    if (attSource != null) {
        try {
            if (attSource.getAttributeValue() != null) {
                validateMediator.setSource(SynapsePathFactory.getSynapsePath(elem, ATT_SOURCE));
            }
        } catch (JaxenException e) {
            handleException("Invalid XPath expression specified for attribute 'source'", e);
        }
    }
    // process schema cacheability.
    OMAttribute attSchemaCache = elem.getAttribute(ATT_CACHE_SCHEMA);
    if (attSchemaCache != null) {
        final boolean cacheSchema = Boolean.parseBoolean(attSchemaCache.getAttributeValue());
        if (log.isDebugEnabled()) {
            log.debug("Schema cached: " + cacheSchema);
        }
        validateMediator.setCacheSchema(cacheSchema);
    }
    // process external schema resources
    validateMediator.setResourceMap(ResourceMapFactory.createResourceMap(elem));
    // process on-fail
    OMElement onFail = null;
    Iterator iterator = elem.getChildrenWithName(ON_FAIL_Q);
    if (iterator.hasNext()) {
        onFail = (OMElement) iterator.next();
    }
    if (onFail != null && onFail.getChildElements().hasNext()) {
        addChildren(onFail, validateMediator, properties);
    } else {
        handleException("A non-empty <on-fail> child element is required for " + "the <validate> mediator");
    }
    // after successfully creating the mediator
    // set its common attributes such as tracing etc
    processAuditStatus(validateMediator, elem);
    // set the features
    for (Map.Entry<String, String> entry : collectNameValuePairs(elem, FEATURE_Q).entrySet()) {
        String value = entry.getValue();
        boolean isFeatureEnabled;
        if ("true".equals(value)) {
            isFeatureEnabled = true;
        } else if ("false".equals(value)) {
            isFeatureEnabled = false;
        } else {
            handleException("The feature must have value true or false");
            break;
        }
        try {
            validateMediator.addFeature(entry.getKey(), isFeatureEnabled);
        } catch (SAXException e) {
            handleException("Error setting validation feature : " + entry.getKey() + " to : " + value, e);
        }
    }
    addAllCommentChildrenToMediator(elem, validateMediator);
    return validateMediator;
}
Also used : ValidateMediator(org.apache.synapse.mediators.builtin.ValidateMediator) OMElement(org.apache.axiom.om.OMElement) SAXException(org.xml.sax.SAXException) JaxenException(org.jaxen.JaxenException) Value(org.apache.synapse.mediators.Value) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 2 with ValidateMediator

use of org.apache.synapse.mediators.builtin.ValidateMediator in project wso2-synapse by wso2.

the class ValidateMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof ValidateMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    ValidateMediator mediator = (ValidateMediator) m;
    OMElement validate = fac.createOMElement("validate", synNS);
    saveTracingState(validate, mediator);
    if (mediator.getSource() != null) {
        SynapsePathSerializer.serializePath(mediator.getSource(), validate, "source");
    }
    for (Value key : mediator.getSchemaKeys()) {
        OMElement schema = fac.createOMElement("schema", synNS, validate);
        // Serialize Value using ValueSerializer
        ValueSerializer keySerializer = new ValueSerializer();
        keySerializer.serializeValue(key, XMLConfigConstants.KEY, schema);
    }
    ResourceMapSerializer.serializeResourceMap(validate, mediator.getResourceMap());
    List<MediatorProperty> features = mediator.getFeatures();
    if (!features.isEmpty()) {
        for (MediatorProperty mp : features) {
            OMElement feature = fac.createOMElement("feature", synNS, validate);
            if (mp.getName() != null) {
                feature.addAttribute(fac.createOMAttribute("name", nullNS, mp.getName()));
            } else {
                handleException("The Feature name is missing");
            }
            if (mp.getValue() != null) {
                feature.addAttribute(fac.createOMAttribute("value", nullNS, mp.getValue()));
            } else {
                handleException("The Feature value is missing");
            }
        }
    }
    OMElement onFail = fac.createOMElement("on-fail", synNS, validate);
    serializeChildren(onFail, mediator.getList());
    OMAttribute cacheSchemaAtt = fac.createOMAttribute("cache-schema", nullNS, String.valueOf(mediator.isCacheSchema()));
    validate.addAttribute(cacheSchemaAtt);
    return validate;
}
Also used : MediatorProperty(org.apache.synapse.mediators.MediatorProperty) ValidateMediator(org.apache.synapse.mediators.builtin.ValidateMediator) Value(org.apache.synapse.mediators.Value) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

OMAttribute (org.apache.axiom.om.OMAttribute)2 OMElement (org.apache.axiom.om.OMElement)2 Value (org.apache.synapse.mediators.Value)2 ValidateMediator (org.apache.synapse.mediators.builtin.ValidateMediator)2 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)1 JaxenException (org.jaxen.JaxenException)1 SAXException (org.xml.sax.SAXException)1