net.schmizz.sshj.sftp.Response Java Examples

The following examples show how to use net.schmizz.sshj.sftp.Response. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsFailedToStat() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    final SFTPClient sftpClient = mock(SFTPClient.class);
    // stat for the parent was successful, simulating that dir2 exists, but no dir3.
    when(sftpClient.stat("/dir1/dir2/dir3")).thenThrow(new SFTPException(Response.StatusCode.FAILURE, "Failure"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    try {
        sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);
        fail("Should fail");
    } catch (IOException e) {
        assertEquals("Failed to determine if remote directory exists at /dir1/dir2/dir3 due to 4: Failure", e.getMessage());
    }

    // Dir existence check should be done by stat
    verify(sftpClient).stat(eq("/dir1/dir2/dir3"));
}
 
Example #2
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsNotExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    final SFTPClient sftpClient = mock(SFTPClient.class);
    // stat for the parent was successful, simulating that dir2 exists, but no dir3.
    when(sftpClient.stat("/dir1/dir2/dir3")).thenThrow(new SFTPException(Response.StatusCode.NO_SUCH_FILE, "No such file"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // Dir existence check should be done by stat
    verify(sftpClient).stat(eq("/dir1/dir2/dir3")); // dir3 was not found
    verify(sftpClient).stat(eq("/dir1/dir2")); // so, dir2 was checked
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // dir2 existed, so dir3 was created.
}
 
Example #3
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsParentNotExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    final SFTPClient sftpClient = mock(SFTPClient.class);

    // stat for the dir1 was successful, simulating that dir1 exists, but no dir2 and dir3.
    when(sftpClient.stat("/dir1/dir2/dir3")).thenThrow(new SFTPException(Response.StatusCode.NO_SUCH_FILE, "No such file"));
    when(sftpClient.stat("/dir1/dir2")).thenThrow(new SFTPException(Response.StatusCode.NO_SUCH_FILE, "No such file"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // Dir existence check should be done by stat
    verify(sftpClient).stat(eq("/dir1/dir2/dir3")); // dir3 was not found
    verify(sftpClient).stat(eq("/dir1/dir2")); // dir2 was not found, too
    verify(sftpClient).stat(eq("/dir1")); // dir1 was found
    verify(sftpClient).mkdir(eq("/dir1/dir2")); // dir1 existed, so dir2 was created.
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // then dir3 was created.
}
 
Example #4
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsNotExistedFailedToCreate() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    final SFTPClient sftpClient = mock(SFTPClient.class);

    // stat for the parent was successful, simulating that dir2 exists, but no dir3.
    when(sftpClient.stat("/dir1/dir2/dir3")).thenThrow(new SFTPException(Response.StatusCode.NO_SUCH_FILE, "No such file"));
    // Failed to create dir3.
    doThrow(new SFTPException(Response.StatusCode.FAILURE, "Failed")).when(sftpClient).mkdir(eq("/dir1/dir2/dir3"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    try {
        sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);
        fail("Should fail");
    } catch (IOException e) {
        assertEquals("Failed to create remote directory /dir1/dir2/dir3 due to 4: Failed", e.getMessage());
    }

    // Dir existence check should be done by stat
    verify(sftpClient).stat(eq("/dir1/dir2/dir3")); // dir3 was not found
    verify(sftpClient).stat(eq("/dir1/dir2")); // so, dir2 was checked
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // dir2 existed, so dir3 was created.
}
 
Example #5
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsBlindlyAlreadyExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    when(processContext.getProperty(SFTPTransfer.DISABLE_DIRECTORY_LISTING)).thenReturn(new MockPropertyValue("true"));

    final SFTPClient sftpClient = mock(SFTPClient.class);
    // If the dir existed, a failure exception is thrown, but should be swallowed.
    doThrow(new SFTPException(Response.StatusCode.FAILURE, "Failure")).when(sftpClient).mkdir(eq("/dir1/dir2/dir3"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // stat should not be called.
    verify(sftpClient, times(0)).stat(eq("/dir1/dir2/dir3"));
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // dir3 was created blindly.
}
 
Example #6
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsBlindlyFailed() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    when(processContext.getProperty(SFTPTransfer.DISABLE_DIRECTORY_LISTING)).thenReturn(new MockPropertyValue("true"));

    final SFTPClient sftpClient = mock(SFTPClient.class);
    doThrow(new SFTPException(Response.StatusCode.PERMISSION_DENIED, "Permission denied")).when(sftpClient).mkdir(eq("/dir1/dir2/dir3"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    try {
        sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);
        fail("Should fail");
    } catch (IOException e) {
        assertEquals("Could not blindly create remote directory due to Permission denied", e.getMessage());
    }

    // stat should not be called.
    verify(sftpClient, times(0)).stat(eq("/dir1/dir2/dir3"));
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // dir3 was created blindly.
}
 
Example #7
Source File: SFTPQuotaFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
private Space getSpaceStatVFSOpenSSH(SFTPEngine sftp, final Path directory) throws BackgroundException {
    try {
        final Request request = sftp.newExtendedRequest("[email protected]").putString(directory.getAbsolute());
        final Response response = sftp.request(request).retrieve();
        switch(response.getType()) {
            case EXTENDED_REPLY:
                long blockSize = response.readUInt64(); /* file system block size */
                long filesystemBlockSize = response.readUInt64(); /* fundamental fs block size */
                long totalBlocks = response.readUInt64(); /* number of blocks (unit f_frsize) */
                long filesystemFreeBlocks = response.readUInt64(); /* free blocks in file system */
                long blocksAvailable = response.readUInt64(); /* free blocks for non-root */
                long fileInodes = response.readUInt64(); /* total file inodes */
                long fileInodesFree = response.readUInt64(); /* free file inodes */
                long fileInodesAvailable = response.readUInt64(); /* free file inodes for to non-root */
                byte[] filesystemID = new byte[8]; /* file system id */
                response.readRawBytes(filesystemID);
                long flags = response.readUInt64(); /* bit mask of f_flag values */
                long maximumFilenameLength = response.readUInt64(); /* maximum filename length */

                long total = totalBlocks * filesystemBlockSize;
                long available = blocksAvailable * filesystemBlockSize;
                long used = total - available;

                return new Space(used, available);
            default:
                throw new IOException(String.format("Unexpected response type %s", response.getType()));
        }
    }
    catch(IOException e) {
        throw new SFTPExceptionMappingService().map("Failure to read attributes of {0}", e, directory);
    }
}
 
Example #8
Source File: SFTPRemoteFile.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isReadable() throws IOException {
  try (InputStream ignored = createInputStream()) {
    LOG.trace("File is readable");
    return true;
  } catch (IOException e) {
    if (e instanceof SFTPException) {
      SFTPException ex = (SFTPException)e;
      LOG.error(Utils.format("Error checking isReadable {}", ex.getStatusCode()), ex);
      if (ex.getStatusCode() == Response.StatusCode.PERMISSION_DENIED || ex.getStatusCode() == Response.StatusCode.NO_SUCH_FILE) {
        return false;
      }
    }
    throw e;
  }
}
 
Example #9
Source File: SFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public FileInfo getRemoteFileInfo(final FlowFile flowFile, final String path, String filename) throws IOException {
    final SFTPClient sftpClient = getSFTPClient(flowFile);

    final List<RemoteResourceInfo> remoteResources;
    try {
        remoteResources = sftpClient.ls(path);
    } catch (final SFTPException e) {
        if (e.getStatusCode() == Response.StatusCode.NO_SUCH_FILE) {
            return null;
        } else {
            throw new IOException("Failed to obtain file listing for " + path, e);
        }
    }

    RemoteResourceInfo matchingEntry = null;
    for (final RemoteResourceInfo entry : remoteResources) {
        if (entry.getName().equalsIgnoreCase(filename)) {
            matchingEntry = entry;
            break;
        }
    }

    // Previously JSCH would perform a listing on the full path (path + filename) and would get an exception when it wasn't
    // a file and then return null, so to preserve that behavior we return null if the matchingEntry is a directory
    if (matchingEntry != null && matchingEntry.isDirectory()) {
        return null;
    } else {
        return newFileInfo(matchingEntry, path);
    }
}
 
Example #10
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnsureDirectoryExistsBlindlyParentNotExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    when(processContext.getProperty(SFTPTransfer.DISABLE_DIRECTORY_LISTING)).thenReturn(new MockPropertyValue("true"));

    final SFTPClient sftpClient = mock(SFTPClient.class);
    final AtomicInteger mkdirCount = new AtomicInteger(0);
    doAnswer(invocation -> {
        final int cnt = mkdirCount.getAndIncrement();
        if (cnt == 0) {
            // If the parent dir does not exist, no such file exception is thrown.
            throw new SFTPException(Response.StatusCode.NO_SUCH_FILE, "Failure");
        } else {
            logger.info("Created the dir successfully for the 2nd time");
        }
        return true;
    }).when(sftpClient).mkdir(eq("/dir1/dir2/dir3"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // stat should not be called.
    verify(sftpClient, times(0)).stat(eq("/dir1/dir2/dir3"));
    // dir3 was created blindly, but failed for the 1st time, and succeeded for the 2nd time.
    verify(sftpClient, times(2)).mkdir(eq("/dir1/dir2/dir3"));
    verify(sftpClient).mkdir(eq("/dir1/dir2")); // dir2 was created successfully.
}
 
Example #11
Source File: SFTPSession.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Promise<Response, SFTPException> request(final Request req) throws IOException {
    transcript.log(Type.request, String.format("%d %s", req.getRequestID(), req.getType()));
    return super.request(req);
}