From 4e02c2f86da3b31b2f051b414215d38cf752740f Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Mon, 15 Apr 2024 10:59:21 +0200 Subject: [PATCH] Added license header + small refactor --- .../sftp/RemoteResourceFilterConverter.java | 15 +++++++++++++ .../sshj/sftp/RemoteResourceSelector.java | 21 +++++++++++++++++++ .../schmizz/sshj/sftp/RemoteDirectory.java | 9 +++++--- .../net/schmizz/sshj/sftp/SFTPClient.java | 6 +++--- .../schmizz/sshj/sftp/StatefulSFTPClient.java | 8 +++---- 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/hierynomus/sshj/sftp/RemoteResourceFilterConverter.java b/src/main/java/com/hierynomus/sshj/sftp/RemoteResourceFilterConverter.java index 13a41a05..673558a7 100644 --- a/src/main/java/com/hierynomus/sshj/sftp/RemoteResourceFilterConverter.java +++ b/src/main/java/com/hierynomus/sshj/sftp/RemoteResourceFilterConverter.java @@ -1,3 +1,18 @@ +/* + * Copyright (C)2009 - SSHJ Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.hierynomus.sshj.sftp; import com.hierynomus.sshj.sftp.RemoteResourceSelector.Result; diff --git a/src/main/java/com/hierynomus/sshj/sftp/RemoteResourceSelector.java b/src/main/java/com/hierynomus/sshj/sftp/RemoteResourceSelector.java index ae887fb2..3a9e4993 100644 --- a/src/main/java/com/hierynomus/sshj/sftp/RemoteResourceSelector.java +++ b/src/main/java/com/hierynomus/sshj/sftp/RemoteResourceSelector.java @@ -1,8 +1,29 @@ +/* + * Copyright (C)2009 - SSHJ Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.hierynomus.sshj.sftp; import net.schmizz.sshj.sftp.RemoteResourceInfo; public interface RemoteResourceSelector { + public static RemoteResourceSelector ALL = new RemoteResourceSelector() { + @Override + public Result select(RemoteResourceInfo resource) { + return Result.ACCEPT; + } + }; enum Result { /** diff --git a/src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java b/src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java index 8b90d940..102b45f1 100644 --- a/src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java +++ b/src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java @@ -34,11 +34,15 @@ public RemoteDirectory(SFTPEngine requester, String path, byte[] handle) { public List scan(RemoteResourceFilter filter) throws IOException { - return scan(filter == null ? null : selectorFrom(filter)); + return scan(filter == null ? RemoteResourceSelector.ALL : selectorFrom(filter)); } public List scan(RemoteResourceSelector selector) throws IOException { + if (selector == null) { + selector = RemoteResourceSelector.ALL; + } + List remoteResourceInfos = new LinkedList<>(); while (true) { @@ -59,8 +63,7 @@ public List scan(RemoteResourceSelector selector) continue; } - final RemoteResourceSelector.Result selectionResult = selector == null ? - RemoteResourceSelector.Result.ACCEPT : selector.select(inf); + final RemoteResourceSelector.Result selectionResult = selector.select(inf); switch (selectionResult) { case ACCEPT: remoteResourceInfos.add(inf); diff --git a/src/main/java/net/schmizz/sshj/sftp/SFTPClient.java b/src/main/java/net/schmizz/sshj/sftp/SFTPClient.java index 7395cdce..4e148b2b 100644 --- a/src/main/java/net/schmizz/sshj/sftp/SFTPClient.java +++ b/src/main/java/net/schmizz/sshj/sftp/SFTPClient.java @@ -60,18 +60,18 @@ public SFTPFileTransfer getFileTransfer() { public List ls(String path) throws IOException { - return ls(path, (RemoteResourceSelector) null); + return ls(path, RemoteResourceSelector.ALL); } public List ls(String path, RemoteResourceFilter filter) throws IOException { - return ls(path, filter == null ? null : selectorFrom(filter)); + return ls(path, filter == null ? RemoteResourceSelector.ALL : selectorFrom(filter)); } public List ls(String path, RemoteResourceSelector selector) throws IOException { try (RemoteDirectory dir = engine.openDir(path)) { - return dir.scan(selector); + return dir.scan(selector == null ? RemoteResourceSelector.ALL : selector); } } diff --git a/src/main/java/net/schmizz/sshj/sftp/StatefulSFTPClient.java b/src/main/java/net/schmizz/sshj/sftp/StatefulSFTPClient.java index 6ac5103e..6be9af28 100644 --- a/src/main/java/net/schmizz/sshj/sftp/StatefulSFTPClient.java +++ b/src/main/java/net/schmizz/sshj/sftp/StatefulSFTPClient.java @@ -60,7 +60,7 @@ public synchronized void cd(String dirname) public synchronized List ls() throws IOException { - return ls(cwd, (RemoteResourceSelector) null); + return ls(cwd, RemoteResourceSelector.ALL); } public synchronized List ls(RemoteResourceFilter filter) @@ -75,19 +75,19 @@ public synchronized String pwd() public List ls(String path) throws IOException { - return ls(path, (RemoteResourceSelector) null); + return ls(path, RemoteResourceSelector.ALL); } public List ls(String path, RemoteResourceFilter filter) throws IOException { - return ls(path, filter == null ? null : selectorFrom(filter)); + return ls(path, filter == null ? RemoteResourceSelector.ALL : selectorFrom(filter)); } @Override public List ls(String path, RemoteResourceSelector selector) throws IOException { try (RemoteDirectory dir = getSFTPEngine().openDir(cwdify(path))) { - return dir.scan(selector); + return dir.scan(selector == null ? RemoteResourceSelector.ALL : selector); } }