Wednesday, 24 June 2015

Base64 encoding/decoding in OSB

Of course there are several java examples to do a base64 encoding on the internet. And there are almost nearly as much encoding implementations in different environments. But which one works in Weblogic/OSB11g? And to implement those examples, compile and jar them, I find myself on a quest for the necessary jars. Of course you can refer to the weblogic.jar in your project of ant file. But that is a little too much, I think. I'd like to find and deliver the bare minimum of jars needed for my project.

For my latest customer/project I came up with this class:

package nl.alliander.osb.base64;

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import weblogic.utils.encoders.BASE64Decoder;
import weblogic.utils.encoders.BASE64Encoder;
import java.nio.charset.Charset;

public class Base64EncoderDecoder
{
    private static final Charset UTF8_CHARSET;
    
    public static void main(final String[] args) {
    }
    
    public static String encode(final byte[] bytes) {
        final BASE64Encoder encoder = new BASE64Encoder();
        final String encodedString = encoder.encodeBuffer(bytes);
        return encodedString;
    }
    public static int getLength(final byte[] bytes) {
       int length = bytes.length;
        return length;
    }    
    public static byte[] decode(final String b64Document) throws IOException {
        final BASE64Decoder decoder = new BASE64Decoder();
        final InputStream inputStream = new ByteArrayInputStream(b64Document.getBytes(Base64EncoderDecoder.UTF8_CHARSET));
        final byte[] decodedBytes = decoder.decodeBuffer(inputStream);
        return decodedBytes;
    }
    
    static {
        UTF8_CHARSET = Charset.forName("UTF-8");
    }
}

And if you use JDeveloper11g as a IDE the only lib you need to compile this is: com.bea.core.utils.full_1.9.0.1.jar. Or com.bea.core.utils.full_1.10.0.0.jar, if using oepe version 11.1.1.8. The jars can be found in ${oracle.home}/modules. Where ${oracle.home} refers to your JDeveloper11g or OEPE-Middleware installation.

By the way, in my OSB project I need to process Attachments in my message (Soap with Attachments), where I need to upload the documents to a ContentServer. Unfortunately the ContentServer needs the filesize (it apparenlty does not determine it by base64-decoding it). So I added the getLength() method to determine it with a java-callout similar to the base64-encode.


Input of the methods is a variable like 'attachmentBin' resulted from an Assing with an expression like:
$attachments/ctx:attachment[ctx:Content-ID/text()=$contentId]/ctx:body/ctx:binary-content


2 comments :

buyi wen said...

you can try this free online service to convert string to base64 online(http://www.online-code.net/base64-string.html).

Martien van den Akker said...

Yes, there are tons of them. When I need to do so, I google and pick one.
But this article talks about how to do so within an OSB proxy-service.