Search in sources :

Example 61 with FTPClient

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

the class FTPServerTest method testFtpQuotaAndFtp.

/**
 * Test a quota failue exception over FTP.
 * A file should not exist after a create and quota exception.
 */
public void testFtpQuotaAndFtp() throws Exception {
    // Enable usages
    ContentUsageImpl contentUsage = (ContentUsageImpl) applicationContext.getBean("contentUsageImpl");
    contentUsage.setEnabled(true);
    contentUsage.init();
    UserUsageTrackingComponent userUsageTrackingComponent = (UserUsageTrackingComponent) applicationContext.getBean("userUsageTrackingComponent");
    userUsageTrackingComponent.setEnabled(true);
    userUsageTrackingComponent.bootstrapInternal();
    final String TEST_DIR = "/Alfresco/User Homes/" + USER_THREE;
    FTPClient ftpOne = connectClient();
    try {
        int reply = ftpOne.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            fail("FTP server refused connection.");
        }
        boolean login = ftpOne.login(USER_THREE, PASSWORD_THREE);
        assertTrue("user three login not successful", login);
        boolean success = ftpOne.changeWorkingDirectory("Alfresco");
        assertTrue("user three unable to cd to Alfreco", success);
        success = ftpOne.changeWorkingDirectory("User*Homes");
        assertTrue("user one unable to cd to User*Homes", success);
        success = ftpOne.changeWorkingDirectory(USER_THREE);
        assertTrue("user one unable to cd to " + USER_THREE, success);
        /**
         * Create a file as user three which is bigger than the quota
         */
        String FILE3_CONTENT_3 = "test file 3 content that needs to be greater than 100 bytes to result in a quota exception being thrown";
        String FILE1_NAME = "test.docx";
        // Should not be success
        success = ftpOne.appendFile(FILE1_NAME, new ByteArrayInputStream(FILE3_CONTENT_3.getBytes("UTF-8")));
        assertFalse("user one can ignore quota", success);
        boolean deleted = ftpOne.deleteFile(FILE1_NAME);
        assertFalse("quota exception expected", deleted);
        logger.debug("test done");
    } finally {
        // Disable usages
        contentUsage.setEnabled(false);
        contentUsage.init();
        userUsageTrackingComponent.setEnabled(false);
        userUsageTrackingComponent.bootstrapInternal();
        ftpOne.dele(TEST_DIR);
        if (ftpOne != null) {
            ftpOne.disconnect();
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UserUsageTrackingComponent(org.alfresco.repo.usage.UserUsageTrackingComponent) ContentUsageImpl(org.alfresco.repo.usage.ContentUsageImpl) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 62 with FTPClient

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

use of org.apache.commons.net.ftp.FTPClient in project eol-globi-data by jhpoelen.

the class ResourceUtil method asInputStream.

public static InputStream asInputStream(URI resource, InputStreamFactory factory) throws IOException {
    try {
        InputStream is;
        if (isHttpURI(resource)) {
            LOG.info("caching of [" + resource + "] started...");
            is = getCachedRemoteInputStream(resource, factory);
            LOG.info("caching of [" + resource + "] complete.");
        } else if (isFileURI(resource)) {
            is = factory.create(new FileInputStream(new File(resource)));
        } else if (StringUtils.startsWith(resource.toString(), "jar:file:/")) {
            URL url = resource.toURL();
            URLConnection urlConnection = url.openConnection();
            // Prevent leaking of jar file descriptors by disabling jar cache.
            // see https://stackoverflow.com/a/36518430
            urlConnection.setUseCaches(false);
            is = factory.create(urlConnection.getInputStream());
        } else if (StringUtils.startsWith(resource.getScheme(), "ftp")) {
            FTPClient ftpClient = new FTPClient();
            try {
                ftpClient.connect(resource.getHost());
                ftpClient.enterLocalPassiveMode();
                ftpClient.login("anonymous", "info@globalbioticinteractions.org");
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
                ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                is = ftpClient.isConnected() ? cacheAndOpenStream(ftpClient.retrieveFileStream(resource.getPath()), factory) : null;
            } finally {
                if (ftpClient.isConnected()) {
                    ftpClient.disconnect();
                }
            }
        } else {
            String classpathResource = resource.toString();
            if (StringUtils.startsWith(classpathResource, "classpath:")) {
                classpathResource = StringUtils.replace(classpathResource, "classpath:", "");
            }
            is = factory.create(ResourceUtil.class.getResourceAsStream(classpathResource));
        }
        if (is == null) {
            final URI uri = fromShapefileDir(resource);
            if (uri == null) {
                throw new IOException("failed to open resource [" + resource + "]");
            } else {
                is = new FileInputStream(new File(uri));
            }
        }
        if (StringUtils.endsWith(resource.toString(), ".gz")) {
            is = new GZIPInputStream(is);
        }
        return is;
    } catch (IOException ex) {
        throw new IOException("issue accessing [" + resource + "]", ex);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) FileInputStream(java.io.FileInputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 64 with FTPClient

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

Example 65 with FTPClient

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

the class FTPFileSystem method create.

/**
   * A stream obtained via this call must be closed before using other APIs of
   * this class or else the invocation will block.
   */
@Override
public FSDataOutputStream create(Path file, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
    final FTPClient client = connect();
    Path workDir = new Path(client.printWorkingDirectory());
    Path absolute = makeAbsolute(workDir, file);
    FileStatus status;
    try {
        status = getFileStatus(client, file);
    } catch (FileNotFoundException fnfe) {
        status = null;
    }
    if (status != null) {
        if (overwrite && !status.isDirectory()) {
            delete(client, file, false);
        } else {
            disconnect(client);
            throw new FileAlreadyExistsException("File already exists: " + file);
        }
    }
    Path parent = absolute.getParent();
    if (parent == null || !mkdirs(client, parent, FsPermission.getDirDefault())) {
        parent = (parent == null) ? new Path("/") : parent;
        disconnect(client);
        throw new IOException("create(): Mkdirs failed to create: " + parent);
    }
    client.allocate(bufferSize);
    // Change to parent directory on the server. Only then can we write to the
    // file on the server by opening up an OutputStream. 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
    // FSDataOutputStream.
    client.changeWorkingDirectory(parent.toUri().getPath());
    FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file.getName()), statistics) {

        @Override
        public void close() throws IOException {
            super.close();
            if (!client.isConnected()) {
                throw new FTPException("Client not connected");
            }
            boolean cmdCompleted = client.completePendingCommand();
            disconnect(client);
            if (!cmdCompleted) {
                throw new FTPException("Could not complete transfer, Reply Code - " + client.getReplyCode());
            }
        }
    };
    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
        fos.close();
        throw new IOException("Unable to create file: " + file + ", Aborting");
    }
    return fos;
}
Also used : Path(org.apache.hadoop.fs.Path) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) FileStatus(org.apache.hadoop.fs.FileStatus) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) 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