Example usage for org.apache.commons.net.ftp FTPFile FILE_TYPE

List of usage examples for org.apache.commons.net.ftp FTPFile FILE_TYPE

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPFile FILE_TYPE.

Prototype

int FILE_TYPE

To view the source code for org.apache.commons.net.ftp FTPFile FILE_TYPE.

Click Source Link

Document

A constant indicating an FTPFile is a file.

Usage

From source file:org.apache.sqoop.util.TestMainframeFTPClientUtils.java

@Test
public void testListSequentialDatasets() {
    try {//www  . ja v a  2s.c  om
        when(mockFTPClient.login("user", "pssword")).thenReturn(true);
        when(mockFTPClient.logout()).thenReturn(true);
        when(mockFTPClient.isConnected()).thenReturn(false);
        when(mockFTPClient.getReplyCode()).thenReturn(200);
        when(mockFTPClient.changeWorkingDirectory("a.b.c")).thenReturn(true);
        FTPFile file1 = new FTPFile();
        file1.setName("blah1");
        file1.setType(FTPFile.FILE_TYPE);
        FTPFile file2 = new FTPFile();
        file2.setName("blah2");
        file2.setType(FTPFile.FILE_TYPE);
        when(mockFTPClient.listFiles()).thenReturn(new FTPFile[] { file1, file2 });
    } catch (IOException e) {
        fail("No IOException should be thrown!");
    }

    conf.set(DBConfiguration.URL_PROPERTY, "localhost:11111");
    conf.set(DBConfiguration.USERNAME_PROPERTY, "user");
    conf.set(DBConfiguration.PASSWORD_PROPERTY, "pssword");
    conf.set(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE, "s");
    conf.set(MainframeConfiguration.MAINFRAME_INPUT_DATASET_NAME, "a.b.c.blah1");
    // set the password in the secure credentials object
    Text PASSWORD_SECRET_KEY = new Text(DBConfiguration.PASSWORD_PROPERTY);
    conf.getCredentials().addSecretKey(PASSWORD_SECRET_KEY, "pssword".getBytes());

    try {
        List<String> files = MainframeFTPClientUtils.listSequentialDatasets("a.b.c.blah1", conf);
        Assert.assertEquals(1, files.size());
    } catch (IOException ioe) {
        fail("No IOException should be thrown!");
    }
}

From source file:org.apache.sqoop.util.TestMainframeFTPClientUtils.java

@Test
public void testListEmptySequentialDatasets() {
    try {/*from   ww w . java  2 s . c om*/
        when(mockFTPClient.login("user", "pssword")).thenReturn(true);
        when(mockFTPClient.logout()).thenReturn(true);
        when(mockFTPClient.isConnected()).thenReturn(false);
        when(mockFTPClient.getReplyCode()).thenReturn(200);
        when(mockFTPClient.changeWorkingDirectory("a.b.c")).thenReturn(true);
        FTPFile file1 = new FTPFile();
        file1.setName("blah0");
        file1.setType(FTPFile.FILE_TYPE);
        FTPFile file2 = new FTPFile();
        file2.setName("blah2");
        file2.setType(FTPFile.FILE_TYPE);
        when(mockFTPClient.listFiles()).thenReturn(new FTPFile[] { file1, file2 });
    } catch (IOException e) {
        fail("No IOException should be thrown!");
    }

    conf.set(DBConfiguration.URL_PROPERTY, "localhost:11111");
    conf.set(DBConfiguration.USERNAME_PROPERTY, "user");
    conf.set(DBConfiguration.PASSWORD_PROPERTY, "pssword");
    conf.set(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE, "s");
    conf.set(MainframeConfiguration.MAINFRAME_INPUT_DATASET_NAME, "a.b.c.blah1");
    // set the password in the secure credentials object
    Text PASSWORD_SECRET_KEY = new Text(DBConfiguration.PASSWORD_PROPERTY);
    conf.getCredentials().addSecretKey(PASSWORD_SECRET_KEY, "pssword".getBytes());

    try {
        String dsName = conf.get(MainframeConfiguration.MAINFRAME_INPUT_DATASET_NAME);
        List<String> files = MainframeFTPClientUtils.listSequentialDatasets(dsName, conf);
        Assert.assertEquals(0, files.size());
    } catch (IOException ioe) {
        fail("No IOException should be thrown!");
    }
}

From source file:org.apache.sqoop.util.TestMainframeFTPClientUtils.java

