Two years ago, i did a post on how to use proxy java callout to base64 decode with OSB 11.1.1.3 http://yuanmengblog.blogspot.com/2011/04/base64-decoder-for-osb.html.
Now i have 11.1.1.6. Obviously, oracle has moved those jar files around, so you will have to find out where the xerces jar file is if you want to do the same thing in 11.1.1.6.
Well, a colleague brought to my attention something I thought was very cool: using XSLT java call to do the base64 decode. Then you don't need to do the proxy java callout, therefore, saving the trouble of uploading some custom jar file. That sounded very cool, so i decided to give it a shot.
For that we need to resolve two issues. 1. what's the Oracle xslt processor's syntax for java call (it appears to me the XSLT java call syntax varies among different xslt processors). 2. What is the new base64 decoder class in 11.1.1.6. The xslt code below answers both of these questions:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:b64="http://www.oracle.com/XSL/Transform/java/weblogic.apache.xerces.impl.dv.util.Base64"
>
<xsl:template match="/">
<foo>
<xsl:value-of select="b64:decode('d2xzdXNlcjp3ZWxjb21lMQ==')"/>
</foo>
</xsl:template>
</xsl:stylesheet>
plug it into your proxy assignment activity, then you will get an output of
<foo xmlns:b64="http://www.oracle.com/XSL/Transform/java/weblogic.apache.xerces.impl.dv.util.Base64">wlsuser:welcome1</foo>
Keep in mind, directly referencing an undocumented/unsupported oracle class "weblogic.apache.xerces.impl.dv.util.Base64" is a tricky business, proceed at your own risk. currently, this class is inside "com.bea.core.apache_1.3.0.1.jar" under "C:\Oracle\Middleware\Oracle_OSB1\modules" for my 11.1.1.6 install. If oracle decides to change that, then you need to update your xslt as well.
testing on the command line (in my environment):
set classpath=C:\Oracle\Middleware\oracle_common\modules\oracle.xdk_11.1.0\xmlparserv2.jar;c:\Oracle\Middleware\Oracle_OSB1\modules\com.bea.core.apache_1.3.0.1.jar;
first jar is for oraxsl, 2nd jar is for base64 decoding:
java oracle.xml.parser.v2.oraxsl input.xml test.xslt
Showing posts with label Java Callout. Show all posts
Showing posts with label Java Callout. Show all posts
Thursday, May 30, 2013
Tuesday, April 5, 2011
Base64 Code Snippets for OSB and BPEL
This post applies to SOA suite 11.1.1.3.
I need to decode a base64 payload in my OSB proxy service. Here is a native solution that I came up with:
1. The java source:
I need to decode a base64 payload in my OSB proxy service. Here is a native solution that I came up with:
1. The java source:
//import org.apache.xerces.impl.dv.util.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
public class MyBase64
{
public static String decode(String x)
{
try {
String path = "file:///"+System.getenv("ALSB_HOME") + "/../modules/com.bea.core.apache.xercesImpl_2.8.1.jar";
URL[] urls = new URL[] { new URL(path) };
ClassLoader loader = new URLClassLoader(urls);
Class<?> cls = loader.loadClass("org.apache.xerces.impl.dv.util.Base64");
Method theMethod = cls.getMethod("decode", String.class);
return new String( (byte[])theMethod.invoke(null, x));
}
catch(Exception e)
{
e.printStackTrace();
return "decode failed";
}
}
public static void main(String args[])
{
System.out.println(decode("Zm9vIGJhciBhbmQgY2FyIHRyeSBhZ2Fpbg=="));
}
}
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
public class MyBase64
{
public static String decode(String x)
{
try {
String path = "file:///"+System.getenv("ALSB_HOME") + "/../modules/com.bea.core.apache.xercesImpl_2.8.1.jar";
URL[] urls = new URL[] { new URL(path) };
ClassLoader loader = new URLClassLoader(urls);
Class<?> cls = loader.loadClass("org.apache.xerces.impl.dv.util.Base64");
Method theMethod = cls.getMethod("decode", String.class);
return new String( (byte[])theMethod.invoke(null, x));
}
catch(Exception e)
{
e.printStackTrace();
return "decode failed";
}
}
public static void main(String args[])
{
System.out.println(decode("Zm9vIGJhciBhbmQgY2FyIHRyeSBhZ2Fpbg=="));
}
}
2. complie java source (you need to add C:\Oracle\Middleware\modules\com.bea.core.apache.xercesImpl_2.8.1.jar to your compiler classpath, your path maybe different)
3. jar it up
4. add jar to your osb project. (In your project, choose "Create Resource", select "JAR" under "Utility")
4. use in your java callout
Here is a BPEL snippet to encode base64
<bpelx:exec name="base64EncodeTemp" version="1.5" language="java">
<![CDATA[
String sTemp = (String)getVariableData("temp");
addAuditTrailEntry("Covert Temp to Byte Array : \n" + sTemp);
byte[] bTemp = new byte[sTemp.length() / 2];
for (int i = 0; i < bTemp.length; i++) {
bTemp[i] = (byte) Integer.parseInt(sTemp.substring(i*2, (i*2)+2), 16);
}
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
String encodedTemp = encoder.encodeBuffer(bTemp);
setVariableData("temp", encodedTemp);]]>
</bpelx:exec>
Here is a BPEL snippet to encode base64
<bpelx:exec name="base64EncodeTemp" version="1.5" language="java">
<![CDATA[
String sTemp = (String)getVariableData("temp");
addAuditTrailEntry("Covert Temp to Byte Array : \n" + sTemp);
byte[] bTemp = new byte[sTemp.length() / 2];
for (int i = 0; i < bTemp.length; i++) {
bTemp[i] = (byte) Integer.parseInt(sTemp.substring(i*2, (i*2)+2), 16);
}
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
String encodedTemp = encoder.encodeBuffer(bTemp);
setVariableData("temp", encodedTemp);]]>
</bpelx:exec>
Subscribe to:
Posts (Atom)