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

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

Introduction

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

Prototype

public boolean isDirectory() 

Source Link

Document

Determine if the file is a directory.

Usage

From source file:org.punksearch.crawler.adapters.FtpAdapterTest.java

public void testIsDirectory() throws Exception {
    FTPFile file = getSomeFile();
    assertEquals(file.isDirectory(), adapter.isDirectory(new FtpItem(file, null)));
    FTPFile dir = getSomeDir();/*from ww w  . j  a  va 2s .  c  o  m*/
    assertEquals(dir.isDirectory(), adapter.isDirectory(new FtpItem(dir, null)));
}

From source file:org.punksearch.crawler.adapters.FtpAdapterTest.java

public void testIsFile() throws Exception {
    FTPFile file = getSomeFile();
    assertEquals(!file.isDirectory(), adapter.isFile(new FtpItem(file, null)));
    FTPFile dir = getSomeDir();//from w ww.ja v a  2  s.  c o m
    assertEquals(!dir.isDirectory(), adapter.isFile(new FtpItem(dir, null)));
}

From source file:org.punksearch.crawler.adapters.FtpAdapterTest.java

private FTPFile getSomeFile() {
    try {//from w  ww.j  a  v  a2  s  .  c o  m
        FTPFile[] items = ftp.listFiles(rootPath);
        for (FTPFile item : items) {
            if (item.isDirectory() && !item.getName().startsWith(".")) {
                FTPFile[] items2 = ftp.listFiles(item.getName());
                if (items2 == null) {
                    continue;
                }
                ftp.changeWorkingDirectory(rootPath + item.getName());
                for (FTPFile item2 : items2) {
                    if (!item2.isDirectory() && !item2.isSymbolicLink() && !item2.getName().startsWith(".")
                            && item2.getName().contains(".")) {
                        return item2;
                    }
                }
            }
        }
        return null;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.punksearch.crawler.adapters.FtpAdapterTest.java

private FTPFile getSomeDir(int level) throws Exception {
    String curPath = "/";
    FTPFile result = null;/*from   w ww.  j av a2s .c o m*/
    for (int cur = 0; cur <= level; cur++) {
        FTPFile[] items = ftp.listFiles(curPath);
        for (FTPFile item : items) {
            if (item.isDirectory() && !item.getName().startsWith(".")) {
                curPath += "/" + item.getName();
                result = item;
                break;
            }
        }
    }
    return result;
}

From source file:org.ramadda.repository.type.FtpTypeHandler.java

/**
 * _more_//  w ww  .j  av  a 2s .c  om
 *
 * @param request _more_
 * @param parentEntry _more_
 * @param id _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
public Entry makeSynthEntry(Request request, Entry parentEntry, String id) throws Exception {
    Object[] values = parentEntry.getValues();
    if (values == null) {
        return null;
    }
    String baseDir = (String) values[COL_BASEDIR];
    String server = (String) values[COL_SERVER];
    MyFTPFile myFtpFile = getFileFromId(parentEntry, id, baseDir);
    if (myFtpFile == null) {
        return null;
    }
    if (myFtpFile.path.equals(baseDir)) {
        return parentEntry;
    }
    FTPFile ftpFile = myFtpFile.ftpFile;
    TypeHandler handler = (ftpFile.isDirectory() ? getRepository().getTypeHandler(TypeHandler.TYPE_GROUP)
            : getRepository().getTypeHandler(TypeHandler.TYPE_FILE));
    handler = this;
    String synthId = Repository.ID_PREFIX_SYNTH + parentEntry.getId() + ":" + id;

    boolean isDir = ftpFile.isDirectory();
    Entry entry = (isDir ? (Entry) new Entry(synthId, handler, true) : new Entry(synthId, handler));

    String name = IOUtil.getFileTail(ftpFile.getName());
    entry.setIsLocalFile(true);
    Entry parent;
    if (myFtpFile.path.equals(baseDir)) {
        parent = (Entry) parentEntry;
    } else {
        File tmp = new File(myFtpFile.path);
        String parentPath = tmp.getParent().replace("\\", "/");
        String parentId = getSynthId(parentEntry, baseDir, parentPath);
        if (parentPath.equals(baseDir)) {
            parent = (Entry) parentEntry;
        } else {
            parent = (Entry) getEntryManager().getEntry(request, parentId, false, false);
        }
    }

    double maxSize = 0;
    if (values[COL_MAXSIZE] != null) {
        maxSize = ((Double) values[COL_MAXSIZE]).doubleValue();
    }

    long dttm = ftpFile.getTimestamp().getTime().getTime();
    /*
      String datePattern = (String)values[COL_DATE_PATTERN];
      //TODO cache the compiled patterns
      if(datePattern!=null && datePattern.length()>0)  {
      Pattern p = Pattern.compile(datePattern);
      Matcher m = p.matcher(path);
      if(m.matches()) {
          String dateString = m.group(1);
          String dateFormat = (String)values[COL_DATE_FORMAT];
          Date date=null;
          if(dateFormat!=null && dateFormat.length()==0) {
               date = new SimpleDateFormat(dateFormat).parse(dateString);
            
          } else {
               date = DateUtil.parse(dateString);
          }
          dttm  = date.getTime();
      }
      }
     */
    Resource resource;
    if (isDir) {
        resource = new Resource("ftp://" + server + myFtpFile.path, Resource.TYPE_URL);
    } else {
        if (ftpFile.getSize() > 1000000 * maxSize) {
            resource = new Resource("ftp://" + server + myFtpFile.path, Resource.TYPE_URL);
        } else {
            resource = new Resource(name, Resource.TYPE_REMOTE_FILE);

        }
        resource.setFileSize(ftpFile.getSize());
    }
    entry.initEntry(name, "", parent, getUserManager().getLocalFileUser(), resource, "", dttm, dttm, dttm, dttm,
            null);

    return entry;
}

From source file:org.shept.util.FtpFileCopy.java

protected boolean acceptFile(FTPFile file, String pattern) {
    if (file.isDirectory())
        return false;
    return PatternMatchUtils.simpleMatch(pattern, file.getName());
}

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

private void delete(FTPFile... files) {
    try {//from  ww w.  j av a  2 s  .  co m
        for (FTPFile file : files) {
            if (file.isFile()) {
                m_client.deleteFile(file.getName());
            } else if (file.isDirectory()) {
                changeDirectory(file.getName());
                delete(m_client.listFiles());
                changeDirectory(UP);
                m_client.removeDirectory(file.getName());
            }
        }

    } catch (IOException e) {
        LOG.error(e);
        throw new UserException(INTERNAL_FTP_ERROR);
    }
}

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

private void download(String locationPath, FTPFile... files) {
    try {//ww  w  .j  a  v a  2 s  . co m
        for (FTPFile file : files) {
            if (file.isFile()) {
                OutputStream output = new FileOutputStream(locationPath + File.separator + file.getName());
                m_client.retrieveFile(file.getName(), output);
                output.close();
            } else if (file.isDirectory()) {
                changeDirectory(file.getName());
                File fileNew = new File(locationPath + File.separator + file.getName());
                fileNew.mkdir();
                download(locationPath + File.separator + file.getName(), m_client.listFiles());
                changeDirectory(UP);
            }
        }

    } catch (IOException e) {
        LOG.error(e);
        throw new UserException(INTERNAL_FTP_ERROR);
    }
}

From source file:org.springframework.integration.ftp.gateway.FtpOutboundGateway.java

@Override
protected boolean isDirectory(FTPFile file) {
    return file.isDirectory();
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
@SuppressWarnings("unchecked")
public void testLsForNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();//from   ww  w . j  a v a 2s .c  o m

    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()));
        }
    }
}