Author: hiranya
Date: Sat May 28 20:11:29 2016
New Revision: 1745927
URL: http://svn.apache.org/viewvc?rev=1745927&view=rev
Log:
Implemented the new XPath function url-encode; Applied patch from Buddhima from
SYNAPSE-1022
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/URLEncodeFunction.java
Modified:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathConstants.java
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathFunctionContext.java
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/builtin/PropertyMediatorTest.java
Modified:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathConstants.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathConstants.java?rev=1745927&r1=1745926&r2=1745927&view=diff
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathConstants.java
(original)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathConstants.java
Sat May 28 20:11:29 2016
@@ -33,6 +33,9 @@ public final class SynapseXPathConstants
/** base64Decode XPath extension function name */
public static final String BASE64_DECODE_FUNCTION = "base64Decode";
+ /** URL-Encode XPath extension function name */
+ public static final String URL_ENCODE_FUNCTION = "url-encode";
+
/** Body relative XPath variale name for the SOAPBody */
public static final String SOAP_BODY_VARIABLE = "body";
Modified:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathFunctionContext.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathFunctionContext.java?rev=1745927&r1=1745926&r2=1745927&view=diff
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathFunctionContext.java
(original)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/SynapseXPathFunctionContext.java
Sat May 28 20:11:29 2016
@@ -86,6 +86,10 @@ public class SynapseXPathFunctionContext
SynapseXPathConstants.BASE64_DECODE_FUNCTION.equals(localName)) {
// create a base64Decode function and set it to the XPath
return new Base64DecodeFunction();
+ } else if (localName != null &&
+
SynapseXPathConstants.URL_ENCODE_FUNCTION.equals(localName)) {
+ // create a url-encode function and set it to the XPath
+ return new URLEncodeFunction();
}
//We check if custom Xpath extensions are available
Function extensionFunction = XpathExtensionUtil.getFunctionContext(
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/URLEncodeFunction.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/URLEncodeFunction.java?rev=1745927&view=auto
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/URLEncodeFunction.java
(added)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/URLEncodeFunction.java
Sat May 28 20:11:29 2016
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.util.xpath;
+
+import org.apache.commons.httpclient.URIException;
+import org.apache.commons.httpclient.util.URIUtil;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jaxen.Context;
+import org.jaxen.Function;
+import org.jaxen.FunctionCallException;
+import org.jaxen.function.StringFunction;
+
+import java.util.List;
+
+/**
+ * Implements the XPath extension function synapse:url-encode(string)
+ */
+public class URLEncodeFunction implements Function {
+
+ private static final Log log = LogFactory.getLog(URLEncodeFunction.class);
+
+ /**
+ * Returns the url-encoded string value of the first argument.
+ *
+ * @param context the context at the point in the expression when the
function is called
+ * @param args arguments of the functions
+ * @return The string value of a property
+ * @throws FunctionCallException
+ */
+ public Object call(Context context, List args) throws
FunctionCallException {
+ boolean debugOn = log.isDebugEnabled();
+
+ if (args == null || args.size() == 0) {
+ if (debugOn) {
+ log.debug("Property key value for lookup is not specified");
+ }
+ return SynapseXPathConstants.NULL_STRING;
+ }
+
+ int size = args.size();
+ if (size == 1) {
+ // get the first argument, it can be a function returning a string
as well
+ String value = StringFunction.evaluate(args.get(0),
context.getNavigator());
+
+ // use the default UTF-8 encoding
+ return encode(debugOn, SynapseXPathConstants.DEFAULT_CHARSET,
value);
+ } else if (size == 2) {
+ // get the first argument, it can be a function returning a string
as well
+ String value = StringFunction.evaluate(args.get(0),
context.getNavigator());
+
+ // encoding is in the second argument
+ String encoding = StringFunction.evaluate(args.get(1),
context.getNavigator());
+
+ return encode(debugOn, encoding, value);
+ } else if (debugOn) {
+ log.debug("url-encode function expects only one argument,
returning empty string");
+ }
+ // return empty string if the arguments are wrong
+ return SynapseXPathConstants.NULL_STRING;
+ }
+
+ private Object encode(boolean debugOn, String encoding, String value)
throws FunctionCallException {
+ if (value == null || "".equals(value)) {
+ if (debugOn) {
+ log.debug("Non empty string value should be provided for
encoding");
+ }
+
+ return SynapseXPathConstants.NULL_STRING;
+ }
+
+ String encodedString;
+ try {
+ encodedString = URIUtil.encodePathQuery(value, encoding);
+ } catch (URIException e) {
+ String msg = "Unsupported charset encoding";
+ log.error(msg, e);
+ throw new FunctionCallException(msg, e);
+ }
+
+ if (debugOn) {
+ log.debug("Converted string: " + value + " with encoding: " +
encoding +
+ " to url encoded value: " + encodedString);
+ }
+
+ return encodedString;
+ }
+}
Modified:
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/builtin/PropertyMediatorTest.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/builtin/PropertyMediatorTest.java?rev=1745927&r1=1745926&r2=1745927&view=diff
==============================================================================
---
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/builtin/PropertyMediatorTest.java
(original)
+++
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/builtin/PropertyMediatorTest.java
Sat May 28 20:11:29 2016
@@ -209,6 +209,24 @@ public class PropertyMediatorTest extend
"value".equals(medProp.getEvaluatedExpression(synCtx)));
}
+ public void testPropertyURLEncoding() throws Exception {
+ // Evaluate url-encode function
+ PropertyMediator propMediator = new PropertyMediator();
+ propMediator.setName("name");
+ propMediator.setValue("this/is+a/synapse test?for=url+encoding");
+
+ MessageContext synCtx = TestUtils.getTestContext("<empty/>");
+ propMediator.mediate(synCtx);
+
+ // read property through a mediator property
+ MediatorProperty mediatorProperty = new MediatorProperty();
+ mediatorProperty.setExpression(new
SynapseXPath("url-encode($ctx:name)"));
+
+ assertEquals("this/is%2Ba/synapse%20test?for=url+encoding",
+ mediatorProperty.getEvaluatedExpression(synCtx));
+
+ }
+
public void testPropertyRegexTest() throws Exception {
String outputProperty = "regexProperty";