Search in sources :

Example 1 with ValueFactory

use of org.apache.synapse.config.xml.ValueFactory in project wso2-synapse by wso2.

the class ScriptMediatorFactory method getIncludeKeysMap.

private Map<Value, Object> getIncludeKeysMap(OMElement elem) {
    // get <include /> scripts
    // map key = registry entry key, value = script source
    // at this time map values are null, later loaded
    // from void ScriptMediator.prepareExternalScript(MessageContext synCtx)
    // TreeMap used to keep given scripts order if needed
    Map<Value, Object> includeKeysMap = new LinkedHashMap<Value, Object>();
    Iterator itr = elem.getChildrenWithName(INCLUDE_Q);
    while (itr.hasNext()) {
        OMElement includeElem = (OMElement) itr.next();
        OMAttribute key = includeElem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key"));
        // 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, includeElem);
        if (key == null) {
            throw new SynapseException("Cannot use 'include' element without 'key'" + " attribute for a script mediator");
        }
        includeKeysMap.put(generatedKey, null);
    }
    return includeKeysMap;
}
Also used : SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) Value(org.apache.synapse.mediators.Value) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) ValueFactory(org.apache.synapse.config.xml.ValueFactory) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 2 with ValueFactory

use of org.apache.synapse.config.xml.ValueFactory in project wso2-synapse by wso2.

