Sunday 23 September 2012

How to FTP flat file from BPEL

Recently I had a requirement, I’ve to publish a flat file which is position based to FTP server from BPEL.But the problem is I don’t have any nXSD/DTD to use in FTP adapter.Client will pick the file from FTP location based on image of file content.I first tried with opaque option in FTP adapter in vain because from BPEL I’m generating the whole data in String format and opaque require base64 encoding.Another constraint is that if I publish any file with a predefined schema then the actual content will be enclosed by XML tag which i don’t want. So here is my solution…

At first download ftp4j jar from http://www.sauronsoftware.it/projects/ftp4j/ which makes your life more easier.In BPEL I’ve the data in string format , I’ve to convert the same in a file content , then need to put the same in FTP server.

Place the ftp4j file in SCA-INF/lib folder of your BPEL project, obviously I’m using SOA11.1.1.5.So when you compile or deploy the project it will be referenced in the classpath.You need not to place that jar anywhere else :)

Then add the following import in your .bpel file,

<bpelx:exec import="java.util.*"/>
<bpelx:exec import="java.lang.*"/>
<bpelx:exec import="it.sauronsoftware.ftp4j.FTPClient"/>
<bpelx:exec import="it.sauronsoftware.ftp4j.FTPFile"/>
<bpelx:exec import="java.io.File"/>
<bpelx:exec import="java.io.FileWriter"/>
<bpelx:exec import="oracle.xml.parser.v2.XMLElement"/>

Here is the code which will FTP your file,

try{    
                String FTP_SERVER="XXXX";    
                String USRE_NAME="YYY";    
                String PASS="ZZZ";    
                String PDATA =(String)getVariableData("filePDATA");  
                addAuditTrailEntry(PDATA);   
                XMLElement fileIn=(XMLElement)getVariableData("SelectPDATAOfFileDB_InputVariable","SelectPDATAOfFileDBInput_msg","/ns6:SelectPDATAOfFileDBInput/ns6:FILE_NAME");
                addAuditTrailEntry(fileIn.getTextContent()); 
                FTPClient client=new FTPClient();  
                client.connect(FTP_SERVER);  
                client.login(USRE_NAME,PASS);  
                System.out.println("Connected to server");  
                addAuditTrailEntry("Connected to FTP server");   
                File makefile=new File(fileIn.getTextContent());  
                FileWriter fwrite = new FileWriter(makefile);  
                fwrite.write(PDATA);  
                fwrite.flush();  
                fwrite.close();  
                client.upload(makefile);  
                client.logout();  
                client.disconnect(true);   
            }catch(Exception e){    
                System.out.println(e.getMessage());    
                addAuditTrailEntry(e.getMessage());     
            }

Here you have to provide your FTP server URL , username and password.You can create 3 preference variable and refer the same using getVariableData from Java.In my program filePDATA variable store file content in BPEL and I’m also passing the filename as input to my program and at last using the method of FTPClient of ftp4j library I’m placing the file in remote server.As the file content is String , so you have full control on that , I mean you can manipulate data as you need. 

Looks so simple , but took some amount of time to figure out :)