Search in sources :

Example 86 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project spring-integration by spring-projects.

the class FtpRemoteFileTemplateTests method testINT3412AppendStatRmdir.

@Test
public void testINT3412AppendStatRmdir() throws IOException {
    FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sessionFactory);
    DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
    fileNameGenerator.setExpression("'foobar.txt'");
    template.setFileNameGenerator(fileNameGenerator);
    template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
    template.setUseTemporaryFileName(false);
    template.execute(session -> {
        session.mkdir("foo/");
        return session.mkdir("foo/bar/");
    });
    template.append(new GenericMessage<String>("foo"));
    template.append(new GenericMessage<String>("bar"));
    assertTrue(template.exists("foo/foobar.txt"));
    template.executeWithClient((ClientCallbackWithoutResult<FTPClient>) client -> {
        try {
            FTPFile[] files = client.listFiles("foo/foobar.txt");
            assertEquals(6, files[0].getSize());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    template.execute((SessionCallbackWithoutResult<FTPFile>) session -> {
        assertTrue(session.remove("foo/foobar.txt"));
        assertTrue(session.rmdir("foo/bar/"));
        FTPFile[] files = session.list("foo/");
        assertEquals(0, files.length);
        assertTrue(session.rmdir("foo/"));
    });
    assertFalse(template.getSession().exists("foo"));
}
Also used : MessagingException(org.springframework.messaging.MessagingException) SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) RunWith(org.junit.runner.RunWith) LiteralExpression(org.springframework.expression.common.LiteralExpression) Autowired(org.springframework.beans.factory.annotation.Autowired) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) SessionCallbackWithoutResult(org.springframework.integration.file.remote.SessionCallbackWithoutResult) Assert.fail(org.junit.Assert.fail) FTPClient(org.apache.commons.net.ftp.FTPClient) ClientCallbackWithoutResult(org.springframework.integration.file.remote.ClientCallbackWithoutResult) Assert.assertTrue(org.junit.Assert.assertTrue) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) FtpTestSupport(org.springframework.integration.ftp.FtpTestSupport) File(java.io.File) Configuration(org.springframework.context.annotation.Configuration) Assert.assertFalse(org.junit.Assert.assertFalse) ContextConfiguration(org.springframework.test.context.ContextConfiguration) FTPFile(org.apache.commons.net.ftp.FTPFile) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) LiteralExpression(org.springframework.expression.common.LiteralExpression) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) FTPClient(org.apache.commons.net.ftp.FTPClient) Test(org.junit.Test)

Example 87 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project spring-integration by spring-projects.

the class FtpOutboundGateway method doInWorkingDirectory.

private <V> V doInWorkingDirectory(Message<?> message, Session<FTPFile> session, Callable<V> task) throws IOException {
    Expression workingDirExpression = this.workingDirExpression;
    FTPClient ftpClient = (FTPClient) session.getClientInstance();
    String currentWorkingDirectory = null;
    boolean restoreWorkingDirectory = false;
    try {
        if (workingDirExpression != null) {
            currentWorkingDirectory = ftpClient.printWorkingDirectory();
            String newWorkingDirectory = workingDirExpression.getValue(this.evaluationContext, message, String.class);
            if (!Objects.equals(currentWorkingDirectory, newWorkingDirectory)) {
                ftpClient.changeWorkingDirectory(newWorkingDirectory);
                restoreWorkingDirectory = true;
            }
        }
        return task.call();
    } catch (Exception e) {
        if (e instanceof IOException) {
            throw (IOException) e;
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new IOException("Uncategorised IO exception", e);
        }
    } finally {
        if (restoreWorkingDirectory) {
            ftpClient.changeWorkingDirectory(currentWorkingDirectory);
        }
    }
}
Also used : Expression(org.springframework.expression.Expression) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) IOException(java.io.IOException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 88 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project spring-integration by spring-projects.

the class FtpServerOutboundTests method testMGETOnNullDir.

@Test
@SuppressWarnings("unchecked")
public void testMGETOnNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();
    this.inboundMGet.send(new GenericMessage<Object>(""));
    Message<?> result = this.output.receive(1000);
    assertNotNull(result);
    List<File> localFiles = (List<File>) result.getPayload();
    assertThat(localFiles.size(), Matchers.greaterThan(0));
    for (File file : localFiles) {
        assertThat(file.getName(), isOneOf(" localTarget1.txt", "localTarget2.txt"));
        assertThat(file.getName(), not(containsString("null")));
    }
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) List(java.util.List) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) FTPClient(org.apache.commons.net.ftp.FTPClient) Test(org.junit.Test)

Example 89 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project spring-integration by spring-projects.

the class FtpServerOutboundTests method testLsForNullDir.

@Test
@SuppressWarnings("unchecked")
public void testLsForNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();
    this.inboundLs.send(new GenericMessage<String>("foo"));
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(List.class));
    List<String> files = (List<String>) receive.getPayload();
    assertEquals(2, files.size());
    assertThat(files, containsInAnyOrder(" ftpSource1.txt", "ftpSource2.txt"));
    FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null);
    for (FTPFile ftpFile : ftpFiles) {
        if (!ftpFile.isDirectory()) {
            assertTrue(files.contains(ftpFile.getName()));
        }
    }
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) List(java.util.List) Matchers.containsString(org.hamcrest.Matchers.containsString) FTPClient(org.apache.commons.net.ftp.FTPClient) Test(org.junit.Test)

Example 90 with FTPClient

use of org.apache.commons.net.ftp.FTPClient in project commons-utils-core by jiayongming.

the class FTPUtilImpl method createFTPClien.

// 创建变连接FTP
private FTPClient createFTPClien(FTPVo vo) {
    FTPClient client = new FTPClient();
    int reply = -1;
    try {
        client.connect(vo.getHostName(), vo.getPort());
        client.login(vo.getUsername(), vo.getPassword());
        reply = client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            client.disconnect();
            return null;
        } else {
            client.setControlEncoding(vo.getRemoteEncoding());
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
            if (vo.isPassiveMode()) {
                client.enterLocalPassiveMode();
            } else {
                client.enterRemotePassiveMode();
            }
            client.cwd(vo.getRemoteDir());
        }
        return client;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : 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