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

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

Introduction

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

Prototype

public void setName(String name) 

Source Link

Document

Set the name of the file.

Usage

From source file:nl.nn.adapterframework.filesystem.FtpFileSystem.java

@Override
public FTPFile toFile(String filename) throws FileSystemException {
    FTPFile ftpFile = new FTPFile();
    ftpFile.setName(filename);

    return ftpFile;
}

From source file:org.apache.camel.component.file.remote.FtpConsumer.java

protected boolean doPollDirectory(String absolutePath, String dirName, List<GenericFile<FTPFile>> fileList,
        int depth) {
    log.trace("doPollDirectory from absolutePath: {}, dirName: {}", absolutePath, dirName);

    depth++;//w  w  w  .  ja v  a  2  s  .  co  m

    // remove trailing /
    dirName = FileUtil.stripTrailingSeparator(dirName);

    // compute dir depending on stepwise is enabled or not
    String dir;
    if (isStepwise()) {
        dir = ObjectHelper.isNotEmpty(dirName) ? dirName : absolutePath;
        operations.changeCurrentDirectory(dir);
    } else {
        dir = absolutePath;
    }

    log.trace("Polling directory: {}", dir);
    List<FTPFile> files = null;
    if (isUseList()) {
        if (isStepwise()) {
            files = operations.listFiles();
        } else {
            files = operations.listFiles(dir);
        }
    } else {
        // we cannot use the LIST command(s) so we can only poll a named file
        // so created a pseudo file with that name
        FTPFile file = new FTPFile();
        file.setType(FTPFile.FILE_TYPE);
        fileExpressionResult = evaluateFileExpression();
        if (fileExpressionResult != null) {
            file.setName(fileExpressionResult);
            files = new ArrayList<FTPFile>(1);
            files.add(file);
        }
    }

    if (files == null || files.isEmpty()) {
        // no files in this directory to poll
        log.trace("No files found in directory: {}", dir);
        return true;
    } else {
        // we found some files
        log.trace("Found {} in directory: {}", files.size(), dir);
    }

    for (FTPFile file : files) {

        if (log.isTraceEnabled()) {
            log.trace("FtpFile[name={}, dir={}, file={}]",
                    new Object[] { file.getName(), file.isDirectory(), file.isFile() });
        }

        // check if we can continue polling in files
        if (!canPollMoreFiles(fileList)) {
            return false;
        }

        if (file.isDirectory()) {
            RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file);
            if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && isValidFile(remote, true, files)) {
                // recursive scan and add the sub files and folders
                String subDirectory = file.getName();
                String path = absolutePath + "/" + subDirectory;
                boolean canPollMore = pollSubDirectory(path, subDirectory, fileList, depth);
                if (!canPollMore) {
                    return false;
                }
            }
        } else if (file.isFile()) {
            RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file);
            if (depth >= endpoint.getMinDepth() && isValidFile(remote, false, files)) {
                // matched file so add
                fileList.add(remote);
            }
        } else {
            log.debug("Ignoring unsupported remote file type: " + file);
        }
    }

    return true;
}

From source file:org.apache.sqoop.mapreduce.mainframe.TestMainframeDatasetFTPRecordReader.java

