public MediatorCustomVariable(QName name) { super(name); // create the default XPath try { this.expression = new SynapseXPath(SourceXPathSupport.DEFAULT_XPATH); this.expression.addNamespace("s11", SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); this.expression.addNamespace("s12", SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI); } catch (JaxenException e) { handleException("Error creating source XPath expression", e); } }
/** * Specialized form of xpath evaluation function.An xpath evaluate() will be performed using two contexts * (ie:-soap-envelope and on Synapse Message Context). This is useful for evaluating xpath on a * nodeset for function contexts (we need both nodeset and synapse ctxts for evaluating function * scope expressions) * @param primaryContext a context object ie:- a soap envelope * @param secondaryContext a context object ie:-synapse message ctxt * @return result */ public Object evaluate(Object primaryContext, MessageContext secondaryContext) { Object result = null; //if result is still not found use second ctxt ie:-syn-ctxt with a wrapper to evaluate if (secondaryContext != null) { try { //wrapper Context is used to evaluate 'dynamic' function scope objects result = evaluate(new ContextWrapper((SOAPEnvelope) primaryContext,secondaryContext)); } catch (Exception e) { handleException("Evaluation of the XPath expression " + this.toString() + " resulted in an error", e); } } else { try { result = evaluate(primaryContext); } catch (JaxenException e) { handleException("Evaluation of the XPath expression " + this.toString() + " resulted in an error", e); } } return result; }
public Object evalutePropertyExpression(MessageContext synCtx) { SynapseXPath expr = null; try { expr = new SynapseXPath(this.keyValue.substring(1, keyValue.length() - 1)); for (OMNamespace aNamespaceList : namespaceList) { expr.addNamespace(aNamespaceList); } String result = expr.stringValueOf(synCtx); SynapseXPath expression = new SynapseXPath(result); for (OMNamespace aNamespaceList : namespaceList) { expression.addNamespace(aNamespaceList); } return expression; } catch (Exception e) { handleException("Can not evaluate escaped expression : " + expr); } return this.expression; }
/** * Reads the data source configuration for all mediators based on the <code>AbstractDBMediator</code> * and stores the configuration in the mediator for datasource initialization and de-serialization. * * @param elem the configuration element of the mediator * @param mediator the mediator on which the configuration shall be stored */ protected void buildDataSource(OMElement elem, AbstractDBMediator mediator) { OMElement pool; try { // get the 'pool' element and determine if we need to create a DataSource or // lookup using JNDI SynapseXPath xpath = new SynapseXPath("self::node()/syn:connection/syn:pool"); xpath.addNamespace("syn", XMLConfigConstants.SYNAPSE_NAMESPACE); pool = (OMElement) xpath.selectSingleNode(elem); if (pool.getFirstChildWithName(DSNAME_Q) != null) { readLookupConfig(mediator, pool); } else if (pool.getFirstChildWithName(DRIVER_Q) != null) { readCustomDataSourceConfig(pool, mediator); } else { handleException("The DataSource connection information must be specified for " + "using a custom DataSource connection pool or for a JNDI lookup"); } } catch (JaxenException e) { handleException("Error looking up DataSource connection information", e); } }
public void testHeaderRelativeXPath() throws Exception { MessageContext ctx = TestUtils.getTestContext("<test>" + message + "</test>"); OMFactory fac = ctx.getEnvelope().getOMFactory(); OMNamespace ns = fac.createOMNamespace("http://test", "t"); ctx.getEnvelope().getHeader().addHeaderBlock("test", ns).setText(message); ctx.getEnvelope().getHeader().addHeaderBlock("test2", ns); SynapseXPath xpath = new SynapseXPath("$header/t:test"); xpath.addNamespace(ns); assertEquals(message, xpath.stringValueOf(ctx)); xpath = new SynapseXPath("$header/*"); assertEquals(2, xpath.selectNodes(ctx).size()); }
public void testAxis2ContextProperties() throws Exception { Axis2MessageContext synCtx = TestUtils.getAxis2MessageContext("<test/>", null); synCtx.getAxis2MessageContext().setProperty("test", message); synCtx.getAxis2MessageContext().setProperty("test2", "1234"); assertEquals(message, new SynapseXPath("$axis2:test").evaluate(synCtx)); assertEquals(1234, new SynapseXPath("$axis2:test2").numberValueOf(synCtx).intValue()); assertTrue(new SynapseXPath("$axis2:test2 = 1234").booleanValueOf(synCtx)); }
private OMElement getRequestPayload(MessageContext synCtx) throws AxisFault { if (requestKey != null) { Object request = synCtx.getProperty(requestKey); if (request == null) { request = synCtx.getEntry(requestKey); } if (request != null && request instanceof OMElement) { return (OMElement) request; } else { handleException("The property : " + requestKey + " is not an OMElement", synCtx); } } else if (requestXPath != null) { try { Object o = requestXPath.evaluate(MessageHelper.cloneMessageContext(synCtx)); if (o instanceof OMElement) { return (OMElement) o; } else if (o instanceof List && !((List) o).isEmpty()) { return (OMElement) ((List) o).get(0); // Always fetches *only* the first } else { handleException("The evaluation of the XPath expression : " + requestXPath.toString() + " did not result in an OMElement", synCtx); } } catch (JaxenException e) { handleException("Error evaluating XPath expression : " + requestXPath.toString(), e, synCtx); } } return null; }
public void testContextProperties() throws Exception { SynapseXPath xpath = new SynapseXPath("$ctx:test"); MessageContext synCtx = new TestMessageContext(); synCtx.setProperty("test", message); assertEquals(xpath.evaluate(synCtx), message); }
public void testBodyRelativeXPath() throws Exception { SynapseXPath xpath = new SynapseXPath("$body/test"); MessageContext ctx = TestUtils.getTestContext("<test>" + message + "</test>"); assertEquals(message, xpath.stringValueOf(ctx)); Object node = xpath.selectSingleNode(ctx); assertTrue(node instanceof OMElement); assertEquals(message, ((OMElement)node).getText()); }
public static SynapseXPath getSynapseXPath(OMElement elem, String expression) throws JaxenException { if (expression == null) { handleException("XPath expression cannot be null"); } SynapseXPath xpath = new SynapseXPath(expression); OMElementUtils.addNameSpaces(xpath, elem, log); return xpath; }
public String getEvaluatedExpression(MessageContext synCtx) { return expression.stringValueOf(synCtx); }
public void testCustomVariables() throws Exception { SynapseXPath xpath = new SynapseXPath("$myvar"); SimpleVariableContext variableContext = new SimpleVariableContext(); variableContext.setVariableValue("myvar", "myvalue"); xpath.setVariableContext(variableContext); assertEquals("myvalue", xpath.evaluate(TestUtils.getTestContext("<test/>"))); }
public void testAbsoluteXPath() throws Exception { SynapseXPath xpath = new SynapseXPath("//test"); MessageContext ctx = TestUtils.getTestContext("<test>" + message + "</test>"); assertEquals(message, xpath.stringValueOf(ctx)); }
/** * Return the set of elements specified by the XPath over the given envelope * * @param envelope SOAPEnvelope from which the elements will be extracted * @param expression SynapseXPath expression describing the elements to be extracted * @return List OMElements in the envelope matching the expression * @throws JaxenException if the XPath expression evaluation fails */ public static List getMatchingElements(SOAPEnvelope envelope,MessageContext synCtxt, SynapseXPath expression) throws JaxenException { Object o = expression.evaluate(envelope, synCtxt); if (o instanceof OMNode) { List list = new ArrayList(); list.add(o); return list; } else if (o instanceof List) { return (List) o; } else { return new ArrayList(); } }
Object result = null; try { result = faultDetailExpr.evaluate(synCtx); } catch (JaxenException e) { handleException("Evaluation of the XPath expression " + this.toString() + soapFaultDetail.setText(faultDetailExpr.stringValueOf(synCtx));
public void testContentAwareness() throws Exception { assertEquals(false, new SynapseXPath("$trp:xxx" ).isContentAware()); assertEquals(true, new SynapseXPath("/test/cdaea" ).isContentAware()); assertEquals(false, new SynapseXPath("get-property('SYSTEM_TIME')" ).isContentAware()); assertEquals(false, new SynapseXPath("get-property('registry', 'gov:/apimgt/metadata/log')" ).isContentAware()); //test for get-property with ',' with whitespase assertEquals(false, new SynapseXPath("get-property('registry' , 'gov:/apimgt/metadata/log')" ).isContentAware()); assertEquals(false, new SynapseXPath("get-property('transport', 'Content-Type')" ).isContentAware()); assertEquals(false, new SynapseXPath("get-property('system', 'JAVA_HOME')" ).isContentAware()); } }
/** * Get a string representation of the XPath expression for debugging purposes. */ @Override public String toString() { if (xpathString != null) { return xpathString; } else if (xpath == null) { return DEFAULT_XPATH; } else { return xpath.toString(); } } }
public void testStandardXPathFunctions() throws Exception { MessageContext ctx = TestUtils.getTestContext("<test>123456</test>"); assertEquals(6, new SynapseXPath("string-length(//test)").numberValueOf(ctx).intValue()); }
return xpath.booleanValueOf(synCtx); } catch (JaxenException e) { handleException("Error evaluating XPath expression : " + xpath, e, synCtx); String sourceString = source.stringValueOf(synCtx); if (sourceString == null) { if (synLog.isTraceOrDebugEnabled()) {
public void testStringXPath2() throws Exception { SynapseXPath xpath = SynapseXPath.parseXPathString("$body//{http://somens}test/{http://someother}another"); MessageContext ctx = TestUtils.getTestContext("<m0:test xmlns:m0=\"http://somens\"><m1:another xmlns:m1=\"http://someother\">" + message + "</m1:another></m0:test>"); assertEquals(message, xpath.stringValueOf(ctx)); }