Example usage for org.apache.commons.net.ftp FTPClient getRemoteAddress

List of usage examples for org.apache.commons.net.ftp FTPClient getRemoteAddress

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClient getRemoteAddress.

Prototype

public InetAddress getRemoteAddress() 

Source Link

Usage

From source file:fr.acxio.tools.agia.ftp.FTPUploadTasklet.java

@Override
public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception {
    FTPClient aClient = ftpClientFactory.getFtpClient();

    RegexFilenameFilter aFilter = new RegexFilenameFilter();
    aFilter.setRegex(regexFilename);/*w  w w.ja  va2 s .  c  o m*/

    try {
        File aLocalDir = new File(localBaseDir);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Listing : {} ({}) for upload to [{}]", localBaseDir, regexFilename,
                    aClient.getRemoteAddress().toString());
        }

        File[] aLocalFiles = aLocalDir.listFiles(aFilter);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("  {} file(s) found", aLocalFiles.length);
        }

        for (File aLocalFile : aLocalFiles) {

            if (sContribution != null) {
                sContribution.incrementReadCount();
            }

            URI aRemoteFile = new URI(remoteBaseDir).resolve(aLocalFile.getName());
            InputStream aInputStream;
            aInputStream = new FileInputStream(aLocalFile);
            try {

                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(" Uploading : {} => {}", aLocalFile.getAbsolutePath(),
                            aRemoteFile.toASCIIString());
                }

                aClient.storeFile(aRemoteFile.toASCIIString(), aInputStream);

                if (sContribution != null) {
                    sContribution.incrementWriteCount(1);
                }
            } finally {
                aInputStream.close();
            }
        }
    } finally {
        aClient.logout();
        aClient.disconnect();
    }

    return RepeatStatus.FINISHED;
}

From source file:fr.acxio.tools.agia.ftp.FTPDownloadTasklet.java

@Override
public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception {
    FTPClient aClient = ftpClientFactory.getFtpClient();

    RegexFilenameFilter aFilter = new RegexFilenameFilter();
    aFilter.setRegex(regexFilename);//from  w w w . j av a  2 s .co  m
    try {
        URI aRemoteBaseURI = new URI(remoteBaseDir);
        URI aRemoteBasePath = new URI(aRemoteBaseURI.toASCIIString() + SEPARATOR);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Listing : [{}] {} ({})", aClient.getRemoteAddress().toString(),
                    aRemoteBaseURI.toASCIIString(), regexFilename);
        }

        FTPFile[] aRemoteFiles = aClient.listFiles(aRemoteBaseURI.toASCIIString(), aFilter);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("  {} file(s) found", aRemoteFiles.length);
        }

        for (FTPFile aRemoteFile : aRemoteFiles) {

            if (sContribution != null) {
                sContribution.incrementReadCount();
            }

            File aLocalFile = new File(localBaseDir, aRemoteFile.getName());
            URI aRemoteTFile = aRemoteBasePath.resolve(aRemoteFile.getName());

            FileOutputStream aOutputStream = new FileOutputStream(aLocalFile);
            try {

                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(" Downloading : {} => {}", aRemoteTFile.toASCIIString(),
                            aLocalFile.getAbsolutePath());
                }

                aClient.retrieveFile(aRemoteTFile.toASCIIString(), aOutputStream);
                if (removeRemote) {

                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info(" Deleting : {}", aRemoteTFile.toASCIIString());
                    }

                    aClient.deleteFile(aRemoteTFile.toASCIIString());
                }

                if (sContribution != null) {
                    sContribution.incrementWriteCount(1);
                }

            } finally {
                aOutputStream.close();
            }
        }
    } finally {
        aClient.logout();
        aClient.disconnect();
    }

    return RepeatStatus.FINISHED;
}

From source file:lucee.runtime.tag.Ftp.java

/**
 * get url of the working directory//  w  ww .j  a v a 2 s .  c  o  m
 * @return FTPCLient
 * @throws IOException
 * @throws PageException
 */
private FTPClient actionGetCurrentURL() throws PageException, IOException {
    FTPClient client = getClient();
    String pwd = client.printWorkingDirectory();
    Struct cfftp = writeCfftp(client);
    cfftp.setEL("returnValue", "ftp://" + client.getRemoteAddress().getHostName() + pwd);
    return client;
}

From source file:lucee.runtime.tag.Ftp.java