@Before
public void setUp() throws IOException {
    mockFTPClient = mock(FTPClient.class);
    MainframeFTPClientUtils.setMockFTPClient(mockFTPClient);
    try {//from  w w  w.j  a va2  s.  c om
        when(mockFTPClient.login("user", "pssword")).thenReturn(true);
        when(mockFTPClient.logout()).thenReturn(true);
        when(mockFTPClient.isConnected()).thenReturn(true);
        when(mockFTPClient.completePendingCommand()).thenReturn(true);
        when(mockFTPClient.changeWorkingDirectory(anyString())).thenReturn(true);
        when(mockFTPClient.getReplyCode()).thenReturn(200);
        when(mockFTPClient.noop()).thenReturn(200);
        when(mockFTPClient.setFileType(anyInt())).thenReturn(true);

        FTPFile ftpFile1 = new FTPFile();
        ftpFile1.setType(FTPFile.FILE_TYPE);
        ftpFile1.setName("test1");
        FTPFile ftpFile2 = new FTPFile();
        ftpFile2.setType(FTPFile.FILE_TYPE);
        ftpFile2.setName("test2");
        FTPFile[] ftpFiles = { ftpFile1, ftpFile2 };
        when(mockFTPClient.listFiles()).thenReturn(ftpFiles);

        when(mockFTPClient.retrieveFileStream("test1"))
                .thenReturn(new ByteArrayInputStream("123\n456\n".getBytes()));
        when(mockFTPClient.retrieveFileStream("test2"))
                .thenReturn(new ByteArrayInputStream("789\n".getBytes()));
        when(mockFTPClient.retrieveFileStream("NotComplete"))
                .thenReturn(new ByteArrayInputStream("NotComplete\n".getBytes()));
    } catch (IOException e) {
        fail("No IOException should be thrown!");
    }

    JobConf conf = new JobConf();
    conf.set(DBConfiguration.URL_PROPERTY, "localhost:" + "11111");
    conf.set(DBConfiguration.USERNAME_PROPERTY, "user");
    conf.set(DBConfiguration.PASSWORD_PROPERTY, "pssword");
    // 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());
    conf.setClass(DBConfiguration.INPUT_CLASS_PROPERTY, DummySqoopRecord.class, DBWritable.class);

    Job job = new Job(conf);
    mfDIS = new MainframeDatasetInputSplit();
    mfDIS.addDataset("test1");
    mfDIS.addDataset("test2");
    context = mock(TaskAttemptContext.class);
    when(context.getConfiguration()).thenReturn(job.getConfiguration());
    mfDFTPRR = new MainframeDatasetFTPRecordReader();
}

From source file:org.apache.sqoop.mapreduce.mainframe.TestMainframeDatasetInputFormat.java

@Before
public void setUp() {
    format = new MainframeDatasetInputFormat<SqoopRecord>();
    mockFTPClient = mock(FTPClient.class);
    MainframeFTPClientUtils.setMockFTPClient(mockFTPClient);
    try {/*w  w w .jav a2 s. c o  m*/
        when(mockFTPClient.login("user", "pssword")).thenReturn(true);
        when(mockFTPClient.logout()).thenReturn(true);
        when(mockFTPClient.isConnected()).thenReturn(true);
        when(mockFTPClient.completePendingCommand()).thenReturn(true);
        when(mockFTPClient.changeWorkingDirectory(anyString())).thenReturn(true);
        when(mockFTPClient.getReplyCode()).thenReturn(200);
        when(mockFTPClient.getReplyString()).thenReturn("");
        when(mockFTPClient.noop()).thenReturn(200);
        when(mockFTPClient.setFileType(anyInt())).thenReturn(true);

        FTPFile ftpFile1 = new FTPFile();
        ftpFile1.setType(FTPFile.FILE_TYPE);
        ftpFile1.setName("test1");
        FTPFile ftpFile2 = new FTPFile();
        ftpFile2.setType(FTPFile.FILE_TYPE);
        ftpFile2.setName("test2");
        FTPFile[] ftpFiles = { ftpFile1, ftpFile2 };
        when(mockFTPClient.listFiles()).thenReturn(ftpFiles);
    } catch (IOException e) {
        fail("No IOException should be thrown!");
    }
}

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

