Search in sources :

Example 1 with XpathNamespace

use of org.talend.esb.policy.correlation.impl.xpath.XpathNamespace in project tesb-rt-se by Talend.

the class XPathProcessor method processJXpathParts.

private Map<String, String> processJXpathParts(List<XpathPart> parts, List<XpathNamespace> namespaces, Node body) {
    Map<String, String> resultMap = new HashMap<String, String>();
    JXPathContext messageContext = JXPathContext.newContext(body);
    if (namespaces != null) {
        for (XpathNamespace namespace : namespaces) {
            String prefix = namespace.getPrefix();
            String uri = namespace.getUri();
            if (null != uri && null != prefix) {
                messageContext.registerNamespace(prefix, uri);
            }
        }
    }
    for (XpathPart part : parts) {
        try {
            JXPathContext.compile(part.getXpath());
        } catch (JXPathException ex) {
            throw new RuntimeException("Validation of XPATH expression" + "{ name: " + part.getName() + "; xpath: " + part.getXpath() + " } failed", ex);
        }
        try {
            Object val = messageContext.getValue(part.getXpath());
            String result = (val == null) ? null : val.toString();
            resultMap.put(part.getXpath(), val.toString());
            if ((result == null || result.isEmpty()) && !part.isOptional()) {
                throw new RuntimeException("Can not evaluate Xpath expression" + "{ name: " + part.getName() + "; xpath: " + part.getXpath() + " }");
            }
        } catch (RuntimeException ex) {
            if (!part.isOptional()) {
                throw new RuntimeException("Evaluation of XPATH expression" + "{ name: " + part.getName() + "; xpath: " + part.getXpath() + " } failed", ex);
            }
        }
    }
    return resultMap;
}
Also used : XpathNamespace(org.talend.esb.policy.correlation.impl.xpath.XpathNamespace) JXPathContext(org.apache.commons.jxpath.JXPathContext) HashMap(java.util.HashMap) XpathPart(org.talend.esb.policy.correlation.impl.xpath.XpathPart) JXPathException(org.apache.commons.jxpath.JXPathException)

Example 2 with XpathNamespace

use of org.talend.esb.policy.correlation.impl.xpath.XpathNamespace in project tesb-rt-se by Talend.

the class XPathProcessor method getCorrelationID.

public String getCorrelationID() {
    CorrelationIDAssertion cAssertion = null;
    if (!(assertion instanceof CorrelationIDAssertion)) {
        throw new RuntimeException("Can not find correlation assertion");
    }
    cAssertion = (CorrelationIDAssertion) assertion;
    Node body = getSoapBody(message);
    if (body == null) {
        throw new RuntimeException("SoapBody elements are not found in soap message");
    }
    List<XpathPart> parts = cAssertion.getCorrelationParts();
    if (parts == null || parts.isEmpty())
        return null;
    List<XpathNamespace> namespaces = cAssertion.getCorrelationNamespaces();
    Map<String, String> res = processJXpathParts(parts, namespaces, body);
    return buildCorrelationIdFromXpathParts(parts, cAssertion.getCorrelationName(), res);
}
Also used : XpathNamespace(org.talend.esb.policy.correlation.impl.xpath.XpathNamespace) Node(org.w3c.dom.Node) XpathPart(org.talend.esb.policy.correlation.impl.xpath.XpathPart)

Example 3 with XpathNamespace

use of org.talend.esb.policy.correlation.impl.xpath.XpathNamespace in project tesb-rt-se by Talend.

the class CorrelationIdAssertionTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    // $NON-NLS-1$
    outputFactory.setProperty(// $NON-NLS-1$
    "javax.xml.stream.isRepairingNamespaces", Boolean.TRUE);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(baos, "UTF-8");
    CorrelationIDAssertion cia = new CorrelationIDAssertion(generateStubElement());
    XpathNamespace xns = new XpathNamespace();
    xns.setPrefix("Prefix");
    xns.setUri("uri");
    cia.addNamespace(xns);
    XpathPart xp = new XpathPart();
    xp.setName("Name");
    xp.setOptional(false);
    xp.setXpath("Xpath");
    cia.addXpathPart(xp);
    cia.setCorrelationName("correlationName");
    cia.serialize(writer);
    writer.flush();
    writer.close();
    assertTrue(baos.toString().equalsIgnoreCase("<tpa:CorrelationID xmlns:tpa=\"http://types.talend.com/policy/assertion/1.0\" type=\"callback\" name=\"correlationName\"><tpa:Part name=\"Name\" xpath=\"Xpath\"/><tpa:Namespace prefix=\"Prefix\" uri=\"uri\"/></tpa:CorrelationID>"));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XpathNamespace(org.talend.esb.policy.correlation.impl.xpath.XpathNamespace) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XpathPart(org.talend.esb.policy.correlation.impl.xpath.XpathPart) CorrelationIDAssertion(org.talend.esb.policy.correlation.impl.CorrelationIDAssertion) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 4 with XpathNamespace

