Search in sources :

Example 1 with FtpServer

use of org.apache.ftpserver.FtpServer in project alliance by codice.

the class MockNsili method startFtpWebServer.

public void startFtpWebServer(int port) {
    FtpServerFactory ftpServerFactory = new FtpServerFactory();
    ListenerFactory listenerFactory = new ListenerFactory();
    listenerFactory.setPort(port);
    ftpServerFactory.addListener("default", listenerFactory.createListener());
    PropertiesUserManagerFactory propertiesUserManagerFactory = new PropertiesUserManagerFactory();
    UserManager userManager = propertiesUserManagerFactory.createUserManager();
    BaseUser baseUser = new BaseUser();
    baseUser.setName(MOCK_SERVER_USERNAME);
    baseUser.setPassword(MOCK_SERVER_PASSWORD);
    try {
        ftpHomeDirectoryPath = Files.createTempDirectory("home_");
        Runtime.getRuntime().addShutdownHook(new Thread(() -> FileUtils.deleteQuietly(ftpHomeDirectoryPath.toFile())));
        baseUser.setHomeDirectory(ftpHomeDirectoryPath.toString());
    } catch (IOException e) {
        LOGGER.info("Unable to set ftp endpoint to a temporary home directory.");
    }
    try {
        userManager.save(baseUser);
        ftpServerFactory.setUserManager(userManager);
        FtpServer ftpServer = ftpServerFactory.createServer();
        ftpServer.start();
    } catch (FtpException e) {
        LOGGER.error("Unable to start FTP server.", e);
    }
    LOGGER.info("Setting the ftp server's publish address to be ftp://localhost:{}/", port);
}
Also used : BaseUser(org.apache.ftpserver.usermanager.impl.BaseUser) FtpServerFactory(org.apache.ftpserver.FtpServerFactory) UserManager(org.apache.ftpserver.ftplet.UserManager) PropertiesUserManagerFactory(org.apache.ftpserver.usermanager.PropertiesUserManagerFactory) FtpServer(org.apache.ftpserver.FtpServer) FtpException(org.apache.ftpserver.ftplet.FtpException) IOException(java.io.IOException) ListenerFactory(org.apache.ftpserver.listener.ListenerFactory)

Example 2 with FtpServer

use of org.apache.ftpserver.FtpServer in project fess-crawler by codelibs.

the class FtpClientTest method test_doHead_dir1.

public void test_doHead_dir1() throws Exception {
    FtpServer server = null;
    try {
        String username = "testuser";
        String password = "testpass";
        server = startFtpServer(FTP_PORT, username, password);
        Map<String, Object> params = new HashMap<String, Object>();
        FtpAuthentication auth = new FtpAuthentication();
        auth.setUsername(username);
        auth.setPassword(password);
        params.put(FtpClient.FTP_AUTHENTICATIONS_PROPERTY, new FtpAuthentication[] { auth });
        ftpClient.setInitParameterMap(params);
        final ResponseData responseData = ftpClient.doHead("ftp://localhost:" + FTP_PORT + "/dir1");
        assertNull(responseData);
    } finally {
        if (server != null) {
            server.stop();
        }
    }
}
Also used : HashMap(java.util.HashMap) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) FtpServer(org.apache.ftpserver.FtpServer)

Example 3 with FtpServer

use of org.apache.ftpserver.FtpServer in project fess-crawler by codelibs.

the class FtpClientTest method startFtpServer.

public FtpServer startFtpServer(int port, String username, String password) throws FtpException {
    FtpServerFactory factory = new FtpServerFactory();
    ListenerFactory lisnerFactory = new ListenerFactory();
    lisnerFactory.setPort(port);
    factory.addListener("default", lisnerFactory.createListener());
    if (username != null) {
        UserManager userManager = factory.getUserManager();
        BaseUser ftpUser = new BaseUser();
        ftpUser.setName(username);
        ftpUser.setPassword(password);
        final File file = ResourceUtil.getResourceAsFile("test");
        String path = file.getAbsolutePath();
        ftpUser.setHomeDirectory(path);
        userManager.save(ftpUser);
    }
    FtpServer server = factory.createServer();
    server.start();
    return server;
}
Also used : BaseUser(org.apache.ftpserver.usermanager.impl.BaseUser) FtpServerFactory(org.apache.ftpserver.FtpServerFactory) UserManager(org.apache.ftpserver.ftplet.UserManager) FtpServer(org.apache.ftpserver.FtpServer) File(java.io.File) ListenerFactory(org.apache.ftpserver.listener.ListenerFactory)