/**
 * List data of a ftp connection/*from w  w w.  ja v  a  2s.co  m*/
 * @return FTPCLient
 * @throws PageException
 * @throws IOException
 */
private FTPClient actionListDir() throws PageException, IOException {
    required("name", name);
    required("directory", directory);

    FTPClient client = getClient();
    FTPFile[] files = client.listFiles(directory);
    if (files == null)
        files = new FTPFile[0];

    String[] cols = new String[] { "attributes", "isdirectory", "lastmodified", "length", "mode", "name",
            "path", "url", "type", "raw" };
    String[] types = new String[] { "VARCHAR", "BOOLEAN", "DATE", "DOUBLE", "VARCHAR", "VARCHAR", "VARCHAR",
            "VARCHAR", "VARCHAR", "VARCHAR" };

    lucee.runtime.type.Query query = new QueryImpl(cols, types, 0, "query");

    // translate directory path for display
    if (directory.length() == 0)
        directory = "/";
    else if (directory.startsWith("./"))
        directory = directory.substring(1);
    else if (directory.charAt(0) != '/')
        directory = '/' + directory;
    if (directory.charAt(directory.length() - 1) != '/')
        directory = directory + '/';

    pageContext.setVariable(name, query);
    int row = 0;
    for (int i = 0; i < files.length; i++) {
        FTPFile file = files[i];
        if (file.getName().equals(".") || file.getName().equals(".."))
            continue;
        query.addRow();
        row++;
        query.setAt("attributes", row, "");
        query.setAt("isdirectory", row, Caster.toBoolean(file.isDirectory()));
        query.setAt("lastmodified", row, new DateTimeImpl(file.getTimestamp()));
        query.setAt("length", row, Caster.toDouble(file.getSize()));
        query.setAt("mode", row, FTPConstant.getPermissionASInteger(file));
        query.setAt("type", row, FTPConstant.getTypeAsString(file.getType()));
        //query.setAt("permission",row,FTPConstant.getPermissionASInteger(file));
        query.setAt("raw", row, file.getRawListing());
        query.setAt("name", row, file.getName());
        query.setAt("path", row, directory + file.getName());
        query.setAt("url", row,
                "ftp://" + client.getRemoteAddress().getHostName() + "" + directory + file.getName());
    }
    writeCfftp(client);
    return client;
}

From source file:org.grouter.core.readers.FtpReaderJob.java

/**
 * Use configuration parameters to create a connection to the ftp endpoint.
 * @return a FTPClient instance/*from  ww  w  . ja v  a 2 s . c  o  m*/
 * @throws Exception if connection problems
 */
private FTPClient initConnection() throws Exception {
    FTPClient client;
    String host = null;
    String strPort;
    try {

        Map map = node.getInBound().getEndPointContext();
        host = node.getInBound().getUri();
        strPort = (String) map.get(FTP_PORT);
        client = new FTPClient();
        int port = FTP_DEFAULT_PORT;
        if (StringUtils.isNotEmpty(strPort)) {
            port = Integer.parseInt(strPort);
            client.connect(host, port);
            int reply = client.getReplyCode();
            logger.debug("reply :" + reply);
            if (FTPReply.isPositiveCompletion(reply)) {
                logger.info("Connected to ftp server :" + client.getRemoteAddress().getHostAddress());
            }
        } else {
            client.connect(host, port);
            int reply = client.getReplyCode();
            logger.debug("reply :" + reply);
            if (FTPReply.isPositiveCompletion(reply)) {
                logger.info("Connected to ftp server :" + client.getRemoteAddress().getHostAddress());
            }

        }

        String user = (String) map.get(FTP_AUTH_USER);
        String pwd = (String) map.get(FTP_AUTH_PASSWORD);
        if (StringUtils.isNotEmpty(user) && StringUtils.isNotEmpty(pwd)) {
            client.login(user, pwd);
            int replyCode = client.getReplyCode();
            if (FTPReply.isPositiveCompletion(replyCode)) {
                logger.info("Logged into ftp server :" + host);
            }
        } else {
            client.login("anonymous", "mymail@mail.com");
            int replyCode = client.getReplyCode();
            if (FTPReply.isPositiveCompletion(replyCode)) {
                logger.info("Logged into ftp server :" + host);
            }
        }
    } catch (Exception e) {
        throw new Exception("Could not establish connection with ftp endpoint using host:" + host, e);
    }
    return client;
}