@Test
public void testTrailingDotSequentialDatasets() {
    try {//  w ww . j a v a  2 s.  c o m
        when(mockFTPClient.login("user", "pssword")).thenReturn(true);
        when(mockFTPClient.logout()).thenReturn(true);
        when(mockFTPClient.isConnected()).thenReturn(false);
        when(mockFTPClient.getReplyCode()).thenReturn(200);
        when(mockFTPClient.changeWorkingDirectory("'a.b.c.blah1'"))
                .thenThrow(new IOException("Folder not found"));
        FTPFile file1 = new FTPFile();
        file1.setName("blah1");
        file1.setType(FTPFile.FILE_TYPE);
        FTPFile file2 = new FTPFile();
        file2.setName("blah2");
        file2.setType(FTPFile.FILE_TYPE);
        when(mockFTPClient.listFiles()).thenReturn(new FTPFile[] { file1, file2 });
    } catch (IOException e) {
        fail("No IOException should be thrown!");
    }

    conf.set(DBConfiguration.URL_PROPERTY, "localhost:11111");
    conf.set(DBConfiguration.USERNAME_PROPERTY, "user");
    conf.set(DBConfiguration.PASSWORD_PROPERTY, "pssword");
    conf.set(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE, "s");
    conf.set(MainframeConfiguration.MAINFRAME_INPUT_DATASET_NAME, "a.b.c.blah1.");
    // set the password in the secure credentials object
    Text PASSWORD_SECRET_KEY = new Text(DBConfiguration.PASSWORD_PROPERTY);
    conf.getCredentials().addSecretKey(PASSWORD_SECRET_KEY, "pssword".getBytes());

    try {
        String dsName = conf.get(MainframeConfiguration.MAINFRAME_INPUT_DATASET_NAME);
        List<String> files = MainframeFTPClientUtils.listSequentialDatasets(dsName, conf);
        Assert.assertEquals(0, files.size());
    } catch (IOException ioe) {
        String ioeString = ioe.getMessage();
        Assert.assertEquals("Could not list datasets from a.b.c.blah1:java.io.IOException: Folder not found",
                ioeString);
    }
}

From source file:org.apache.sqoop.util.TestMainframeFTPClientUtils.java

@Test
public void testGdgGetLatest() {
    try {/*from ww w . j ava 2  s .c  o  m*/
        when(mockFTPClient.login("user", "pssword")).thenReturn(true);
        when(mockFTPClient.logout()).thenReturn(true);
        when(mockFTPClient.isConnected()).thenReturn(false);
        when(mockFTPClient.getReplyCode()).thenReturn(200);
        when(mockFTPClient.changeWorkingDirectory("'a.b.c'")).thenReturn(true);
        FTPFile file1 = new FTPFile();
        file1.setName("G0100V00");
        file1.setType(FTPFile.FILE_TYPE);
        FTPFile file2 = new FTPFile();
        file2.setName("G0101V00");
        file2.setType(FTPFile.FILE_TYPE);
        when(mockFTPClient.listFiles()).thenReturn(new FTPFile[] { file1, file2 });
    } catch (IOException e) {
        fail("No IOException should be thrown!");
    }

    conf.set(DBConfiguration.URL_PROPERTY, "localhost:11111");
    conf.set(DBConfiguration.USERNAME_PROPERTY, "user");
    conf.set(DBConfiguration.PASSWORD_PROPERTY, "pssword");
    conf.set(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE, "g");
    conf.set(MainframeConfiguration.MAINFRAME_INPUT_DATASET_NAME, "a.b.c.d");
    // set the password in the secure credentials object
    Text PASSWORD_SECRET_KEY = new Text(DBConfiguration.PASSWORD_PROPERTY);
    conf.getCredentials().addSecretKey(PASSWORD_SECRET_KEY, "pssword".getBytes());

    try {
        String dsName = conf.get(MainframeConfiguration.MAINFRAME_INPUT_DATASET_NAME);
        List<String> files = MainframeFTPClientUtils.listSequentialDatasets(dsName, conf);
        Assert.assertEquals("G0101V00", files.get(0));
        Assert.assertEquals(1, files.size());
    } catch (IOException ioe) {
        String ioeString = ioe.getMessage();
        Assert.assertEquals("Could not list datasets from a.b.c.blah1:java.io.IOException: Folder not found",
                ioeString);
    }
}

From source file:org.mockftpserver.stub.StubFtpServerIntegrationTest.java

/**
 * Test the LIST and SYST commands.//from   w w  w . j  a v a2s. c om
 */