the class XQueryMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    XQueryMediator xQueryMediator = new XQueryMediator();
    OMAttribute xqueryKey = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key"));
    OMAttribute attrTarget = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "target"));
    if (xqueryKey != null) {
        // KeyFactory for creating dynamic or static Key
        ValueFactory keyFac = new ValueFactory();
        // create dynamic or static key based on OMElement
        Value generatedKey = keyFac.createValue(XMLConfigConstants.KEY, elem);
        if (generatedKey != null) {
            // set generated key as the Key
            xQueryMediator.setQueryKey(generatedKey);
        } else {
            handleException("The 'key' attribute is required for the XQuery mediator");
        }
    } else {
        handleException("The 'key' attribute is required for the XQuery mediator");
    }
    if (attrTarget != null) {
        String targetValue = attrTarget.getAttributeValue();
        if (targetValue != null && !"".equals(targetValue)) {
            try {
                xQueryMediator.setTarget(SynapseXPathFactory.getSynapseXPath(elem, ATT_TARGET));
            } catch (JaxenException e) {
                handleException("Invalid XPath specified for the target attribute : " + targetValue);
            }
        }
    }
    // after successfully creating the mediator
    // set its common attributes such as tracing etc
    processAuditStatus(xQueryMediator, elem);
    OMElement dataSource = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "dataSource"));
    if (dataSource != null) {
        xQueryMediator.addAllDataSourceProperties(MediatorPropertyFactory.getMediatorProperties(dataSource));
    }
    Iterator it = elem.getChildrenWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "variable"));
    while (it.hasNext()) {
        OMElement variableOM = (OMElement) it.next();
        String name = variableOM.getAttributeValue(ATT_NAME_Q);
        if (name != null && !"".equals(name)) {
            String type = variableOM.getAttributeValue(ATT_TYPE_Q);
            if (type != null && !"".equals(type)) {
                String value = variableOM.getAttributeValue(ATT_VALUE_Q);
                MediatorVariable variable;
                if (value != null && !"".equals(value)) {
                    variable = new MediatorBaseVariable(new QName(name.trim()));
                    variable.setValue(value.trim());
                } else {
                    String key = variableOM.getAttributeValue(ATT_KEY_Q);
                    String expr = variableOM.getAttributeValue(ATT_EXPR_Q);
                    variable = new MediatorCustomVariable(new QName(name.trim()));
                    if (key != null) {
                        ((MediatorCustomVariable) variable).setRegKey(key.trim());
                    }
                    if (expr != null && !"".equals(expr)) {
                        try {
                            SynapseXPath xpath = new SynapseXPath(expr);
                            OMElementUtils.addNameSpaces(xpath, variableOM, log);
                            ((MediatorCustomVariable) variable).setExpression(xpath);
                        } catch (JaxenException e) {
                            handleException("Invalid XPath specified for" + " the expression attribute : " + expr);
                        }
                    }
                }
                if ("INT".equals(type.trim())) {
                    variable.setType(ItemType.INT);
                } else if ("INTEGER".equals(type.trim())) {
                    variable.setType(ItemType.INTEGER);
                } else if ("BOOLEAN".equals(type.trim())) {
                    variable.setType(ItemType.BOOLEAN);
                } else if ("BYTE".equals(type.trim())) {
                    variable.setType(ItemType.BYTE);
                } else if ("DOUBLE".equals(type.trim())) {
                    variable.setType(ItemType.DOUBLE);
                } else if ("SHORT".equals(type.trim())) {
                    variable.setType(ItemType.SHORT);
                } else if ("LONG".equals(type.trim())) {
                    variable.setType(ItemType.LONG);
                } else if ("FLOAT".equals(type.trim())) {
                    variable.setType(ItemType.FLOAT);
                } else if ("STRING".equals(type.trim())) {
                    variable.setType(ItemType.STRING);
                } else if ("DOCUMENT".equals(type.trim())) {
                    variable.setNodeKind(XdmNodeKind.DOCUMENT);
                } else if ("ELEMENT".equals(type.trim())) {
                    variable.setNodeKind(XdmNodeKind.ELEMENT);
                } else {
                    handleException("Unsupported Type");
                }
                xQueryMediator.addVariable(variable);
            }
        }
    }
    return xQueryMediator;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) ValueFactory(org.apache.synapse.config.xml.ValueFactory) JaxenException(org.jaxen.JaxenException) Value(org.apache.synapse.mediators.Value) Iterator(java.util.Iterator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 3 with ValueFactory

use of org.apache.synapse.config.xml.ValueFactory in project wso2-synapse by wso2.

the class RecipientListEndpointFactory method createEndpoint.

@Override
protected Endpoint createEndpoint(OMElement epConfig, boolean anonymousEndpoint, Properties properties) {
    OMElement recipientListElement = epConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "recipientlist"));
    if (recipientListElement != null) {
        // create endpoint
        RecipientListEndpoint recipientListEndpoint = new RecipientListEndpoint();
        // set endpoint name
        OMAttribute name = epConfig.getAttribute(new QName(org.apache.synapse.config.xml.XMLConfigConstants.NULL_NAMESPACE, "name"));
        if (name != null) {
            recipientListEndpoint.setName(name.getAttributeValue());
        }
        // set endpoints or members
        if (recipientListElement.getFirstChildWithName(XMLConfigConstants.ENDPOINT_ELT) != null) {
            if (recipientListElement.getChildrenWithName((MEMBER)).hasNext()) {
                String msg = "Invalid Synapse configuration. " + "child elements";
                log.error(msg);
                throw new SynapseException(msg);
            }
            List<Endpoint> endpoints = getEndpoints(recipientListElement, recipientListEndpoint, properties);
            recipientListEndpoint.setChildren(endpoints);
        } else if (recipientListElement.getFirstChildWithName(MEMBER) != null) {
            if (recipientListElement.getChildrenWithName((XMLConfigConstants.ENDPOINT_ELT)).hasNext()) {
                String msg = "Invalid Synapse configuration. " + "recipientListElement element cannot have both member & endpoint " + "child elements";
                log.error(msg);
                throw new SynapseException(msg);
            }
            List<Member> members = getMembers(recipientListElement);
            recipientListEndpoint.setMembers(members);
        } else if (recipientListElement.getFirstChildWithName(DYNAMIC_SET) != null) {
            OMElement dynamicSetElement = recipientListElement.getFirstChildWithName(DYNAMIC_SET);
            Value dynamicEndpointSet = new ValueFactory().createValue("value", dynamicSetElement);
            String maxStr = dynamicSetElement.getAttributeValue(new QName("max-cache"));
            int maxCache = -1;
            try {
                maxCache = Integer.parseInt(maxStr);
            } catch (NumberFormatException e) {
            }
            recipientListEndpoint = new RecipientListEndpoint(maxCache < 0 ? RecipientListEndpoint.DEFAULT_MAX_POOL : maxCache);
            if (name != null) {
                recipientListEndpoint.setName(name.getAttributeValue());
            }
            recipientListEndpoint.setDynamicEnpointSet(dynamicEndpointSet);
        }
        if (recipientListEndpoint.getChildren() == null && recipientListEndpoint.getMembers() == null && recipientListEndpoint.getDynamicEnpointSet() == null) {
            String msg = "Invalid Synapse configuration.\n" + "A RecipientListEndpoint must have child/member elements, but the RecipientListEndpoint " + "'" + recipientListEndpoint.getName() + "' does not have any child/member/dynamic endpoint elements.";
            log.error(msg);
            throw new SynapseException(msg);
        }
        // process the parameters
        processProperties(recipientListEndpoint, epConfig);
        return recipientListEndpoint;
    }
    return null;
}
Also used : SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) ValueFactory(org.apache.synapse.config.xml.ValueFactory) Endpoint(org.apache.synapse.endpoints.Endpoint) RecipientListEndpoint(org.apache.synapse.endpoints.RecipientListEndpoint) RecipientListEndpoint(org.apache.synapse.endpoints.RecipientListEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) RecipientListEndpoint(org.apache.synapse.endpoints.RecipientListEndpoint) Value(org.apache.synapse.mediators.Value) ArrayList(java.util.ArrayList) List(java.util.List) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 4 with ValueFactory

