Esempio n. 1
0
  private void mkdirs(String path) throws Exception {
    // sftp.cd("/"); //change to the root dir

    // String dirs[] = splitPath(path);

    String cwd = sftp.pwd();
    // System.out.println("CWD: " + cwd);

    while (!path.startsWith(cwd)) {
      System.out.println(cwd + " " + path);
      sftp.cd(".."); // should throw exception if can't cdup
      System.out.println("CDUP!");
      cwd = sftp.pwd();
    }

    String mkPath = path.substring(cwd.length(), path.length());
    System.out.println("DIRS TO MAKE: " + mkPath);

    String dirs[] = splitPath(mkPath);

    for (int i = 0; i < dirs.length; i++) {
      System.out.println("mkdir " + dirs[i]);
      logger.info("mkdir " + dirs[i]);
      // swallow exception that results from trying to
      // make a dir that already exists
      try {
        sftp.mkdir(dirs[i]);
      } catch (Exception ex) {
      }

      // change to the new dir
      // throws an exception if something went wrong
      sftp.cd(dirs[i]);
    }
  }
Esempio n. 2
0
 public String pwd() throws KettleJobException {
   try {
     return c.pwd();
   } catch (SftpException e) {
     throw new KettleJobException(e);
   }
 }
 @Test
 public void testCreateClientFailsIfPwdReturnsRelativePath() throws Exception {
   final String remoteRoot = "some/directory/in/my/home/dir";
   hostConfig = createWithOverrideUsernameAndPassword(mockJSch);
   getHostConfig().setRemoteRootDir(remoteRoot);
   final BapSshCommonConfiguration commonConfiguration =
       new BapSshCommonConfiguration("Ignore me", null, null, false);
   getHostConfig().setCommonConfig(commonConfiguration);
   expect(
           mockJSch.getSession(
               getHostConfig().getUsername(),
               getHostConfig().getHostname(),
               getHostConfig().getPort()))
       .andReturn(mockSession);
   mockSession.setPassword(TEST_PASSPHRASE);
   mockSession.setConfig((Properties) anyObject());
   mockSession.connect(getHostConfig().getTimeout());
   expect(mockSession.openChannel("sftp")).andReturn(mockSftp);
   mockSftp.connect(getHostConfig().getTimeout());
   testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true);
   mockSftp.cd(getHostConfig().getRemoteRootDir());
   expect(mockSftp.pwd()).andReturn("home/bap/" + remoteRoot);
   expect(mockSftp.isConnected()).andReturn(false);
   expect(mockSession.isConnected()).andReturn(false);
   assertCreateClientThrowsException("home/bap/" + remoteRoot);
 }
Esempio n. 4
0
 public String getCurrentDirectory() throws GenericFileOperationFailedException {
   LOG.trace("getCurrentDirectory()");
   try {
     String answer = channel.pwd();
     LOG.trace("Current dir: {}", answer);
     return answer;
   } catch (SftpException e) {
     throw new GenericFileOperationFailedException("Cannot get current directory", e);
   }
 }
Esempio n. 5
0
  /* (non-Javadoc)
   * @see net.sf.thingamablog.transport.PublishTransport#publishFile(java.lang.String, java.io.File, net.sf.thingamablog.transport.TransportProgress)
   */
  public boolean publishFile(String pubPath, File file, TransportProgress tp) {
    if (sftp == null) {
      failMsg = "SFTP Client not initialized!";
      return false;
    }

    if (!isConnected()) {
      failMsg = "Not Connected!!!";
      return false;
    }

    if (tp.isAborted()) {
      failMsg = "Aborted";
      return false;
    }

    if (!pubPath.endsWith("/")) pubPath += "/"; // append a trailing slash if needed

    try {
      String cwd = sftp.pwd();
      if (!cwd.endsWith("/")) cwd += "/";
      if (!pubPath.equals(cwd)) {
        boolean changedDir = false;
        try {
          sftp.cd(pubPath); // try to change to the pub path
          changedDir = true; // changed dir OK
          System.out.println("Changed to " + pubPath);
        } catch (Exception cdEx) {
          logger.log(Level.WARNING, "Problem changing SFTP dir", cdEx);
        }

        if (!changedDir) {
          // was unable to change dir. the dir likely does not exist
          // so we'll try making the dir structure of pubPath
          mkdirs(pubPath);
          // sftp.cd(pubPath);
        }
      }

      int mode = ChannelSftp.OVERWRITE;
      // String dest = pubPath + file.getName();
      InputStream is = new FileInputStream(file);
      sftp.put(is, file.getName(), new MyProgressMonitor(tp), mode);
      is.close();

      return true;
    } catch (Exception ex) {
      failMsg = "Error publishing file to " + pubPath;
      failMsg += "\n" + ex.getMessage();
      logger.log(Level.WARNING, failMsg, ex);
      ex.printStackTrace();
    }

    return false;
  }
 private BapSshClient assertCreateWithDefaultInfo(final String responseFromPwd)
     throws JSchException, SftpException {
   final BapSshCommonConfiguration commonConfiguration =
       new BapSshCommonConfiguration("Ignore me", null, null, false);
   getHostConfig().setCommonConfig(commonConfiguration);
   expect(
           mockJSch.getSession(
               getHostConfig().getUsername(),
               getHostConfig().getHostname(),
               getHostConfig().getPort()))
       .andReturn(mockSession);
   mockSession.setPassword(getHostConfig().getPassword());
   mockSession.setConfig((Properties) anyObject());
   mockSession.connect(getHostConfig().getTimeout());
   expect(mockSession.openChannel("sftp")).andReturn(mockSftp);
   mockSftp.connect(getHostConfig().getTimeout());
   testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true);
   mockSftp.cd(getHostConfig().getRemoteRootDir());
   if (responseFromPwd != null) expect(mockSftp.pwd()).andReturn(responseFromPwd);
   return assertCreateClient();
 }