You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2024/01/23 09:10:35 UTC

(camel) branch main updated: (chores) camel-file: cleanup code duplications (#12874)

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

orpiske 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 1bcab0700cd (chores) camel-file: cleanup code duplications (#12874)
1bcab0700cd is described below

commit 1bcab0700cd94057fec3f42ae77cfb4f17670abc
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Tue Jan 23 10:10:26 2024 +0100

    (chores) camel-file: cleanup code duplications (#12874)
---
 .../apache/camel/component/file/FileEndpoint.java  | 11 +++--
 .../camel/component/file/FileOperations.java       | 44 +++++++------------
 .../camel/component/file/GenericFileEndpoint.java  | 12 ++++--
 .../file/GenericFileSendDynamicAware.java          | 50 +++++++++++++---------
 4 files changed, 60 insertions(+), 57 deletions(-)

diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
index 93536b5b8b8..2b48db194f1 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
@@ -55,9 +55,12 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
     private static final Integer CHMOD_WRITE_MASK = 02;
     private static final Integer CHMOD_READ_MASK = 04;
     private static final Integer CHMOD_EXECUTE_MASK = 01;
+    private static final String PARAM_OPERATIONS = "operations";
+    public static final String PARAM_FILE = "file";
 
     private final FileOperations operations = new FileOperations(this);
 
+
     @UriPath(name = "directoryName")
     @Metadata(required = true)
     private File file;
@@ -95,7 +98,7 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
 
     @Override
     public FileConsumer createConsumer(Processor processor) throws Exception {
-        ObjectHelper.notNull(operations, "operations");
+        ObjectHelper.notNull(operations, PARAM_OPERATIONS);
         ObjectHelper.notNull(file, "file");
 
         // auto create starting directory if needed
@@ -157,8 +160,8 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
 
     @Override
     public PollingConsumer createPollingConsumer() throws Exception {
-        ObjectHelper.notNull(operations, "operations");
-        ObjectHelper.notNull(file, "file");
+        ObjectHelper.notNull(operations, PARAM_OPERATIONS);
+        ObjectHelper.notNull(file, PARAM_FILE);
 
         if (LOG.isDebugEnabled()) {
             LOG.debug("Creating GenericFilePollingConsumer with queueSize: {} blockWhenFull: {} blockTimeout: {}",
@@ -176,7 +179,7 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
 
     @Override
     public GenericFileProducer<File> createProducer() throws Exception {
-        ObjectHelper.notNull(operations, "operations");
+        ObjectHelper.notNull(operations, PARAM_OPERATIONS);
 
         // you cannot use temp file and file exists append
         if (getFileExist() == GenericFileExist.Append && (getTempPrefix() != null || getTempFileName() != null)) {
diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
index a9ace6fd6d5..69922bcb574 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
@@ -330,16 +330,7 @@ public class FileOperations implements GenericFileOperations<File> {
                             // do so
                             keepLastModified(exchange, file);
                             // set permissions if the chmod option was set
-                            if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
-                                Set<PosixFilePermission> permissions = endpoint.getPermissions();
-                                if (!permissions.isEmpty()) {
-                                    if (LOG.isTraceEnabled()) {
-                                        LOG.trace("Setting chmod: {} on file: {}", PosixFilePermissions.toString(permissions),
-                                                file);
-                                    }
-                                    Files.setPosixFilePermissions(file.toPath(), permissions);
-                                }
-                            }
+                            setPermissions(file);
                             // clear header as we have renamed the file
                             exchange.getIn().setHeader(FileConstants.FILE_LOCAL_WORK_PATH, null);
                             // return as the operation is complete, we just renamed
@@ -356,15 +347,7 @@ public class FileOperations implements GenericFileOperations<File> {
                     // so
                     keepLastModified(exchange, file);
                     // set permissions if the chmod option was set
-                    if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
-                        Set<PosixFilePermission> permissions = endpoint.getPermissions();
-                        if (!permissions.isEmpty()) {
-                            if (LOG.isTraceEnabled()) {
-                                LOG.trace("Setting chmod: {} on file: {}", PosixFilePermissions.toString(permissions), file);
-                            }
-                            Files.setPosixFilePermissions(file.toPath(), permissions);
-                        }
-                    }
+                    setPermissions(file);
                     return true;
                 }
             }
@@ -396,15 +379,7 @@ public class FileOperations implements GenericFileOperations<File> {
             // try to keep last modified timestamp if configured to do so
             keepLastModified(exchange, file);
             // set permissions if the chmod option was set
-            if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
-                Set<PosixFilePermission> permissions = endpoint.getPermissions();
-                if (!permissions.isEmpty()) {
-                    if (LOG.isTraceEnabled()) {
-                        LOG.trace("Setting chmod: {} on file: {}", PosixFilePermissions.toString(permissions), file);
-                    }
-                    Files.setPosixFilePermissions(file.toPath(), permissions);
-                }
-            }
+            setPermissions(file);
 
             return true;
         } catch (InvalidPayloadException | IOException e) {
@@ -412,6 +387,19 @@ public class FileOperations implements GenericFileOperations<File> {
         }
     }
 
+    private void setPermissions(File file) throws IOException {
+        if (ObjectHelper.isNotEmpty(endpoint.getChmod())) {
+            Set<PosixFilePermission> permissions = endpoint.getPermissions();
+            if (!permissions.isEmpty()) {
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("Setting chmod: {} on file: {}", PosixFilePermissions.toString(permissions),
+                            file);
+                }
+                Files.setPosixFilePermissions(file.toPath(), permissions);
+            }
+        }
+    }
+
     private void keepLastModified(Exchange exchange, File file) {
         if (endpoint.isKeepLastModified()) {
             Long last;
diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
index f3acc80fd90..0b14f1fe5e1 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
@@ -1586,7 +1586,7 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint imple
     protected String configureMoveOrPreMoveExpression(String expression) {
         // if the expression already have ${ } placeholders then pass it
         // unmodified
-        if (StringHelper.hasStartToken(expression, "simple")) {
+        if (isSimpleLanguage(expression)) {
             return expression;
         }
 
@@ -1702,7 +1702,7 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint imple
         pattern = pattern.replaceFirst("\\$simple\\{file:name.noext\\}", FileUtil.stripExt(onlyName, true));
 
         // must be able to resolve all placeholders supported
-        if (StringHelper.hasStartToken(pattern, "simple")) {
+        if (isSimpleLanguage(pattern)) {
             throw new ExpressionIllegalSyntaxException(fileName + ". Cannot resolve reminder: " + pattern);
         }
 
@@ -1732,7 +1732,7 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint imple
         String pattern = getDoneFileName();
         StringHelper.notEmpty(pattern, "doneFileName", pattern);
 
-        if (!StringHelper.hasStartToken(pattern, "simple")) {
+        if (!isSimpleLanguage(pattern)) {
             // no tokens, so just match names directly
             return pattern.equals(fileName);
         }
@@ -1749,7 +1749,7 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint imple
         pattern = pattern.replaceFirst("\\$simple\\{file:name.noext\\}", "");
 
         // must be able to resolve all placeholders supported
-        if (StringHelper.hasStartToken(pattern, "simple")) {
+        if (isSimpleLanguage(pattern)) {
             throw new ExpressionIllegalSyntaxException(fileName + ". Cannot resolve reminder: " + pattern);
         }
 
@@ -1760,6 +1760,10 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint imple
         }
     }
 
+    private static boolean isSimpleLanguage(String pattern) {
+        return StringHelper.hasStartToken(pattern, "simple");
+    }
+
     @Override
     protected void doInit() throws Exception {
         super.doInit();
diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
index 5894cbbbd8d..a3b650cf002 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
@@ -25,6 +25,14 @@ import org.apache.camel.util.URISupport;
 
 public abstract class GenericFileSendDynamicAware extends SendDynamicAwareSupport {
 
+    public static final String PROP_FILE_NAME = "fileName";
+    public static final String PROP_TEMP_FILE_NAME = "tempFileName";
+    public static final String PROP_IDEMPOTENT_KEY = "idempotentKey";
+    public static final String PROP_MOVE = "move";
+    public static final String PROP_MOVE_FAILED = "moveFailed";
+    public static final String PROP_PRE_MOVE = "preMove";
+    public static final String PROP_MOVE_EXISTING = "moveExisting";
+
     @Override
     public boolean isLenientProperties() {
         return false;
@@ -43,13 +51,13 @@ public abstract class GenericFileSendDynamicAware extends SendDynamicAwareSuppor
         // which should then be reversed
         uri = uri.replace("\t", "\\\\t");
 
-        boolean fileName = entry.getProperties().containsKey("fileName");
-        boolean tempFileName = entry.getProperties().containsKey("tempFileName");
-        boolean idempotentKey = entry.getProperties().containsKey("idempotentKey");
-        boolean move = entry.getProperties().containsKey("move");
-        boolean moveFailed = entry.getProperties().containsKey("moveFailed");
-        boolean preMove = entry.getProperties().containsKey("preMove");
-        boolean moveExisting = entry.getProperties().containsKey("moveExisting");
+        boolean fileName = entry.getProperties().containsKey(PROP_FILE_NAME);
+        boolean tempFileName = entry.getProperties().containsKey(PROP_TEMP_FILE_NAME);
+        boolean idempotentKey = entry.getProperties().containsKey(PROP_IDEMPOTENT_KEY);
+        boolean move = entry.getProperties().containsKey(PROP_MOVE);
+        boolean moveFailed = entry.getProperties().containsKey(PROP_MOVE_FAILED);
+        boolean preMove = entry.getProperties().containsKey(PROP_PRE_MOVE);
+        boolean moveExisting = entry.getProperties().containsKey(PROP_MOVE_EXISTING);
         // if any of the above are in use, then they should not be pre evaluated
         // and we need to rebuild a new uri with them as-is
         if (fileName || tempFileName || idempotentKey || move || moveFailed || preMove || moveExisting) {
@@ -57,45 +65,45 @@ public abstract class GenericFileSendDynamicAware extends SendDynamicAwareSuppor
 
             Map<String, Object> originalParams = URISupport.parseQuery(URISupport.extractQuery(entry.getOriginalUri()));
             if (fileName) {
-                Object val = originalParams.get("fileName");
+                Object val = originalParams.get(PROP_FILE_NAME);
                 if (val != null) {
-                    params.put("fileName", val.toString());
+                    params.put(PROP_FILE_NAME, val.toString());
                 }
             }
             if (tempFileName) {
-                Object val = originalParams.get("tempFileName");
+                Object val = originalParams.get(PROP_TEMP_FILE_NAME);
                 if (val != null) {
-                    params.put("tempFileName", val.toString());
+                    params.put(PROP_TEMP_FILE_NAME, val.toString());
                 }
             }
             if (idempotentKey) {
-                Object val = originalParams.get("idempotentKey");
+                Object val = originalParams.get(PROP_IDEMPOTENT_KEY);
                 if (val != null) {
-                    params.put("idempotentKey", val.toString());
+                    params.put(PROP_IDEMPOTENT_KEY, val.toString());
                 }
             }
             if (move) {
-                Object val = originalParams.get("move");
+                Object val = originalParams.get(PROP_MOVE);
                 if (val != null) {
-                    params.put("move", val.toString());
+                    params.put(PROP_MOVE, val.toString());
                 }
             }
             if (moveFailed) {
-                Object val = originalParams.get("moveFailed");
+                Object val = originalParams.get(PROP_MOVE_FAILED);
                 if (val != null) {
-                    params.put("moveFailed", val.toString());
+                    params.put(PROP_MOVE_FAILED, val.toString());
                 }
             }
             if (preMove) {
-                Object val = originalParams.get("preMove");
+                Object val = originalParams.get(PROP_PRE_MOVE);
                 if (val != null) {
-                    params.put("preMove", val.toString());
+                    params.put(PROP_PRE_MOVE, val.toString());
                 }
             }
             if (moveExisting) {
-                Object val = originalParams.get("moveExisting");
+                Object val = originalParams.get(PROP_MOVE_EXISTING);
                 if (val != null) {
-                    params.put("moveExisting", val.toString());
+                    params.put(PROP_MOVE_EXISTING, val.toString());
                 }
             }