This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
commit 3d28d18b815c7cafe0bed1b7e96e3f346d174e50 Author: Guillaume Nodet <gno...@gmail.com> AuthorDate: Sat Apr 18 20:13:07 2020 +0200 [SSHD-980] Make the SFTP Api cleaner by moving the implementation classes into the non public package --- .../sshd/client/subsystem/sftp/SftpClient.java | 34 +++++---------- .../subsystem/sftp/fs/SftpFileSystemChannel.java | 41 ------------------ .../subsystem/sftp/fs/SftpFileSystemProvider.java | 12 +++--- .../subsystem/sftp/impl/AbstractSftpClient.java | 50 ++++++++++++++++++++++ .../sftp/{ => impl}/SftpDirEntryIterator.java | 3 +- .../sftp/{ => impl}/SftpIterableDirEntry.java | 3 +- .../sftp/{ => impl}/SftpRemotePathChannel.java | 28 +++++------- .../sftp/{ => impl}/StfpIterableDirHandle.java | 3 +- .../sftp/{ => impl}/SftpRemotePathChannelTest.java | 4 +- 9 files changed, 86 insertions(+), 92 deletions(-) diff --git a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpClient.java b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpClient.java index e78fa00..a31d973 100644 --- a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpClient.java +++ b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpClient.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.Channel; +import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.OpenOption; @@ -771,13 +772,7 @@ public interface SftpClient extends SubsystemClient { * can be iterated only <U>once</U> * @throws IOException If failed to access the directory */ - default Iterable<DirEntry> listDir(Handle handle) throws IOException { - if (!isOpen()) { - throw new IOException("listDir(" + handle + ") client is closed"); - } - - return new StfpIterableDirHandle(this, handle); - } + Iterable<DirEntry> listDir(Handle handle) throws IOException; /** * The effective "normalized" remote path @@ -875,34 +870,31 @@ public interface SftpClient extends SubsystemClient { // High level API // - default SftpRemotePathChannel openRemotePathChannel(String path, OpenOption... options) throws IOException { + default FileChannel openRemotePathChannel(String path, OpenOption... options) throws IOException { return openRemotePathChannel(path, GenericUtils.isEmpty(options) ? Collections.emptyList() : Arrays.asList(options)); } - default SftpRemotePathChannel openRemotePathChannel(String path, Collection<? extends OpenOption> options) - throws IOException { + default FileChannel openRemotePathChannel(String path, Collection<? extends OpenOption> options) throws IOException { return openRemoteFileChannel(path, OpenMode.fromOpenOptions(options)); } - default SftpRemotePathChannel openRemoteFileChannel(String path, OpenMode... modes) throws IOException { + default FileChannel openRemoteFileChannel(String path, OpenMode... modes) throws IOException { return openRemoteFileChannel(path, GenericUtils.isEmpty(modes) ? Collections.emptyList() : Arrays.asList(modes)); } /** - * Opens an {@link SftpRemotePathChannel} on the specified remote path + * Opens an {@link FileChannel} on the specified remote path * * @param path The remote path * @param modes The access mode(s) - if {@code null}/empty then the {@link #DEFAULT_CHANNEL_MODES} are used - * @return The open {@link SftpRemotePathChannel} - <B>Note:</B> do not close this owner client instance - * until the channel is no longer needed since it uses the client for providing the channel's + * @return The open {@link FileChannel} - <B>Note:</B> do not close this owner client instance until the + * channel is no longer needed since it uses the client for providing the channel's * functionality. * @throws IOException If failed to open the channel * @see java.nio.channels.Channels#newInputStream(java.nio.channels.ReadableByteChannel) * @see java.nio.channels.Channels#newOutputStream(java.nio.channels.WritableByteChannel) */ - default SftpRemotePathChannel openRemoteFileChannel(String path, Collection<OpenMode> modes) throws IOException { - return new SftpRemotePathChannel(path, this, false, GenericUtils.isEmpty(modes) ? DEFAULT_CHANNEL_MODES : modes); - } + FileChannel openRemoteFileChannel(String path, Collection<OpenMode> modes) throws IOException; /** * @param path The remote directory path @@ -911,13 +903,7 @@ public interface SftpClient extends SubsystemClient { * @throws IOException If failed to access the remote site * @see #readDir(Handle) */ - default Iterable<DirEntry> readDir(String path) throws IOException { - if (!isOpen()) { - throw new IOException("readDir(" + path + ") client is closed"); - } - - return new SftpIterableDirEntry(this, path); - } + Iterable<DirEntry> readDir(String path) throws IOException; default InputStream read(String path) throws IOException { return read(path, DEFAULT_READ_BUFFER_SIZE); diff --git a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/fs/SftpFileSystemChannel.java b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/fs/SftpFileSystemChannel.java deleted file mode 100644 index 03c5fbc..0000000 --- a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/fs/SftpFileSystemChannel.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sshd.client.subsystem.sftp.fs; - -import java.io.IOException; -import java.util.Collection; -import java.util.Objects; - -import org.apache.sshd.client.subsystem.sftp.SftpClient; -import org.apache.sshd.client.subsystem.sftp.SftpRemotePathChannel; - -/** - * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> - */ -public class SftpFileSystemChannel extends SftpRemotePathChannel { - public SftpFileSystemChannel(SftpPath p, Collection<SftpClient.OpenMode> modes) throws IOException { - this(Objects.requireNonNull(p, "No target path").toString(), p.getFileSystem(), modes); - } - - public SftpFileSystemChannel(String remotePath, SftpFileSystem fs, - Collection<SftpClient.OpenMode> modes) throws IOException { - super(remotePath, Objects.requireNonNull(fs, "No SFTP file system").getClient(), true, modes); - } -} diff --git a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/fs/SftpFileSystemProvider.java b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/fs/SftpFileSystemProvider.java index 666658e..5e5ffe3 100644 --- a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/fs/SftpFileSystemProvider.java +++ b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/fs/SftpFileSystemProvider.java @@ -76,9 +76,9 @@ import org.apache.sshd.client.subsystem.sftp.SftpClient; import org.apache.sshd.client.subsystem.sftp.SftpClient.Attributes; import org.apache.sshd.client.subsystem.sftp.SftpClient.OpenMode; import org.apache.sshd.client.subsystem.sftp.SftpClientFactory; -import org.apache.sshd.client.subsystem.sftp.SftpRemotePathChannel; import org.apache.sshd.client.subsystem.sftp.SftpVersionSelector; import org.apache.sshd.client.subsystem.sftp.extensions.CopyFileExtension; +import org.apache.sshd.client.subsystem.sftp.impl.SftpRemotePathChannel; import org.apache.sshd.common.PropertyResolver; import org.apache.sshd.common.PropertyResolverUtils; import org.apache.sshd.common.SshConstants; @@ -480,9 +480,9 @@ public class SftpFileSystemProvider extends FileSystemProvider { @Override public FileChannel newFileChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { - Collection<SftpClient.OpenMode> modes = SftpClient.OpenMode.fromOpenOptions(options); + Collection<OpenMode> modes = OpenMode.fromOpenOptions(options); if (modes.isEmpty()) { - modes = EnumSet.of(SftpClient.OpenMode.Read, SftpClient.OpenMode.Write); + modes = EnumSet.of(OpenMode.Read, OpenMode.Write); } // TODO: process file attributes SftpPath p = toSftpPath(path); @@ -491,9 +491,9 @@ public class SftpFileSystemProvider extends FileSystemProvider { @Override public InputStream newInputStream(Path path, OpenOption... options) throws IOException { - Collection<SftpClient.OpenMode> modes = SftpClient.OpenMode.fromOpenOptions(Arrays.asList(options)); + Collection<OpenMode> modes = OpenMode.fromOpenOptions(Arrays.asList(options)); if (modes.isEmpty()) { - modes = EnumSet.of(SftpClient.OpenMode.Read); + modes = EnumSet.of(OpenMode.Read); } SftpPath p = toSftpPath(path); return p.getFileSystem().getClient().read(p.toString(), modes); @@ -501,7 +501,7 @@ public class SftpFileSystemProvider extends FileSystemProvider { @Override public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException { - Set<SftpClient.OpenMode> modes = SftpClient.OpenMode.fromOpenOptions(Arrays.asList(options)); + Set<OpenMode> modes = OpenMode.fromOpenOptions(Arrays.asList(options)); if (modes.contains(OpenMode.Read)) { throw new IllegalArgumentException("READ not allowed"); } diff --git a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/AbstractSftpClient.java b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/AbstractSftpClient.java index b96a24a..211f3ee 100644 --- a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/AbstractSftpClient.java +++ b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/AbstractSftpClient.java @@ -21,6 +21,7 @@ package org.apache.sshd.client.subsystem.sftp.impl; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.file.attribute.FileTime; import java.util.ArrayList; @@ -1280,6 +1281,55 @@ public abstract class AbstractSftpClient extends AbstractSubsystemClient impleme checkCommandStatus(SftpConstants.SSH_FXP_UNBLOCK, buffer); } + /** + * @param path The remote directory path + * @return An {@link Iterable} that can be used to iterate over all the directory entries (unlike + * {@link #readDir(Handle)}) + * @throws IOException If failed to access the remote site + * @see #readDir(Handle) + */ + @Override + public Iterable<DirEntry> readDir(String path) throws IOException { + if (!isOpen()) { + throw new IOException("readDir(" + path + ") client is closed"); + } + + return new SftpIterableDirEntry(this, path); + } + + /** + * @param handle A directory {@link Handle} + * @return An {@link Iterable} that can be used to iterate over all the directory entries (like + * {@link #readDir(String)}). <B>Note:</B> the iterable instance is not re-usable - i.e., files + * can be iterated only <U>once</U> + * @throws IOException If failed to access the directory + */ + @Override + public Iterable<DirEntry> listDir(Handle handle) throws IOException { + if (!isOpen()) { + throw new IOException("listDir(" + handle + ") client is closed"); + } + + return new StfpIterableDirHandle(this, handle); + } + + /** + * Opens an {@link FileChannel} on the specified remote path + * + * @param path The remote path + * @param modes The access mode(s) - if {@code null}/empty then the {@link #DEFAULT_CHANNEL_MODES} are used + * @return The open {@link FileChannel} - <B>Note:</B> do not close this owner client instance until the + * channel is no longer needed since it uses the client for providing the channel's + * functionality. + * @throws IOException If failed to open the channel + * @see java.nio.channels.Channels#newInputStream(java.nio.channels.ReadableByteChannel) + * @see java.nio.channels.Channels#newOutputStream(java.nio.channels.WritableByteChannel) + */ + @Override + public SftpRemotePathChannel openRemoteFileChannel(String path, Collection<OpenMode> modes) throws IOException { + return new SftpRemotePathChannel(path, this, false, GenericUtils.isEmpty(modes) ? DEFAULT_CHANNEL_MODES : modes); + } + @Override public InputStream read(String path, int bufferSize, Collection<OpenMode> mode) throws IOException { if (bufferSize < MIN_WRITE_BUFFER_SIZE) { diff --git a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpDirEntryIterator.java b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/SftpDirEntryIterator.java similarity index 98% rename from sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpDirEntryIterator.java rename to sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/SftpDirEntryIterator.java index 1a18902..f650cc4 100644 --- a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpDirEntryIterator.java +++ b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/SftpDirEntryIterator.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.sshd.client.subsystem.sftp; +package org.apache.sshd.client.subsystem.sftp.impl; import java.io.Closeable; import java.io.IOException; @@ -27,6 +27,7 @@ import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import org.apache.sshd.client.subsystem.sftp.SftpClient; import org.apache.sshd.client.subsystem.sftp.SftpClient.DirEntry; import org.apache.sshd.client.subsystem.sftp.SftpClient.Handle; import org.apache.sshd.common.util.ValidateUtils; diff --git a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpIterableDirEntry.java b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/SftpIterableDirEntry.java similarity index 95% rename from sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpIterableDirEntry.java rename to sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/SftpIterableDirEntry.java index 269b723..6617654 100644 --- a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpIterableDirEntry.java +++ b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/SftpIterableDirEntry.java @@ -16,11 +16,12 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.sshd.client.subsystem.sftp; +package org.apache.sshd.client.subsystem.sftp.impl; import java.io.IOException; import java.util.Objects; +import org.apache.sshd.client.subsystem.sftp.SftpClient; import org.apache.sshd.client.subsystem.sftp.SftpClient.DirEntry; import org.apache.sshd.common.util.ValidateUtils; diff --git a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannel.java b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/SftpRemotePathChannel.java similarity index 94% rename from sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannel.java rename to sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/SftpRemotePathChannel.java index e9d5f5a..b6275d1 100644 --- a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannel.java +++ b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/SftpRemotePathChannel.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.sshd.client.subsystem.sftp; +package org.apache.sshd.client.subsystem.sftp.impl; import java.io.IOException; import java.io.StreamCorruptedException; @@ -41,10 +41,9 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import org.apache.sshd.client.session.ClientSession; +import org.apache.sshd.client.subsystem.sftp.SftpClient; import org.apache.sshd.client.subsystem.sftp.SftpClient.Attributes; -import org.apache.sshd.client.subsystem.sftp.impl.AbstractSftpClient; -import org.apache.sshd.client.subsystem.sftp.impl.SftpInputStreamAsync; -import org.apache.sshd.client.subsystem.sftp.impl.SftpOutputStreamAsync; +import org.apache.sshd.client.subsystem.sftp.SftpClient.OpenMode; import org.apache.sshd.common.subsystem.sftp.SftpConstants; import org.apache.sshd.common.subsystem.sftp.SftpException; import org.apache.sshd.common.util.GenericUtils; @@ -62,17 +61,13 @@ public class SftpRemotePathChannel extends FileChannel { /** Default value for {@value #COPY_BUFSIZE_PROP} setting */ public static final int DEFAULT_TRANSFER_BUFFER_SIZE = IoUtils.DEFAULT_COPY_SIZE; - public static final Set<SftpClient.OpenMode> READ_MODES = Collections.unmodifiableSet(EnumSet.of(SftpClient.OpenMode.Read)); + public static final Set<OpenMode> READ_MODES = Collections.unmodifiableSet(EnumSet.of(OpenMode.Read)); - public static final Set<SftpClient.OpenMode> WRITE_MODES = Collections.unmodifiableSet( - EnumSet.of( - SftpClient.OpenMode.Write, - SftpClient.OpenMode.Append, - SftpClient.OpenMode.Create, - SftpClient.OpenMode.Truncate)); + public static final Set<OpenMode> WRITE_MODES = Collections.unmodifiableSet( + EnumSet.of(OpenMode.Write, OpenMode.Append, OpenMode.Create, OpenMode.Truncate)); protected final Logger log; - protected final Collection<SftpClient.OpenMode> modes; + protected final Collection<OpenMode> modes; protected final boolean closeOnExit; protected final SftpClient sftp; protected final SftpClient.CloseableHandle handle; @@ -82,9 +77,8 @@ public class SftpRemotePathChannel extends FileChannel { private final String path; - public SftpRemotePathChannel( - String path, SftpClient sftp, boolean closeOnExit, Collection<SftpClient.OpenMode> modes) - throws IOException { + public SftpRemotePathChannel(String path, SftpClient sftp, boolean closeOnExit, + Collection<OpenMode> modes) throws IOException { this.log = LoggerFactory.getLogger(getClass()); this.path = ValidateUtils.checkNotNullAndNotEmpty(path, "No remote file path specified"); this.modes = Objects.requireNonNull(modes, "No channel modes specified"); @@ -533,13 +527,13 @@ public class SftpRemotePathChannel extends FileChannel { * @param reqModes The required modes - ignored if {@code null}/empty * @throws IOException If channel not open or the required modes are not satisfied */ - private void ensureOpen(Collection<SftpClient.OpenMode> reqModes) throws IOException { + private void ensureOpen(Collection<OpenMode> reqModes) throws IOException { if (!isOpen()) { throw new ClosedChannelException(); } if (GenericUtils.size(reqModes) > 0) { - for (SftpClient.OpenMode m : reqModes) { + for (OpenMode m : reqModes) { if (this.modes.contains(m)) { return; } diff --git a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/StfpIterableDirHandle.java b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/StfpIterableDirHandle.java similarity index 94% rename from sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/StfpIterableDirHandle.java rename to sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/StfpIterableDirHandle.java index c3be157..a4ee770 100644 --- a/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/StfpIterableDirHandle.java +++ b/sshd-sftp/src/main/java/org/apache/sshd/client/subsystem/sftp/impl/StfpIterableDirHandle.java @@ -16,10 +16,11 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.sshd.client.subsystem.sftp; +package org.apache.sshd.client.subsystem.sftp.impl; import java.util.Objects; +import org.apache.sshd.client.subsystem.sftp.SftpClient; import org.apache.sshd.client.subsystem.sftp.SftpClient.DirEntry; import org.apache.sshd.client.subsystem.sftp.SftpClient.Handle; diff --git a/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannelTest.java b/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/impl/SftpRemotePathChannelTest.java similarity index 98% rename from sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannelTest.java rename to sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/impl/SftpRemotePathChannelTest.java index 161d966..db2fcac 100644 --- a/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpRemotePathChannelTest.java +++ b/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/impl/SftpRemotePathChannelTest.java @@ -17,7 +17,7 @@ * under the License. */ -package org.apache.sshd.client.subsystem.sftp; +package org.apache.sshd.client.subsystem.sftp.impl; import java.io.IOException; import java.io.Writer; @@ -31,6 +31,8 @@ import java.util.Date; import java.util.EnumSet; import org.apache.sshd.client.session.ClientSession; +import org.apache.sshd.client.subsystem.sftp.AbstractSftpClientTestSupport; +import org.apache.sshd.client.subsystem.sftp.SftpClient; import org.apache.sshd.common.subsystem.sftp.SftpConstants; import org.apache.sshd.util.test.CommonTestSupportUtils; import org.junit.Before;