Search in sources :

Example 51 with FTPFile

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

the class Ftp method retrieveFileList.

/**
   * Get the list of files to transfer
   */
void retrieveFileList(String dir) {
    if (info) {
        if (dir == null || dir.isEmpty()) {
            info(null, "  Listing the current working FTP directory");
        } else {
            info(null, "  Listing " + dir);
        }
    }
    try {
        FTPFile[] files = ftp.listFiles(dir);
        ArrayList<FTPFile> dirs = new ArrayList<FTPFile>();
        for (FTPFile file : files) {
            String name = file.getName();
            if (file.isFile()) {
                if (filePattern == null || Pattern.matches(filePattern, name)) {
                    if (dir != null && !dir.isEmpty()) {
                        if (dir.endsWith("/")) {
                            name = dir + name;
                        } else {
                            name = dir + "/" + name;
                        }
                    }
                    if (!newOnly || !isTargetExists(name)) {
                        fileCnt++;
                        ftpSizeInBytes += file.getSize();
                        filesQueue.add(name);
                        filesMap.put(name, file);
                    }
                }
            } else {
                if (subdir && !name.equals(".") && !name.equals("..")) {
                    dirCnt++;
                    dirs.add(file);
                }
            }
        }
        if (subdir) {
            for (FTPFile d : dirs) {
                String sd = d.getName();
                if (dir != null && !dir.isEmpty()) {
                    if (dir.endsWith("/")) {
                        sd = dir + sd;
                    } else {
                        sd = dir + "/" + sd;
                    }
                }
                retrieveFileList(sd);
            }
        }
    } catch (IOException e) {
        exec.signal(e);
    }
}
Also used : ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Example 52 with FTPFile

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

the class FTPFileSystem method getFileStatus.

/**
   * Convenience method, so that we don't open a new connection when using this
   * method from within another method. Otherwise every API invocation incurs
   * the overhead of opening/closing a TCP connection.
   */
private FileStatus getFileStatus(FTPClient client, Path file) throws IOException {
    FileStatus fileStat = null;
    Path workDir = new Path(client.printWorkingDirectory());
    Path absolute = makeAbsolute(workDir, file);
    Path parentPath = absolute.getParent();
    if (parentPath == null) {
        // root dir
        // Length of root dir on server not known
        long length = -1;
        boolean isDir = true;
        int blockReplication = 1;
        // Block Size not known.
        long blockSize = DEFAULT_BLOCK_SIZE;
        // Modification time of root dir not known.
        long modTime = -1;
        Path root = new Path("/");
        return new FileStatus(length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this));
    }
    String pathName = parentPath.toUri().getPath();
    FTPFile[] ftpFiles = client.listFiles(pathName);
    if (ftpFiles != null) {
        for (FTPFile ftpFile : ftpFiles) {
            if (ftpFile.getName().equals(file.getName())) {
                // file found in dir
                fileStat = getFileStatus(ftpFile, parentPath);
                break;
            }
        }
        if (fileStat == null) {
            throw new FileNotFoundException("File " + file + " does not exist.");
        }
    } else {
        throw new FileNotFoundException("File " + file + " does not exist.");
    }
    return fileStat;
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) FileNotFoundException(java.io.FileNotFoundException) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 53 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 54 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)

Example 55 with FTPFile

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

the class FtpOperations method retrieveFileToStreamInBody.

@SuppressWarnings("unchecked")
private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException {
    boolean result;
    try {
        GenericFile<FTPFile> target = (GenericFile<FTPFile>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
        ObjectHelper.notNull(target, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
        String remoteName = name;
        String currentDir = null;
        if (endpoint.getConfiguration().isStepwise()) {
            // remember current directory
            currentDir = getCurrentDirectory();
            // change directory to path where the file is to be retrieved
            // (must do this as some FTP servers cannot retrieve using absolute path)
            String path = FileUtil.onlyPath(name);
            if (path != null) {
                changeCurrentDirectory(path);
            }
            // remote name is now only the file name as we just changed directory
            remoteName = FileUtil.stripPath(name);
        }
        log.trace("Client retrieveFile: {}", remoteName);
        if (endpoint.getConfiguration().isStreamDownload()) {
            InputStream is = client.retrieveFileStream(remoteName);
            target.setBody(is);
            exchange.getIn().setHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, is);
            result = true;
        } else {
            // read the entire file into memory in the byte array
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            result = client.retrieveFile(remoteName, bos);
            // close the stream after done
            IOHelper.close(bos);
            target.setBody(bos.toByteArray());
        }
        // store client reply information after the operation
        exchange.getIn().setHeader(FtpConstants.FTP_REPLY_CODE, client.getReplyCode());
        exchange.getIn().setHeader(FtpConstants.FTP_REPLY_STRING, client.getReplyString());
        // change back to current directory
        if (endpoint.getConfiguration().isStepwise()) {
            changeCurrentDirectory(currentDir);
        }
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    }
    return result;
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GenericFile(org.apache.camel.component.file.GenericFile)

Aggregations

FTPFile (org.apache.commons.net.ftp.FTPFile)120 IOException (java.io.IOException)59 FTPClient (org.apache.commons.net.ftp.FTPClient)34 Test (org.junit.Test)32 File (java.io.File)28 InputStream (java.io.InputStream)16 ArrayList (java.util.ArrayList)15 FrameworkException (org.structr.common.error.FrameworkException)15 Tx (org.structr.core.graph.Tx)15 FtpTest (org.structr.web.files.FtpTest)15 FileOutputStream (java.io.FileOutputStream)11 OutputStream (java.io.OutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 BuildException (org.apache.tools.ant.BuildException)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 HashSet (java.util.HashSet)4