Search in sources :

Example 46 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project alfresco-repository by Alfresco.

the class FTPServerTest method testModificationTime.

/**
 * Test Setting the modification time FTP server
 *
 * @throws Exception
 */
public void testModificationTime() throws Exception {
    final String PATH1 = "FTPServerTest";
    final String PATH2 = "ModificationTime";
    logger.debug("Start testModificationTime");
    FTPClient ftp = connectClient();
    try {
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            fail("FTP server refused connection.");
        }
        boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
        assertTrue("admin login successful", login);
        reply = ftp.cwd("/Alfresco/User Homes");
        assertTrue(FTPReply.isPositiveCompletion(reply));
        // Delete the root directory in case it was left over from a previous test run
        try {
            ftp.removeDirectory(PATH1);
        } catch (IOException e) {
        // ignore this error
        }
        // make root directory
        ftp.makeDirectory(PATH1);
        ftp.cwd(PATH1);
        // make sub-directory in new directory
        ftp.makeDirectory(PATH2);
        ftp.cwd(PATH2);
        // List the files in the new directory
        FTPFile[] files = ftp.listFiles();
        assertTrue("files not empty", files.length == 0);
        // Create a file
        String FILE1_CONTENT_1 = "test file 1 content";
        String FILE1_NAME = "testFile1.txt";
        ftp.appendFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_1.getBytes("UTF-8")));
        String pathname = "/Alfresco/User Homes" + "/" + PATH1 + "/" + PATH2 + "/" + FILE1_NAME;
        logger.debug("set modification time");
        // YYYYMMDDhhmmss Time set to 2012 August 30 12:39:05
        String olympicTime = "20120830123905";
        ftp.setModificationTime(pathname, olympicTime);
        String extractedTime = ftp.getModificationTime(pathname);
        // Feature of the commons ftp library ExtractedTime has a "status code" first and is followed by newline chars
        assertTrue("time not set correctly by explicit set time", extractedTime.contains(olympicTime));
        // Get the new file
        FTPFile[] files2 = ftp.listFiles();
        assertTrue("files not one", files2.length == 1);
        InputStream is = ftp.retrieveFileStream(FILE1_NAME);
        String content = inputStreamToString(is);
        assertEquals("Content is not as expected", content, FILE1_CONTENT_1);
        ftp.completePendingCommand();
        // Update the file contents without setting time directly
        String FILE1_CONTENT_2 = "That's how it is says Pooh!";
        ftp.storeFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8")));
        InputStream is2 = ftp.retrieveFileStream(FILE1_NAME);
        String content2 = inputStreamToString(is2);
        assertEquals("Content is not as expected", FILE1_CONTENT_2, content2);
        ftp.completePendingCommand();
        extractedTime = ftp.getModificationTime(pathname);
        assertFalse("time not moved on if time not explicitly set", extractedTime.contains(olympicTime));
        // now delete the file we have been using.
        assertTrue(ftp.deleteFile(FILE1_NAME));
        // negative test - file should have gone now.
        assertFalse(ftp.deleteFile(FILE1_NAME));
    } finally {
        // clean up tree if left over from previous run
        ftp.disconnect();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 47 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project alfresco-repository by Alfresco.

the class FTPServerTest method testFTPConnectExternalAddressSet.

// test set time
/**
 * Test for Passive Mode -> FTPCommand.Pasv command with external address functionality.
 * see MNT-16433
 */
public void testFTPConnectExternalAddressSet() throws Exception {
    logger.debug("Start testFTPConnectExternalAddressSet");
    try {
        // use a highly improbable IP to tests Passive Mode -> FTPCommand.Pasv command
        // this is supposed to be the address of a proxy in front of Alfrsco FTP server
        String improbableIPAddress = "127.255.255.42";
        ftpConfigSection.setFTPExternalAddress(improbableIPAddress);
        FTPClient ftp = connectClient();
        try {
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                fail("FTP server refused connection.");
            }
            boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
            assertTrue("admin login not successful", login);
            // activate passive mode
            boolean sucess = ftp.enterRemotePassiveMode();
            assertTrue(sucess);
            assertTrue("Client should be in passive mode now", ftp.getDataConnectionMode() == FTPClient.PASSIVE_REMOTE_DATA_CONNECTION_MODE);
            reply = ftp.getReplyCode();
            // see https://www.ietf.org/rfc/rfc959.txt
            assertTrue("reply code should be 227", reply == 227);
            String replyLine = ftp.getReplyString();
            assertTrue(replyLine != null);
            String encodedImprobableIPAddress = improbableIPAddress.replaceAll("\\.", ",");
            assertTrue("Pasv command should contain the set external address encoded", replyLine.contains(encodedImprobableIPAddress));
            // now attempt to list the files and check that the command does not succeed
            FTPFile[] files = ftp.listFiles();
            assertNotNull(files);
            assertTrue("list command should not succeed", files.length == 0);
            assertTrue("The passive host should be the one set earlier.", improbableIPAddress.equals(ftp.getPassiveHost()));
        } finally {
            safeDisconnect(ftp);
        }
    } finally {
        // always revert back to default, or the other tests will fail
        ftpConfigSection.setFTPExternalAddress(null);
    }
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 48 with FTPFile

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

the class FtpFileSystem method findFile.

private FTPFile findFile(FTPFile file) throws IOException {
    FTPFile[] files = null;
    if (file instanceof FTPFileRef) {
        FTPFileRef fileRef = (FTPFileRef) file;
        files = ftpClient.listFiles(fileRef.getFolder(), f -> f.getName().equals(fileRef.getFileName()));
    } else {
        files = ftpClient.listFiles(file.getName(), f -> f.getName().equals(file.getName()));
    }
    if (files != null && files.length > 0) {
        return files[0];
    }
    return null;
}
Also used : OutputStream(java.io.OutputStream) NotImplementedException(org.apache.commons.lang3.NotImplementedException) Message(nl.nn.adapterframework.stream.Message) Iterator(java.util.Iterator) FtpSession(nl.nn.adapterframework.ftp.FtpSession) Date(java.util.Date) FTPFileRef(nl.nn.adapterframework.ftp.FTPFileRef) FilterOutputStream(java.io.FilterOutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DirectoryStream(java.nio.file.DirectoryStream) List(java.util.List) Map(java.util.Map) FTPFile(org.apache.commons.net.ftp.FTPFile) NoSuchElementException(java.util.NoSuchElementException) FtpConnectException(nl.nn.adapterframework.ftp.FtpConnectException) InputStream(java.io.InputStream) FTPReply(org.apache.commons.net.ftp.FTPReply) FTPFile(org.apache.commons.net.ftp.FTPFile) FTPFileRef(nl.nn.adapterframework.ftp.FTPFileRef)

Example 49 with FTPFile

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

the class FtpFileSystemTestHelper method cleanFolder.

private void cleanFolder() {
    try {
        FTPFile[] files = ftpSession.ftpClient.listFiles();
        for (FTPFile o : files) {
            if (o.isDirectory() && !o.getName().equals(".") && !o.getName().equals("..")) {
                FTPFile[] filesInFolder = ftpSession.ftpClient.listFiles(o.getName());
                for (FTPFile ftpFile : filesInFolder) {
                    ftpSession.ftpClient.deleteFile(o.getName() + "/" + ftpFile.getName());
                }
                ftpSession.ftpClient.removeDirectory(o.getName());
            } else {
                ftpSession.ftpClient.deleteFile(o.getName());
            }
        }
    } catch (IOException e) {
        System.err.println(e);
    }
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Example 50 with FTPFile

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

the class Ftp method run.

/**
   * Run a thread to transfer files
   */
public void run() {
    byte[] data = null;
    Timer timer = new Timer();
    FTPClient ftp = this.ftp;
    if (currentThreadCnt.getAndIncrement() > 0) {
        ftp = openConnection(null);
    }
    while (true) {
        String file = filesQueue.poll();
        if (file == null) {
            break;
        }
        int num = currentFileCnt.getAndIncrement();
        FTPFile ftpFile = filesMap.get(file);
        long ftpSizeInBytes = ftpFile.getSize();
        String fmtSizeInBytes = Utils.formatSizeInBytes(ftpSizeInBytes);
        String targetFile = getTargetFileName(file);
        if (info) {
            info(null, "  " + file + " - started (" + num + " of " + fileCnt + ", " + fmtSizeInBytes + ")");
        }
        try {
            InputStream in = ftp.retrieveFileStream(file);
            OutputStream out = null;
            java.io.File targetLocalFile = null;
            File targetHdfsFile = null;
            if (local) {
                targetLocalFile = new java.io.File(targetFile);
                if (!targetLocalFile.exists()) {
                    targetLocalFile.getParentFile().mkdirs();
                    targetLocalFile.createNewFile();
                }
                out = new FileOutputStream(targetLocalFile, false);
            } else {
                targetHdfsFile = new File();
                out = targetHdfsFile.create(targetFile, true);
            }
            if (data == null) {
                data = new byte[3 * 1024 * 1024];
            }
            int bytesRead = -1;
            long bytesReadAll = 0;
            long start = timer.start();
            long prev = start;
            long readTime = 0;
            long writeTime = 0;
            long cur, cur2, cur3;
            while (true) {
                cur = timer.current();
                bytesRead = in.read(data);
                cur2 = timer.current();
                readTime += (cur2 - cur);
                if (bytesRead == -1) {
                    break;
                }
                out.write(data, 0, bytesRead);
                out.flush();
                cur3 = timer.current();
                writeTime += (cur3 - cur2);
                bytesReadAll += bytesRead;
                if (info) {
                    cur = timer.current();
                    if (cur - prev > 13000) {
                        long elapsed = cur - start;
                        info(null, "  " + file + " - in progress (" + Utils.formatSizeInBytes(bytesReadAll) + " of " + fmtSizeInBytes + ", " + Utils.formatPercent(bytesReadAll, ftpSizeInBytes) + ", " + Utils.formatTime(elapsed) + ", " + Utils.formatBytesPerSec(bytesReadAll, elapsed) + ", " + Utils.formatBytesPerSec(bytesReadAll, readTime) + " read, " + Utils.formatBytesPerSec(bytesReadAll, writeTime) + " write)");
                        prev = cur;
                    }
                }
            }
            if (ftp.completePendingCommand()) {
                in.close();
                cur = timer.current();
                out.close();
                readTime += (timer.current() - cur);
                bytesTransferredAll.addAndGet(bytesReadAll);
                fileCntSuccess.incrementAndGet();
                if (info) {
                    long elapsed = timer.stop();
                    info(null, "  " + file + " - complete (" + Utils.formatSizeInBytes(bytesReadAll) + ", " + Utils.formatTime(elapsed) + ", " + Utils.formatBytesPerSec(bytesReadAll, elapsed) + ", " + Utils.formatBytesPerSec(bytesReadAll, readTime) + " read, " + Utils.formatBytesPerSec(bytesReadAll, writeTime) + " write)");
                }
            } else {
                in.close();
                out.close();
                if (info) {
                    info(null, "  " + file + " - failed");
                }
                exec.signal(Signal.Type.SQLEXCEPTION, "File transfer failed: " + file);
            }
        } catch (IOException e) {
            exec.signal(e);
        }
    }
    try {
        if (ftp.isConnected()) {
            ftp.logout();
            ftp.disconnect();
        }
    } catch (IOException e) {
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) FileOutputStream(java.io.FileOutputStream) FTPFile(org.apache.commons.net.ftp.FTPFile)

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