2

I have been using sshj libraries

<dependency>
    <groupId>net.schmizz</groupId>
    <artifactId>sshj</artifactId>
    <version>0.3.1</version>
</dependency>

Following was my code using 0.3.1 which worked fine for doing uploading of files supporting wildcard patterns.

SSHClient client = null;
SCPUploadClient uploader = null;
try {
    client = getClient();
    uploader = client.newSCPFileTransfer().newSCPUploadClient();
    uploader.setFileFilter(new WildcardFileFilter(wildCardPattern));

    //determine the remote directory
    File f = new File(localDirLocation);
    String dir = remoteDirLocation + f.getName();
    uploader.copy(localDirLocation, remoteDirLocation);
} catch (IOException e) {
      //processing exceptions here
} finally {
    disconnectClient(client);
}

But now the code gives me a lot of compilation errors when I tried moving over to 0.5.0.

I would like to understand how do I go about setting fileFilters when I want to do uploading and downloading of files from local to remote machine and vice versa

Can someone please help me with this ?

leppie
  • 115,091
  • 17
  • 196
  • 297
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66

2 Answers2

3

Currently using 0.5.0 this is no longer possible. I created a pull request for Shikhar (the maintainer) of SSHJ.

Below I've adapted your code sample to make it work with 0.5.0. The basic change is that you now need to provide a LocalSourceFile to the copy method. In order to make it possible for the SCPUploadClient to only send the filtered contents of the directory, I've overridden the getChildren(LocalFileFilter) method.

SSHClient client = null;
SCPUploadClient uploader = null;
try {
    client = getClient();
    uploader = client.newSCPFileTransfer().newSCPUploadClient();
    File f = new File(localDirLocation);
    FilteredFileSystemDirectory filteredDir = new FilteredFileSystemDirectory(f, new WildcardFileFilter());
    String dir = remoteDirLocation + f.getName();
    uploader.copy(filteredDir, remoteDirLocation);
} catch (IOException e) {
    //processing exceptions here
} finally {
    disconnectClient(client);
}

public class FilteredFileSystemDirectory extends FileSystemFile {
    private final LocalFileFilter filter;

    public FilteredFileSystemDirectory(File f, LocalFileFilter filter) {
        super(f);
        this.filter = filter;
    }

    @Override
    public Iterable<? extends LocalSourceFile> getChildren(LocalFileFilter filter)
        throws IOException {
        return super.getChildren(filter);
    }

}

For the WildcardFileFilter see my second answer on how to do this in 0.6.0.

Hope this helps.

Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
  • Thank you so much for helping me with that pull. Is this available as part of 0.6.0 ? I tried upgrading to 0.6.0 but I am not able to find WildcardFileFilter class in it :(. – Krishnan Mahadevan Sep 27 '11 at 03:28
2

Using 0.6.0 you can write your code as follows:

SSHClient client = null;
SCPUploadClient uploader = null;
try {
    client = getClient();
    uploader = client.newSCPFileTransfer().newSCPUploadClient();
    uploader.setUploadFilter(new WildcardFileFilter(wildCardPattern));

    //determine the remote directory
    File f = new File(localDirLocation);
    String dir = remoteDirLocation + f.getName();
    uploader.copy(new FileSystemFile(localDirLocation), remoteDirLocation);
} catch (IOException e) {
    //processing exceptions here
} finally {
    disconnectClient(client);
}

The WildcardFileFilter you used came from commons-io I guess. As that is a FilenameFilter and not a LocalFileFilter, you could very simply implement this as:

public WildcardFileFilter implements LocalFileFilter {
    private String wildcardPattern;

    public WildcardFileFilter(String wildcardPattern) {
        this.wildcardPattern = wildcardPattern;
    }

    @Override
    public boolean accept(LocalSourceFile file) {
        return FilenameUtils.wildcardMatchOnSystem(file.getName(), wildcardPattern);
    }
}
Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
  • That was the same thing that I ended up doing after I took a look at the actual implementation of org.apache.commons.io.filefilter.WildcardFileFilter.accept(File).. Thank you so much for helping me with an answer... Appreciate your help Jeroen – Krishnan Mahadevan Sep 27 '11 at 10:17