[SSHD-698] Use lambda and method references, streams Deprecate Utils inner classes and use interface methods instead.
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/977b7b52 Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/977b7b52 Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/977b7b52 Branch: refs/heads/master Commit: 977b7b529e1dba7f24fde674ba16aa451912cd0b Parents: 0fde885 Author: Guillaume Nodet <[email protected]> Authored: Thu Sep 15 11:29:48 2016 +0200 Committer: Guillaume Nodet <[email protected]> Committed: Thu Sep 15 21:31:08 2016 +0200 ---------------------------------------------------------------------- pom.xml | 2 +- .../apache/sshd/agent/SshAgentConstants.java | 15 +- .../org/apache/sshd/agent/local/AgentImpl.java | 6 +- .../agent/local/ChannelAgentForwarding.java | 9 +- .../org/apache/sshd/agent/unix/AgentServer.java | 20 +- .../sshd/agent/unix/AgentServerProxy.java | 50 ++-- .../sshd/agent/unix/ChannelAgentForwarding.java | 33 +-- .../client/ClientAuthenticationManager.java | 4 +- .../org/apache/sshd/client/ClientBuilder.java | 32 +-- .../java/org/apache/sshd/client/SshClient.java | 149 ++++------- .../java/org/apache/sshd/client/SshKeyScan.java | 8 +- .../auth/AuthenticationIdentitiesProvider.java | 157 ++++++----- .../client/auth/BuiltinUserAuthFactories.java | 2 +- .../auth/hostbased/HostKeyIdentityProvider.java | 48 ++-- .../auth/hostbased/UserAuthHostBased.java | 4 +- .../keyboard/UserAuthKeyboardInteractive.java | 2 +- .../auth/password/PasswordIdentityProvider.java | 267 +++++++++--------- .../client/auth/password/UserAuthPassword.java | 2 +- .../client/auth/pubkey/KeyPairIdentity.java | 4 +- .../auth/pubkey/UserAuthPublicKeyIterator.java | 2 +- .../client/channel/AbstractClientChannel.java | 42 ++- .../sshd/client/channel/ChannelSession.java | 7 +- .../sshd/client/channel/ChannelSubsystem.java | 9 +- .../channel/PtyCapableChannelSession.java | 22 +- .../client/config/hosts/HostConfigEntry.java | 29 +- .../client/config/hosts/KnownHostDigest.java | 2 +- .../keys/BuiltinClientIdentitiesWatcher.java | 14 +- .../config/keys/ClientIdentitiesWatcher.java | 7 +- .../sshd/client/config/keys/ClientIdentity.java | 7 +- .../org/apache/sshd/client/kex/DHGClient.java | 8 +- .../org/apache/sshd/client/kex/DHGEXClient.java | 10 +- .../sshd/client/scp/AbstractScpClient.java | 22 +- .../client/session/AbstractClientSession.java | 16 +- .../client/session/ClientConnectionService.java | 7 +- .../client/session/ClientUserAuthService.java | 4 +- .../client/simple/AbstractSimpleClient.java | 165 +++--------- .../AbstractSimpleClientSessionCreator.java | 2 +- .../sshd/client/simple/SimpleSessionClient.java | 34 ++- .../sshd/client/simple/SimpleSftpClient.java | 34 ++- .../subsystem/sftp/AbstractSftpClient.java | 1 + .../subsystem/sftp/DefaultSftpClient.java | 18 +- .../subsystem/sftp/SftpClientCreator.java | 2 +- .../client/subsystem/sftp/SftpFileSystem.java | 5 +- .../subsystem/sftp/SftpFileSystemProvider.java | 18 +- .../subsystem/sftp/SftpPosixFileAttributes.java | 1 + .../subsystem/sftp/SftpVersionSelector.java | 188 ++++++------- .../extensions/BuiltinSftpClientExtensions.java | 2 +- .../helpers/AbstractSftpClientExtension.java | 4 +- .../org/apache/sshd/common/AttributeStore.java | 95 ++++--- .../org/apache/sshd/common/BaseBuilder.java | 6 +- .../org/apache/sshd/common/BuiltinFactory.java | 21 +- .../java/org/apache/sshd/common/Closeable.java | 43 +++ .../org/apache/sshd/common/NamedFactory.java | 67 ++--- .../org/apache/sshd/common/NamedResource.java | 144 +++++----- .../org/apache/sshd/common/OptionalFeature.java | 69 ++--- .../org/apache/sshd/common/ServiceFactory.java | 39 +-- .../org/apache/sshd/common/SshConstants.java | 25 +- .../apache/sshd/common/SyspropsMapWrapper.java | 38 +-- .../sshd/common/channel/AbstractChannel.java | 23 +- .../common/channel/ChannelRequestHandler.java | 8 +- .../org/apache/sshd/common/channel/PtyMode.java | 14 +- .../apache/sshd/common/channel/SttySupport.java | 4 +- .../org/apache/sshd/common/channel/Window.java | 20 +- .../sshd/common/cipher/BuiltinCiphers.java | 4 +- .../org/apache/sshd/common/cipher/Cipher.java | 41 +-- .../org/apache/sshd/common/cipher/ECCurves.java | 57 ++-- .../common/compression/BuiltinCompressions.java | 2 +- .../config/NamedFactoriesListParseResult.java | 2 +- .../config/NamedResourceListParseResult.java | 2 +- .../sshd/common/config/SshConfigFileReader.java | 10 +- .../common/config/keys/BuiltinIdentities.java | 13 +- .../sshd/common/config/keys/KeyUtils.java | 1 + .../sshd/common/config/keys/PublicKeyEntry.java | 4 +- .../sshd/common/digest/BuiltinDigests.java | 2 +- .../sshd/common/file/util/BaseFileSystem.java | 9 +- .../sshd/common/file/util/MockFileSystem.java | 1 + .../common/forward/DefaultTcpipForwarder.java | 37 +-- .../common/forward/LocalForwardingEntry.java | 2 +- .../apache/sshd/common/forward/SocksProxy.java | 15 +- .../global/AbstractOpenSshHostKeysHandler.java | 4 +- .../common/helpers/AbstractFactoryManager.java | 2 +- .../io/BuiltinIoServiceFactoryFactories.java | 2 +- .../sshd/common/io/mina/MinaConnector.java | 17 +- .../apache/sshd/common/io/mina/MinaSession.java | 23 +- .../common/io/nio2/Nio2CompletionHandler.java | 18 +- .../apache/sshd/common/io/nio2/Nio2Service.java | 3 +- .../sshd/common/kex/BuiltinDHFactories.java | 2 +- .../java/org/apache/sshd/common/kex/DHG.java | 1 + .../sshd/common/kex/KexFactoryManager.java | 12 +- .../sshd/common/kex/KexProposalOption.java | 32 +-- .../org/apache/sshd/common/kex/KeyExchange.java | 45 +++- .../AbstractFileKeyPairProvider.java | 22 +- .../keyprovider/AbstractKeyPairProvider.java | 37 +-- .../AbstractResourceKeyPairProvider.java | 11 +- .../common/keyprovider/KeyIdentityProvider.java | 268 ++++++++++--------- .../common/keyprovider/KeyPairProvider.java | 118 ++++---- .../keyprovider/MappedKeyPairProvider.java | 7 +- .../org/apache/sshd/common/mac/BuiltinMacs.java | 2 +- .../org/apache/sshd/common/scp/ScpHelper.java | 78 +++--- .../ConnectionServiceRequestHandler.java | 2 +- .../helpers/AbstractConnectionService.java | 74 +++-- .../common/session/helpers/AbstractSession.java | 86 +++--- .../session/helpers/SessionTimeoutListener.java | 1 + .../common/signature/BuiltinSignatures.java | 2 +- .../signature/SignatureFactoriesManager.java | 4 +- .../common/subsystem/sftp/SftpConstants.java | 19 +- .../sftp/SftpUniversalOwnerAndGroup.java | 2 +- .../sshd/common/util/EventListenerUtils.java | 117 ++++---- .../apache/sshd/common/util/GenericUtils.java | 256 +++++++++++++----- .../sshd/common/util/Int2IntFunction.java | 73 +++-- .../apache/sshd/common/util/NumberUtils.java | 18 +- .../java/org/apache/sshd/common/util/Pair.java | 16 +- .../sshd/common/util/ReflectionUtils.java | 7 +- .../apache/sshd/common/util/SelectorUtils.java | 4 +- .../apache/sshd/common/util/Transformer.java | 37 +-- .../apache/sshd/common/util/ValidateUtils.java | 6 + .../apache/sshd/common/util/buffer/Buffer.java | 4 +- .../sshd/common/util/buffer/BufferUtils.java | 7 +- .../util/buffer/keys/BufferPublicKeyParser.java | 71 ++--- .../util/closeable/AbstractCloseable.java | 14 +- .../util/closeable/AbstractInnerCloseable.java | 9 +- .../common/util/closeable/CloseableUtils.java | 25 +- .../common/util/closeable/FuturesCloseable.java | 18 +- .../common/util/closeable/IoBaseCloseable.java | 13 - .../util/closeable/ParallelCloseable.java | 18 +- .../util/io/CloseableEmptyInputStream.java | 1 + .../sshd/common/util/io/FileInfoExtractor.java | 50 +--- .../org/apache/sshd/common/util/io/IoUtils.java | 3 +- .../sshd/common/util/io/LimitInputStream.java | 1 + .../sshd/common/util/io/NullInputStream.java | 1 + .../sshd/common/util/io/NullOutputStream.java | 1 + .../sshd/common/util/logging/LoggingUtils.java | 46 ++-- .../sshd/common/util/logging/SimplifiedLog.java | 1 - .../sshd/common/util/net/SshdSocketAddress.java | 21 +- .../sshd/common/util/threads/ThreadUtils.java | 9 +- .../server/ServerAuthenticationManager.java | 129 +++++---- .../org/apache/sshd/server/ServerBuilder.java | 27 +- .../java/org/apache/sshd/server/Signal.java | 26 +- .../java/org/apache/sshd/server/SshServer.java | 56 ++-- .../server/auth/BuiltinUserAuthFactories.java | 2 +- .../sshd/server/auth/gss/CredentialHelper.java | 1 + .../sshd/server/auth/gss/GSSAuthenticator.java | 1 + .../auth/hostbased/UserAuthHostBased.java | 2 +- .../pubkey/CachingPublicKeyAuthenticator.java | 2 +- .../server/auth/pubkey/UserAuthPublicKey.java | 4 +- .../sshd/server/channel/ChannelSession.java | 34 +-- .../keys/AuthorizedKeysAuthenticator.java | 2 +- .../sshd/server/config/keys/ServerIdentity.java | 7 +- .../sshd/server/forward/ForwardingFilter.java | 2 +- .../sshd/server/forward/TcpipServerChannel.java | 50 ++-- .../global/CancelTcpipForwardHandler.java | 2 +- .../server/global/OpenSshHostKeysHandler.java | 4 +- .../sshd/server/global/TcpipForwardHandler.java | 2 +- .../server/jaas/JaasPasswordAuthenticator.java | 21 +- .../org/apache/sshd/server/kex/DHGEXServer.java | 8 +- .../org/apache/sshd/server/kex/DHGServer.java | 6 +- .../server/session/AbstractServerSession.java | 12 +- .../server/session/ServerUserAuthService.java | 21 +- .../sshd/server/shell/InvertedShellWrapper.java | 7 +- .../sshd/server/shell/ProcessShellFactory.java | 1 + .../server/shell/TtyFilterOutputStream.java | 3 +- .../sshd/server/subsystem/sftp/Handle.java | 1 + .../server/subsystem/sftp/SftpSubsystem.java | 84 ++---- .../subsystem/sftp/SftpSubsystemFactory.java | 18 +- .../java/org/apache/sshd/KeyReExchangeTest.java | 27 +- .../src/test/java/org/apache/sshd/LoadTest.java | 26 +- .../java/org/apache/sshd/WindowAdjustTest.java | 59 ++-- .../client/ClientAuthenticationManagerTest.java | 2 +- .../sshd/client/ClientSessionListenerTest.java | 11 +- .../java/org/apache/sshd/client/ClientTest.java | 119 +++----- .../auth/PasswordIdentityProviderTest.java | 4 +- .../sshd/client/channel/ChannelExecTest.java | 19 +- .../hosts/HostConfigEntryResolverTest.java | 49 ++-- .../client/config/keys/ClientIdentityTest.java | 2 +- .../KnownHostsServerKeyVerifierTest.java | 24 +- .../org/apache/sshd/client/scp/ScpTest.java | 5 +- .../sshd/client/session/ClientSessionTest.java | 54 ++-- .../client/simple/SimpleSessionClientTest.java | 33 +-- .../sftp/AbstractSftpClientTestSupport.java | 2 +- .../sftp/DefaultCloseableHandleTest.java | 26 +- .../subsystem/sftp/SftpFileSystemTest.java | 60 ++--- .../sshd/client/subsystem/sftp/SftpTest.java | 40 +-- .../subsystem/sftp/SftpVersionSelectorTest.java | 13 +- .../client/subsystem/sftp/SftpVersionsTest.java | 32 +-- .../helpers/AbstractCheckFileExtensionTest.java | 56 ++-- .../helpers/AbstractMD5HashExtensionTest.java | 8 +- .../apache/sshd/common/AttributeStoreTest.java | 45 ++-- .../org/apache/sshd/common/SshBuilderTest.java | 2 +- .../sshd/common/auth/AuthenticationTest.java | 135 ++++------ .../common/auth/SinglePublicKeyAuthTest.java | 44 ++- .../channel/ChannelPipedOutputStreamTest.java | 22 +- .../apache/sshd/common/channel/WindowTest.java | 4 +- .../sshd/common/cipher/BuiltinCiphersTest.java | 8 +- .../apache/sshd/common/cipher/CipherTest.java | 23 +- .../compression/BuiltinCompressionsTest.java | 6 +- .../common/compression/CompressionTest.java | 4 +- .../common/config/SshConfigFileReaderTest.java | 8 +- .../sshd/common/config/TimeValueConfigTest.java | 10 +- .../config/keys/AuthorizedKeysTestSupport.java | 2 +- .../common/config/keys/KeyRandomArtTest.java | 9 +- .../KeyUtilsFingerprintCaseSensitivityTest.java | 2 +- .../keys/KeyUtilsFingerprintGenerationTest.java | 4 +- .../sshd/common/config/keys/KeyUtilsTest.java | 4 +- .../file/root/RootedFileSystemProviderTest.java | 8 +- .../common/forward/PortForwardingLoadTest.java | 9 +- .../sshd/common/forward/PortForwardingTest.java | 25 +- .../common/future/DefaultSshFutureTest.java | 33 +-- .../sshd/common/kex/BuiltinDHFactoriesTest.java | 6 +- .../sshd/common/kex/KexFactoryManagerTest.java | 8 +- .../apache/sshd/common/kex/KeyExchangeTest.java | 15 +- .../common/keyprovider/KeyPairProviderTest.java | 16 +- .../apache/sshd/common/mac/BuiltinMacsTest.java | 6 +- .../org/apache/sshd/common/mac/MacTest.java | 18 +- .../ReservedSessionMessagesHandlerTest.java | 24 +- .../session/helpers/AbstractSessionTest.java | 14 +- .../common/signature/BuiltinSignaturesTest.java | 6 +- .../signature/SignatureFactoriesTest.java | 50 ++-- .../sshd/common/signature/SignatureRSATest.java | 21 +- .../common/util/EventListenerUtilsTest.java | 2 +- .../sshd/common/util/NumberUtilsTest.java | 4 +- .../sshd/common/util/SecurityUtilsTest.java | 10 +- .../sshd/common/util/TransformerTest.java | 6 +- .../util/closeable/CloseableUtilsTest.java | 13 +- .../apache/sshd/deprecated/UserAuthAgent.java | 2 +- .../deprecated/UserAuthKeyboardInteractive.java | 2 +- .../sshd/deprecated/UserAuthPassword.java | 2 +- .../sshd/deprecated/UserAuthPublicKey.java | 4 +- .../server/ServerAuthenticationManagerTest.java | 2 +- .../sshd/server/ServerProxyAcceptorTest.java | 10 +- .../sshd/server/ServerSessionListenerTest.java | 12 +- .../java/org/apache/sshd/server/ServerTest.java | 146 +++++----- .../sshd/server/StandardEnvironmentTest.java | 7 +- .../sshd/server/auth/WelcomeBannerTest.java | 4 +- .../sshd/server/channel/ChannelSessionTest.java | 11 +- .../server/config/keys/ServerIdentityTest.java | 4 +- .../jaas/JaasPasswordAuthenticatorTest.java | 2 +- .../server/shell/TtyFilterInputStreamTest.java | 18 +- .../apache/sshd/spring/SpringConfigTest.java | 1 - .../sshd/util/test/AsyncEchoShellFactory.java | 36 +-- .../apache/sshd/util/test/BaseTestSupport.java | 2 +- .../util/test/BogusPasswordAuthenticator.java | 2 +- .../org/apache/sshd/util/test/JSchLogger.java | 1 - .../java/org/apache/sshd/util/test/Utils.java | 9 +- .../common/util/net/LdapNetworkConnector.java | 2 +- .../sshd/server/auth/BaseAuthenticatorTest.java | 2 +- 245 files changed, 2639 insertions(+), 3529 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 067142a..1d4b678 100644 --- a/pom.xml +++ b/pom.xml @@ -110,7 +110,7 @@ <bouncycastle.version>1.54</bouncycastle.version> <slf4j.version>1.7.21</slf4j.version> - <spring.version>3.0.6.RELEASE</spring.version> + <spring.version>3.2.17.RELEASE</spring.version> <jgit.version>3.4.1.201406201815-r</jgit.version> <junit.version>4.12</junit.version> <surefire.plugin.version>2.19.1</surefire.plugin.version> http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java b/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java index aecf89d..3c86197 100644 --- a/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java +++ b/sshd-core/src/main/java/org/apache/sshd/agent/SshAgentConstants.java @@ -18,9 +18,7 @@ */ package org.apache.sshd.agent; -import java.lang.reflect.Field; import java.util.Map; -import java.util.function.Predicate; import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.logging.LoggingUtils; @@ -76,16 +74,11 @@ public final class SshAgentConstants { private static class LazyMessagesMapHolder { private static final Map<Integer, String> MESSAGES_MAP = - LoggingUtils.generateMnemonicMap(SshAgentConstants.class, new Predicate<Field>() { - @Override - public boolean test(Field f) { - String name = f.getName(); - if (name.startsWith("SSH_AGENT_CONSTRAIN")) { - return false; - } + LoggingUtils.generateMnemonicMap(SshAgentConstants.class, f -> { + String name = f.getName(); + return !name.startsWith("SSH_AGENT_CONSTRAIN") + && (name.startsWith("SSH_AGENT") || name.startsWith("SSH2_AGENT")); - return name.startsWith("SSH_AGENT") || name.startsWith("SSH2_AGENT"); - } }); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java b/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java index 3ccb551..6f7525f 100644 --- a/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java +++ b/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentImpl.java @@ -61,11 +61,7 @@ public class AgentImpl implements SshAgent { throw new SshException("Agent closed"); } - List<Pair<PublicKey, String>> pks = new ArrayList<>(); - for (Pair<KeyPair, String> kp : keys) { - pks.add(new Pair<>(kp.getFirst().getPublic(), kp.getSecond())); - } - return pks; + return GenericUtils.map(keys, kp -> new Pair<>(kp.getFirst().getPublic(), kp.getSecond())); } @Override http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java b/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java index 9a2309c..c8e6395 100644 --- a/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java +++ b/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java @@ -31,7 +31,6 @@ import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.channel.ChannelListener; import org.apache.sshd.common.channel.ChannelOutputStream; import org.apache.sshd.common.future.CloseFuture; -import org.apache.sshd.common.future.SshFutureListener; import org.apache.sshd.common.session.Session; import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.ValidateUtils; @@ -103,13 +102,7 @@ public class ChannelAgentForwarding extends AbstractServerChannel { @Override public CloseFuture close(boolean immediately) { - return super.close(immediately).addListener(new SshFutureListener<CloseFuture>() { - @Override - @SuppressWarnings("synthetic-access") - public void operationComplete(CloseFuture sshFuture) { - closeImmediately0(); - } - }); + return super.close(immediately).addListener(sshFuture -> closeImmediately0()); } @Override http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServer.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServer.java b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServer.java index d4e6ea1..113ea23 100644 --- a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServer.java +++ b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServer.java @@ -95,19 +95,15 @@ public class AgentServer extends AbstractLoggingBean implements Closeable, Execu } ExecutorService executor = getExecutorService(); - agentThread = executor.submit(new Runnable() { - @SuppressWarnings("synthetic-access") - @Override - public void run() { - try { - while (true) { - long clientSock = Local.accept(handle); - Socket.timeoutSet(clientSock, 10000000); // TODO make this configurable - new SshAgentSession(clientSock, agent).run(); - } - } catch (Exception e) { - log.error("Failed to run session", e); + agentThread = executor.submit(() -> { + try { + while (true) { + long clientSock = Local.accept(handle); + Socket.timeoutSet(clientSock, 10000000); // TODO make this configurable + new SshAgentSession(clientSock, agent).run(); } + } catch (Exception e) { + log.error("Failed to run session", e); } }); return authSocket; http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java index cd0f22b..62b8e8f 100644 --- a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java +++ b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentServerProxy.java @@ -92,36 +92,32 @@ public class AgentServerProxy extends AbstractLoggingBean implements SshAgentSer pipeService = (executor == null) ? ThreadUtils.newSingleThreadExecutor("sshd-AgentServerProxy-PIPE-" + authSocket) : executor; pipeCloseOnExit = executor != pipeService || shutdownOnExit; - piper = pipeService.submit(new Runnable() { - @SuppressWarnings("synthetic-access") - @Override - public void run() { - try { - while (isOpen()) { - try { - long clientSock = Local.accept(handle); - if (!isOpen()) { - break; - } - - Session session = AgentServerProxy.this.service.getSession(); - Socket.timeoutSet(clientSock, PropertyResolverUtils.getIntProperty(session, AUTH_SOCKET_TIMEOUT, DEFAULT_AUTH_SOCKET_TIMEOUT)); - AgentForwardedChannel channel = new AgentForwardedChannel(clientSock); - AgentServerProxy.this.service.registerChannel(channel); - channel.open().verify(PropertyResolverUtils.getLongProperty(session, CHANNEL_OPEN_TIMEOUT_PROP, DEFAULT_CHANNEL_OPEN_TIMEOUT)); - } catch (Exception e) { - if (log.isDebugEnabled()) { - log.debug("run(open={}) {} while authentication forwarding: {}", - isOpen(), e.getClass().getSimpleName(), e.getMessage()); - } - if (log.isTraceEnabled()) { - log.trace("run(open=" + isOpen() + ") authentication forwarding failure details", e); - } + piper = pipeService.submit(() -> { + try { + while (isOpen()) { + try { + long clientSock = Local.accept(handle); + if (!isOpen()) { + break; + } + + Session session = AgentServerProxy.this.service.getSession(); + Socket.timeoutSet(clientSock, PropertyResolverUtils.getIntProperty(session, AUTH_SOCKET_TIMEOUT, DEFAULT_AUTH_SOCKET_TIMEOUT)); + AgentForwardedChannel channel = new AgentForwardedChannel(clientSock); + AgentServerProxy.this.service.registerChannel(channel); + channel.open().verify(PropertyResolverUtils.getLongProperty(session, CHANNEL_OPEN_TIMEOUT_PROP, DEFAULT_CHANNEL_OPEN_TIMEOUT)); + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("run(open={}) {} while authentication forwarding: {}", + isOpen(), e.getClass().getSimpleName(), e.getMessage()); + } + if (log.isTraceEnabled()) { + log.trace("run(open=" + isOpen() + ") authentication forwarding failure details", e); } } - } finally { - innerFinished.set(true); } + } finally { + innerFinished.set(true); } }); } catch (IOException e) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java b/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java index ef28f34..e87b5e0 100644 --- a/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java +++ b/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java @@ -32,7 +32,6 @@ import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.channel.ChannelListener; import org.apache.sshd.common.channel.ChannelOutputStream; import org.apache.sshd.common.future.CloseFuture; -import org.apache.sshd.common.future.SshFutureListener; import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.ValidateUtils; import org.apache.sshd.common.util.buffer.Buffer; @@ -94,22 +93,18 @@ public class ChannelAgentForwarding extends AbstractServerChannel { ValidateUtils.checkTrue(copyBufSize >= MIN_FORWARDER_BUF_SIZE, "Copy buf size below min.: %d", copyBufSize); ValidateUtils.checkTrue(copyBufSize <= MAX_FORWARDER_BUF_SIZE, "Copy buf size above max.: %d", copyBufSize); - forwarder = forwardService.submit(new Runnable() { - @SuppressWarnings("synthetic-access") - @Override - public void run() { - try { - byte[] buf = new byte[copyBufSize]; - while (true) { - int len = Socket.recv(handle, buf, 0, buf.length); - if (len > 0) { - out.write(buf, 0, len); - out.flush(); - } + forwarder = forwardService.submit(() -> { + try { + byte[] buf = new byte[copyBufSize]; + while (true) { + int len = Socket.recv(handle, buf, 0, buf.length); + if (len > 0) { + out.write(buf, 0, len); + out.flush(); } - } catch (IOException e) { - close(true); } + } catch (IOException e) { + close(true); } }); @@ -175,13 +170,7 @@ public class ChannelAgentForwarding extends AbstractServerChannel { @Override public CloseFuture close(boolean immediately) { - return super.close(immediately).addListener(new SshFutureListener<CloseFuture>() { - @SuppressWarnings("synthetic-access") - @Override - public void operationComplete(CloseFuture sshFuture) { - closeImmediately0(); - } - }); + return super.close(immediately).addListener(sshFuture -> closeImmediately0()); } @Override http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/ClientAuthenticationManager.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/ClientAuthenticationManager.java b/sshd-core/src/main/java/org/apache/sshd/client/ClientAuthenticationManager.java index ceb4105..0e0ff05 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/ClientAuthenticationManager.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/ClientAuthenticationManager.java @@ -131,10 +131,10 @@ public interface ClientAuthenticationManager extends KeyPairProviderHolder { */ List<NamedFactory<UserAuth>> getUserAuthFactories(); default String getUserAuthFactoriesNameList() { - return NamedResource.Utils.getNames(getUserAuthFactories()); + return NamedResource.getNames(getUserAuthFactories()); } default List<String> getUserAuthFactoriesNames() { - return NamedResource.Utils.getNameList(getUserAuthFactories()); + return NamedResource.getNameList(getUserAuthFactories()); } void setUserAuthFactories(List<NamedFactory<UserAuth>> userAuthFactories); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/ClientBuilder.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/ClientBuilder.java b/sshd-core/src/main/java/org/apache/sshd/client/ClientBuilder.java index 933ff42..00917b5 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/ClientBuilder.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/ClientBuilder.java @@ -19,7 +19,6 @@ package org.apache.sshd.client; -import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -48,29 +47,22 @@ import org.apache.sshd.server.forward.ForwardedTcpipFactory; * SshClient builder */ public class ClientBuilder extends BaseBuilder<SshClient, ClientBuilder> { - public static final Transformer<DHFactory, NamedFactory<KeyExchange>> DH2KEX = - new Transformer<DHFactory, NamedFactory<KeyExchange>>() { - @Override - public NamedFactory<KeyExchange> transform(DHFactory factory) { - if (factory == null) { - return null; - } else if (factory.isGroupExchange()) { - return DHGEXClient.newFactory(factory); - } else { - return DHGClient.newFactory(factory); - } - } - }; + + public static final Transformer<DHFactory, NamedFactory<KeyExchange>> DH2KEX = factory -> + factory == null + ? null + : factory.isGroupExchange() + ? DHGEXClient.newFactory(factory) + : DHGClient.newFactory(factory); // Compression is not enabled by default for the client public static final List<CompressionFactory> DEFAULT_COMPRESSION_FACTORIES = - Collections.unmodifiableList(Arrays.<CompressionFactory>asList(BuiltinCompressions.none)); + Collections.unmodifiableList(Collections.singletonList(BuiltinCompressions.none)); public static final List<NamedFactory<Channel>> DEFAULT_CHANNEL_FACTORIES = - Collections.unmodifiableList(Arrays.<NamedFactory<Channel>>asList(ForwardedTcpipFactory.INSTANCE)); + Collections.unmodifiableList(Collections.singletonList(ForwardedTcpipFactory.INSTANCE)); public static final List<RequestHandler<ConnectionService>> DEFAULT_GLOBAL_REQUEST_HANDLERS = - Collections.unmodifiableList(Arrays.<RequestHandler<ConnectionService>>asList( - OpenSshHostKeysHandler.INSTANCE)); + Collections.unmodifiableList(Collections.singletonList(OpenSshHostKeysHandler.INSTANCE)); public static final ServerKeyVerifier DEFAULT_SERVER_KEY_VERIFIER = AcceptAllServerKeyVerifier.INSTANCE; public static final HostConfigEntryResolver DEFAULT_HOST_CONFIG_ENTRY_RESOLVER = DefaultConfigFileHostEntryResolver.INSTANCE; @@ -111,7 +103,7 @@ public class ClientBuilder extends BaseBuilder<SshClient, ClientBuilder> { super.fillWithDefaultValues(); if (compressionFactories == null) { - compressionFactories = NamedFactory.Utils.setUpBuiltinFactories(false, DEFAULT_COMPRESSION_FACTORIES); + compressionFactories = NamedFactory.setUpBuiltinFactories(false, DEFAULT_COMPRESSION_FACTORIES); } if (keyExchangeFactories == null) { @@ -172,7 +164,7 @@ public class ClientBuilder extends BaseBuilder<SshClient, ClientBuilder> { * @see org.apache.sshd.common.kex.BuiltinDHFactories#isSupported() */ public static List<NamedFactory<KeyExchange>> setUpDefaultKeyExchanges(boolean ignoreUnsupported) { - return NamedFactory.Utils.setUpTransformedFactories(ignoreUnsupported, DEFAULT_KEX_PREFERENCE, DH2KEX); + return NamedFactory.setUpTransformedFactories(ignoreUnsupported, DEFAULT_KEX_PREFERENCE, DH2KEX); } public static ClientBuilder builder() { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java b/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java index 2cdfca0..17f8bb4 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java @@ -37,7 +37,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.security.GeneralSecurityException; import java.security.KeyPair; -import java.security.PublicKey; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -50,7 +49,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.CopyOnWriteArrayList; -import java.util.function.Supplier; import java.util.logging.ConsoleHandler; import java.util.logging.Formatter; import java.util.logging.Handler; @@ -71,14 +69,12 @@ import org.apache.sshd.client.channel.ClientChannel; import org.apache.sshd.client.channel.ClientChannelEvent; import org.apache.sshd.client.config.hosts.HostConfigEntry; import org.apache.sshd.client.config.hosts.HostConfigEntryResolver; -import org.apache.sshd.client.config.hosts.KnownHostEntry; import org.apache.sshd.client.config.keys.ClientIdentityLoader; import org.apache.sshd.client.config.keys.DefaultClientIdentitiesWatcher; import org.apache.sshd.client.future.ConnectFuture; import org.apache.sshd.client.future.DefaultConnectFuture; import org.apache.sshd.client.keyverifier.DefaultKnownHostsServerKeyVerifier; import org.apache.sshd.client.keyverifier.KnownHostsServerKeyVerifier; -import org.apache.sshd.client.keyverifier.ModifiedServerKeyAcceptor; import org.apache.sshd.client.keyverifier.ServerKeyVerifier; import org.apache.sshd.client.session.AbstractClientSession; import org.apache.sshd.client.session.ClientConnectionServiceFactory; @@ -177,12 +173,7 @@ import org.apache.sshd.common.util.net.SshdSocketAddress; */ public class SshClient extends AbstractFactoryManager implements ClientFactoryManager, ClientSessionCreator, Closeable { - public static final Factory<SshClient> DEFAULT_SSH_CLIENT_FACTORY = new Factory<SshClient>() { - @Override - public SshClient create() { - return new SshClient(); - } - }; + public static final Factory<SshClient> DEFAULT_SSH_CLIENT_FACTORY = SshClient::new; /** * Command line option used to indicate non-default target port @@ -222,7 +213,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa private final AuthenticationIdentitiesProvider identitiesProvider; public SshClient() { - identitiesProvider = AuthenticationIdentitiesProvider.Utils.wrap(identities); + identitiesProvider = AuthenticationIdentitiesProvider.wrap(identities); } public SessionFactory getSessionFactory() { @@ -342,8 +333,8 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa return null; } - int index = AuthenticationIdentitiesProvider.Utils.findIdentityIndex( - identities, AuthenticationIdentitiesProvider.Utils.PASSWORD_IDENTITY_COMPARATOR, password); + int index = AuthenticationIdentitiesProvider.findIdentityIndex( + identities, AuthenticationIdentitiesProvider.PASSWORD_IDENTITY_COMPARATOR, password); if (index >= 0) { return (String) identities.remove(index); } else { @@ -370,8 +361,8 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa return null; } - int index = AuthenticationIdentitiesProvider.Utils.findIdentityIndex( - identities, AuthenticationIdentitiesProvider.Utils.KEYPAIR_IDENTITY_COMPARATOR, kp); + int index = AuthenticationIdentitiesProvider.findIdentityIndex( + identities, AuthenticationIdentitiesProvider.KEYPAIR_IDENTITY_COMPARATOR, kp); if (index >= 0) { return (KeyPair) identities.remove(index); } else { @@ -393,18 +384,8 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa KeyPairProvider defaultIdentities = getKeyPairProvider(); if (defaultIdentities == null) { setKeyPairProvider(new DefaultClientIdentitiesWatcher( - new Supplier<ClientIdentityLoader>() { - @Override - public ClientIdentityLoader get() { - return getClientIdentityLoader(); - } - }, - new Supplier<FilePasswordProvider>() { - @Override - public FilePasswordProvider get() { - return getFilePasswordProvider(); - } - })); + this::getClientIdentityLoader, + this::getFilePasswordProvider)); } // Register the additional agent forwarding channel if needed @@ -466,26 +447,16 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa @Override protected Closeable getInnerCloseable() { return builder() - .run(new Runnable() { - @SuppressWarnings("synthetic-access") - @Override - public void run() { - removeSessionTimeout(sessionFactory); - } - }) + .run(() -> removeSessionTimeout(sessionFactory)) .sequential(connector, ioServiceFactory) - .run(new Runnable() { - @SuppressWarnings("synthetic-access") - @Override - public void run() { - connector = null; - ioServiceFactory = null; - if (shutdownExecutor && (executor != null) && (!executor.isShutdown())) { - try { - executor.shutdownNow(); - } finally { - executor = null; - } + .run(() -> { + connector = null; + ioServiceFactory = null; + if (shutdownExecutor && (executor != null) && (!executor.isShutdown())) { + try { + executor.shutdownNow(); + } finally { + executor = null; } } }) @@ -610,25 +581,21 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa protected SshFutureListener<IoConnectFuture> createConnectCompletionListener( final ConnectFuture connectFuture, final String username, final SocketAddress address, final Collection<? extends KeyPair> identities, final boolean useDefaultIdentities) { - return new SshFutureListener<IoConnectFuture>() { - @Override - @SuppressWarnings("synthetic-access") - public void operationComplete(IoConnectFuture future) { - if (future.isCanceled()) { - connectFuture.cancel(); - return; - } + return future -> { + if (future.isCanceled()) { + connectFuture.cancel(); + return; + } - Throwable t = future.getException(); - if (t != null) { - if (log.isDebugEnabled()) { - log.debug("operationComplete({}@{}) failed ({}): {}", - username, address, t.getClass().getSimpleName(), t.getMessage()); - } - connectFuture.setException(t); - } else { - onConnectOperationComplete(future.getSession(), connectFuture, username, address, identities, useDefaultIdentities); + Throwable t = future.getException(); + if (t != null) { + if (log.isDebugEnabled()) { + log.debug("operationComplete({}@{}) failed ({}): {}", + username, address, t.getClass().getSimpleName(), t.getMessage()); } + connectFuture.setException(t); + } else { + onConnectOperationComplete(future.getSession(), connectFuture, username, address, identities, useDefaultIdentities); } }; } @@ -1021,12 +988,9 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa public static AbstractFileKeyPairProvider setupSessionIdentities(ClientFactoryManager client, Collection<File> identities, final BufferedReader stdin, final PrintStream stdout, final PrintStream stderr) throws Throwable { - client.setFilePasswordProvider(new FilePasswordProvider() { - @Override - public String getPassword(String file) throws IOException { - stdout.print("Enter password for private key file=" + file + ": "); - return stdin.readLine(); - } + client.setFilePasswordProvider(file -> { + stdout.print("Enter password for private key file=" + file + ": "); + return stdin.readLine(); }); if (GenericUtils.isEmpty(identities)) { @@ -1109,24 +1073,20 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa current = new DefaultKnownHostsServerKeyVerifier(current, false, Paths.get(filePath)); } - ((KnownHostsServerKeyVerifier) current).setModifiedServerKeyAcceptor(new ModifiedServerKeyAcceptor() { - @Override - public boolean acceptModifiedServerKey(ClientSession clientSession, SocketAddress remoteAddress, - KnownHostEntry entry, PublicKey expected, PublicKey actual) throws Exception { - stderr.append("Mismatched keys presented by ").append(Objects.toString(remoteAddress)) - .append(" for entry=").println(entry); - stderr.append('\t').append("Expected=").append(KeyUtils.getKeyType(expected)) - .append('-').println(KeyUtils.getFingerPrint(expected)); - stderr.append('\t').append("Actual=").append(KeyUtils.getKeyType(actual)) - .append('-').println(KeyUtils.getFingerPrint(actual)); - stderr.flush(); // just making sure - - stdout.append("Accept key and update known hosts: y/[N]"); - stdout.flush(); // just making sure - - String ans = GenericUtils.trimToEmpty(stdin.readLine()); - return (GenericUtils.length(ans) > 0) && (Character.toLowerCase(ans.charAt(0)) == 'y'); - } + ((KnownHostsServerKeyVerifier) current).setModifiedServerKeyAcceptor((clientSession, remoteAddress, entry, expected, actual) -> { + stderr.append("Mismatched keys presented by ").append(Objects.toString(remoteAddress)) + .append(" for entry=").println(entry); + stderr.append('\t').append("Expected=").append(KeyUtils.getKeyType(expected)) + .append('-').println(KeyUtils.getFingerPrint(expected)); + stderr.append('\t').append("Actual=").append(KeyUtils.getKeyType(actual)) + .append('-').println(KeyUtils.getFingerPrint(actual)); + stderr.flush(); // just making sure + + stdout.append("Accept key and update known hosts: y/[N]"); + stdout.flush(); // just making sure + + String ans = GenericUtils.trimToEmpty(stdin.readLine()); + return (GenericUtils.length(ans) > 0) && (Character.toLowerCase(ans.charAt(0)) == 'y'); }); manager.setServerKeyVerifier(current); @@ -1201,7 +1161,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa public static List<NamedFactory<Compression>> setupCompressions( String argName, String argVal, List<NamedFactory<Compression>> current, PrintStream stderr) { if (GenericUtils.size(current) > 0) { - showError(stderr, argName + " option value re-specified: " + NamedResource.Utils.getNames(current)); + showError(stderr, argName + " option value re-specified: " + NamedResource.getNames(current)); return null; } @@ -1229,7 +1189,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa public static List<NamedFactory<Mac>> setupMacs(String argName, String argVal, List<NamedFactory<Mac>> current, PrintStream stderr) { if (GenericUtils.size(current) > 0) { - showError(stderr, argName + " option value re-specified: " + NamedResource.Utils.getNames(current)); + showError(stderr, argName + " option value re-specified: " + NamedResource.getNames(current)); return null; } @@ -1258,7 +1218,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa // returns null - e.g., re-specified or no supported cipher found public static List<NamedFactory<Cipher>> setupCiphers(String argName, String argVal, List<NamedFactory<Cipher>> current, PrintStream stderr) { if (GenericUtils.size(current) > 0) { - showError(stderr, argName + " option value re-specified: " + NamedResource.Utils.getNames(current)); + showError(stderr, argName + " option value re-specified: " + NamedResource.getNames(current)); return null; } @@ -1437,12 +1397,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa ((ChannelShell) channel).setAgentForwarding(agentForward); channel.setIn(new NoCloseInputStream(System.in)); } else { - StringBuilder w = new StringBuilder(command.size() * Integer.SIZE); - for (String cmd : command) { - w.append(cmd).append(' '); - } - - channel = session.createExecChannel(w.toString().trim()); + channel = session.createExecChannel(String.join(" ", command).trim()); } try (OutputStream channelOut = new NoCloseOutputStream(System.out); @@ -1463,7 +1418,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa session.close(); } } finally { - if ((logStream != stdout) && (logStream != stderr)) { + if (logStream != null && logStream != stdout && logStream != stderr) { logStream.close(); } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java b/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java index 7bf4ff6..bb7ff42 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/SshKeyScan.java @@ -308,9 +308,7 @@ public class SshKeyScan extends AbstractSimplifiedLog log(Level.FINER, "Authenticating with key type=" + kt + " to " + remoteLocation); } - for (KeyPair kp : ids) { - session.addPublicKeyIdentity(kp); - } + GenericUtils.forEach(ids, session::addPublicKeyIdentity); try { // shouldn't really succeed, but do it since key exchange occurs only on auth attempt @@ -321,9 +319,7 @@ public class SshKeyScan extends AbstractSimplifiedLog log(Level.FINER, "Failed to authenticate using key type=" + kt + " with " + remoteLocation); } } finally { - for (KeyPair kp : ids) { - session.removePublicKeyIdentity(kp); - } + GenericUtils.forEach(ids, session::removePublicKeyIdentity); } } finally { session.removeSessionListener(this); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/auth/AuthenticationIdentitiesProvider.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/AuthenticationIdentitiesProvider.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/AuthenticationIdentitiesProvider.java index 7afd266..edba4e3 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/auth/AuthenticationIdentitiesProvider.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/AuthenticationIdentitiesProvider.java @@ -36,6 +36,33 @@ import org.apache.sshd.common.util.GenericUtils; * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ public interface AuthenticationIdentitiesProvider extends KeyIdentityProvider, PasswordIdentityProvider { + + /** + * Compares 2 password identities - returns zero ONLY if <U>both</U> compared + * objects are {@link String}s and equal to each other + */ + Comparator<Object> PASSWORD_IDENTITY_COMPARATOR = (o1, o2) -> { + if (!(o1 instanceof String) || !(o2 instanceof String)) { + return -1; + } else { + return ((String) o1).compareTo((String) o2); + } + }; + + /** + * Compares 2 {@link KeyPair} identities - returns zero ONLY if <U>both</U> compared + * objects are {@link KeyPair}s and equal to each other + */ + Comparator<Object> KEYPAIR_IDENTITY_COMPARATOR = (o1, o2) -> { + if ((!(o1 instanceof KeyPair)) || (!(o2 instanceof KeyPair))) { + return -1; + } else if (KeyUtils.compareKeyPairs((KeyPair) o1, (KeyPair) o2)) { + return 0; + } else { + return 1; + } + }; + /** * @return All the currently available identities - passwords, keys, etc... */ @@ -46,97 +73,79 @@ public interface AuthenticationIdentitiesProvider extends KeyIdentityProvider, P * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ // CHECKSTYLE:OFF + @Deprecated final class Utils { // CHECKSTYLE:ON - /** - * Compares 2 password identities - returns zero ONLY if <U>both</U> compared - * objects are {@link String}s and equal to each other - */ - public static final Comparator<Object> PASSWORD_IDENTITY_COMPARATOR = new Comparator<Object>() { - @Override - public int compare(Object o1, Object o2) { - if (!(o1 instanceof String) || !(o2 instanceof String)) { - return -1; - } else { - return ((String) o1).compareTo((String) o2); - } - } - }; - /** - * Compares 2 {@link KeyPair} identities - returns zero ONLY if <U>both</U> compared - * objects are {@link KeyPair}s and equal to each other - */ - public static final Comparator<Object> KEYPAIR_IDENTITY_COMPARATOR = new Comparator<Object>() { - @Override - public int compare(Object o1, Object o2) { - if ((!(o1 instanceof KeyPair)) || (!(o2 instanceof KeyPair))) { - return -1; - } else if (KeyUtils.compareKeyPairs((KeyPair) o1, (KeyPair) o2)) { - return 0; - } else { - return 1; - } - } - }; + public static final Comparator<Object> PASSWORD_IDENTITY_COMPARATOR = AuthenticationIdentitiesProvider.PASSWORD_IDENTITY_COMPARATOR; + + public static final Comparator<Object> KEYPAIR_IDENTITY_COMPARATOR = AuthenticationIdentitiesProvider.KEYPAIR_IDENTITY_COMPARATOR; private Utils() { throw new UnsupportedOperationException("No instance allowed"); } public static int findIdentityIndex(List<?> identities, Comparator<? super Object> comp, Object target) { - for (int index = 0; index < identities.size(); index++) { - Object value = identities.get(index); - if (comp.compare(value, target) == 0) { - return index; - } - } - - return -1; + return AuthenticationIdentitiesProvider.findIdentityIndex(identities, comp, target); } - /** - * @param identities The {@link Iterable} identities - OK if {@code null}/empty - * @return An {@link AuthenticationIdentitiesProvider} wrapping the identities - */ public static AuthenticationIdentitiesProvider wrap(final Iterable<?> identities) { - return new AuthenticationIdentitiesProvider() { - @Override - public Iterable<KeyPair> loadKeys() { - return selectIdentities(KeyPair.class); - } + return AuthenticationIdentitiesProvider.wrap(identities); + } + } - @Override - public Iterable<String> loadPasswords() { - return selectIdentities(String.class); - } + static int findIdentityIndex(List<?> identities, Comparator<? super Object> comp, Object target) { + for (int index = 0; index < identities.size(); index++) { + Object value = identities.get(index); + if (comp.compare(value, target) == 0) { + return index; + } + } - @Override - public Iterable<?> loadIdentities() { - return selectIdentities(Object.class); - } + return -1; + } + + /** + * @param identities The {@link Iterable} identities - OK if {@code null}/empty + * @return An {@link AuthenticationIdentitiesProvider} wrapping the identities + */ + static AuthenticationIdentitiesProvider wrap(final Iterable<?> identities) { + return new AuthenticationIdentitiesProvider() { + @Override + public Iterable<KeyPair> loadKeys() { + return selectIdentities(KeyPair.class); + } - // NOTE: returns a NEW Collection on every call so that the original - // identities remain unchanged - private <T> Collection<T> selectIdentities(Class<T> type) { - Collection<T> matches = null; - for (Iterator<?> iter = GenericUtils.iteratorOf(identities); iter.hasNext();) { - Object o = iter.next(); - Class<?> t = o.getClass(); - if (!type.isAssignableFrom(t)) { - continue; - } - - if (matches == null) { - matches = new LinkedList<>(); - } - - matches.add(type.cast(o)); + @Override + public Iterable<String> loadPasswords() { + return selectIdentities(String.class); + } + + @Override + public Iterable<?> loadIdentities() { + return selectIdentities(Object.class); + } + + // NOTE: returns a NEW Collection on every call so that the original + // identities remain unchanged + private <T> Collection<T> selectIdentities(Class<T> type) { + Collection<T> matches = null; + for (Iterator<?> iter = GenericUtils.iteratorOf(identities); iter.hasNext();) { + Object o = iter.next(); + Class<?> t = o.getClass(); + if (!type.isAssignableFrom(t)) { + continue; } - return (matches == null) ? Collections.<T>emptyList() : matches; + if (matches == null) { + matches = new LinkedList<>(); + } + + matches.add(type.cast(o)); } - }; - } + + return (matches == null) ? Collections.<T>emptyList() : matches; + } + }; } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/auth/BuiltinUserAuthFactories.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/BuiltinUserAuthFactories.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/BuiltinUserAuthFactories.java index ba40caa..670efcc 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/auth/BuiltinUserAuthFactories.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/BuiltinUserAuthFactories.java @@ -72,7 +72,7 @@ public enum BuiltinUserAuthFactories implements NamedFactory<UserAuthFactory> { * @return The matching factory instance - {@code null} if no match found */ public static UserAuthFactory fromFactoryName(String name) { - Factory<UserAuthFactory> factory = NamedResource.Utils.findByName(name, String.CASE_INSENSITIVE_ORDER, VALUES); + Factory<UserAuthFactory> factory = NamedResource.findByName(name, String.CASE_INSENSITIVE_ORDER, VALUES); if (factory == null) { return null; } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/HostKeyIdentityProvider.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/HostKeyIdentityProvider.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/HostKeyIdentityProvider.java index 5362d8f..0e37e57 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/HostKeyIdentityProvider.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/HostKeyIdentityProvider.java @@ -21,7 +21,6 @@ package org.apache.sshd.client.auth.hostbased; import java.security.KeyPair; import java.security.cert.X509Certificate; -import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -43,6 +42,7 @@ public interface HostKeyIdentityProvider { * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ // CHECKSTYLE:OFF + @Deprecated final class Utils { // CHECKSTYLE:ON private Utils() { @@ -50,44 +50,30 @@ public interface HostKeyIdentityProvider { } public static Iterator<Pair<KeyPair, List<X509Certificate>>> iteratorOf(HostKeyIdentityProvider provider) { - return GenericUtils.iteratorOf((provider == null) ? null : provider.loadHostKeys()); + return HostKeyIdentityProvider.iteratorOf(provider); } public static HostKeyIdentityProvider wrap(KeyPair ... pairs) { - return wrap(GenericUtils.isEmpty(pairs) ? Collections.emptyList() : Arrays.asList(pairs)); + return HostKeyIdentityProvider.wrap(pairs); } public static HostKeyIdentityProvider wrap(final Iterable<? extends KeyPair> pairs) { - return new HostKeyIdentityProvider() { - @Override - public Iterable<Pair<KeyPair, List<X509Certificate>>> loadHostKeys() { - return new Iterable<Pair<KeyPair, List<X509Certificate>>>() { - @Override - public Iterator<Pair<KeyPair, List<X509Certificate>>> iterator() { - final Iterator<? extends KeyPair> iter = GenericUtils.iteratorOf(pairs); - return new Iterator<Pair<KeyPair, List<X509Certificate>>>() { - - @Override - public boolean hasNext() { - return iter.hasNext(); - } + return HostKeyIdentityProvider.wrap(pairs); + } + } - @Override - public Pair<KeyPair, List<X509Certificate>> next() { - KeyPair kp = iter.next(); - return new Pair<>(kp, Collections.emptyList()); - } + static Iterator<Pair<KeyPair, List<X509Certificate>>> iteratorOf(HostKeyIdentityProvider provider) { + return GenericUtils.iteratorOf((provider == null) ? null : provider.loadHostKeys()); + } - @Override - public void remove() { - throw new UnsupportedOperationException("No removal allowed"); - } - }; - } - }; - } + static HostKeyIdentityProvider wrap(KeyPair ... pairs) { + return wrap(GenericUtils.asList(pairs)); + } - }; - } + static HostKeyIdentityProvider wrap(final Iterable<? extends KeyPair> pairs) { + return () -> () -> { + final Iterator<? extends KeyPair> iter = GenericUtils.iteratorOf(pairs); + return GenericUtils.wrapIterator(iter, kp -> new Pair<>(kp, Collections.<X509Certificate>emptyList())); + }; } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/UserAuthHostBased.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/UserAuthHostBased.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/UserAuthHostBased.java index 3d8c832..398cc4c 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/UserAuthHostBased.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/hostbased/UserAuthHostBased.java @@ -62,7 +62,7 @@ public class UserAuthHostBased extends AbstractUserAuth implements SignatureFact @Override public void init(ClientSession session, String service) throws Exception { super.init(session, service); - keys = HostKeyIdentityProvider.Utils.iteratorOf(clientHostKeys); // in case multiple calls to the method + keys = HostKeyIdentityProvider.iteratorOf(clientHostKeys); // in case multiple calls to the method } @Override @@ -117,7 +117,7 @@ public class UserAuthHostBased extends AbstractUserAuth implements SignatureFact "No signature factories for session=%s", session); Signature verifier = ValidateUtils.checkNotNull( - NamedFactory.Utils.create(factories, keyType), + NamedFactory.create(factories, keyType), "No signer could be located for key type=%s", keyType); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/auth/keyboard/UserAuthKeyboardInteractive.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/keyboard/UserAuthKeyboardInteractive.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/keyboard/UserAuthKeyboardInteractive.java index f1fc526..0add7ed 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/auth/keyboard/UserAuthKeyboardInteractive.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/keyboard/UserAuthKeyboardInteractive.java @@ -88,7 +88,7 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth { @Override public void init(ClientSession session, String service) throws Exception { super.init(session, service); - passwords = PasswordIdentityProvider.Utils.iteratorOf(session); + passwords = PasswordIdentityProvider.iteratorOf(session); maxTrials = PropertyResolverUtils.getIntProperty(session, ClientAuthenticationManager.PASSWORD_PROMPTS, ClientAuthenticationManager.DEFAULT_PASSWORD_PROMPTS); ValidateUtils.checkTrue(maxTrials > 0, "Non-positive max. trials: %d", maxTrials); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/auth/password/PasswordIdentityProvider.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/password/PasswordIdentityProvider.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/password/PasswordIdentityProvider.java index fe37f39..268779c 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/auth/password/PasswordIdentityProvider.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/password/PasswordIdentityProvider.java @@ -19,12 +19,9 @@ package org.apache.sshd.client.auth.password; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Iterator; -import java.util.function.Supplier; import org.apache.sshd.client.session.ClientSession; import org.apache.sshd.common.util.GenericUtils; @@ -34,6 +31,7 @@ import org.apache.sshd.common.util.Transformer; * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ public interface PasswordIdentityProvider { + /** * An "empty" implementation of {@link PasswordIdentityProvider} that returns * and empty group of passwords @@ -51,6 +49,13 @@ public interface PasswordIdentityProvider { }; /** + * Invokes {@link PasswordIdentityProvider#loadPasswords()} and returns the result. + * Ignores {@code null} providers (i.e., returns an empty iterable instance) + */ + Transformer<PasswordIdentityProvider, Iterable<String>> LOADER = p -> + (p == null) ? Collections.emptyList() : p.loadPasswords(); + + /** * @return The currently available passwords - ignored if {@code null} */ Iterable<String> loadPasswords(); @@ -60,166 +65,168 @@ public interface PasswordIdentityProvider { * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ // CHECKSTYLE:OFF + @Deprecated final class Utils { // CHECKSTYLE:ON - /** - * Invokes {@link PasswordIdentityProvider#loadPasswords()} and returns the result. - * Ignores {@code null} providers (i.e., returns an empty iterable instance) - */ + public static final Transformer<PasswordIdentityProvider, Iterable<String>> LOADER = - new Transformer<PasswordIdentityProvider, Iterable<String>>() { - @Override - public Iterable<String> transform(PasswordIdentityProvider p) { - return (p == null) ? Collections.emptyList() : p.loadPasswords(); - } - }; + PasswordIdentityProvider.LOADER; private Utils() { throw new UnsupportedOperationException("No instance allowed"); } - /** - * Creates a "unified" {@link Iterator} of passwords out of the registered - * passwords and the extra available ones as a single iterator of passwords - * - * @param session The {@link ClientSession} - ignored if {@code null} (i.e., empty - * iterator returned) - * @return The wrapping iterator - * @see ClientSession#getRegisteredIdentities() - * @see ClientSession#getPasswordIdentityProvider() - */ public static Iterator<String> iteratorOf(ClientSession session) { - return (session == null) ? Collections.emptyIterator() : iteratorOf(session.getRegisteredIdentities(), session.getPasswordIdentityProvider()); + return PasswordIdentityProvider.iteratorOf(session); } - /** - * Creates a "unified" {@link Iterator} of passwords out of 2 possible - * {@link PasswordIdentityProvider} - * - * @param identities The registered passwords - * @param passwords Extra available passwords - * @return The wrapping iterator - * @see #resolvePasswordIdentityProvider(PasswordIdentityProvider, PasswordIdentityProvider) - */ public static Iterator<String> iteratorOf(PasswordIdentityProvider identities, PasswordIdentityProvider passwords) { - return iteratorOf(resolvePasswordIdentityProvider(identities, passwords)); + return PasswordIdentityProvider.iteratorOf(identities, passwords); } - /** - * Resolves a non-{@code null} iterator of the available passwords - * - * @param provider The {@link PasswordIdentityProvider} - ignored if {@code null} (i.e., - * return an empty iterator) - * @return A non-{@code null} iterator - which may be empty if no provider or no passwords - */ public static Iterator<String> iteratorOf(PasswordIdentityProvider provider) { - return GenericUtils.iteratorOf((provider == null) ? null : provider.loadPasswords()); + return PasswordIdentityProvider.iteratorOf(provider); } - /** - * <P>Creates a "unified" {@link PasswordIdentityProvider} out of 2 possible ones - * as follows:</P></BR> - * <UL> - * <LI>If both are {@code null} then return {@code null}.</LI> - * <LI>If either one is {@code null} then use the non-{@code null} one.</LI> - * <LI>If both are the same instance then use it.</U> - * <LI>Otherwise, returns a wrapper that groups both providers.</LI> - * </UL> - * @param identities The registered passwords - * @param passwords The extra available passwords - * @return The resolved provider - * @see #multiProvider(PasswordIdentityProvider...) - */ public static PasswordIdentityProvider resolvePasswordIdentityProvider(PasswordIdentityProvider identities, PasswordIdentityProvider passwords) { - if ((passwords == null) || (identities == passwords)) { - return identities; - } else if (identities == null) { - return passwords; - } else { - return multiProvider(identities, passwords); - } + return PasswordIdentityProvider.resolvePasswordIdentityProvider(identities, passwords); } - /** - * Wraps a group of {@link PasswordIdentityProvider} into a single one - * - * @param providers The providers - ignored if {@code null}/empty (i.e., returns - * {@link #EMPTY_PASSWORDS_PROVIDER} - * @return The wrapping provider - * @see #multiProvider(Collection) - */ public static PasswordIdentityProvider multiProvider(PasswordIdentityProvider ... providers) { - return GenericUtils.isEmpty(providers) ? EMPTY_PASSWORDS_PROVIDER : multiProvider(Arrays.asList(providers)); + return PasswordIdentityProvider.multiProvider(providers); } - /** - * Wraps a group of {@link PasswordIdentityProvider} into a single one - * - * @param providers The providers - ignored if {@code null}/empty (i.e., returns - * {@link #EMPTY_PASSWORDS_PROVIDER} - * @return The wrapping provider - */ public static PasswordIdentityProvider multiProvider(Collection<? extends PasswordIdentityProvider> providers) { - return GenericUtils.isEmpty(providers) ? EMPTY_PASSWORDS_PROVIDER : wrap(iterableOf(providers)); + return PasswordIdentityProvider.multiProvider(providers); } - /** - * Wraps a group of {@link PasswordIdentityProvider} into an {@link Iterable} of their combined passwords - * - * @param providers The providers - ignored if {@code null}/empty (i.e., returns an empty iterable instance) - * @return The wrapping iterable - */ public static Iterable<String> iterableOf(Collection<? extends PasswordIdentityProvider> providers) { - if (GenericUtils.isEmpty(providers)) { - return Collections.emptyList(); - } - - Collection<Supplier<Iterable<String>>> suppliers = new ArrayList<>(providers.size()); - for (final PasswordIdentityProvider p : providers) { - if (p == null) { - continue; - } - - suppliers.add(new Supplier<Iterable<String>>() { - @Override - public Iterable<String> get() { - return p.loadPasswords(); - } - }); - } - - if (GenericUtils.isEmpty(suppliers)) { - return Collections.emptyList(); - } - - return GenericUtils.multiIterableSuppliers(suppliers); + return PasswordIdentityProvider.iterableOf(providers); } - /** - * Wraps a group of passwords into a {@link PasswordIdentityProvider} - * - * @param passwords The passwords - ignored if {@code null}/empty - * (i.e., returns {@link #EMPTY_PASSWORDS_PROVIDER}) - * @return The provider wrapper - */ public static PasswordIdentityProvider wrap(String ... passwords) { - return GenericUtils.isEmpty(passwords) ? EMPTY_PASSWORDS_PROVIDER : wrap(Arrays.asList(passwords)); + return PasswordIdentityProvider.wrap(passwords); } - /** - * Wraps a group of passwords into a {@link PasswordIdentityProvider} - * - * @param passwords The passwords {@link Iterable} - ignored if {@code null} - * (i.e., returns {@link #EMPTY_PASSWORDS_PROVIDER}) - * @return The provider wrapper - */ public static PasswordIdentityProvider wrap(final Iterable<String> passwords) { - return (passwords == null) ? EMPTY_PASSWORDS_PROVIDER : new PasswordIdentityProvider() { - @Override - public Iterable<String> loadPasswords() { - return passwords; - } - }; + return PasswordIdentityProvider.wrap(passwords); + } + } + + /** + * Creates a "unified" {@link Iterator} of passwords out of the registered + * passwords and the extra available ones as a single iterator of passwords + * + * @param session The {@link ClientSession} - ignored if {@code null} (i.e., empty + * iterator returned) + * @return The wrapping iterator + * @see ClientSession#getRegisteredIdentities() + * @see ClientSession#getPasswordIdentityProvider() + */ + static Iterator<String> iteratorOf(ClientSession session) { + return (session == null) ? Collections.<String>emptyIterator() : iteratorOf(session.getRegisteredIdentities(), session.getPasswordIdentityProvider()); + } + + /** + * Creates a "unified" {@link Iterator} of passwords out of 2 possible + * {@link PasswordIdentityProvider} + * + * @param identities The registered passwords + * @param passwords Extra available passwords + * @return The wrapping iterator + * @see #resolvePasswordIdentityProvider(PasswordIdentityProvider, PasswordIdentityProvider) + */ + static Iterator<String> iteratorOf(PasswordIdentityProvider identities, PasswordIdentityProvider passwords) { + return iteratorOf(resolvePasswordIdentityProvider(identities, passwords)); + } + + /** + * Resolves a non-{@code null} iterator of the available passwords + * + * @param provider The {@link PasswordIdentityProvider} - ignored if {@code null} (i.e., + * return an empty iterator) + * @return A non-{@code null} iterator - which may be empty if no provider or no passwords + */ + static Iterator<String> iteratorOf(PasswordIdentityProvider provider) { + return GenericUtils.iteratorOf((provider == null) ? null : provider.loadPasswords()); + } + + /** + * <P>Creates a "unified" {@link PasswordIdentityProvider} out of 2 possible ones + * as follows:</P></BR> + * <UL> + * <LI>If both are {@code null} then return {@code null}.</LI> + * <LI>If either one is {@code null} then use the non-{@code null} one.</LI> + * <LI>If both are the same instance then use it.</U> + * <LI>Otherwise, returns a wrapper that groups both providers.</LI> + * </UL> + * @param identities The registered passwords + * @param passwords The extra available passwords + * @return The resolved provider + * @see #multiProvider(PasswordIdentityProvider...) + */ + static PasswordIdentityProvider resolvePasswordIdentityProvider(PasswordIdentityProvider identities, PasswordIdentityProvider passwords) { + if ((passwords == null) || (identities == passwords)) { + return identities; + } else if (identities == null) { + return passwords; + } else { + return multiProvider(identities, passwords); } } + + /** + * Wraps a group of {@link PasswordIdentityProvider} into a single one + * + * @param providers The providers - ignored if {@code null}/empty (i.e., returns + * {@link #EMPTY_PASSWORDS_PROVIDER} + * @return The wrapping provider + * @see #multiProvider(Collection) + */ + static PasswordIdentityProvider multiProvider(PasswordIdentityProvider ... providers) { + return multiProvider(GenericUtils.asList(providers)); + } + + /** + * Wraps a group of {@link PasswordIdentityProvider} into a single one + * + * @param providers The providers - ignored if {@code null}/empty (i.e., returns + * {@link #EMPTY_PASSWORDS_PROVIDER} + * @return The wrapping provider + */ + static PasswordIdentityProvider multiProvider(Collection<? extends PasswordIdentityProvider> providers) { + return GenericUtils.isEmpty(providers) ? EMPTY_PASSWORDS_PROVIDER : wrap(iterableOf(providers)); + } + + /** + * Wraps a group of {@link PasswordIdentityProvider} into an {@link Iterable} of their combined passwords + * + * @param providers The providers - ignored if {@code null}/empty (i.e., returns an empty iterable instance) + * @return The wrapping iterable + */ + static Iterable<String> iterableOf(Collection<? extends PasswordIdentityProvider> providers) { + return GenericUtils.multiIterableSuppliers(GenericUtils.wrapIterable(providers, p -> p::loadPasswords)); + } + + /** + * Wraps a group of passwords into a {@link PasswordIdentityProvider} + * + * @param passwords The passwords - ignored if {@code null}/empty + * (i.e., returns {@link #EMPTY_PASSWORDS_PROVIDER}) + * @return The provider wrapper + */ + static PasswordIdentityProvider wrap(String ... passwords) { + return wrap(GenericUtils.asList(passwords)); + } + + /** + * Wraps a group of passwords into a {@link PasswordIdentityProvider} + * + * @param passwords The passwords {@link Iterable} - ignored if {@code null} + * (i.e., returns {@link #EMPTY_PASSWORDS_PROVIDER}) + * @return The provider wrapper + */ + static PasswordIdentityProvider wrap(final Iterable<String> passwords) { + return (passwords == null) ? EMPTY_PASSWORDS_PROVIDER : () -> passwords; + } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/auth/password/UserAuthPassword.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/password/UserAuthPassword.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/password/UserAuthPassword.java index 37f3ba5..ad19637 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/auth/password/UserAuthPassword.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/password/UserAuthPassword.java @@ -48,7 +48,7 @@ public class UserAuthPassword extends AbstractUserAuth { @Override public void init(ClientSession session, String service) throws Exception { super.init(session, service); - passwords = PasswordIdentityProvider.Utils.iteratorOf(session); + passwords = PasswordIdentityProvider.iteratorOf(session); } @Override http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/KeyPairIdentity.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/KeyPairIdentity.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/KeyPairIdentity.java index 02400cd..2b5531c 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/KeyPairIdentity.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/KeyPairIdentity.java @@ -54,7 +54,7 @@ public class KeyPairIdentity implements PublicKeyIdentity { public byte[] sign(byte[] data) throws Exception { String keyType = KeyUtils.getKeyType(getPublicKey()); Signature verifier = ValidateUtils.checkNotNull( - NamedFactory.Utils.create(signatureFactories, keyType), + NamedFactory.create(signatureFactories, keyType), "No signer could be located for key type=%s", keyType); verifier.initSigner(pair.getPrivate()); @@ -67,7 +67,7 @@ public class KeyPairIdentity implements PublicKeyIdentity { PublicKey pubKey = getPublicKey(); return getClass().getSimpleName() + " type=" + KeyUtils.getKeyType(pubKey) - + ", factories=" + NamedResource.Utils.getNames(signatureFactories) + + ", factories=" + NamedResource.getNames(signatureFactories) + ", fingerprint=" + KeyUtils.getFingerPrint(pubKey); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/977b7b52/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKeyIterator.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKeyIterator.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKeyIterator.java index b8d934b..a75e13a 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKeyIterator.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/pubkey/UserAuthPublicKeyIterator.java @@ -50,7 +50,7 @@ public class UserAuthPublicKeyIterator extends AbstractKeyPairIterator<PublicKey super(session); Collection<Iterator<? extends PublicKeyIdentity>> identities = new LinkedList<>(); - identities.add(new SessionKeyPairIterator(session, signatureFactories, KeyIdentityProvider.Utils.iteratorOf(session))); + identities.add(new SessionKeyPairIterator(session, signatureFactories, KeyIdentityProvider.iteratorOf(session))); FactoryManager manager = ValidateUtils.checkNotNull(session.getFactoryManager(), "No session factory manager"); SshAgentFactory factory = manager.getAgentFactory();
