Wednesday, December 18, 2013

How to zip/base64 string response on Oracle Service Bus

How to zip/base64 string response on Oracle Service Bus


                When you are working with huge results like big XMLs or Json  is handy to compress the OSB output. As an example if you are connecting to an Android device you can send the data compressed and then let Android decompress it.

Here is an example of how to accomplish this task in Oracle Service Bus.

1.    1.   First we need to create a class that will compress a String.

public class ZipContent {
    public ZipContent() {
        super();
    }

    public static String zipme(String content) throws IOException {


        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(content.getBytes());
        gzip.close();

        byte[] bytes = out.toByteArray();
        return  Base64.encodeBase64String(bytes);
       
    }
}

2.       2. Add your library to a jar file and add it in the resource folder of your Service Bus project
3.      3. Create a new java callout with your body

Method: ZipContent.zipme
Expression: $body
Result value: body









4.    4.   Your results will be compresses and in base64



2 comments:

  1. How do we do it the other way around...I am getting a response in compressed form from the provider and i have to decompress it to structure it in a formatted XML??
    Its urgent..KIndly suggest....

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete