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);
}
}
}