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.
Prior to beginning this recipe, you will want to prepare some test data, consisting of XML files compressed using GZIP.
GzipAdapter
and then select Next.com.bea.core.xml.xmlbeans_2.2.0.0.jar
from the modules
subdirectory of your Oracle middleware installation and click on Open.com.rubiconred.osb.gzip
in the example) and the class name to GzipAdapter
. Leave everything else as default and then click on Finish.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(); } }
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.
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.
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.
3.12.166.131