public String[] dir() throws KettleJobException {
    String[] fileList = null;

    try {
      java.util.Vector<?> v = c.ls(".");
      java.util.Vector<String> o = new java.util.Vector<String>();
      if (v != null) {
        for (int i = 0; i < v.size(); i++) {
          Object obj = v.elementAt(i);
          if (obj != null && obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
            LsEntry lse = (com.jcraft.jsch.ChannelSftp.LsEntry) obj;
            if (!lse.getAttrs().isDir()) {
              o.add(lse.getFilename());
            }
          }
        }
      }
      if (o.size() > 0) {
        fileList = new String[o.size()];
        o.copyInto(fileList);
      }
    } catch (SftpException e) {
      throw new KettleJobException(e);
    }

    return fileList;
  }
  static void downloadAllFiles(String fileType) {
    try {
      java.util.Vector fileList = sftpChannel.ls(".");
      if (fileList != null) {
        for (int ii = 0; ii < fileList.size(); ii++) {
          Object obj = fileList.elementAt(ii);
          LsEntry lsentry = (com.jcraft.jsch.ChannelSftp.LsEntry) obj;

          if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
            String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getFilename();
            if (fileType.equals(".*")) {
              if (!(lsentry.getAttrs().isDir())) {
                boolean valid = checkFileName(fileName);
                if (valid) {
                  totalFileCount++;
                  downloadFileByName(fileName);
                }
              }
            } else if (fileName.toLowerCase().endsWith(fileType)) {
              boolean valid = checkFileName(fileName);
              if (valid) {
                totalFileCount++;
                downloadFileByName(fileName);
              }
            }
          }
        }
      }
    } catch (Exception e) {
      System.out.println("Exception in downloadAllFiles(): " + e.toString());
      errorMessage += "Exception in downloadAllFiles(): " + e.toString() + "\n";
    }
  }
Beispiel #3
0
 public List list(String parent) throws IOException {
   try {
     ChannelSftp c = getSftpChannel(parent);
     String path = getPath(parent);
     Collection r = c.ls(path);
     if (r != null) {
       if (!path.endsWith("/")) {
         path = parent + "/";
       }
       List result = new ArrayList();
       for (Iterator iter = r.iterator(); iter.hasNext(); ) {
         Object obj = iter.next();
         if (obj instanceof LsEntry) {
           LsEntry entry = (LsEntry) obj;
           if (".".equals(entry.getFilename()) || "..".equals(entry.getFilename())) {
             continue;
           }
           result.add(path + entry.getFilename());
         }
       }
       return result;
     }
   } catch (SftpException e) {
     IOException ex = new IOException("Failed to return a listing for '" + parent + "'");
     ex.initCause(e);
     throw ex;
   } catch (URISyntaxException usex) {
     IOException ex = new IOException("Failed to return a listing for '" + parent + "'");
     ex.initCause(usex);
     throw ex;
   }
   return null;
 }
Beispiel #4
0
  /**
   * This method is similar to getResource, except that the returned resource is fully initialized
   * (resolved in the sftp repository), and that the given string is a full remote path
   *
   * @param path the full remote path in the repository of the resource
   * @return a fully initialized resource, able to answer to all its methods without needing any
   *     further connection
   */
  public Resource resolveResource(String path) {
    try {
      ChannelSftp c = getSftpChannel(path);

      Collection r = c.ls(getPath(path));

      if (r != null) {
        for (Iterator iter = r.iterator(); iter.hasNext(); ) {
          Object obj = iter.next();
          if (obj instanceof LsEntry) {
            LsEntry entry = (LsEntry) obj;
            SftpATTRS attrs = entry.getAttrs();
            return new BasicResource(
                path, true, attrs.getSize(), attrs.getMTime() * MILLIS_PER_SECOND, false);
          }
        }
      }
    } catch (Exception e) {
      Message.debug("Error while resolving resource " + path, e);
      // silent fail, return unexisting resource
    }

    return new BasicResource(path, false, 0, 0, false);
  }