/**
 * @author Raj Prakash
 */
package com.tandbergtv.workflow.webservice.filesubsystem;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPClientWrapper extends FTPClient {

	public boolean connLogin (String host, String userName, String password)
	throws  IOException
	{
		connect(host);
		if (FTPReply.isPositiveCompletion(getReplyCode()))
		{
			if (login(userName, password))
				return true;
			else
			{
				disconnect();
				return false;
			}
		}
		else
			return false;	
	}

	/**
	 * @param remoteFile
	 * @param locFile			should have complete path to the file (including file name)
	 * @return
	 * @throws IOException
	 */
	public boolean downloadFile (String remoteFile, String locFile)
		throws IOException
	{
		enterLocalPassiveMode();
		setFileType(FTP.BINARY_FILE_TYPE);
		FileOutputStream out = new FileOutputStream(locFile);
		boolean result = retrieveFile(remoteFile, out);
		out.close();
		return result;
	}

	/**
	 * @param locFile
	 * @param remoteFile		should have complete path to the file (including file name)
	 * @return
	 * @throws IOException
	 */
	public boolean uploadFile (String locFile, String remoteFile)
		throws IOException
	{
		enterLocalPassiveMode();
		setFileType(FTP.BINARY_FILE_TYPE);
		FileInputStream in = new FileInputStream(locFile);
		boolean result = storeFile(remoteFile, in);
		in.close();
		return result;
	}

}