use of org.talend.esb.policy.correlation.impl.xpath.XpathNamespace in project tesb-rt-se by Talend.

the class CorrelationIDAssertion method addNamespace.

public final void addNamespace(String prefix, String uri) {
    XpathNamespace namespace = new XpathNamespace();
    namespace.setPrefix(prefix);
    namespace.setUri(uri);
    namespaces.add(namespace);
}
Also used : XpathNamespace(org.talend.esb.policy.correlation.impl.xpath.XpathNamespace)

Example 5 with XpathNamespace

use of org.talend.esb.policy.correlation.impl.xpath.XpathNamespace in project tesb-rt-se by Talend.

the class CorrelationIDAssertion method serialize.

@Override
public void serialize(XMLStreamWriter writer) throws XMLStreamException {
    String prefix = writer.getPrefix(CorrelationIDPolicyBuilder.NAMESPACE);
    if (prefix == null) {
        prefix = "tpa";
        writer.setPrefix(prefix, CorrelationIDPolicyBuilder.NAMESPACE);
    }
    // <tpa:CorrelationID>
    writer.writeStartElement(prefix, CorrelationIDPolicyBuilder.CORRELATION_ID_NAME, CorrelationIDPolicyBuilder.NAMESPACE);
    // xmlns:tpa="http://types.talend.com/policy/assertion/1.0"
    writer.writeNamespace(prefix, CorrelationIDPolicyBuilder.NAMESPACE);
    // attributes
    writer.writeAttribute(null, "type", methodType.name().toLowerCase());
    if (correlationName != null) {
        writer.writeAttribute(null, CORRELATION_NAME_ATTRIBUTE_NAME, correlationName);
    }
    if (parts != null && !parts.isEmpty()) {
        for (XpathPart part : parts) {
            // <tpa:Part>
            writer.writeStartElement(prefix, XpathPart.XPATH_PART_NODE_NAME, CorrelationIDPolicyBuilder.NAMESPACE);
            // xmlns:tpa="http://types.talend.com/policy/assertion/1.0"
            writer.writeNamespace(prefix, CorrelationIDPolicyBuilder.NAMESPACE);
            // part attribute name
            writer.writeAttribute(null, XpathPart.PART_NAME_ATTRIBUTE, part.getName());
            // part attribute xpath
            writer.writeAttribute(null, XpathPart.PART_XPATH_ATTRIBUTE, part.getXpath());
            // </tpa:Part>
            writer.writeEndElement();
        }
    }
    if (namespaces != null && !namespaces.isEmpty()) {
        for (XpathNamespace namespace : namespaces) {
            // <tpa:Namespace>
            writer.writeStartElement(prefix, XpathNamespace.XPATH_NAMESPACE_NODE_NAME, CorrelationIDPolicyBuilder.NAMESPACE);
            // xmlns:tpa="http://types.talend.com/policy/assertion/1.0"
            writer.writeNamespace(prefix, CorrelationIDPolicyBuilder.NAMESPACE);
            // name space prefix
            writer.writeAttribute(null, XpathNamespace.PREFIX_ATTRIBUTE, namespace.getPrefix());
            // name space uri
            writer.writeAttribute(null, XpathNamespace.URI_ATTRIBUTE, namespace.getUri());
            // </tpa:Namespace>
            writer.writeEndElement();
        }
    }
    // </tpa:SchemaValidation>
    writer.writeEndElement();
}
Also used : XpathNamespace(org.talend.esb.policy.correlation.impl.xpath.XpathNamespace) XpathPart(org.talend.esb.policy.correlation.impl.xpath.XpathPart)

Aggregations

XpathNamespace (org.talend.esb.policy.correlation.impl.xpath.XpathNamespace)5 XpathPart (org.talend.esb.policy.correlation.impl.xpath.XpathPart)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 JXPathContext (org.apache.commons.jxpath.JXPathContext)1 JXPathException (org.apache.commons.jxpath.JXPathException)1 Test (org.junit.Test)1 CorrelationIDAssertion (org.talend.esb.policy.correlation.impl.CorrelationIDAssertion)1 Node (org.w3c.dom.Node)1