Author: hiranya
Date: Thu May 26 23:39:09 2016
New Revision: 1745684
URL: http://svn.apache.org/viewvc?rev=1745684&view=rev
Log:
Added the base64 decode XPath function; Applied patch from Prabath at
SYNAPSE-1012
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64DecodeFunction.java
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/xpath/
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/xpath/Base64DecodeFunctionTest.java
Modified:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64EncodeFunction.java
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
Added:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64DecodeFunction.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64DecodeFunction.java?rev=1745684&view=auto
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64DecodeFunction.java
(added)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64DecodeFunction.java
Thu May 26 23:39:09 2016
@@ -0,0 +1,110 @@
+/*
+ * 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.codec.binary.Base64;
+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.io.UnsupportedEncodingException;
+import java.util.List;
+
+/**
+ * Implements the XPath extension function synapse:base64Decode(string)
+ */
+public class Base64DecodeFunction implements Function {
+
+ private static final Log log =
LogFactory.getLog(Base64DecodeFunction.class);
+
+ /**
+ * Returns the base64 decoded 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 {
+ if (args == null || args.size() == 0) {
+ if (log.isDebugEnabled()) {
+ 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 encodedValue = StringFunction.evaluate(args.get(0),
context.getNavigator());
+ // use the default UTF-8 decoding.
+ return decode(log.isDebugEnabled(),
SynapseXPathConstants.DEFAULT_CHARSET, encodedValue);
+ } else if (size == 2) {
+ // get the first argument, it can be a function returning a string
as well
+ String encodedValue = StringFunction.evaluate(args.get(0),
context.getNavigator());
+ // charset is in the second argument
+ String charset = StringFunction.evaluate(args.get(1),
context.getNavigator());
+ return decode(log.isDebugEnabled(), charset, encodedValue);
+ } else if (log.isDebugEnabled()) {
+ log.debug("base64Decode function expects only two arguments
maximum, returning empty string");
+ }
+ // return empty string if the arguments are wrong
+ return SynapseXPathConstants.NULL_STRING;
+ }
+
+
+ private Object decode(boolean debugOn, String charset, String value)
throws FunctionCallException {
+ if (value == null || value.isEmpty()) {
+ if (debugOn) {
+ log.debug("Non empty string value should be provided for
decode");
+ }
+ return SynapseXPathConstants.NULL_STRING;
+ }
+
+ byte[] decodedValue;
+ try {
+ decodedValue = new Base64().decode(value.getBytes(charset));
+ } catch (UnsupportedEncodingException e) {
+ String msg = "Unsupported Charset";
+ log.error(msg, e);
+ throw new FunctionCallException(msg, e);
+ }
+
+ String decodedString;
+ try {
+ decodedString = new String(decodedValue, charset).trim();
+ } catch (UnsupportedEncodingException e) {
+ String msg = "Unsupported Charset";
+ log.error(msg, e);
+ throw new FunctionCallException(msg, e);
+ }
+
+ if (debugOn) {
+ log.debug("Decoded base64 encoded value: " + value + " with
charset: " + charset +
+ " to String: " + decodedString);
+ }
+
+ return decodedString;
+ }
+}
Modified:
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64EncodeFunction.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64EncodeFunction.java?rev=1745684&r1=1745683&r2=1745684&view=diff
==============================================================================
---
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64EncodeFunction.java
(original)
+++
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/xpath/Base64EncodeFunction.java
Thu May 26 23:39:09 2016
@@ -34,11 +34,8 @@ import java.io.UnsupportedEncodingExcept
* Implements the XPath extension function synapse:base64Encode(string)
*/
public class Base64EncodeFunction implements Function {
- private static final Log log =
LogFactory.getLog(Base64EncodeFunction.class);
-
- public static final String NULL_STRING = "";
- private static final String DEFAULT_CHARSET = "UTF-8";
+ private static final Log log =
LogFactory.getLog(Base64EncodeFunction.class);
/**
* Returns the base64 encoded string value of the first argument.
@@ -55,7 +52,7 @@ public class Base64EncodeFunction implem
if (debugOn) {
log.debug("Property key value for lookup is not specified");
}
- return NULL_STRING;
+ return SynapseXPathConstants.NULL_STRING;
}
int size = args.size();
@@ -64,7 +61,7 @@ public class Base64EncodeFunction implem
String value = StringFunction.evaluate(args.get(0),
context.getNavigator());
// use the default UTF-8 encoding
- return encode(debugOn, DEFAULT_CHARSET, value);
+ 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());
@@ -79,7 +76,7 @@ public class Base64EncodeFunction implem
}
}
// return empty string if the arguments are wrong
- return NULL_STRING;
+ return SynapseXPathConstants.NULL_STRING;
}
private Object encode(boolean debugOn, String encoding, String value)
@@ -89,7 +86,7 @@ public class Base64EncodeFunction implem
log.debug("Non emprty string value should be provided for
encoding");
}
- return NULL_STRING;
+ return SynapseXPathConstants.NULL_STRING;
}
byte[] encodedValue;
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=1745684&r1=1745683&r2=1745684&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
Thu May 26 23:39:09 2016
@@ -30,6 +30,9 @@ public final class SynapseXPathConstants
/** base64Encode XPath extension function name */
public static final String BASE64_ENCODE_FUNCTION = "base64Encode";
+ /** base64Decode XPath extension function name */
+ public static final String BASE64_DECODE_FUNCTION = "base64Decode";
+
/** Body relative XPath variale name for the SOAPBody */
public static final String SOAP_BODY_VARIABLE = "body";
@@ -50,4 +53,8 @@ public final class SynapseXPathConstants
/** Variable prefix for accessing URL parameters of the message through
XPath variables */
public static final String URL_VARIABLE_PREFIX = "url";
+
+ public static final String DEFAULT_CHARSET = "UTF-8";
+
+ public static final String NULL_STRING = "";
}
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=1745684&r1=1745683&r2=1745684&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
Thu May 26 23:39:09 2016
@@ -82,6 +82,10 @@ public class SynapseXPathFunctionContext
SynapseXPathConstants.BASE64_ENCODE_FUNCTION.equals(localName)) {
// create a base64Encode function and set it to the XPath
return new Base64EncodeFunction();
+ } else if (localName != null &&
+
SynapseXPathConstants.BASE64_DECODE_FUNCTION.equals(localName)) {
+ // create a base64Decode function and set it to the XPath
+ return new Base64DecodeFunction();
}
//We check if custom Xpath extensions are available
Function extensionFunction = XpathExtensionUtil.getFunctionContext(
Added:
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/xpath/Base64DecodeFunctionTest.java
URL:
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/xpath/Base64DecodeFunctionTest.java?rev=1745684&view=auto
==============================================================================
---
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/xpath/Base64DecodeFunctionTest.java
(added)
+++
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/util/xpath/Base64DecodeFunctionTest.java
Thu May 26 23:39:09 2016
@@ -0,0 +1,64 @@
+/*
+ * 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 junit.framework.Assert;
+import junit.framework.TestCase;
+import org.jaxen.Context;
+import org.jaxen.ContextSupport;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Base64DecodeFunctionTest extends TestCase {
+
+ public void testBase64DecodeFunctionWithCharset() throws Exception {
+ String encodedString = "U3luYXBzZQ==";
+ Base64DecodeFunction base64DecodeFunction = new Base64DecodeFunction();
+ List<String> params = new ArrayList<String>();
+ params.add(encodedString);
+ params.add("UTF-8");
+ Context context = new Context(null);
+ context.setContextSupport(new ContextSupport());
+ String decodedString = (String) base64DecodeFunction.call(context,
params);
+ assertEquals("Wrong decoded value found", "Synapse", decodedString);
+
+ }
+
+ public void testBase64DecodeFunctionWithoutCharset() throws Exception {
+ String encodedString = "U3luYXBzZQ==";
+ Base64DecodeFunction base64DecodeFunction = new Base64DecodeFunction();
+ List<String> params = new ArrayList<String>();
+ params.add(encodedString);
+ Context context = new Context(null);
+ context.setContextSupport(new ContextSupport());
+ String decodedString = (String) base64DecodeFunction.call(context,
params);
+ assertEquals("Wrong decoded value found", "Synapse", decodedString);
+ }
+
+ public void testBase64DecodeFunctionWithoutParameters() throws Exception {
+ Base64DecodeFunction base64DecodeFunction = new Base64DecodeFunction();
+ List<String> params = new ArrayList<String>();
+ Context context = new Context(null);
+ context.setContextSupport(new ContextSupport());
+ String decodedString = (String) base64DecodeFunction.call(context,
params);
+ assertEquals("Wrong decoded value found", "", decodedString);
+ }
+}