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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 60cc46c  CAMEL-17037: added for sftp producer chmodDirectory 
functionality (#6199)
60cc46c is described below

commit 60cc46ccb97758227cf2742be0cb9e453725a042
Author: Michael R <mich...@rambichler.at>
AuthorDate: Mon Oct 4 06:55:06 2021 +0200

    CAMEL-17037: added for sftp producer chmodDirectory functionality (#6199)
    
    * CAMEL-17037: added for sftp producer chmodDirectory functionality
    
    * added unittest
    
    * fixed typo in unittest
---
 .../component/file/remote/SftpConfiguration.java   | 13 +++++++
 .../camel/component/file/remote/SftpEndpoint.java  |  2 +-
 .../component/file/remote/SftpOperations.java      | 24 ++++++++++++
 .../sftp/integration/SftpChmodDirectoryIT.java     | 43 ++++++++++++++++++++++
 4 files changed, 81 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
index 9183e72..e1643b5 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
@@ -75,6 +75,8 @@ public class SftpConfiguration extends 
RemoteFileConfiguration {
     private boolean existDirCheckUsingLs = true;
     @UriParam(label = "security")
     private String keyExchangeProtocols;
+    @UriParam(label = "producer, advanced")
+    private String chmodDirectory;
 
     public SftpConfiguration() {
         setProtocol("sftp");
@@ -250,6 +252,17 @@ public class SftpConfiguration extends 
RemoteFileConfiguration {
     }
 
     /**
+     * Allows you to set chmod during path creation. For example chmod=640.
+     */
+    public void setChmodDirectory(String chmodDirectory) {
+        this.chmodDirectory = chmodDirectory;
+    }
+
+    public String getChmodDirectory() {
+        return chmodDirectory;
+    }
+
+    /**
      * Set a comma separated list of ciphers that will be used in order of 
preference. Possible cipher names are defined
      * by JCraft JSCH. Some examples include:
      * 
aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc. If 
not specified the default list
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpEndpoint.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpEndpoint.java
index b079dcc..ba1b7b4 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpEndpoint.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpEndpoint.java
@@ -34,7 +34,7 @@ import org.apache.camel.spi.UriParam;
              syntax = "sftp:host:port/directoryName", label = "file")
 @Metadata(excludeProperties = "appendChars,bufferSize,siteCommand,"
                               + 
"directoryMustExist,extendedAttributes,probeContentType,startingDirectoryMustExist,"
-                              + 
"startingDirectoryMustHaveAccess,chmodDirectory,forceWrites,copyAndDeleteOnRenameFail,"
+                              + 
"startingDirectoryMustHaveAccess,forceWrites,copyAndDeleteOnRenameFail,"
                               + "renameUsingCopy")
 public class SftpEndpoint extends RemoteFileEndpoint<SftpRemoteFile> {
 
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index ba31d31..1bf3ce7 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -553,6 +553,9 @@ public class SftpOperations implements 
RemoteFileOperations<SftpRemoteFile> {
                     // so create the folder one by one
                     success = buildDirectoryChunks(directory);
                 }
+
+                // after creating directory, we may set chmod on the file
+                chmodOfDirectory(directory);
             }
         } catch (SftpException e) {
             throw new GenericFileOperationFailedException("Cannot build 
directory: " + directory, e);
@@ -592,6 +595,9 @@ public class SftpOperations implements 
RemoteFileOperations<SftpRemoteFile> {
                 } catch (SftpException e) {
                     // ignore keep trying to create the rest of the path
                 }
+
+                // after creating directory, we may set chmod on the file
+                chmodOfDirectory(directory);
             }
         }
 
@@ -1199,4 +1205,22 @@ public class SftpOperations implements 
RemoteFileOperations<SftpRemoteFile> {
             exchange.getIn().setHeader(FtpConstants.FTP_REPLY_STRING, 
sftpException.getMessage());
         }
     }
+
+    /**
+     * Helper method which sets the path permissions
+     */
+    private void chmodOfDirectory(String directory) {
+
+        String chmodDirectory = 
endpoint.getConfiguration().getChmodDirectory();
+        if (ObjectHelper.isNotEmpty(chmodDirectory)) {
+            LOG.trace("Setting permission: {} on directory: {}", 
chmodDirectory, directory);
+            // parse to int using 8bit mode
+            int permissions = Integer.parseInt(chmodDirectory, 8);
+            try {
+                channel.chmod(permissions, directory);
+            } catch (SftpException e) {
+                throw new GenericFileOperationFailedException("Cannot set 
permission on directory: " + directory, e);
+            }
+        }
+    }
 }
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpChmodDirectoryIT.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpChmodDirectoryIT.java
new file mode 100644
index 0000000..8242db1
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpChmodDirectoryIT.java
@@ -0,0 +1,43 @@
+/*
+ * 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.camel.component.file.remote.sftp.integration;
+
+import java.io.File;
+
+import org.apache.camel.Exchange;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIf;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@EnabledIf(value = 
"org.apache.camel.component.file.remote.services.SftpEmbeddedService#hasRequiredAlgorithms")
+public class SftpChmodDirectoryIT extends SftpServerTestSupport {
+
+    @Test
+    public void testSftpChmodDirectoryWriteable() {
+        template.sendBodyAndHeader(
+                "sftp://localhost:{{ftp.server.port}}/{{ftp.root.dir}}/folder"; 
+
+                                   
"?username=admin&password=admin&chmod=777&chmodDirectory=770",
+                "Hello World", Exchange.FILE_NAME,
+                "hello.txt");
+
+        File path = ftpFile("folder/hello.txt").getParent().toFile();
+        assertTrue(path.canRead(), "Path should have permission readable: " + 
path);
+        assertTrue(path.canWrite(), "Path should have permission writeable: " 
+ path);
+    }
+
+}

Reply via email to