Writing compressed files with OSB

In this next section we will consider the inverse scenario, in which files are written using the same GZIP compression algorithm to the filesystem.

This recipe will demonstrate how a simple proxy service which accepts XML content and writes GZIP'd XML files can be built using OSB.

Getting ready

This recipe assumes you have created an OSB project GzipFileAdapter and carried out steps 1 to 5 in the previous recipe, as well as completed the first recipe and created the GzipAdapter.jar.

How to do it...

  1. Right-click on the GzipFileAdapter OSB project in the Project Explorer and select New | Business Service. Name it GzipFileWriter and click on Finish.
  2. On the General Configuration tab, select Messaging Service as the Service Type.
  3. On the Messaging tab, select Binary as the Request Message Type. Leave the Response Message Type as None.
  4. On the Transport tab, select file as the Protocol. Enter a directory you would like to write to as the Endpoint URI, then click on Add.
    How to do it...
  5. On the File Transport tab, set the Suffix as .xml.gz.
    How to do it...

    Save your changes.

  6. Right-click on the GzipFileAdapter OSB project in the Project Explorer and select New | Proxy Service. Name the service WriteGZipFile and select on Finish.
  7. On the Message Flow tab, drag a Route node from the Design Palette into the main editor.
  8. Next, drag a Routing action into the route. Set the Service property to GzipFileWriter.biz.
  9. Drag a new Java Callout action from the Design Palette into the Request Action section of the Routing action. In the Properties tab for the new Java Callout, select Browse next to the Method field. Select the GzipAdapter.jar and click on OK.

    In the Select a Java Method window, select the writeGzipObject method and click on OK.

  10. With the Properties tab, click on < Expression> for the only parameter and set it to $body and set Result Value to gzContent.
    How to do it...
  11. Next, add a Replace action after the Java Callout, and set its properties as follows:

    Field

    Value

    XPath

    .

    In Variable

    body

    Expression

    $gzContent

     

    select Replace node Contents

  12. Drag a Transport Headers action into the Request Action section, after the Replace action.
  13. In the Properties tab, click on Add Header. Select the file category and fileName element and then Set Header to your desired filename. This could be a string variable or a constant.
    How to do it...

    Note, that if a constant filename is used, then subsequent uses of the business service will automatically append an integer to the filename so as not to overwrite existing files.

  14. Deploy your project and test out the proxy service. You should see the GZIP'd XML files being produced containing the content you place in the body of the request.
    How to do it...

How it works…

The writeGZipObject() method converts the input XML into an output stream; in our case, the GZIPOutputStream which compresses the content into the output stream it wraps.

By using the byte[] return type for our Java Callout, we leverage OSB's binary content capability to include a reference to the raw data produced by our GZIP compression code.

Passing this to a binary messaging business service then automatically pushes the content out into the file, exactly as expected.

Note, the use of transport headers to set the filename. This is necessary meta-info, and should usually be used to name the file using variable content.

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 ZipOutputStream for GZIPOutputStream 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.141.31.125