Search in sources :

Example 16 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project camel by apache.

the class FtpOperations method listFiles.

public List<FTPFile> listFiles() throws GenericFileOperationFailedException {
    log.trace("listFiles()");
    try {
        final List<FTPFile> list = new ArrayList<FTPFile>();
        FTPFile[] files = client.listFiles();
        // can return either null or an empty list depending on FTP servers
        if (files != null) {
            list.addAll(Arrays.asList(files));
        }
        return list;
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Example 17 with FTPFile

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

the class WebAppUtils method removeFtpDirectory.

public static void removeFtpDirectory(FTPClient ftpClient, String path, IProgressIndicator pi) throws IOException {
    String prefix = "Removing from FTP server: ";
    FTPFile[] subFiles = ftpClient.listFiles(path);
    if (subFiles.length > 0) {
        for (FTPFile ftpFile : subFiles) {
            if (pi != null && pi.isCanceled())
                break;
            String currentFileName = ftpFile.getName();
            if (currentFileName.equals(".") || currentFileName.equals("..")) {
                // skip
                continue;
            }
            String path1 = path + "/" + currentFileName;
            if (ftpFile.isDirectory()) {
                // remove the sub directory
                removeFtpDirectory(ftpClient, path1, pi);
            } else {
                // delete the file
                if (pi != null)
                    pi.setText2(prefix + path1);
                ftpClient.deleteFile(path1);
            }
        }
    }
    if (pi != null)
        pi.setText2(prefix + path);
    ftpClient.removeDirectory(path);
    if (pi != null)
        pi.setText2("");
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 18 with FTPFile

use of org.apache.commons.net.ftp.FTPFile 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

FTPFile (org.apache.commons.net.ftp.FTPFile)18 IOException (java.io.IOException)9 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)4 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 GenericFile (org.apache.camel.component.file.GenericFile)2 FTPClient (org.apache.commons.net.ftp.FTPClient)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 Path (org.apache.hadoop.fs.Path)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 SocketException (java.net.SocketException)1 URI (java.net.URI)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 CancellationException (java.util.concurrent.CancellationException)1