Implement GZIP wrapper for OSB

Before jumping into creating anything for OSB, we first need to create a Java library that can be used by the Oracle Service Bus to read and write GZIP files.

Getting ready

Prior to beginning this recipe, you will want to prepare some test data, consisting of XML files compressed using GZIP.

How to do it...

  1. Start up Eclipse and switch to the Java perspective.
    How to do it...
  2. Right-click in the Project Explorer area and select New | Java Project. Name the project GzipAdapter and then select Next.
  3. Select the Libraries tab and click on Add External JARs.... Select com.bea.core.xml.xmlbeans_2.2.0.0.jar from the modules subdirectory of your Oracle middleware installation and click on Open.
    How to do it...
  4. Click on Finish in the Add External JARs dialog box, and then on Finish again in the New Java Project dialog box.
  5. Right-click on the new project and select New | Class. Set the package name (com.rubiconred.osb.gzip in the example) and the class name to GzipAdapter. Leave everything else as default and then click on Finish.
  6. Replace the contents of the new file GzipAdapter.java with the following code:
    package com.rubiconred.osb.gzip;
    
    import org.apache.xmlbeans.*;
    import java.io.*;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;
    
    public class GzipAdapter {
      
      public static XmlObject readGzipObject(Object param) 
        throws IOException, XmlException 
      {
        byte[] bytes = (byte[]) param;
        if (bytes != null) {
          InputStream input input = new ByteArrayInputStream(bytes);
          InputStream gzipInput = new GZIPInputStream(input);
    
          Writer writer = new StringWriter();
          char[] buffer = new char[1024];
    
          try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(gzipInput, "UTF-8"));
    
            int n;
            while ((n = reader.read(buffer)) != -1)
              writer.write(buffer, 0, n);
          } finally {
            gzipInput.close();
          }
    
          // return the contents of the file
          return XmlObject.Factory.parse(writer.toString());
        } else {
          // input parameter is null, return null
          return null;
        }
      }
    
      public static byte[] writeGzipObject(XmlObject input) throws IOException, XmlException 
      {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        GZIPOutputStream gzipOutput = new GZIPOutputStream(output);
    
        input.save(gzipOutput);
        gzipOutput.close();
    
        return output.toByteArray();
      }
    }
  7. Right click on the GzipAdapter Java project and select Export.... Select Java | JAR file and then click on Next.

    Provide the export destination for the JAR file as GzipAdapter.jar in the directory of your choice and then click on Finish.

How it works…

In the previous code we have implemented two methods that allow us to convert to/from a binary format (that is GZIP) to an XML Beans Interface. As Oracle Service Bus uses the standard XMLBeans interface to manipulate XML, this provides a simple wrapper around the existing GZIP libraries that can be used by OSB to read/write GZIP data.

We will use these methods in the next two recipes to do just that.

There's more…

In addition to GZIP, the Java standard libraries also include support for the popular ZIP file format. The same design pattern can be used for this format, substituting ZIP for GZIP in the example code.

For other compression file formats (for example, RAR), there are generally open source libraries available for manipulation, with which similar approaches may be taken.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.166.131