Search in sources :

Example 26 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class AbstractDBMediatorFactory method processStatements.

protected void processStatements(OMElement elem, AbstractDBMediator mediator) {
    Iterator iter = elem.getChildrenWithName(STMNT_Q);
    while (iter.hasNext()) {
        OMElement stmntElt = (OMElement) iter.next();
        Statement statement = new Statement(getValue(stmntElt, SQL_Q));
        Iterator paramIter = stmntElt.getChildrenWithName(PARAM_Q);
        while (paramIter.hasNext()) {
            OMElement paramElt = (OMElement) paramIter.next();
            String xpath = getAttribute(paramElt, ATT_EXPRN);
            String value = getAttribute(paramElt, ATT_VALUE);
            if (xpath != null || value != null) {
                SynapseXPath xp = null;
                if (xpath != null) {
                    try {
                        xp = SynapseXPathFactory.getSynapseXPath(paramElt, ATT_EXPRN);
                    } catch (JaxenException e) {
                        handleException("Invalid XPath specified for the source attribute : " + xpath);
                    }
                }
                statement.addParameter(value, xp, getAttribute(paramElt, ATT_TYPE));
            }
        }
        Iterator resultIter = stmntElt.getChildrenWithName(RESULT_Q);
        while (resultIter.hasNext()) {
            OMElement resultElt = (OMElement) resultIter.next();
            if (getAttribute(resultElt, ATT_NAME) == null || getAttribute(resultElt, ATT_COLUMN) == null) {
                handleException("Invalid result element found missing either name and column [DBLookup] mediator");
            }
            statement.addResult(getAttribute(resultElt, ATT_NAME), getAttribute(resultElt, ATT_COLUMN));
        }
        mediator.addStatement(statement);
    }
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) Statement(org.apache.synapse.mediators.db.Statement) JaxenException(org.jaxen.JaxenException) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement)

