Cursling.getFtpDetails.java Source code

Java tutorial

Introduction

Here is the source code for Cursling.getFtpDetails.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Cursling;

import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

/**
 *
 * @author Ajit (P7109156) <Ness>
 */
public class getFtpDetails {

    parseFTPUrl obj_parseftpurl = new parseFTPUrl();
    FTPClient ftpClient;
    public static int global_ftp_timeout = 15;
    static final Logger logger = Logger.getLogger(getFtpDetails.class);
    String status = "---";
    String state = "---";

    public String getFtpMonitorDetails(String url, String text_check) throws IOException {

        try {
            try {
                ftpClient.logout();
                ftpClient.disconnect();
            } catch (Exception e) {
                //ignore line
            }
            ftpClient = new FTPClient();
            // ftpClient.setSoTimeout(10);
            ftpClient.setConnectTimeout(global_ftp_timeout * 1000);
            String ftp_host = "";
            int ftp_port = 0000;
            String ftp_user = "";
            String ftp_pass = "";
            String isTextPresenset = "No";

            //Parsing ftp url
            String[] details = obj_parseftpurl.getParseFTPUrl(url);
            ftp_host = details[0];
            ftp_port = Integer.parseInt(details[1]);
            ftp_user = details[2];
            ftp_pass = details[3];

            if (!ftp_host.equalsIgnoreCase("") && ftp_port != 0000) {
                ftpClient.connect(ftp_host, ftp_port);
            }

            if (!ftp_host.equalsIgnoreCase("") && ftp_port == 0000) {
                ftpClient.connect(ftp_host);
            }
            if (ftpClient.isConnected()) {
                status = "Up";
                state = "alive";
            } else {
                status = "Down";
                state = "dead";
            }
            if (!ftp_user.equalsIgnoreCase("") && !ftp_pass.equalsIgnoreCase("")) {
                ftpClient.login(ftp_user, ftp_pass);
                int reply = ftpClient.getReplyCode();

                if (FTPReply.isPositiveCompletion(reply)) {
                    status = "Up";
                    state = "alive";
                    if (text_check.equalsIgnoreCase("---")) { //do nothing
                        isTextPresenset = "---";
                    } else {
                        FTPFile[] files = ftpClient.listFiles(text_check);
                        if (files.length == 1) {
                            isTextPresenset = "Yes";
                        } else if (files.length == 0) {
                            isTextPresenset = "No";
                        }
                    }
                } else {
                    status = "Auth Failed";
                    isTextPresenset = "No";
                    state = "dead";
                }

            } else {
                isTextPresenset = "---";
            }

            ftpClient.logout();
            ftpClient.disconnect();
            return ("\"" + url + "\",\"" + status + "\",\"" + "---" + "\",\"" + isTextPresenset + "\",\"" + state
                    + "\"");

        } catch (Exception e) {

            if (e.toString().matches(".*java.net.ConnectException.*")) {
                logger.error("Error : java.net.ConnectException - " + url);
                status = "ConnectException";
            } else if (e.toString().matches(".*java.net.UnknownHostException.*")) {
                logger.error("Error : java.net.UnknownHostException - " + url);
                status = "UnknownHost";
            } else if (e.toString().matches(".*Timed.*out.*")) {
                logger.error("Error : " + e + " --- " + url);
                status = "TimeOut";
            } else {
                logger.error("Error - " + e + " --- " + url);
                status = "Failed";
            }

            return ("\"" + url + "\",\"" + status + "\",\"" + "---" + "\",\"" + "---" + "\",\"" + "dead" + "\"");

        }
    }
}