blooprynt

View Original

Compressing Boomi response content for web requests

A simple performance boost for responses from a Listening Web Service process start is to incorporate GZIP compression in your stream. GZIP decompression is trivial to implement for web applications and if you’re build Single Page Applications (SPA), you’ll be dependant on snappy responses. JSON especially, is beneficial of compression.

The assumption is that you’ve performed an earlier transformation to JSON or what ever your preferred output is.

import java.util.Properties;
import java.io.InputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.DeflaterInputStream;
import java.util.zip.DeflaterOutputStream;

for( int i = 0; i < dataContext.getDataCount(); i++ ) {
  InputStream is = dataContext.getStream(i);
  Properties props = dataContext.getProperties(i);
  byte[] ba = is.getBytes();    

  ByteArrayOutputStream baos = new ByteArrayOutputStream(ba.length);

  GZIPOutputStream gos = new GZIPOutputStream(baos);
  gos.write(ba);
  gos.finish();
  gos.close();

  dataContext.storeStream(new ByteArrayInputStream(baos.toByteArray()), props);

}