@Test
public void testListSequentialDatasets() {
    try {/*from   w  w w .ja v a  2 s .co  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("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 . j a  v a2  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  . ja  va 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  w w w .  j  a v a  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("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.brandroid.openmanager.util.FileManager.java

public static OpenPath getOpenCache(String path, Boolean bGetNetworkedFiles, SortType sort) throws IOException //, SmbAuthException, SmbException
{
    if (path == null)
        return null;
    //Logger.LogDebug("Checking cache for " + path);
    if (mOpenCache == null)
        mOpenCache = new Hashtable<String, OpenPath>();
    OpenPath ret = mOpenCache.get(path);
    if (ret == null) {
        if (path.startsWith("ftp:/") && OpenServers.DefaultServers != null) {
            Logger.LogDebug("Checking cache for " + path);
            FTPManager man = new FTPManager(path);
            FTPFile file = new FTPFile();
            file.setName(path.substring(path.lastIndexOf("/") + 1));
            Uri uri = Uri.parse(path);//from  w  w w . j  a v a2 s .c om
            OpenServer server = OpenServers.DefaultServers.findByHost("ftp", uri.getHost());
            man.setUser(server.getUser());
            man.setPassword(server.getPassword());
            ret = new OpenFTP(null, file, man);
        } else if (path.startsWith("scp:/")) {
            Uri uri = Uri.parse(path);
            ret = new OpenSCP(uri.getHost(), uri.getUserInfo(), uri.getPath(), null);
        } else if (path.startsWith("sftp:/") && OpenServers.DefaultServers != null) {
            Uri uri = Uri.parse(path);
            OpenServer server = OpenServers.DefaultServers.findByHost("sftp", uri.getHost());
            ret = new OpenSFTP(uri);
            SimpleUserInfo info = new SimpleUserInfo();
            if (server != null)
                info.setPassword(server.getPassword());
            ((OpenSFTP) ret).setUserInfo(info);
        } else if (path.startsWith("smb:/") && OpenServers.DefaultServers != null) {
            try {
                Uri uri = Uri.parse(path);
                String user = uri.getUserInfo();
                if (user != null && user.indexOf(":") > -1)
                    user = user.substring(0, user.indexOf(":"));
                else
                    user = "";
                OpenServer server = OpenServers.DefaultServers.findByPath("smb", uri.getHost(), user,
                        uri.getPath());
                if (server == null)
                    server = OpenServers.DefaultServers.findByUser("smb", uri.getHost(), user);
                if (server == null)
                    server = OpenServers.DefaultServers.findByHost("smb", uri.getHost());
                if (user == "")
                    user = server.getUser();
                if (server != null && server.getPassword() != null && server.getPassword() != "")
                    user += ":" + server.getPassword();
                if (!user.equals(""))
                    user += "@";
                ret = new OpenSMB(uri.getScheme() + "://" + user + uri.getHost() + uri.getPath());
            } catch (Exception e) {
                Logger.LogError("Couldn't get samba from cache.", e);
            }
        } else if (path.startsWith("/data") || path.startsWith("/system"))
            ret = new OpenFileRoot(new OpenFile(path));
        else if (path.startsWith("/"))
            ret = new OpenFile(path);
        else if (path.startsWith("file://"))
            ret = new OpenFile(path.replace("file://", ""));
        else if (path.equals("Videos"))
            ret = OpenExplorer.getVideoParent();
        else if (path.equals("Photos"))
            ret = OpenExplorer.getPhotoParent();
        else if (path.equals("Music"))
            ret = OpenExplorer.getMusicParent();
        else if (path.equals("Downloads"))
            ret = OpenExplorer.getDownloadParent();
        if (ret == null)
            return ret;
        if (ret instanceof OpenFile && ret.isArchive() && Preferences.Pref_Zip_Internal)
            ret = new OpenZip((OpenFile) ret);
        if (ret.requiresThread() && bGetNetworkedFiles) {
            if (ret.listFiles() != null)
                setOpenCache(path, ret);
        } else if (ret instanceof OpenNetworkPath) {
            if (ret.listFromDb(sort))
                setOpenCache(path, ret);
        }
    }
    //if(ret == null)
    //   ret = setOpenCache(path, new OpenFile(path));
    //else setOpenCache(path, ret);
    return ret;
}

From source file:org.mule.transport.ftp.FtpMuleMessageFactoryTestCase.java

@Test
public void setsMimeType() throws Exception {
    FtpMuleMessageFactory factory = new FtpMuleMessageFactory();
    factory.setFtpClient(new TestFTPClient());

    MuleContext muleContext = mock(MuleContext.class, RETURNS_DEEP_STUBS);

    final FTPFile file = new FTPFile();
    file.setName("test.txt");

    MuleMessage message = factory.create(file, null, muleContext);

    assertThat(message.getDataType().getMimeType(), equalTo(MimeTypes.TEXT));
}