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.
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
.
GzipFileWriter
and click on Finish.None
..xml.gz
.Save your changes.
WriteGZipFile
and select on Finish.In the Select a Java Method window, select the writeGzipObject
method and click on OK.
$body
and set Result Value to gzContent
.
Field |
Value |
---|---|
XPath |
|
In Variable |
|
Expression |
|
select |
file
category and fileName
element and then Set Header
to your desired filename. This could be a string variable or a constant.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.
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.
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.