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.darwin-it.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


4 comments :

DomFilk 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.

Anonymous said...

Hi
I want to use decode method, in order to convert String to base64.
But I always get as an answer, something like that: [B@186db54.
There isn't a base64 result at all...

Thanks in advance
Elad

Anonymous said...

Hi,

I would say that the content you provide to the method isn't appropriate or complete Base64 content. Check it with https://www.base64decode.org/. Or https://coderstoolbox.net/string/#!encoding=xml&action=encode&charset=us_ascii.

But could you ask the same question on community.oracle.com? Then more people can help and you could provide more details.

Regards,
Martien