public void testList() throws Exception {
    ftpClientConnect();

    // Set directory listing
    ListCommandHandler listCommandHandler = (ListCommandHandler) stubFtpServer
            .getCommandHandler(CommandNames.LIST);
    listCommandHandler
            .setDirectoryListing("11-09-01 12:30PM  406348 File2350.log\n" + "11-01-01 1:30PM <DIR> 0 archive");

    // LIST
    FTPFile[] files = ftpClient.listFiles();
    assertEquals("number of files", 2, files.length);
    verifyFTPFile(files[0], FTPFile.FILE_TYPE, "File2350.log", 406348L);
    verifyFTPFile(files[1], FTPFile.DIRECTORY_TYPE, "archive", 0L);
    verifyReplyCode("list", 226);
}

From source file:org.runmyprocess.sec.FTP.java

private void listFiles(JSONObject jsonObject) throws Exception {
    // get all files from server and store them in an array of
    // FTPFiles//from   ww w .j  av a2 s.  c om
    JSONArray fileInfo = new JSONArray();
    LOG.log("LIST directories: " + jsonObject.getString("path"), Level.INFO);
    String[] directories = this.client.listNames(jsonObject.getString("path"));
    LOG.log("LIST files: " + jsonObject.getString("path"), Level.INFO);
    FTPFile[] files = this.client.listFiles(jsonObject.getString("path"));
    LOG.log("LIST DONE", Level.INFO);

    for (FTPFile file : files) {
        if (file.getType() == FTPFile.FILE_TYPE) {
            JSONObject fileObject = new JSONObject();
            fileObject.put("Name", file.getName());
            fileObject.put("Size", FileUtils.byteCountToDisplaySize(file.getSize()));
            fileObject.put("Timestamp", file.getTimestamp().getTime().toString());
            fileInfo.add(fileObject);
        }
    }
    response.setStatus(200);//sets the return status to 200
    JSONObject resp = new JSONObject();
    resp.put("files", fileInfo);//sends the info inside an object
    resp.put("directories", directories);
    response.setData(resp);
}

From source file:org.sipfoundry.sipxconfig.admin.ftp.FtpContextImpl.java

public String[] listFiles(String path) {
    return listByType(path, new Integer(FTPFile.FILE_TYPE));
}

From source file:org.soitoolkit.commons.mule.ftp.FtpUtil.java

/**
 * Deletes a directory with all its files and sub-directories.
 * //  w ww. j  av a2  s.  c  o  m
 * @param ftpClient
 * @param path
 * @throws IOException
 */
static public void recursiveDeleteDirectory(FTPClient ftpClient, String path) throws IOException {

    logger.info("Delete directory: {}", path);

    FTPFile[] ftpFiles = ftpClient.listFiles(path);
    logger.debug("Number of files that will be deleted: {}", ftpFiles.length);

    for (FTPFile ftpFile : ftpFiles) {
        String filename = path + "/" + ftpFile.getName();
        if (ftpFile.getType() == FTPFile.FILE_TYPE) {
            boolean deleted = ftpClient.deleteFile(filename);
            logger.debug("Deleted {}? {}", filename, deleted);
        } else {
            recursiveDeleteDirectory(ftpClient, filename);
        }
    }

    boolean dirDeleted = ftpClient.deleteFile(path);
    logger.debug("Directory {} deleted: {}", path, dirDeleted);
}

From source file:rems.Global.java

public static String[] GetFileList(InetAddress ftpServerAddrs, String serverAppDirectory, String dirName,
        String userName, String password) {
    String[] downloadFiles = new String[1];
    StringBuilder result = new StringBuilder();
    FTPClient ftpClient = new FTPClient();
    try {//w  w w  .  ja v a  2s.  c  om
        // pass directory path on server to connect  
        ftpClient.connect(ftpServerAddrs);
        // pass username and password, returned true if authentication is  
        // successful  
        boolean login = ftpClient.login(userName, password);
        if (login) {
            //System.out.println("Connection established...");
            // get all files from server and store them in an array of  
            // FTPFiles  
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file : files) {
                if (file.getType() == FTPFile.FILE_TYPE) {
                    result.append(file.getName());
                    result.append("\n");
                }
            }
            result.replace(result.toString().lastIndexOf("\n"), result.toString().length(), "");
            // logout the user, returned true if logout successfully  
            boolean logout = ftpClient.logout();
            if (logout) {
                System.out.println("Connection close...");
            }
        } else {
            System.out.println("Connection fail...");
        }
        return result.toString().split("\\\n");
    } catch (SocketException e) {
        Global.errorLog += Arrays.toString(e.getStackTrace());
        Global.writeToLog();
    } catch (IOException e) {
        Global.errorLog += Arrays.toString(e.getStackTrace());
        Global.writeToLog();
    } finally {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            Global.errorLog += Arrays.toString(e.getStackTrace());
            Global.writeToLog();
        }
    }
    return downloadFiles;
}