A test Java Mapping code for FTP connection and file upload and download (Input payload is sent as it is as output in this code).

This Code is just kind of POC for those who wants to involve File upload and download feature in their project.

Add\modify the input file stream code for sending your input payload to the FTP location. In my code I just sent local file to FTP.

/*

* Created on May 30, 2014

*

* To change the template for this generated file go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

/**

* @author ashutosh.a.upadhyay

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.AbstractTrace;

import com.sap.aii.mapping.api.StreamTransformationException;

import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Result;

import javax.xml.transform.Source;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPReply;

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class FTPTest implements StreamTransformation {

  private Map map = null;

  private AbstractTrace trace = null;

  public void setParameter(Map arg0) {

  map = arg0;  // Store reference to the mapping parameters

  if (map == null) {

  this.map = new HashMap();

  }

  }

  /*

  public static void main(String args[]) {  //FOR EXTERNAL STANDALONE TESTING

  try{

  FileInputStream fin = new FileInputStream (“C:/Users/ashutosh.a.upadhyay/My Documents/ashuXML1.xml”); //INPUT FILE (PAYLOAD)

  FileOutputStream fout = new FileOutputStream (“C:/Users/ashutosh.a.upadhyay/My Documents/ashuXML2.xml”); //OUTPUT FILE (PAYLOAD)

  FTPTest mapping = new FTPTest ();

  mapping.execute(fin, fout);

  }

  catch (Exception e1){

  e1.printStackTrace();

  }

  }

        */

  public void execute(InputStream inputstream, OutputStream outputstream) throws StreamTransformationException  {

  try{

  //sending the input XML as it is for further processing…

  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

  DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

  Document xml = documentBuilder.parse(inputstream);

  

  // write XML

  TransformerFactory transformerFactory = TransformerFactory.newInstance();

  Transformer transformer = transformerFactory.newTransformer();

  Result result = new StreamResult(outputstream);

  Source domSource = new DOMSource(xml);

  transformer.transform(domSource, result);

  //FTP locate

  FTPClient client = new FTPClient();

  client.connect(“<FTP ServerHostName>”);

  int reply = client.getReplyCode();

  if (!FTPReply.isPositiveCompletion(reply)) {

  System.out.println(“FTP server refused connection”);

  client.disconnect();

  }

  else

  System.out.println(“FTP Connection Establised”);

  client.login(“anonymous”, “”);

  reply = client.getReplyCode();

  if (!FTPReply.isPositiveCompletion(reply)) {

  System.out.println(“User NOT Authenticated “);

  client.disconnect();

  }

  else{

  System.out.println(“User Authenticated “);

  }

  client.changeWorkingDirectory(“<FTPDirectory>”);

  System.out.println(“Directory searched”);

  FileOutputStream fStream = new FileOutputStream(“C:/Users/ashutosh.a.upadhyay/My Documents/FTPDemo2.txt”);

  client.retrieveFile(“abcd.xml”,fStream);

  fStream.close();

  File file = new File(“C:/Users/ashutosh.a.upadhyay/My Documents/FTPDemo2.txt”);

  String remoteFile = “test.xml”;  

  InputStream inputStream = new FileInputStream(file);  

  System.out.println(“Start uploading file…”);

  boolean done = client.storeFile(remoteFile, inputStream);  

  inputStream.close();  

  if (done) {      

  System.out.println(“The file is uploaded successfully.”);  

  }

  else{

  System.out.println(“The file is not uploaded.”);

  }

  client.disconnect();

  System.out.println(“FTP connection disconnected”);

  }

  //Code to Transform the Input stream to Output

  catch (ParserConfigurationException e) {

  e.printStackTrace();

  throw new StreamTransformationException(“Can not create DocumentBuilder.”+ e.getMessage(), e);

  }

  catch (SAXException e) {

  e.printStackTrace();

  throw new StreamTransformationException(“Can not read XML. “+ e.getMessage(), e);

  }

  catch (IOException e) {

  e.printStackTrace();

  throw new StreamTransformationException(“Can not read XML. “+ e.getMessage(), e);

  }

  catch (TransformerConfigurationException e) {

  e.printStackTrace();

  throw new StreamTransformationException(“Can not create Transformer. “+ e.getMessage(), e);

  }

  catch (TransformerException e) {

  e.printStackTrace();

  throw new StreamTransformationException(“Unhandled Exception. “+ e.getMessage(), e);

  }

  catch(Exception e){

  e.printStackTrace();

  throw new StreamTransformationException(“Download Failed :: General Exception :: “+ e.getMessage(), e);

  }

  }

  }