use of org.apache.synapse.config.xml.ValueFactory in project wso2-synapse by wso2.

the class TemplateMessageExecutor method buildParameters.

private void buildParameters(OMElement elem) {
    Iterator subElements = elem.getChildElements();
    while (subElements.hasNext()) {
        OMElement child = (OMElement) subElements.next();
        Value paramValue = new ValueFactory().createTextValue(child);
        invoker.addExpressionForParamName(child.getLocalName(), paramValue);
    }
}
Also used : Iterator(java.util.Iterator) Value(org.apache.synapse.mediators.Value) OMElement(org.apache.axiom.om.OMElement) ValueFactory(org.apache.synapse.config.xml.ValueFactory)

Example 5 with ValueFactory

use of org.apache.synapse.config.xml.ValueFactory in project wso2-synapse by wso2.

the class ScriptMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    ScriptMediator mediator;
    ClassLoader classLoader = (ClassLoader) properties.get(SynapseConstants.SYNAPSE_LIB_LOADER);
    OMAttribute keyAtt = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key"));
    OMAttribute langAtt = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "language"));
    OMAttribute functionAtt = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "function"));
    if (langAtt == null) {
        throw new SynapseException("The 'language' attribute is required for" + " a script mediator");
    // TODO: couldn't this be determined from the key in some scenarios?
    }
    if (keyAtt == null && functionAtt != null) {
        throw new SynapseException("Cannot use 'function' attribute without 'key' " + "attribute for a script mediator");
    }
    Map<Value, Object> includeKeysMap = getIncludeKeysMap(elem);
    if (keyAtt != null) {
        // ValueFactory for creating dynamic or static Key
        ValueFactory keyFac = new ValueFactory();
        // create dynamic or static key based on OMElement
        Value generatedKey = keyFac.createValue(XMLConfigConstants.KEY, elem);
        String functionName = (functionAtt == null ? null : functionAtt.getAttributeValue());
        mediator = new ScriptMediator(langAtt.getAttributeValue(), includeKeysMap, generatedKey, functionName, classLoader);
    } else {
        mediator = new ScriptMediator(langAtt.getAttributeValue(), elem.getText(), classLoader);
    }
    processAuditStatus(mediator, elem);
    return mediator;
}
Also used : SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) Value(org.apache.synapse.mediators.Value) ValueFactory(org.apache.synapse.config.xml.ValueFactory) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

ValueFactory (org.apache.synapse.config.xml.ValueFactory)5 Value (org.apache.synapse.mediators.Value)5 QName (javax.xml.namespace.QName)4 OMAttribute (org.apache.axiom.om.OMAttribute)4 OMElement (org.apache.axiom.om.OMElement)4 Iterator (java.util.Iterator)3 SynapseException (org.apache.synapse.SynapseException)3 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Endpoint (org.apache.synapse.endpoints.Endpoint)1 RecipientListEndpoint (org.apache.synapse.endpoints.RecipientListEndpoint)1 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)1 JaxenException (org.jaxen.JaxenException)1