Tuesday, June 20, 2017

Oracle OSB download DB BLOB file in browser

Requirement: 

File Data present in tables as BLOB. A rest service should be exposed to download the file content from browser in Oracle Service Bus OSB.

Table structure 
CREATE TABLE X
   ( XNO NUMBER, --id
COLUMN1 BLOB, --file
COLUMN2 TIMESTAMP (6),
COLUMN3 VARCHAR2(200 BYTE), --filename
COLUMN4 VARCHAR2(500 BYTE) --version
   )

Example url : https://host:port/OrigDocFetchService/ProxyService?id=1&documentName=Overview1.pdf&version=1.0

How it is implemented:: 

The proxy first receives the request and identifies the content type based on the documentname passed in the query parameters.
Then it fetches the content from Database via DB Adapter. (Please read documentation on how to configure db adapter which links db via datasource in weblogic)

Once Document is fetched from DB then we have a helper function DataSource decode64ToStream(String) to help convert the base64 encoded value which OSB gives from DB to a stream which browser can act on.
Then the Content-Type and disposition tags Content-Disposition are set as below

Content-Type = $contentType example 'application/pdf;' for  pdf or 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' for docx content 
(Please refer https://technet.microsoft.com/en-us/library/ee309278(office.12).aspx)
Content-Disposition = concat("inline; filename=""",$inbound/ctx:transport/ctx:request/http:query-parameters/http:parameter[@name='documentName']/@value,"""")
example "inline: filename: "Overview1.pdf""


Once these headers are set and binary stream flowing back to browser as body then the browser will be in a position to download the content.


The full java helper functions and OSB project can be available at below github repository.

https://github.com/praveencasimir/DownloadBLOBSBProject

Monday, August 10, 2015

Oracle SOA - GZIP JMS Content

Pretext : 

There was a unique requirement to send a JMS BytesMessage content in GZIP format which showcased 1/6th compression compared to normal BytesMessage.
During the course of implementing the same I could not find much information regarding how this can be accomplished using Oracle SOA. Hence this post. This is my first technical post with Blogger and owing to tool limitations and time constrains I tried to convey the core content which is needed to accomplish this and not discuss in detail. 

Requirement : 

Send JMS BytesMessage with its content in GZIP'ped format

Scenario taken for example here:


1. Read a file using FTP Adapter (your source can be anything according to your requirements)
2. GZIP the message content 
3. Send a BytesMessage over JMS
4. Java client to receive the message and decode. (This is given if you are implementing newly and want a client compatible to read your message)

Implementation: 

SOA Implementation to GZIP


I assume you have the base project which could read the file and post to to JMS as BytesMessage. (If you are not able to perform that please leave your comments I will try to compile one post for the same.)

So I believe you have your input file read and available in variable called InputMsgVariable and the variable which contains the data to be posted to JMS as Invoke_JMSMessageContent_Variable. (The variables can vary as per your project and please replace the same w.r.t your project) .

Now before we GZIP the data in InputMsgVariable  and write it to the Invoke_JMSMessageContent_Variable there are few prerequisite to be verified.
Please ensure your JMS adapter is configured to receive an opaque element as our GZIP content doesnot comply with any xsd.

Then drag and drop a JavaEmbedding Component present under Oracle Extensions.

Then paste the code below to gzip the data.
try            
        {            
  
            ByteArrayOutputStream baos = new ByteArrayOutputStream();           
            GZIPOutputStream gzos1 = new GZIPOutputStream(baos );           
            String str=(String)getVariableData("InputMsgVariable");           
   
            ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes(Charset.forName("UTF-8")));
            
            byte[] bytes = new byte[1024];           
            int length;          
            while ((length = bis.read(bytes)) >= 0) {           
               gzos1.write(bytes, 0, length);           
            }           
            gzos1.finish();       
            gzos1.flush();  
            gzos1.close();
            baos.flush();     
            baos.close();       
         

oracle.soa.common.util.Base64Encoder encoder = new oracle.soa.common.util.Base64Encoder();
String encodedString = oracle.soa.common.util.Base64Encoder.encode(baos.toByteArray());
      
setVariableData("Invoke_JMSMessageContent_Variable","opaque","/ns11:opaqueElement",encodedString);  

        }            
        catch(Exception e)            
        {            
        e.printStackTrace();            
        }

After completion please open the bpel in source view and add the below lines after process tag start. This will enable the compiler to use the respective import classes to compile our java Embedding.


 
 
 
 
 
 

 

Java Function to GunZIP

You can write QueueReceiver class which would connect to the JMS queue and receive the message and pass the byte array of the received message to the below function to unzip the gzipped content.
public static byte[] unZipBytes(byte[] fileContent) { byte[] byteArray = null; byte[] buf = new byte[1024]; int len; ByteArrayInputStream byteArrayInputStream = null; GZIPInputStream gzipInputStream = null; ByteArrayOutputStream byteArrayOutputStream = null; try { byteArrayInputStream = new ByteArrayInputStream(fileContent); gzipInputStream = new GZIPInputStream(byteArrayInputStream); byteArrayOutputStream = new ByteArrayOutputStream(); //Reading the data from the GZIP InputStream and writing it to OutputStream while ((len = gzipInputStream.read(buf)) > 0) { byteArrayOutputStream.write(buf, 0, len); }  
byteArray = byteArrayOutputStream.toByteArray(); //Close the file and stream byteArrayInputStream.close(); gzipInputStream.close(); byteArrayOutputStream.flush(); byteArrayOutputStream.close(); } catch (IOException ex) { throw new SystemException(ex); } return byteArray; } //Courtesy of Java gunzip fn goes to Ramalingam Kumaraswamy, Balasubramaniam

With the two integration pieces of code you can successfully gzip a JMS message from SOA Bpel and gunzip at the receiving java component