For E.g.

———————————————————————————————————————————————————————–

import java.io.BufferedReader;

import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileOutputStream;

  import java.io.IOException;

  import java.io.InputStream;

  import java.io.InputStreamReader;

  import java.io.OutputStream;

  import java.util.Map;

  import com.sap.aii.mapping.api.StreamTransformation;

  import com.sap.aii.mapping.api.AbstractTrace;

  import com.sap.aii.mapping.api.StreamTransformationException;

  import java.util.HashMap;

  import org.apache.commons.net.ftp.FTPClient;

  import org.apache.commons.net.ftp.FTPReply;

  public class FTPTest implements StreamTransformation {

  private Map map = null;

  private AbstractTrace trace = null;

  public void setParameter(Map arg0) {

  map = arg0;   // Store reference to the mapping parameters

  if (map == null) {

  this.map = new HashMap();

  }

  }

  /*public static void main(String args[]) {  //FOR EXTERNAL STANDALONE TESTING

  try{

  FileInputStream fin = new FileInputStream(“C:/Users/ashutosh.a.upadhyay/Desktop/Data.xml”);

  FileOutputStream  fout = new FileOutputStream  (“C:/Users/ashutosh.a.upadhyay/My Documents/ashuXML2.xml”); //OUTPUT FILE (PAYLOAD)

  FTPTest mapping = new FTPTest ();

  mapping.execute(fin, fout);

  }

  catch (Exception e1){

  e1.printStackTrace();

  }

  }*/

    

  public void execute(InputStream inputstream, OutputStream outputstream) throws StreamTransformationException  {

  try{

  String text = “”;

  String line = “”;

  System.out.println(inputstream.toString());

  InputStreamReader in = new InputStreamReader(inputstream);

  BufferedReader bin = new BufferedReader(in);

  while((line = bin.readLine()) != null){

  System.out.println(line);

  text = text+line;

  text = text+”\n”;

  }

  System.out.println(text);

  outputstream.write(text.getBytes());

  FTPClient client = new FTPClient();

  client.connect(“<FTPServerName>”);

  int reply = client.getReplyCode();

  if (!FTPReply.isPositiveCompletion(reply)) {

  //trace.addInfo(“FTP server refused connection”);

  System.out.println(“FTP server refused connection”);

  client.disconnect();

  }

  else{

  //trace.addInfo(“FTP Connection Establised”);

  System.out.println(“FTP Connection Establised”);

  }

  client.login(“anonymous”, “”);

  reply = client.getReplyCode();

  if (!FTPReply.isPositiveCompletion(reply)) {

  System.out.println(“User NOT Authenticated “);

  client.disconnect();

  }

  else{

  //trace.addInfo(“User Authenticated “);

  System.out.println(“User Authenticated “);

  }

  client.changeWorkingDirectory(“<Directory>”);

  System.out.println(“Directory searched”);

  OutputStream outputStream = client.storeFileStream(remoteFile);

  outputStream.write(text.getBytes());

  outputStream.close();

  in.close();

  bin.close();

  outputstream.close();

  inputstream.close();

  boolean done = client.completePendingCommand();

  if (done){

  //trace.addInfo(“The file is uploaded successfully.”);       

  System.out.println(“The file is uploaded successfully.”);    

  }

  else{

  //trace.addInfo(“The file is not uploaded.”);

    System.out.println(“The file is not uploaded.”);

  }

  client.disconnect();

  //trace.addInfo(“FTP connection disconnected”);

  System.out.println(“FTP connection disconnected”);

  //writing output file

  in.close();

  bin.close();

  outputstream.close();

  inputstream.close();

  }

           

  //Code to Transform the Input stream to Output

  catch (IOException e) {

  e.printStackTrace();

  throw new StreamTransformationException(“Can not read XML. “+ e.getMessage(), e);

  }

  catch(Exception e){

  e.printStackTrace();

  throw new StreamTransformationException(“General Exception :: “+ e.getMessage(), e);

  }

  }

  }

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Ashutosh Upadhyay Post author

    MAPPING PROGRAM to DOWNLOAD THE FILE FROM FTP (POC only)

    import java.io.BufferedReader;

      import java.io.FileInputStream;

      import java.io.FileOutputStream;

      import java.io.IOException;

      import java.io.InputStream;

      import java.io.InputStreamReader;

      import java.io.OutputStream;

      import java.util.Map;

      import com.sap.aii.mapping.api.StreamTransformation;

      import com.sap.aii.mapping.api.AbstractTrace;

      import com.sap.aii.mapping.api.StreamTransformationException;

      import java.util.HashMap;

      import org.apache.commons.net.ftp.FTPClient;

      import org.apache.commons.net.ftp.FTPReply;

      public class FTPTestdownload implements StreamTransformation {

      private Map map = null;

      private AbstractTrace trace = null;

      public void setParameter(Map arg0) {

      map = arg0;   // Store reference to the mapping parameters

      if (map == null) {

      this.map = new HashMap();

      }

      }

      public static void main(String args[]) {  //FOR EXTERNAL STANDALONE TESTING

      try{

      FileInputStream fin = new FileInputStream(“C:/Users/ashutosh.a.upadhyay/Desktop/Data.xml”);

      FileOutputStream  fout = new FileOutputStream  (“C:/Users/ashutosh.a.upadhyay/My Documents/FTP2.xml”); //OUTPUT FILE (PAYLOAD)

      FTPTestdownload mapping = new FTPTestdownload();

      mapping.execute(fin, fout);

      }

      catch (Exception e1){

      e1.printStackTrace();

      }

      }

        

      public void execute(InputStream inputstream, OutputStream outputstream) throws StreamTransformationException  {

      try{

      String text = “”;

      String line = “”;

      System.out.println(inputstream.toString());

      InputStreamReader in = new InputStreamReader(inputstream);

      BufferedReader bin = new BufferedReader(in);

      while((line = bin.readLine()) != null){

      System.out.println(line);

      text = text+line;

      text = text+”\n”;

      }

      System.out.println(text);

      outputstream.write(text.getBytes());

      FTPClient client = new FTPClient();

      client.connect(“GSZAJNB604”);

      int reply = client.getReplyCode();

      if (!FTPReply.isPositiveCompletion(reply)) {

      //trace.addInfo(“FTP server refused connection”);

      System.out.println(“FTP server refused connection”);

      client.disconnect();

      }

      else{

      //trace.addInfo(“FTP Connection Establised”);

      System.out.println(“FTP Connection Establised”);

      }

      client.login(“anonymous”, “”);

      reply = client.getReplyCode();

      if (!FTPReply.isPositiveCompletion(reply)) {

      System.out.println(“User NOT Authenticated “);

      client.disconnect();

      }

      else{

      //trace.addInfo(“User Authenticated “);

      System.out.println(“User Authenticated “);

      }

      client.changeWorkingDirectory(“/AA_DATA_MAT_SER_SUP/Outbound/AA_SA_HO_SAP/AA_SA_HO_SAP_O_MAT/Ready”);

      System.out.println(“Directory searched”);     

      System.out.println(“Downloading the file…”);

      OutputStream out = outputstream;

                 client.retrieveFile(“FTPremote.xml”,out);

      in.close();

      bin.close();

      outputstream.close();

      inputstream.close();

      client.disconnect();

      //trace.addInfo(“FTP connection disconnected”);

      System.out.println(“FTP connection disconnected”);

      }

               

      catch (IOException e) {

      e.printStackTrace();

      throw new StreamTransformationException(“Can not read XML. “+ e.getMessage(), e);

      }

      catch(Exception e){

      e.printStackTrace();

      throw new StreamTransformationException(“General Exception :: “+ e.getMessage(), e);

      }

      }

      }

    (0) 

Leave a Reply