This is an automated email from the ASF dual-hosted git repository.

lgoldstein pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git

commit 0414ad7188159b3c9ab6817df7f1237811804ef4
Author: Lyor Goldstein <lgoldst...@apache.org>
AuthorDate: Fri Feb 19 10:46:51 2021 +0200

    [SSHD-1127] Consolidated SftpSubsystem support implenentations into 
SftpSubsystemConfigurator
---
 CHANGES.md                                             |  1 +
 docs/sftp.md                                           |  3 +++
 .../sshd/sftp/server/AbstractSftpSubsystemHelper.java  | 13 +++++++------
 ...er.java => SftpErrorStatusDataHandlerProvider.java} | 10 +++++++---
 .../sftp/server/SftpFileSystemAccessorManager.java     |  4 +---
 ...anager.java => SftpFileSystemAccessorProvider.java} |  8 +++++---
 .../org/apache/sshd/sftp/server/SftpSubsystem.java     | 18 +++++-------------
 ...systemProxy.java => SftpSubsystemConfigurator.java} |  8 +++++---
 .../sshd/sftp/server/SftpSubsystemEnvironment.java     | 14 +++-----------
 .../apache/sshd/sftp/server/SftpSubsystemFactory.java  | 15 ++++++++++-----
 .../apache/sshd/sftp/server/SftpSubsystemProxy.java    |  2 +-
 ...ava => SftpUnsupportedAttributePolicyProvider.java} | 10 ++++++----
 .../org/apache/sshd/sftp/client/SftpVersionsTest.java  |  8 ++------
 .../helpers/SpaceAvailableExtensionImplTest.java       |  4 +---
 .../openssh/helpers/OpenSSHExtensionsTest.java         |  4 +---
 15 files changed, 58 insertions(+), 64 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 9a5bb8b..5f33b19 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -37,3 +37,4 @@
 * [SSHD-1114](https://issues.apache.org/jira/browse/SSHD-1114) Added callbacks 
for client-side host-based authentication progress
 * [SSHD-1114](https://issues.apache.org/jira/browse/SSHD-1114) Added 
capability for interactive password authentication participation via 
UserInteraction
 * [SSHD-1114](https://issues.apache.org/jira/browse/SSHD-1114) Added 
capability for interactive key based authentication participation via 
UserInteraction
+* [SSHD-1127](https://issues.apache.org/jira/browse/SSHD-1127) Consolidated 
`SftpSubsystem` support implementations into `SftpSubsystemConfigurator`
\ No newline at end of file
diff --git a/docs/sftp.md b/docs/sftp.md
index 647792a..1afa4c2 100644
--- a/docs/sftp.md
+++ b/docs/sftp.md
@@ -123,6 +123,9 @@ reasonable buffer size by setting the 
`channel-session-max-extdata-bufsize` prop
 extended data handler is registered it will be buffered (up to the specified 
max. size). **Note:** if a buffer size is configured
 but no extended data handler is registered when channel is spawning the 
command then an exception will occur.
 
+The same applies with any error I/O streams provided to the `SftpSubsystem` - 
if the handler implements the relevant `Aware` interface
+then it will be provided with the relevant stream.
+
 ### Symbolic links handling
 
 Whenever the server needs to execute a command that may behave differently if 
applied to a symbolic link instead of its target
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
index 83d160e..4a5288b 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/AbstractSftpSubsystemHelper.java
@@ -147,13 +147,13 @@ public abstract class AbstractSftpSubsystemHelper
     private final SftpFileSystemAccessor fileSystemAccessor;
     private final SftpErrorStatusDataHandler errorStatusDataHandler;
 
-    protected AbstractSftpSubsystemHelper(
-                                          UnsupportedAttributePolicy policy, 
SftpFileSystemAccessor accessor,
-                                          SftpErrorStatusDataHandler handler) {
-        unsupportedAttributePolicy = Objects.requireNonNull(policy, "No 
unsupported attribute policy provided");
-        fileSystemAccessor = Objects.requireNonNull(accessor, "No file system 
accessor");
+    protected AbstractSftpSubsystemHelper(SftpSubsystemConfigurator 
configurator) {
+        unsupportedAttributePolicy = 
Objects.requireNonNull(configurator.getUnsupportedAttributePolicy(),
+                "No unsupported attribute policy provided");
+        fileSystemAccessor = 
Objects.requireNonNull(configurator.getFileSystemAccessor(), "No file system 
accessor");
         sftpEventListenerProxy = 
EventListenerUtils.proxyWrapper(SftpEventListener.class, sftpEventListeners);
-        errorStatusDataHandler = Objects.requireNonNull(handler, "No error 
status data handler");
+        errorStatusDataHandler
+                = 
Objects.requireNonNull(configurator.getErrorStatusDataHandler(), "No error 
status data handler");
     }
 
     @Override
@@ -185,6 +185,7 @@ public abstract class AbstractSftpSubsystemHelper
         return 
sftpEventListeners.remove(SftpEventListener.validateListener(listener));
     }
 
+    @Override
     public SftpErrorStatusDataHandler getErrorStatusDataHandler() {
         return errorStatusDataHandler;
     }
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpErrorStatusDataHandlerProvider.java
similarity index 75%
copy from 
sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
copy to 
sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpErrorStatusDataHandlerProvider.java
index 466ddfa..2e66844 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpErrorStatusDataHandlerProvider.java
@@ -22,8 +22,12 @@ package org.apache.sshd.sftp.server;
 /**
  * @author <a href="mailto:d...@mina.apache.org";>Apache MINA SSHD Project</a>
  */
-public interface SftpFileSystemAccessorManager {
-    SftpFileSystemAccessor getFileSystemAccessor();
+@FunctionalInterface
+public interface SftpErrorStatusDataHandlerProvider {
 
-    void setFileSystemAccessor(SftpFileSystemAccessor accessor);
+    /**
+     * @return The (never {@code null}) {@link SftpErrorStatusDataHandler} to 
use when generating failed commands error
+     *         messages
+     */
+    SftpErrorStatusDataHandler getErrorStatusDataHandler();
 }
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
index 466ddfa..c5005ef 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
@@ -22,8 +22,6 @@ package org.apache.sshd.sftp.server;
 /**
  * @author <a href="mailto:d...@mina.apache.org";>Apache MINA SSHD Project</a>
  */
-public interface SftpFileSystemAccessorManager {
-    SftpFileSystemAccessor getFileSystemAccessor();
-
+public interface SftpFileSystemAccessorManager extends 
SftpFileSystemAccessorProvider {
     void setFileSystemAccessor(SftpFileSystemAccessor accessor);
 }
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorProvider.java
similarity index 84%
copy from 
sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
copy to 
sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorProvider.java
index 466ddfa..b75148c 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorManager.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpFileSystemAccessorProvider.java
@@ -22,8 +22,10 @@ package org.apache.sshd.sftp.server;
 /**
  * @author <a href="mailto:d...@mina.apache.org";>Apache MINA SSHD Project</a>
  */
-public interface SftpFileSystemAccessorManager {
+@FunctionalInterface
+public interface SftpFileSystemAccessorProvider {
+    /**
+     * @return The {@link SftpFileSystemAccessor} to use for accessing files 
and directories
+     */
     SftpFileSystemAccessor getFileSystemAccessor();
-
-    void setFileSystemAccessor(SftpFileSystemAccessor accessor);
 }
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystem.java 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystem.java
index 8c301de..850f8e3 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystem.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystem.java
@@ -115,21 +115,12 @@ public class SftpSubsystem
     protected CloseableExecutorService executorService;
 
     /**
-     * @param executorService        The {@link CloseableExecutorService} to 
be used by the {@link SftpSubsystem}
-     *                               command when starting execution. If 
{@code null} then a single-threaded ad-hoc
-     *                               service is used.
-     * @param policy                 The {@link UnsupportedAttributePolicy} to 
use if failed to access some local file
-     *                               attributes
-     * @param accessor               The {@link SftpFileSystemAccessor} to use 
for opening files and directories
-     * @param errorStatusDataHandler The (never {@code null}) {@link 
SftpErrorStatusDataHandler} to use when generating
-     *                               failed commands error messages
-     * @see                          
ThreadUtils#newSingleThreadExecutor(String)
+     * @param configurator The {@link SftpSubsystemConfigurator} to use
      */
-    public SftpSubsystem(
-                         CloseableExecutorService executorService, 
UnsupportedAttributePolicy policy,
-                         SftpFileSystemAccessor accessor, 
SftpErrorStatusDataHandler errorStatusDataHandler) {
-        super(policy, accessor, errorStatusDataHandler);
+    public SftpSubsystem(SftpSubsystemConfigurator configurator) {
+        super(configurator);
 
+        CloseableExecutorService executorService = 
configurator.getExecutorService();
         if (executorService == null) {
             this.executorService = 
ThreadUtils.newSingleThreadExecutor(getClass().getSimpleName());
         } else {
@@ -186,6 +177,7 @@ public class SftpSubsystem
 
     @Override
     public void setFileSystem(FileSystem fileSystem) {
+        // reference check on purpose
         if (fileSystem != this.fileSystem) {
             this.fileSystem = fileSystem;
             this.defaultDir = 
fileSystem.getPath("").toAbsolutePath().normalize();
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemConfigurator.java
similarity index 77%
copy from 
sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java
copy to 
sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemConfigurator.java
index 5f4136b..63dbe9c 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemConfigurator.java
@@ -19,11 +19,13 @@
 
 package org.apache.sshd.sftp.server;
 
+import org.apache.sshd.common.util.threads.ExecutorServiceCarrier;
+
 /**
  * @author <a href="mailto:d...@mina.apache.org";>Apache MINA SSHD Project</a>
  */
-public interface SftpSubsystemProxy
-        extends SftpSubsystemEnvironment,
-        SftpEventListenerManager {
+public interface SftpSubsystemConfigurator
+        extends ExecutorServiceCarrier, SftpFileSystemAccessorProvider,
+        SftpUnsupportedAttributePolicyProvider, 
SftpErrorStatusDataHandlerProvider {
     // Nothing extra
 }
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemEnvironment.java
 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemEnvironment.java
index f18496b..037f1e0 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemEnvironment.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemEnvironment.java
@@ -34,7 +34,9 @@ import org.apache.sshd.sftp.common.SftpConstants;
 /**
  * @author <a href="mailto:d...@mina.apache.org";>Apache MINA SSHD Project</a>
  */
-public interface SftpSubsystemEnvironment extends 
SessionHolder<ServerSession>, ServerSessionHolder {
+public interface SftpSubsystemEnvironment
+        extends SessionHolder<ServerSession>, ServerSessionHolder,
+        SftpFileSystemAccessorProvider, SftpUnsupportedAttributePolicyProvider 
{
 
     int LOWER_SFTP_IMPL = SftpConstants.SFTP_V3; // Working implementation 
from v3
 
@@ -58,16 +60,6 @@ public interface SftpSubsystemEnvironment extends 
SessionHolder<ServerSession>,
     int getVersion();
 
     /**
-     * @return The {@link SftpFileSystemAccessor} used to access effective 
server-side paths
-     */
-    SftpFileSystemAccessor getFileSystemAccessor();
-
-    /**
-     * @return The selected behavior in case some unsupported attributes are 
requested
-     */
-    UnsupportedAttributePolicy getUnsupportedAttributePolicy();
-
-    /**
      * @return The default root directory used to resolve relative paths - 
a.k.a. the {@code chroot} location
      */
     Path getDefaultDirectory();
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemFactory.java 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemFactory.java
index dcc2b50..e3e0e61 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemFactory.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemFactory.java
@@ -38,7 +38,8 @@ import org.apache.sshd.sftp.common.SftpConstants;
 public class SftpSubsystemFactory
         extends AbstractSftpEventListenerManager
         implements ManagedExecutorServiceSupplier, SubsystemFactory,
-        SftpEventListenerManager, SftpFileSystemAccessorManager {
+        SftpEventListenerManager, SftpFileSystemAccessorManager,
+        SftpSubsystemConfigurator {
 
     public static final String NAME = SftpConstants.SFTP_SUBSYSTEM_NAME;
     public static final UnsupportedAttributePolicy DEFAULT_POLICY = 
UnsupportedAttributePolicy.Warn;
@@ -110,6 +111,7 @@ public class SftpSubsystemFactory
         executorsProvider = provider;
     }
 
+    @Override
     public UnsupportedAttributePolicy getUnsupportedAttributePolicy() {
         return policy;
     }
@@ -132,6 +134,7 @@ public class SftpSubsystemFactory
         fileSystemAccessor = Objects.requireNonNull(accessor, "No accessor");
     }
 
+    @Override
     public SftpErrorStatusDataHandler getErrorStatusDataHandler() {
         return errorStatusDataHandler;
     }
@@ -141,11 +144,13 @@ public class SftpSubsystemFactory
     }
 
     @Override
+    public CloseableExecutorService getExecutorService() {
+        return resolveExecutorService();
+    }
+
+    @Override
     public Command createSubsystem(ChannelSession channel) throws IOException {
-        SftpSubsystem subsystem = new SftpSubsystem(
-                resolveExecutorService(),
-                getUnsupportedAttributePolicy(), getFileSystemAccessor(),
-                getErrorStatusDataHandler());
+        SftpSubsystem subsystem = new SftpSubsystem(this);
         GenericUtils.forEach(getRegisteredListeners(), 
subsystem::addSftpEventListener);
         return subsystem;
     }
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java
index 5f4136b..f9bab01 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java
@@ -24,6 +24,6 @@ package org.apache.sshd.sftp.server;
  */
 public interface SftpSubsystemProxy
         extends SftpSubsystemEnvironment,
-        SftpEventListenerManager {
+        SftpEventListenerManager, SftpErrorStatusDataHandlerProvider {
     // Nothing extra
 }
diff --git 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpUnsupportedAttributePolicyProvider.java
similarity index 77%
copy from 
sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java
copy to 
sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpUnsupportedAttributePolicyProvider.java
index 5f4136b..7f6f5f1 100644
--- 
a/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpSubsystemProxy.java
+++ 
b/sshd-sftp/src/main/java/org/apache/sshd/sftp/server/SftpUnsupportedAttributePolicyProvider.java
@@ -22,8 +22,10 @@ package org.apache.sshd.sftp.server;
 /**
  * @author <a href="mailto:d...@mina.apache.org";>Apache MINA SSHD Project</a>
  */
-public interface SftpSubsystemProxy
-        extends SftpSubsystemEnvironment,
-        SftpEventListenerManager {
-    // Nothing extra
+@FunctionalInterface
+public interface SftpUnsupportedAttributePolicyProvider {
+    /**
+     * @return The {@link UnsupportedAttributePolicy} to use if failed to 
access some local file attributes
+     */
+    UnsupportedAttributePolicy getUnsupportedAttributePolicy();
 }
diff --git 
a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpVersionsTest.java 
b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpVersionsTest.java
index c23dc5c..de54873 100644
--- a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpVersionsTest.java
+++ b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/SftpVersionsTest.java
@@ -227,9 +227,7 @@ public class SftpVersionsTest extends 
AbstractSftpClientTestSupport {
         SftpSubsystemFactory factory = new SftpSubsystemFactory() {
             @Override
             public Command createSubsystem(ChannelSession channel) throws 
IOException {
-                SftpSubsystem subsystem = new SftpSubsystem(
-                        resolveExecutorService(),
-                        getUnsupportedAttributePolicy(), 
getFileSystemAccessor(), getErrorStatusDataHandler()) {
+                SftpSubsystem subsystem = new SftpSubsystem(this) {
                     @Override
                     protected NavigableMap<String, Object> 
resolveFileAttributes(Path file, int flags, LinkOption... options)
                             throws IOException {
@@ -348,9 +346,7 @@ public class SftpVersionsTest extends 
AbstractSftpClientTestSupport {
         SftpSubsystemFactory factory = new SftpSubsystemFactory() {
             @Override
             public Command createSubsystem(ChannelSession channel) throws 
IOException {
-                SftpSubsystem subsystem = new SftpSubsystem(
-                        resolveExecutorService(),
-                        getUnsupportedAttributePolicy(), 
getFileSystemAccessor(), getErrorStatusDataHandler()) {
+                SftpSubsystem subsystem = new SftpSubsystem(this) {
                     @Override
                     protected NavigableMap<String, Object> 
resolveFileAttributes(Path file, int flags, LinkOption... options)
                             throws IOException {
diff --git 
a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/extensions/helpers/SpaceAvailableExtensionImplTest.java
 
b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/extensions/helpers/SpaceAvailableExtensionImplTest.java
index 3fa070c..bca6be7 100644
--- 
a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/extensions/helpers/SpaceAvailableExtensionImplTest.java
+++ 
b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/extensions/helpers/SpaceAvailableExtensionImplTest.java
@@ -71,9 +71,7 @@ public class SpaceAvailableExtensionImplTest extends 
AbstractSftpClientTestSuppo
         sshd.setSubsystemFactories(Collections.singletonList(new 
SftpSubsystemFactory() {
             @Override
             public Command createSubsystem(ChannelSession channel) throws 
IOException {
-                return new SftpSubsystem(
-                        resolveExecutorService(),
-                        getUnsupportedAttributePolicy(), 
getFileSystemAccessor(), getErrorStatusDataHandler()) {
+                return new SftpSubsystem(this) {
                     @Override
                     protected SpaceAvailableExtensionInfo doSpaceAvailable(int 
id, String path) throws IOException {
                         if (!queryPath.equals(path)) {
diff --git 
a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/extensions/openssh/helpers/OpenSSHExtensionsTest.java
 
b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/extensions/openssh/helpers/OpenSSHExtensionsTest.java
index d0ba605..4525534 100644
--- 
a/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/extensions/openssh/helpers/OpenSSHExtensionsTest.java
+++ 
b/sshd-sftp/src/test/java/org/apache/sshd/sftp/client/extensions/openssh/helpers/OpenSSHExtensionsTest.java
@@ -120,9 +120,7 @@ public class OpenSSHExtensionsTest extends 
AbstractSftpClientTestSupport {
         sshd.setSubsystemFactories(Collections.singletonList(new 
SftpSubsystemFactory() {
             @Override
             public Command createSubsystem(ChannelSession channel) throws 
IOException {
-                return new SftpSubsystem(
-                        resolveExecutorService(),
-                        getUnsupportedAttributePolicy(), 
getFileSystemAccessor(), getErrorStatusDataHandler()) {
+                return new SftpSubsystem(this) {
                     @Override
                     protected List<OpenSSHExtension> 
resolveOpenSSHExtensions(ServerSession session) {
                         List<OpenSSHExtension> original = 
super.resolveOpenSSHExtensions(session);

Reply via email to