Example 27 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class AnnotatedCommandMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    AnnotatedCommandMediator pojoMediator = new AnnotatedCommandMediator();
    processAuditStatus(pojoMediator, elem);
    // Class name of the Command object should be present
    OMAttribute name = elem.getAttribute(ATT_NAME);
    if (name == null) {
        String msg = "The name of the actual POJO command implementation class" + " is a required attribute";
        log.error(msg);
        throw new SynapseException(msg);
    }
    // load the class for the command object
    try {
        pojoMediator.setCommand(getClass().getClassLoader().loadClass(name.getAttributeValue()));
    } catch (ClassNotFoundException e) {
        handleException("Unable to load the class specified as the command " + name.getAttributeValue(), e);
    }
    // at the mediation time
    for (Iterator it = elem.getChildElements(); it.hasNext(); ) {
        OMElement child = (OMElement) it.next();
        if ("property".equals(child.getLocalName())) {
            String propName = child.getAttribute(ATT_NAME).getAttributeValue();
            if (propName == null) {
                handleException("A POJO command mediator property must specify the name attribute");
            } else {
                if (child.getAttribute(ATT_EXPRN) != null) {
                    SynapseXPath xpath;
                    try {
                        xpath = SynapseXPathFactory.getSynapseXPath(child, ATT_EXPRN);
                        pojoMediator.addMessageSetterProperty(propName, xpath);
                    } catch (JaxenException e) {
                        handleException("Error instantiating XPath expression : " + child.getAttribute(ATT_EXPRN), e);
                    }
                } else {
                    if (child.getAttribute(ATT_VALUE) != null) {
                        pojoMediator.addStaticSetterProperty(propName, child.getAttribute(ATT_VALUE).getAttributeValue());
                    } else {
                        handleException("A POJO mediator property must specify either " + "name and expression attributes, or name and value attributes");
                    }
                }
            }
        }
    }
    return pojoMediator;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SynapseException(org.apache.synapse.SynapseException) JaxenException(org.jaxen.JaxenException) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) AnnotatedCommandMediator(org.apache.synapse.mediators.ext.AnnotatedCommandMediator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 28 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath 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 29 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class XQueryMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof XQueryMediator)) {
        handleException("Invalid Mediator has passed to serializer");
    }
    XQueryMediator queryMediator = (XQueryMediator) m;
    OMElement xquery = fac.createOMElement("xquery", synNS);
    Value key = queryMediator.getQueryKey();
    if (key != null) {
        // Serialize Key using KeySerializer
        ValueSerializer keySerializer = new ValueSerializer();
        keySerializer.serializeValue(key, XMLConfigConstants.KEY, xquery);
    }
    saveTracingState(xquery, queryMediator);
    SynapseXPath targetXPath = queryMediator.getTarget();
    if (targetXPath != null && !SourceXPathSupport.DEFAULT_XPATH.equals(targetXPath.toString())) {
        SynapseXPathSerializer.serializeXPath(targetXPath, xquery, "target");
    }
    List<MediatorProperty> pros = queryMediator.getProcessorProperties();
    if (pros != null && !pros.isEmpty()) {
        OMElement dataSource = fac.createOMElement("dataSource", synNS);
        serializeProperties(dataSource, pros);
        xquery.addChild(dataSource);
    }
    List list = queryMediator.getVariables();
    if (list != null && !list.isEmpty()) {
        for (Object o : list) {
            if (o instanceof MediatorBaseVariable) {
                MediatorBaseVariable variable = (MediatorBaseVariable) o;
                QName name = variable.getName();
                Object value = variable.getValue();
                if (name != null && value != null) {
                    OMElement baseElement = fac.createOMElement("variable", synNS);
                    baseElement.addAttribute(fac.createOMAttribute("name", nullNS, name.getLocalPart()));
                    baseElement.addAttribute(fac.createOMAttribute("value", nullNS, (String) value));
                    String type = null;
                    ItemType variableType = variable.getType();
                    XdmNodeKind nodeKind = variable.getNodeKind();
                    if (ItemType.INT == variableType) {
                        type = "INT";
                    } else if (ItemType.INTEGER == variableType) {
                        type = "INTEGER";
                    } else if (ItemType.BOOLEAN == variableType) {
                        type = "BOOLEAN";
                    } else if (ItemType.BYTE == variableType) {
                        type = "BYTE";
                    } else if (ItemType.DOUBLE == variableType) {
                        type = "DOUBLE";
                    } else if (ItemType.SHORT == variableType) {
                        type = "SHORT";
                    } else if (ItemType.LONG == variableType) {
                        type = "LONG";
                    } else if (ItemType.FLOAT == variableType) {
                        type = "FLOAT";
                    } else if (ItemType.STRING == variableType) {
                        type = "STRING";
                    } else if (XdmNodeKind.DOCUMENT == nodeKind) {
                        type = "DOCUMENT";
                    } else if (XdmNodeKind.ELEMENT == nodeKind) {
                        type = "ELEMENT";
                    } else {
                        handleException("Unknown Type " + variableType);
                    }
                    if (type != null) {
                        baseElement.addAttribute(fac.createOMAttribute("type", nullNS, type));
                    }
                    xquery.addChild(baseElement);
                }
            } else if (o instanceof MediatorCustomVariable) {
                MediatorCustomVariable variable = (MediatorCustomVariable) o;
                QName name = variable.getName();
                if (name != null) {
                    OMElement customElement = fac.createOMElement("variable", synNS);
                    customElement.addAttribute(fac.createOMAttribute("name", nullNS, name.getLocalPart()));
                    String regkey = variable.getRegKey();
                    if (regkey != null) {
                        customElement.addAttribute(fac.createOMAttribute("key", nullNS, regkey));
                    }
                    SynapseXPath expression = variable.getExpression();
                    if (expression != null && !SourceXPathSupport.DEFAULT_XPATH.equals(expression.toString())) {
                        SynapseXPathSerializer.serializeXPath(expression, customElement, "expression");
                    }
                    String type = null;
                    ItemType variableType = variable.getType();
                    XdmNodeKind nodeKind = variable.getNodeKind();
                    if (XdmNodeKind.DOCUMENT == nodeKind) {
                        type = "DOCUMENT";
                    } else if (XdmNodeKind.ELEMENT == nodeKind) {
                        type = "ELEMENT";
                    } else if (ItemType.INT == variableType) {
                        type = "INT";
                    } else if (ItemType.INTEGER == variableType) {
                        type = "INTEGER";
                    } else if (ItemType.BOOLEAN == variableType) {
                        type = "BOOLEAN";
                    } else if (ItemType.BYTE == variableType) {
                        type = "BYTE";
                    } else if (ItemType.DOUBLE == variableType) {
                        type = "DOUBLE";
                    } else if (ItemType.SHORT == variableType) {
                        type = "SHORT";
                    } else if (ItemType.LONG == variableType) {
                        type = "LONG";
                    } else if (ItemType.FLOAT == variableType) {
                        type = "FLOAT";
                    } else if (ItemType.STRING == variableType) {
                        type = "STRING";
                    } else {
                        handleException("Unknown Type " + variableType);
                    }
                    if (type != null) {
                        customElement.addAttribute(fac.createOMAttribute("type", nullNS, type));
                    }
                    xquery.addChild(customElement);
                }
            }
        }
    }
    return xquery;
}
Also used : XdmNodeKind(net.sf.saxon.s9api.XdmNodeKind) SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) QName(javax.xml.namespace.QName) ItemType(net.sf.saxon.s9api.ItemType) OMElement(org.apache.axiom.om.OMElement) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) Value(org.apache.synapse.mediators.Value) List(java.util.List) ValueSerializer(org.apache.synapse.config.xml.ValueSerializer)

