Saturday, March 19, 2016

Oracle API Gateway: How to rename ftp file name in OAG

I was working on a requirement in which I have to read a txt file from ftp directory and uploaded to the another ftp server, but the uploaded file name should be appended with the DateTime i,e suppose the file name is code_confuse_sheet.txt and then after uploading the name should change to code_confuse_sheet_20160319.txt
In this blog post I am going to show you how you can rename your ftp file name.

Note: I am not going to show end to end OAG policy and configuration, will show the code snippet and how to use it in your OAG policies.

Steps to rename ftp file name:

When you create FTP Poller listener then it reads the file from the ftp location and pass the file name in attribute ${ftp.file.name}
  • First step is to save the file name in local attribute say ftpfilename. This you can done by using Set Attribute Filter as shown below

  • After that alter the value of this attribute i.e. ftpfilename. This can be done by using Scripting Language Filter and configure as below

importPackage(Packages.com.vordel.trace);
importPackage(Packages.java.lang);
function invoke(msg)         {          
var file = msg.get("ftpfilename");
java.lang.System.out.println("Filename is " + file);
var splitstr =  file.substring(0,file.indexOf("."));
java.lang.System.out.println("Filename is after split " + splitstr);
var newFile = splitstr +"_20160319" + ".txt" ;
Trace.debug(file);
Trace.debug("End of scripting");
java.lang.System.out.println(file);
java.lang.System.out.println("New File Name " +newFile );
java.lang.System.out.println("End of scripting");
msg.put("ftp.file.name",newFile );
return true;

         }
Note: here I have hard coded the value of date just to show how you can add new string, you just change according to your usecase.
  • Use the file upload filter to upload the file and pass the name of the file as ${ftp.file.name}

  • Deploy and test the configurations.


2 comments:

  1. Hi ,

    Could you please let me know how you configured file upload policy here with screenshots.

    ReplyDelete
  2. Hi Anshul,

    Thank you for the post.

    Could you please let us know how to archive the file on source server and which filter we should use to get the same file name on target system which we have picked from source.

    ReplyDelete