You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2021/03/25 19:47:39 UTC

[mina-sshd] 03/05: [SSHD-1132] Added SFTP client-side support for filename-translation-control extension

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 72758b02989ce517ba7685a0cedace3c0aa4292d
Author: Lyor Goldstein <lg...@apache.org>
AuthorDate: Tue Mar 23 12:12:50 2021 +0200

    [SSHD-1132] Added SFTP client-side support for filename-translation-control extension
---
 CHANGES.md                                         |  1 +
 .../extensions/BuiltinSftpClientExtensions.java    |  3 +-
 .../FilenameTranslationControlExtension.java       | 32 ++++++++++
 .../FilenameTranslationControlExtensionImpl.java   | 69 ++++++++++++++++++++++
 .../org/apache/sshd/sftp/common/SftpConstants.java |  1 +
 5 files changed, 105 insertions(+), 1 deletion(-)

diff --git a/CHANGES.md b/CHANGES.md
index 6aef290..f10009f 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -49,6 +49,7 @@
 * [SSHD-1125](https://issues.apache.org/jira/browse/SSHD-1125) Added mechanism to throttle pending write requests in BufferedIoOutputStream
 * [SSHD-1127](https://issues.apache.org/jira/browse/SSHD-1127) Added capability to register a custom receiver for SFTP STDERR channel raw or stream data
 * [SSHD-1132](https://issues.apache.org/jira/browse/SSHD-1132) Added SFTP client-side support for 'filename-charset' extension
+* [SSHD-1132](https://issues.apache.org/jira/browse/SSHD-1132) Added SFTP client-side support for 'filename-translation-control' extension
 * [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Added capability to specify a custom charset for parsing incoming commands to the `ScpShell`
 * [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Added capability to specify a custom charset for returning environment variables related data from the `ScpShell`
 * [SSHD-1133](https://issues.apache.org/jira/browse/SSHD-1133) Added capability to specify a custom charset for handling the SCP protocol textual commands and responses
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/BuiltinSftpClientExtensions.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/BuiltinSftpClientExtensions.java
index 4e15251..b531240 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/BuiltinSftpClientExtensions.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/BuiltinSftpClientExtensions.java
@@ -129,7 +129,8 @@ public enum BuiltinSftpClientExtensions implements SftpClientExtensionFactory {
                 SftpClient client, RawSftpClient raw, Map<String, byte[]> extensions, Map<String, ?> parsed) {
             return new OpenSSHPosixRenameExtensionImpl(client, raw, extensions);
         }
-    };
+    },
+    ;
 
     public static final Set<BuiltinSftpClientExtensions> VALUES
             = Collections.unmodifiableSet(EnumSet.allOf(BuiltinSftpClientExtensions.class));
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/FilenameTranslationControlExtension.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/FilenameTranslationControlExtension.java
new file mode 100644
index 0000000..ab6446a
--- /dev/null
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/FilenameTranslationControlExtension.java
@@ -0,0 +1,32 @@
+/*
+ * 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.sftp.client.extensions;
+
+import java.io.IOException;
+
+/**
+ * Implements &quot;filename-translation-control&quot; extension
+ *
+ * @see    <A HREF="https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#page-16">DRAFT 13 - page 16</A>
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public interface FilenameTranslationControlExtension extends SftpClientExtension {
+    void setFilenameTranslationControl(boolean doTranslate) throws IOException;
+}
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/helpers/FilenameTranslationControlExtensionImpl.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/helpers/FilenameTranslationControlExtensionImpl.java
new file mode 100644
index 0000000..575d17e
--- /dev/null
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/client/extensions/helpers/FilenameTranslationControlExtensionImpl.java
@@ -0,0 +1,69 @@
+/*
+ * 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.sftp.client.extensions.helpers;
+
+import java.io.IOException;
+import java.io.StreamCorruptedException;
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.MapEntryUtils;
+import org.apache.sshd.common.util.buffer.Buffer;
+import org.apache.sshd.sftp.client.RawSftpClient;
+import org.apache.sshd.sftp.client.SftpClient;
+import org.apache.sshd.sftp.client.extensions.FilenameTranslationControlExtension;
+import org.apache.sshd.sftp.common.SftpConstants;
+
+/**
+ * Implements &quot;filename-translation-control&quot; extension command
+ *
+ * @see    <A HREF="https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#page-16">DRAFT 13 - page 16</A>
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class FilenameTranslationControlExtensionImpl
+        extends AbstractSftpClientExtension
+        implements FilenameTranslationControlExtension {
+    public FilenameTranslationControlExtensionImpl(SftpClient client, RawSftpClient raw, Collection<String> extras) {
+        super(SftpConstants.EXT_FILENAME_XLATE_CONTROL, client, raw,
+              GenericUtils.isNotEmpty(extras) && extras.contains(SftpConstants.EXT_FILENAME_CHARSET));
+    }
+
+    public FilenameTranslationControlExtensionImpl(SftpClient client, RawSftpClient raw, Map<String, byte[]> extensions) {
+        super(SftpConstants.EXT_FILENAME_XLATE_CONTROL, client, raw,
+              MapEntryUtils.isNotEmpty(extensions) && extensions.containsKey(SftpConstants.EXT_FILENAME_CHARSET));
+    }
+
+    @Override
+    public void setFilenameTranslationControl(boolean doTranslate) throws IOException {
+        Buffer request = getCommandBuffer(Byte.SIZE);
+        request.putBoolean(doTranslate);
+        if (log.isDebugEnabled()) {
+            log.debug("setFilenameTranslationControl({}) doTranslate={}", getName(), doTranslate);
+        }
+
+        int id = sendExtendedCommand(request);
+        Buffer response = receive(id);
+        response = checkExtendedReplyBuffer(response);
+        if (response != null) {
+            throw new StreamCorruptedException("Unexpected extended reply data");
+        }
+    }
+}
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/SftpConstants.java b/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/SftpConstants.java
index 8f3ed6e..b95a877 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/SftpConstants.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/sftp/common/SftpConstants.java
@@ -251,6 +251,7 @@ public final class SftpConstants {
     public static final String EXT_VERSION_SELECT = "version-select";
     public static final String EXT_COPY_FILE = "copy-file";
     public static final String EXT_FILENAME_CHARSET = "filename-charset";
+    public static final String EXT_FILENAME_XLATE_CONTROL = "filename-translation-control";
 
     public static final String EXT_MD5_HASH = "md5-hash";
     public static final String EXT_MD5_HASH_HANDLE = "md5-hash-handle";