News

Stay up to date on the latest news from Rubicon Red. News, articles, customer success and press releases.

Reading compressed files with Oracle Service Bus

February 27, 2012

Originally Posted by James Goddard

 reasonably common B2B interface convention is to compress the contents of exchanged files, to reduce the impact on network traffic and archiving requirements (see Figure below).  Although Oracle Middleware does not support such interfaces “out of the box,” it is reasonably straight-forward to piece together a simple adapter using existing tools.
01_B2B_Overview
This recipe will guide you through a sample implementation of a proxy service which polls a directory for XML files compressed using the gzip utility.
Gzip is a data compression software application, most commonly encountered as the version implemented by the GNU Project. Gzip only natively supports the compression of one file at a time (although of course that file may itself be the combination of several smaller files), and has a simple format consisting of compressed binary content between a standard header and footer.
J2SE includes a standard library of compression/decompression utilities in the java.util.zip package, which, among other things, includes functionality for working with the gzip data compression algorithm.

Getting ready...

Oracle Enterprise Pack for Eclipse

Although it is possible to develop for the Oracle Service Bus using only the Web Console interface, for a fully featured IDE (including support for version control, source code editing plug-ins, lower latency and independence of development environments) it is generally preferable to use the OSB Workshop perspective of the OEPE.
This recipe assumes the use of an existing OSB Configuration project within the OSB Workshop for development so ensure that you have installed and familiarised yourself with it prior to beginning.

Test Data

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

How to do it...

Writing the Java method

Before jumping into creating anything for OSB, you’ll first need a java library which handles the extraction.
  • Start up Eclipse and switch to the Java perspective.
02_Java_Perspective
  • Right-click in the Project Explorer area and select New > Java Project.
03_new_project
  • Name the project GzExtractor and then select Next.
  • Select the Libraries tab and click Add External JARs....
04_add_library
  • Select com.bea.core.xml.xmlbeans_2.2.0.0.jar from the modules subdirectory of your Oracle middleware installation and click Open.
05_xmlbeans_selection
  • Click Finish in the Add External JARs dialog, and then Finish again in the New Java Project dialog box.
  • Right click on the new project and select New->Class.
06_new_class
  • Set the package to com.rubiconred and the class name to GzExtractor.  Leave everything else as default and then click Finish.
07_class_name
  • Replace the contents of the new file GzExtractor.java with the code below.
[code] package com.rubiconred; import org.apache.xmlbeans.*; import java.io.*; import java.util.zip.GZIPInputStream; public class GzExtractor { public static XmlObject processFile( Object param ) throws IOException, XmlException { byte[] bytes = (byte[])param; if (bytes != null) { InputStream 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; } } } [/code]

Incorporating the Jar File

The following steps explain how the java method created above can be incorporated into an OSB project.
  • Switch to the Oracle Service Bus perspective.
  • If you don’t have one already, create a new OSB Configuration Project and a new OSB project within it.  Name the project GzFileAdapter.
  • Right click on the GzExtractor Java project and select Export->Export....
08_export_java
  • Select Java->JAR file and then click Next.
09_export_to_jar
  • Provide the export destination as GzFileAdapter/GzExtractor.jar and then click Finish.
10_jar_destination
  • Right click on the GzFileAdapter OSB project and select Import->Import....
11_import_import
  • Select File System in the General category and then click Next.
12_import_file_system
  • Select the com.bea.core.xml.xmlbeans_2.2.0.0.jar XMLBeans library from the modules subdirectory of your Oracle middleware installation, as used earlier in the Java project, and then click Finish.
13_xmlbeans_selection
  • Right click on GzExtractor.jar in the Project Explorer and select Oracle Service Bus -> Modify Jar Dependencies from the context menu.
14_osb_modify_jar_dependencies
  • Select the XMLBeans library, then click Add, then OK.
15_jar_dependencies_selection

Creating the Proxy Service

The next step is to create the file adapter, as a binary file-based message proxy service, from the Oracle Service Bus IDE.
  • Right-click on the GzFileAdapter OSB project in the Project Explorer and select New -> Proxy Service.
16_new_proxy_service
  • Name it GzFileAdapter and click Finish.
  • In the General tab of the Proxy Service editor, select Message Service as the Service Type.
17_general_configuration
  • In the Messaging tab, select Binary as the Request Message Type.  Leave the Response Message Type as None.
18_message_type_configuration
  • In the Transport tab, select file as the Protocol and provide a directory name where you would like to poll for files from as the Endpoint URI.
19_transport_configuration
  • In the File Transport tab, customise the adapter to suit.  In this example, we’ll select a file mask of *.gz as well as configuring an archive Post Read Action and specifying all the necessary Directory file destinations.
20_file_transport_configuration

Defining the message flow

The next step is to incorporate a call to the GzExtractor java library into the Proxy Service Message Flow.
  • Start by dragging a new Pipeline Pair from the Design Palette into the Message Flow editor.
21_pipeline_pair
  • Add a new Stage to the Request Pipeline and name it Extract GZIP.
22_extract_gzip_stage
  • Drag an Assign Action into the new Stage.
23_assign
  • In the Properties tab for the Assign action, provide the Expression as $body/ctx:binary-content and theVariable as gzBinaryContent.
24_assign_properties
  • Drag a new Java Callout Action from the Design Palette to just under the Assign action.
25_java_callout
  • In the Properties tab for the new Java Callout, select Browse next to the Method field.
26_java_callout_properties
  • Select GzExtractor/com.rubiconred.GzExtractor.processFile.
  • Select the Expression for the only parameter and specify $gzBinaryContent.
27_java_callout_properties_part_2
  • Specify the Result Value variable as payload.

Testing the implementation

To test the new file adapter, you can place a GZIP’d file in the input directory and confirm the contents are read successfully following these steps:
  • Create a new Reporting Stage below the GZIP Extract Stage and drag a new Log Action in.
28_log_report
  • Set the Log Expression as $payload and the Annotation as “GzFileAdapter message payload”.
29_log_properties
  • Change the Severity to Error so that it is guaranteed to appear on your weblogic’s console output.
  • Deploy the service to your server.
  • Copy a GZIP’d text file to the input directory you specified earlier.
  • Observe that the contents are written to the console.