Search in sources :

Example 66 with FTPClient

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

the class FTPFileSystem method getHomeDirectory.

@Override
public Path getHomeDirectory() {
    FTPClient client = null;
    try {
        client = connect();
        Path homeDir = new Path(client.printWorkingDirectory());
        return homeDir;
    } catch (IOException ioe) {
        throw new FTPException("Failed to get home directory", ioe);
    } finally {
        try {
            disconnect(client);
        } catch (IOException ioe) {
            throw new FTPException("Failed to disconnect", ioe);
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 67 with FTPClient

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

the class FTPFileSystem method open.

@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
    FTPClient client = connect();
    Path workDir = new Path(client.printWorkingDirectory());
    Path absolute = makeAbsolute(workDir, file);
    FileStatus fileStat = getFileStatus(client, absolute);
    if (fileStat.isDirectory()) {
        disconnect(client);
        throw new FileNotFoundException("Path " + file + " is a directory.");
    }
    client.allocate(bufferSize);
    Path parent = absolute.getParent();
    // Change to parent directory on the
    // server. Only then can we read the
    // file
    // on the server by opening up an InputStream. As a side effect the working
    // directory on the server is changed to the parent directory of the file.
    // The FTP client connection is closed when close() is called on the
    // FSDataInputStream.
    client.changeWorkingDirectory(parent.toUri().getPath());
    InputStream is = client.retrieveFileStream(file.getName());
    FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is, client, statistics));
    if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
        // The ftpClient is an inconsistent state. Must close the stream
        // which in turn will logout and disconnect from FTP server
        fis.close();
        throw new IOException("Unable to open file: " + file + ", Aborting");
    }
    return fis;
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 68 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project cubrid-manager by CUBRID.

the class FtpUtil method connectServer.

/**
	 * Connect the server
	 *
	 * @param server String
	 * @param userName String
	 * @param userPassword String
	 * @param driverPath String
	 * @throws IOException The exception
	 */
public void connectServer(String server, String userName, String userPassword, String driverPath) throws IOException {
    ftpClient = new FTPClient();
    ftpClient.connect(server);
    ftpClient.login(userName, userPassword);
    ftpClient.changeWorkingDirectory(driverPath);
    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();
}
Also used : FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 69 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project azure-tools-for-java by Microsoft.

the class WebAppUtils method getFtpConnection.

public static FTPClient getFtpConnection(PublishingProfile pp) throws IOException {
    FTPClient ftp = new FTPClient();
    System.out.println("\t\t" + pp.ftpUrl());
    System.out.println("\t\t" + pp.ftpUsername());
    System.out.println("\t\t" + pp.ftpPassword());
    URI uri = URI.create("ftp://" + pp.ftpUrl());
    ftp.connect(uri.getHost(), 21);
    final int replyCode = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(replyCode)) {
        ftp.disconnect();
        throw new ConnectException("Unable to connect to FTP server");
    }
    if (!ftp.login(pp.ftpUsername(), pp.ftpPassword())) {
        throw new ConnectException("Unable to login to FTP server");
    }
    ftp.setControlKeepAliveTimeout(Constants.connection_read_timeout_ms);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    //Switch to passive mode
    ftp.enterLocalPassiveMode();
    return ftp;
}
Also used : URI(java.net.URI) FTPClient(org.apache.commons.net.ftp.FTPClient) ConnectException(java.net.ConnectException)

Example 70 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project azure-tools-for-java by Microsoft.

the class WebAppUtils method deployCustomJdk.

public static void deployCustomJdk(WebApp webApp, String jdkDownloadUrl, WebContainer webContainer, IProgressIndicator indicator) throws IOException, InterruptedException, WebAppException {
    FTPClient ftp = null;
    String customJdkFolderName = null;
    try {
        PublishingProfile pp = webApp.getPublishingProfile();
        ftp = getFtpConnection(pp);
        if (indicator != null)
            indicator.setText("Deleting custom jdk artifacts, if any (takes a while)...");
        removeCustomJdkArtifacts(ftp, indicator);
        if (indicator != null)
            indicator.setText("Uploading scripts...");
        uploadJdkDownloadScript(ftp, jdkDownloadUrl);
        //            if (indicator != null) indicator.setText("Starting the service...");
        //            webApp.start();
        final String siteUrl = "https://" + webApp.defaultHostName();
        // send get to activate the script
        sendGet(siteUrl);
        // Polling report.txt...
        if (indicator != null)
            indicator.setText("Checking the JDK gets downloaded and unpacked...");
        //int step = 0;
        while (!doesRemoteFileExist(ftp, ftpRootPath, reportFilename)) {
            if (indicator != null && indicator.isCanceled())
                throw new CancellationException("Canceled by user.");
            //if (step++ > 3) checkFreeSpaceAvailability(ftp);
            Thread.sleep(5000);
            sendGet(siteUrl);
        }
        if (indicator != null)
            indicator.setText("Checking status...");
        OutputStream reportFileStream = new ByteArrayOutputStream();
        ftp.retrieveFile("report.txt", reportFileStream);
        String reportFileString = reportFileStream.toString();
        if (reportFileString.startsWith("FAIL")) {
            String err = reportFileString.substring(reportFileString.indexOf(":" + 1));
            throw new WebAppException(err);
        }
        // get top level jdk folder name (under jdk folder)
        FTPFile[] ftpDirs = ftp.listDirectories(ftpJdkPath);
        if (ftpDirs.length != 1) {
            String err = "Bad JDK archive. Please make sure the JDK archive contains a single JDK folder. For example, 'my-jdk1.7.0_79.zip' archive should contain 'jdk1.7.0_79' folder only";
            throw new WebAppException(err);
        }
        customJdkFolderName = ftpDirs[0].getName();
        uploadWebConfigForCustomJdk(ftp, webApp, customJdkFolderName, webContainer, indicator);
    } catch (IOException | WebAppException | InterruptedException ex) {
        if (doesRemoteFolderExist(ftp, ftpRootPath, jdkFolderName)) {
            indicator.setText("Error happened. Cleaning up...");
            removeFtpDirectory(ftp, ftpJdkPath, indicator);
        }
        throw ex;
    } finally {
        indicator.setText("Removing working data from server...");
        cleanupWorkerData(ftp);
        if (ftp != null && ftp.isConnected()) {
            ftp.disconnect();
        }
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) FTPFile(org.apache.commons.net.ftp.FTPFile) FTPClient(org.apache.commons.net.ftp.FTPClient)

Aggregations

FTPClient (org.apache.commons.net.ftp.FTPClient)128 IOException (java.io.IOException)78 FTPFile (org.apache.commons.net.ftp.FTPFile)36 Test (org.junit.Test)25 InputStream (java.io.InputStream)20 FrameworkException (org.structr.common.error.FrameworkException)20 Tx (org.structr.core.graph.Tx)20 FtpTest (org.structr.web.files.FtpTest)20 File (java.io.File)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 FileInputStream (java.io.FileInputStream)10 FileOutputStream (java.io.FileOutputStream)8 OutputStream (java.io.OutputStream)5 UnknownHostException (java.net.UnknownHostException)5 ConnectException (java.net.ConnectException)4 PrintCommandListener (org.apache.commons.net.PrintCommandListener)4 FTPSClient (org.apache.commons.net.ftp.FTPSClient)4 FTPUtils (com.cas.sim.tis.util.FTPUtils)3 FileNotFoundException (java.io.FileNotFoundException)3 MalformedURLException (java.net.MalformedURLException)3