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>
This comment has been removed by the author.
ReplyDelete