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

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

Introduction

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

Prototype

public void setType(int type) 

Source Link

Document

Set the type of the file (DIRECTORY_TYPE, FILE_TYPE, etc.).

Usage

From source file:FileListItemAdapterTests.java

@Test
public void test() {
    adapter = new FTPListItemAdapter();
    FTPFile f = new FTPFile();
    f.setName("a");
    f.setType(FTPFile.DIRECTORY_TYPE);
    FileListItem tested = adapter.getFileListItem(f);
    Assert.assertNotNull(tested);/*ww w.j  a  v  a  2s . c  om*/
    Assert.assertEquals("a", tested.getLabel());
    Assert.assertEquals(FileListItemTypes.DIRECTORY, tested.getType());

}

From source file:jlib.misc.MVSFTPEntryParser.java

public FTPFile parseFTPEntry(String entry) {
    FTPFile f = null;
    if (matches(entry)) {
        f = new FTPFile();
        String group = group(1);/*from   ww  w.  j  a  v  a2  s. co  m*/
        String dataSetName = group(2);
        f.setGroup(group);
        f.setType(FTPFile.FILE_TYPE);
        f.setName(dataSetName);

        return (f);
    }
    return null;
}

From source file:jlib.misc.MVSLibraryFTPEntryParser.java

public FTPFile parseFTPEntry(String entry) {
    FTPFile f = null;
    if (matches(entry)) {
        f = new FTPFile();
        String group = group(1);//w  w  w.j a  v a 2 s  .  c  o  m
        String dataSetName = group;
        if (group != null && group.length() > 8)
            dataSetName = group(1).substring(0, 8).trim();
        f.setGroup(group);
        f.setType(FTPFile.FILE_TYPE);
        f.setName(dataSetName);

        return (f);
    }
    return null;
}

From source file:com.datos.vfs.provider.ftp.FtpFileObject.java

/**
 * Fetches the info for this file.//from  w  w  w  .ja va 2  s  .  c om
 */
private void getInfo(final boolean flush) throws IOException {
    final FtpFileObject parent = (FtpFileObject) FileObjectUtils.getAbstractFileObject(getParent());
    FTPFile newFileInfo;
    if (parent != null) {
        newFileInfo = parent.getChildFile(UriParser.decode(getName().getBaseName()), flush);
    } else {
        // Assume the root is a directory and exists
        newFileInfo = new FTPFile();
        newFileInfo.setType(FTPFile.DIRECTORY_TYPE);
    }

    if (newFileInfo == null) {
        this.fileInfo = UNKNOWN;
    } else {
        this.fileInfo = newFileInfo;
    }
}

From source file:lucee.runtime.tag.Ftp.java

private FTPFile existsFile(FTPClient client, String strPath, boolean isFile) throws PageException, IOException {
    strPath = strPath.trim();//from   www.  j  av  a  2  s .  c  o m
    if (strPath.equals("/")) {
        FTPFile file = new FTPFile();
        file.setName("/");
        file.setType(FTPFile.DIRECTORY_TYPE);
        return file;
    }

    // get parent path
    FTPPath path = new FTPPath(client.printWorkingDirectory(), strPath);
    String p = path.getPath();
    String n = path.getName();

    strPath = p;
    if ("//".equals(p))
        strPath = "/";
    if (isFile)
        strPath += n;

    // when directory
    FTPFile[] files = null;
    try {
        files = client.listFiles(strPath);
    } catch (IOException e) {
    }

    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            if (files[i].getName().equalsIgnoreCase(n)) {
                return files[i];
            }
        }

    }
    return null;
}

From source file:com.ai.api.util.UnixFTPEntryParser.java

/**
 * Parses a line of a unix (standard) FTP server file listing and converts
 * it into a usable format in the form of an <code> FTPFile </code>
 * instance.  If the file listing line doesn't describe a file,
 * <code> null </code> is returned, otherwise a <code> FTPFile </code>
 * instance representing the files in the directory is returned.
 * <p>// ww w  .  j a v a  2 s  .  co  m
 * @param entry A line of text from the file listing
 * @return An FTPFile instance corresponding to the supplied entry
 */
public FTPFile parseFTPEntry(String entry) {
    FTPFile file = new FTPFile();
    file.setRawListing(entry);
    int type;
    boolean isDevice = false;

    if (matches(entry)) {
        String typeStr = group(1);
        String hardLinkCount = group(15);
        String usr = group(16);
        String grp = group(17);
        String filesize = group(18);
        String datestr = group(19) + " " + group(20);
        String name = group(21);
        String endtoken = group(22);

        try {
            //file.setTimestamp(super.parseTimestamp(datestr));
            FTPTimestampParserImplExZH Zh2En = new FTPTimestampParserImplExZH();
            file.setTimestamp(Zh2En.parseTimestamp(datestr));
        } catch (ParseException e) {
            //logger.error(e, e);
            //return null;  // this is a parsing failure too.
            //logger.info(entry+":??");
            file.setTimestamp(Calendar.getInstance());
        }

        // bcdlfmpSs-
        switch (typeStr.charAt(0)) {
        case 'd':
            type = FTPFile.DIRECTORY_TYPE;
            break;
        case 'l':
            type = FTPFile.SYMBOLIC_LINK_TYPE;
            break;
        case 'b':
        case 'c':
            isDevice = true;
            // break; - fall through
        case 'f':
        case '-':
            type = FTPFile.FILE_TYPE;
            break;
        default:
            type = FTPFile.UNKNOWN_TYPE;
        }

        file.setType(type);

        int g = 4;
        for (int access = 0; access < 3; access++, g += 4) {
            // Use != '-' to avoid having to check for suid and sticky bits
            file.setPermission(access, FTPFile.READ_PERMISSION, (!group(g).equals("-")));
            file.setPermission(access, FTPFile.WRITE_PERMISSION, (!group(g + 1).equals("-")));

            String execPerm = group(g + 2);
            if (!execPerm.equals("-") && !Character.isUpperCase(execPerm.charAt(0))) {
                file.setPermission(access, FTPFile.EXECUTE_PERMISSION, true);
            } else {
                file.setPermission(access, FTPFile.EXECUTE_PERMISSION, false);
            }
        }

        if (!isDevice) {
            try {
                file.setHardLinkCount(Integer.parseInt(hardLinkCount));
            } catch (NumberFormatException e) {
                // intentionally do nothing
            }
        }

        file.setUser(usr);
        file.setGroup(grp);

        try {
            file.setSize(Long.parseLong(filesize));
        } catch (NumberFormatException e) {
            // intentionally do nothing
        }

        if (null == endtoken) {
            file.setName(name);
        } else {
            // oddball cases like symbolic links, file names
            // with spaces in them.
            name += endtoken;
            if (type == FTPFile.SYMBOLIC_LINK_TYPE) {

                int end = name.indexOf(" -> ");
                // Give up if no link indicator is present
                if (end == -1) {
                    file.setName(name);
                } else {
                    file.setName(name.substring(0, end));
                    file.setLink(name.substring(end + 4));
                }

            } else {
                file.setName(name);
            }
        }
        return file;
    } else {
        logger.info("matches(entry) failure:" + entry);
    }
    return null;
}

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.  j av  a2  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 {// w  ww  .j  a  va2 s. co 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.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 {//from  w  w w  . j av  a2  s .co 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 {// w w  w  .j  av  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!");
    }
}