Provide ClientSession instance in SftpVersionSelector invocation
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/f1df8a2c Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/f1df8a2c Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/f1df8a2c Branch: refs/heads/master Commit: f1df8a2c76a52e4cb7a379e462f4ed2d067a9ddf Parents: c334fd6 Author: Lyor Goldstein <[email protected]> Authored: Thu Sep 15 19:03:52 2016 +0300 Committer: Lyor Goldstein <[email protected]> Committed: Thu Sep 15 19:03:52 2016 +0300 ---------------------------------------------------------------------- .../subsystem/sftp/DefaultSftpClient.java | 2 +- .../subsystem/sftp/SftpFileSystemProvider.java | 13 +++++++-- .../subsystem/sftp/SftpVersionSelector.java | 30 ++++++++++++++++---- .../client/simple/SimpleSftpClientTest.java | 4 +-- .../subsystem/sftp/SftpFileSystemTest.java | 5 ++-- .../sshd/client/subsystem/sftp/SftpTest.java | 2 +- .../subsystem/sftp/SftpVersionSelectorTest.java | 17 +++++++---- 7 files changed, 52 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/f1df8a2c/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/DefaultSftpClient.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/DefaultSftpClient.java b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/DefaultSftpClient.java index 103773f..8b715a1 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/DefaultSftpClient.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/DefaultSftpClient.java @@ -425,7 +425,7 @@ public class DefaultSftpClient extends AbstractSftpClient { } } - int selected = selector.selectVersion(current, new ArrayList<>(available)); + int selected = selector.selectVersion(getClientSession(), current, new ArrayList<>(available)); if (log.isDebugEnabled()) { log.debug("negotiateVersion({}) current={} {} -> {}", getClientChannel(), current, available, selected); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/f1df8a2c/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemProvider.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemProvider.java b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemProvider.java index a59d972..bfc8100 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemProvider.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemProvider.java @@ -87,6 +87,13 @@ import org.apache.sshd.common.util.io.IoUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * A registered {@link FileSystemProvider} that registers the "sftp://" + * scheme so that URLs with this protocol are handled as remote SFTP {@link Path}-s + * - e.g., "{@code sftp://user:password@host/remote/file/path}" + * + * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> + */ public class SftpFileSystemProvider extends FileSystemProvider { public static final String READ_BUFFER_PROP_NAME = "sftp-fs-read-buffer-size"; public static final int DEFAULT_READ_BUFFER_SIZE = SftpClient.DEFAULT_READ_BUFFER_SIZE; @@ -124,7 +131,7 @@ public class SftpFileSystemProvider extends FileSystemProvider { private final SshClient client; private final SftpVersionSelector selector; - private final Map<String, SftpFileSystem> fileSystems = new HashMap<String, SftpFileSystem>(); + private final Map<String, SftpFileSystem> fileSystems = new HashMap<>(); public SftpFileSystemProvider() { this((SshClient) null); @@ -282,7 +289,7 @@ public class SftpFileSystemProvider extends FileSystemProvider { return Collections.unmodifiableMap(env); } - Map<String, Object> resolved = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER); + Map<String, Object> resolved = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); resolved.putAll(env); resolved.putAll(uriParams); return resolved; @@ -305,7 +312,7 @@ public class SftpFileSystemProvider extends FileSystemProvider { } String[] pairs = GenericUtils.split(params, '&'); - Map<String, Object> map = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER); + Map<String, Object> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); for (String p : pairs) { int pos = p.indexOf('='); if (pos < 0) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/f1df8a2c/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelector.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelector.java b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelector.java index ccdf3ce..c5fdaf0 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelector.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelector.java @@ -22,6 +22,7 @@ package org.apache.sshd.client.subsystem.sftp; import java.util.Collection; import java.util.List; +import org.apache.sshd.client.session.ClientSession; import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.NumberUtils; import org.apache.sshd.common.util.ValidateUtils; @@ -29,6 +30,7 @@ import org.apache.sshd.common.util.ValidateUtils; /** * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ +@FunctionalInterface public interface SftpVersionSelector { /** @@ -36,9 +38,14 @@ public interface SftpVersionSelector { */ SftpVersionSelector CURRENT = new SftpVersionSelector() { @Override - public int selectVersion(int current, List<Integer> available) { + public int selectVersion(ClientSession session, int current, List<Integer> available) { return current; } + + @Override + public String toString() { + return "CURRENT"; + } }; /** @@ -46,7 +53,7 @@ public interface SftpVersionSelector { */ SftpVersionSelector MAXIMUM = new SftpVersionSelector() { @Override - public int selectVersion(int current, List<Integer> available) { + public int selectVersion(ClientSession session, int current, List<Integer> available) { int candidate = current; if (GenericUtils.size(available) > 0) { for (Number version : available) { @@ -57,6 +64,11 @@ public interface SftpVersionSelector { } return candidate; } + + @Override + public String toString() { + return "MAXIMUM"; + } }; /** @@ -64,7 +76,7 @@ public interface SftpVersionSelector { */ SftpVersionSelector MINIMUM = new SftpVersionSelector() { @Override - public int selectVersion(int current, List<Integer> available) { + public int selectVersion(ClientSession session, int current, List<Integer> available) { int candidate = current; if (GenericUtils.size(available) > 0) { for (Number version : available) { @@ -75,15 +87,21 @@ public interface SftpVersionSelector { } return candidate; } + + @Override + public String toString() { + return "MINIMUM"; + } }; /** + * @param session The {@link ClientSession} through which the SFTP connection is made * @param current The current version negotiated with the server * @param available Extra versions available - may be empty and/or contain * only the current one * @return The new requested version - if same as current, then nothing is done */ - int selectVersion(int current, List<Integer> available); + int selectVersion(ClientSession session, int current, List<Integer> available); /** * Utility class to help using {@link SftpVersionSelector}s @@ -108,7 +126,7 @@ public interface SftpVersionSelector { public static SftpVersionSelector fixedVersionSelector(final int version) { return new SftpVersionSelector() { @Override - public int selectVersion(int current, List<Integer> available) { + public int selectVersion(ClientSession session, int current, List<Integer> available) { return version; } }; @@ -148,7 +166,7 @@ public interface SftpVersionSelector { return new SftpVersionSelector() { @Override - public int selectVersion(int current, List<Integer> available) { + public int selectVersion(ClientSession session, int current, List<Integer> available) { for (Number prefValue : preferred) { int version = prefValue.intValue(); if (version == current) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/f1df8a2c/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java b/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java index aed7641..bde1cc2 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/simple/SimpleSftpClientTest.java @@ -23,7 +23,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; +import java.util.Collections; import java.util.EnumSet; import org.apache.sshd.client.subsystem.sftp.SftpClient; @@ -59,7 +59,7 @@ public class SimpleSftpClientTest extends BaseSimpleClientTestSupport { @Override public void setUp() throws Exception { super.setUp(); - sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory())); + sshd.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory())); sshd.setCommandFactory(new ScpCommandFactory()); sshd.setFileSystemFactory(fileSystemFactory); client.start(); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/f1df8a2c/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java index e3ee7ec..b673821 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpFileSystemTest.java @@ -42,7 +42,6 @@ import java.nio.file.attribute.GroupPrincipal; import java.nio.file.attribute.PosixFilePermissions; import java.nio.file.attribute.UserPrincipalLookupService; import java.nio.file.attribute.UserPrincipalNotFoundException; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -94,7 +93,7 @@ public class SftpFileSystemTest extends BaseTestSupport { @BeforeClass public static void setupServerInstance() throws Exception { sshd = Utils.setupTestServer(SftpFileSystemTest.class); - sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory())); + sshd.setSubsystemFactories(Collections.<NamedFactory<Command>>singletonList(new SftpSubsystemFactory())); sshd.setCommandFactory(new ScpCommandFactory()); sshd.start(); port = sshd.getPort(); @@ -297,7 +296,7 @@ public class SftpFileSystemTest extends BaseTestSupport { final AtomicInteger selected = new AtomicInteger(-1); SftpVersionSelector selector = new SftpVersionSelector() { @Override - public int selectVersion(int current, List<Integer> available) { + public int selectVersion(ClientSession session, int current, List<Integer> available) { int numAvailable = GenericUtils.size(available); Integer maxValue = null; if (numAvailable == 1) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/f1df8a2c/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java index 983addf..996036a 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java @@ -1036,7 +1036,7 @@ public class SftpTest extends AbstractSftpClientTestSupport { final AtomicInteger selected = new AtomicInteger(-1); SftpVersionSelector selector = new SftpVersionSelector() { @Override - public int selectVersion(int current, List<Integer> available) { + public int selectVersion(ClientSession session, int current, List<Integer> available) { int numAvailable = GenericUtils.size(available); Integer maxValue = null; if (numAvailable == 1) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/f1df8a2c/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelectorTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelectorTest.java b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelectorTest.java index ed2b3e9..ba0d456 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelectorTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpVersionSelectorTest.java @@ -25,11 +25,13 @@ import java.util.Collections; import java.util.List; import java.util.Random; +import org.apache.sshd.client.session.ClientSession; import org.apache.sshd.server.subsystem.sftp.SftpSubsystem; import org.apache.sshd.util.test.BaseTestSupport; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; +import org.mockito.Mockito; /** * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> @@ -44,8 +46,10 @@ public class SftpVersionSelectorTest extends BaseTestSupport { public void testCurrentVersionSelector() { List<Integer> available = new ArrayList<>(); Random rnd = new Random(System.nanoTime()); + ClientSession session = Mockito.mock(ClientSession.class); for (int expected = SftpSubsystem.LOWER_SFTP_IMPL; expected <= SftpSubsystem.HIGHER_SFTP_IMPL; expected++) { - assertEquals("Mismatched directly selected for available=" + available, expected, SftpVersionSelector.CURRENT.selectVersion(expected, available)); + assertEquals("Mismatched directly selected for available=" + available, + expected, SftpVersionSelector.CURRENT.selectVersion(session, expected, available)); available.add(Integer.valueOf(expected)); } @@ -53,7 +57,7 @@ public class SftpVersionSelectorTest extends BaseTestSupport { for (int index = 0; index < available.size(); index++) { Collections.shuffle(available, rnd); assertEquals("Mismatched suffling selected for current=" + expected + ", available=" + available, - expected, SftpVersionSelector.CURRENT.selectVersion(expected, available)); + expected, SftpVersionSelector.CURRENT.selectVersion(session, expected, available)); } } } @@ -74,6 +78,7 @@ public class SftpVersionSelectorTest extends BaseTestSupport { List<Integer> preferred = new ArrayList<>(available); List<Integer> unavailable = Arrays.asList(7365, 3777347); Random rnd = new Random(System.nanoTime()); + ClientSession session = Mockito.mock(ClientSession.class); for (int index = 0; index < preferred.size(); index++) { Collections.shuffle(preferred, rnd); SftpVersionSelector selector = SftpVersionSelector.Utils.preferredVersionSelector(preferred); @@ -81,12 +86,12 @@ public class SftpVersionSelectorTest extends BaseTestSupport { for (int current = SftpSubsystem.LOWER_SFTP_IMPL; current <= SftpSubsystem.HIGHER_SFTP_IMPL; current++) { assertEquals("Mismatched selected for current= " + current + ", available=" + available + ", preferred=" + preferred, - expected, selector.selectVersion(current, available)); + expected, selector.selectVersion(session, current, available)); try { Collections.shuffle(unavailable, rnd); int version = unavailable.get(0); - int actual = selector.selectVersion(version, unavailable); + int actual = selector.selectVersion(session, version, unavailable); fail("Unexpected selected version (" + actual + ")" + " for current= " + version + ", available=" + unavailable @@ -115,9 +120,11 @@ public class SftpVersionSelectorTest extends BaseTestSupport { } Random rnd = new Random(System.nanoTime()); + ClientSession session = Mockito.mock(ClientSession.class); for (int current = SftpSubsystem.LOWER_SFTP_IMPL; current <= SftpSubsystem.HIGHER_SFTP_IMPL; current++) { for (int index = 0; index < available.size(); index++) { - assertEquals("Mismatched selection for current=" + current + ", availble=" + available, expected, selector.selectVersion(current, available)); + assertEquals("Mismatched selection for current=" + current + ", availble=" + available, + expected, selector.selectVersion(session, current, available)); Collections.shuffle(available, rnd); } }