Example 30 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class SynapseConfigUtils method setDefaultFaultSequence.

/**
 * Return the fault sequence if one is not defined. This implementation defaults to
 * a simple sequence :
 * <log level="full">
 *   <property name="MESSAGE" value="Executing default "fault" sequence"/>
 *   <property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
 *   <property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
 * </log>
 * <drop/>
 *
 * @param config the configuration to be updated
 */
public static void setDefaultFaultSequence(SynapseConfiguration config) {
    SequenceMediator fault = new SequenceMediator();
    fault.setName(org.apache.synapse.SynapseConstants.FAULT_SEQUENCE_KEY);
    LogMediator log = new LogMediator();
    log.setLogLevel(LogMediator.FULL);
    MediatorProperty mp = new MediatorProperty();
    mp.setName("MESSAGE");
    mp.setValue("Executing default \"fault\" sequence");
    log.addProperty(mp);
    mp = new MediatorProperty();
    mp.setName("ERROR_CODE");
    try {
        mp.setExpression(new SynapseXPath("get-property('ERROR_CODE')"));
    } catch (JaxenException ignore) {
    }
    log.addProperty(mp);
    mp = new MediatorProperty();
    mp.setName("ERROR_MESSAGE");
    try {
        mp.setExpression(new SynapseXPath("get-property('ERROR_MESSAGE')"));
    } catch (JaxenException ignore) {
    }
    log.addProperty(mp);
    fault.addChild(log);
    fault.addChild(new DropMediator());
    // set aspect configuration
    AspectConfiguration configuration = new AspectConfiguration(fault.getName());
    fault.configure(configuration);
    config.addSequence(org.apache.synapse.SynapseConstants.FAULT_SEQUENCE_KEY, fault);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) JaxenException(org.jaxen.JaxenException) LogMediator(org.apache.synapse.mediators.builtin.LogMediator) DropMediator(org.apache.synapse.mediators.builtin.DropMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration)

Aggregations

SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)68 MessageContext (org.apache.synapse.MessageContext)24 JaxenException (org.jaxen.JaxenException)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)15 OMElement (org.apache.axiom.om.OMElement)14 OMAttribute (org.apache.axiom.om.OMAttribute)11 Value (org.apache.synapse.mediators.Value)9 TestMessageContext (org.apache.synapse.TestMessageContext)8 Pattern (java.util.regex.Pattern)6 Iterator (java.util.Iterator)5 QName (javax.xml.namespace.QName)5 EndpointReference (org.apache.axis2.addressing.EndpointReference)4 SynapseException (org.apache.synapse.SynapseException)4 TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)4 SynapseJsonPath (org.apache.synapse.util.xpath.SynapseJsonPath)4 Method (java.lang.reflect.Method)3 Map (java.util.Map)3 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)3 Field (java.lang.reflect.Field)2 List (java.util.List)2