Example 4 with FtpServer

use of org.apache.ftpserver.FtpServer in project fess-crawler by codelibs.

the class FtpClientTest method test_doHead_root_dir.

public void test_doHead_root_dir() throws Exception {
    FtpServer server = null;
    try {
        String username = "testuser";
        String password = "testpass";
        server = startFtpServer(FTP_PORT, username, password);
        Map<String, Object> params = new HashMap<String, Object>();
        FtpAuthentication auth = new FtpAuthentication();
        auth.setUsername(username);
        auth.setPassword(password);
        params.put(FtpClient.FTP_AUTHENTICATIONS_PROPERTY, new FtpAuthentication[] { auth });
        ftpClient.setInitParameterMap(params);
        final ResponseData responseData = ftpClient.doHead("ftp://localhost:" + FTP_PORT + "/");
        assertNull(responseData);
    } finally {
        if (server != null) {
            server.stop();
        }
    }
}
Also used : HashMap(java.util.HashMap) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) FtpServer(org.apache.ftpserver.FtpServer)

Example 5 with FtpServer

use of org.apache.ftpserver.FtpServer in project fess-crawler by codelibs.

the class FtpClientTest method test_doGet_file.

public void test_doGet_file() throws Exception {
    FtpServer server = null;
    try {
        String username = "testuser";
        String password = "testpass";
        server = startFtpServer(FTP_PORT, username, password);
        Map<String, Object> params = new HashMap<String, Object>();
        FtpAuthentication auth = new FtpAuthentication();
        auth.setUsername(username);
        auth.setPassword(password);
        params.put(FtpClient.FTP_AUTHENTICATIONS_PROPERTY, new FtpAuthentication[] { auth });
        ftpClient.setInitParameterMap(params);
        final ResponseData responseData = ftpClient.doGet("ftp://localhost:" + FTP_PORT + "/text1.txt");
        assertEquals(200, responseData.getHttpStatusCode());
        assertEquals("UTF-8", responseData.getCharSet());
        assertTrue(6 == responseData.getContentLength());
        assertNotNull(responseData.getLastModified());
        assertEquals(Constants.GET_METHOD, responseData.getMethod());
        assertEquals("text/plain", responseData.getMimeType());
        assertTrue(responseData.getUrl().endsWith("text1.txt"));
        final String content = new String(InputStreamUtil.getBytes(responseData.getResponseBody()), "UTF-8");
        assertEquals("test1", content.trim());
    } finally {
        if (server != null) {
            server.stop();
        }
    }
}
Also used : HashMap(java.util.HashMap) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) FtpServer(org.apache.ftpserver.FtpServer)

Aggregations

FtpServer (org.apache.ftpserver.FtpServer)9 HashMap (java.util.HashMap)7 ResponseData (org.codelibs.fess.crawler.entity.ResponseData)5 Set (java.util.Set)2 FtpServerFactory (org.apache.ftpserver.FtpServerFactory)2 UserManager (org.apache.ftpserver.ftplet.UserManager)2 ListenerFactory (org.apache.ftpserver.listener.ListenerFactory)2 BaseUser (org.apache.ftpserver.usermanager.impl.BaseUser)2 RequestData (org.codelibs.fess.crawler.entity.RequestData)2 ChildUrlsException (org.codelibs.fess.crawler.exception.ChildUrlsException)2 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 FtpException (org.apache.ftpserver.ftplet.FtpException)1 PropertiesUserManagerFactory (org.apache.ftpserver.usermanager.PropertiesUserManagerFactory)1