Search in sources :

Example 26 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project jmeter by apache.

the class FTPSampler method sample.

@Override
public SampleResult sample(Entry e) {
    SampleResult res = new SampleResult();
    // Assume failure
    res.setSuccessful(false);
    String remote = getRemoteFilename();
    String local = getLocalFilename();
    boolean binaryTransfer = isBinaryMode();
    res.setSampleLabel(getName());
    final String label = getLabel();
    res.setSamplerData(label);
    try {
        res.setURL(new URL(label));
    } catch (MalformedURLException e1) {
        log.warn("Cannot set URL: " + e1.getLocalizedMessage());
    }
    InputStream input = null;
    FileInputStream fileIS = null;
    res.sampleStart();
    FTPClient ftp = new FTPClient();
    try {
        savedClient = ftp;
        final int port = getPortAsInt();
        if (port > 0) {
            ftp.connect(getServer(), port);
        } else {
            ftp.connect(getServer());
        }
        res.latencyEnd();
        int reply = ftp.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            if (ftp.login(getUsername(), getPassword())) {
                if (binaryTransfer) {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                // should probably come from the setup dialog
                ftp.enterLocalPassiveMode();
                boolean ftpOK = false;
                if (isUpload()) {
                    String contents = getLocalFileContents();
                    if (contents.length() > 0) {
                        // TODO - charset?
                        byte[] bytes = contents.getBytes();
                        input = new ByteArrayInputStream(bytes);
                        res.setBytes((long) bytes.length);
                    } else {
                        File infile = new File(local);
                        res.setBytes(infile.length());
                        // NOSONAR False positive, fileIS is closed in finally and not overwritten
                        fileIS = new FileInputStream(infile);
                        input = new BufferedInputStream(fileIS);
                    }
                    ftpOK = ftp.storeFile(remote, input);
                } else {
                    final boolean saveResponse = isSaveResponse();
                    // No need to close this
                    ByteArrayOutputStream baos = null;
                    OutputStream target = null;
                    OutputStream output = null;
                    try {
                        if (saveResponse) {
                            baos = new ByteArrayOutputStream();
                            target = baos;
                        }
                        if (local.length() > 0) {
                            // NOSONAR False positive, the output is closed in finally and not overwritten
                            output = new FileOutputStream(local);
                            if (target == null) {
                                target = output;
                            } else {
                                target = new TeeOutputStream(output, baos);
                            }
                        }
                        if (target == null) {
                            target = new NullOutputStream();
                        }
                        input = ftp.retrieveFileStream(remote);
                        if (input == null) {
                            // Could not access file or other error
                            res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                            res.setResponseMessage(ftp.getReplyString());
                        } else {
                            long bytes = IOUtils.copy(input, target);
                            ftpOK = bytes > 0;
                            if (saveResponse && baos != null) {
                                res.setResponseData(baos.toByteArray());
                                if (!binaryTransfer) {
                                    res.setDataType(SampleResult.TEXT);
                                }
                            } else {
                                res.setBytes(bytes);
                            }
                        }
                    } finally {
                        IOUtils.closeQuietly(target);
                        IOUtils.closeQuietly(output);
                    }
                }
                if (ftpOK) {
                    res.setResponseCodeOK();
                    res.setResponseMessageOK();
                    res.setSuccessful(true);
                } else {
                    res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                    res.setResponseMessage(ftp.getReplyString());
                }
            } else {
                res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                res.setResponseMessage(ftp.getReplyString());
            }
        } else {
            // TODO
            res.setResponseCode("501");
            res.setResponseMessage("Could not connect");
            //res.setResponseCode(Integer.toString(ftp.getReplyCode()));
            res.setResponseMessage(ftp.getReplyString());
        }
    } catch (IOException ex) {
        // TODO
        res.setResponseCode("000");
        res.setResponseMessage(ex.toString());
    } finally {
        savedClient = null;
        if (ftp.isConnected()) {
            try {
                ftp.logout();
            } catch (IOException ignored) {
            }
            try {
                ftp.disconnect();
            } catch (IOException ignored) {
            }
        }
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fileIS);
    }
    res.sampleEnd();
    return res;
}
Also used : MalformedURLException(java.net.MalformedURLException) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) FTPClient(org.apache.commons.net.ftp.FTPClient) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) SampleResult(org.apache.jmeter.samplers.SampleResult) File(java.io.File) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 27 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project ddf by codice.

the class TestFtp method createInsecureClient.

private FTPClient createInsecureClient() throws Exception {
    FTPClient ftp = new FTPClient();
    int attempts = 0;
    while (true) {
        try {
            ftp.connect(FTP_SERVER, Integer.parseInt(FTP_PORT.getPort()));
            break;
        } catch (SocketException e) {
            //a socket exception can be thrown if the ftp server is still in the process of coming up or down
            Thread.sleep(1000);
            if (attempts++ > 30) {
                throw e;
            }
        }
    }
    showServerReply(ftp);
    int connectionReply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(connectionReply)) {
        fail("FTP server refused connection: " + connectionReply);
    }
    boolean success = ftp.login(USERNAME, PASSWORD);
    showServerReply(ftp);
    if (!success) {
        fail("Could not log in to the FTP server.");
    }
    ftp.enterLocalPassiveMode();
    ftp.setControlKeepAliveTimeout(300);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    return ftp;
}
Also used : SocketException(java.net.SocketException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Aggregations

FTPClient (org.apache.commons.net.ftp.FTPClient)27 IOException (java.io.IOException)12 InputStream (java.io.InputStream)3 Path (org.apache.hadoop.fs.Path)3 DataXException (com.alibaba.datax.common.exception.DataXException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ConnectException (java.net.ConnectException)2 MalformedURLException (java.net.MalformedURLException)2 UnknownHostException (java.net.UnknownHostException)2 FTPFile (org.apache.commons.net.ftp.FTPFile)2 Configuration (org.apache.hadoop.conf.Configuration)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 ConnectionString (com.microsoft.azure.management.appservice.ConnectionString)1 TrafficManagerAzureEndpoint (com.microsoft.azure.management.trafficmanager.TrafficManagerAzureEndpoint)1 TrafficManagerExternalEndpoint (com.microsoft.azure.management.trafficmanager.TrafficManagerExternalEndpoint)1