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 9ddd82459f0 (chores) camel-file: code cleanup
9ddd82459f0 is described below

commit 9ddd82459f0cd1ce0e3c11bc269a4bf80dbdae29
Author: Otavio Rodolfo Piske <angusyo...@gmail.com>
AuthorDate: Mon Apr 29 11:33:09 2024 +0200

    (chores) camel-file: code cleanup
    
    - break large and complex methods
    - fixed logging
---
 .../apache/camel/component/file/FileConsumer.java  | 84 ++++++++++++++--------
 .../apache/camel/component/file/FileEndpoint.java  | 46 +++++++-----
 .../apache/camel/component/file/GenericFile.java   | 63 ++++++++++------
 3 files changed, 122 insertions(+), 71 deletions(-)

diff --git 
a/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
 
b/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
index 1e871a0b5b2..5d9cebd645a 100644
--- 
a/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
+++ 
b/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
@@ -105,13 +105,7 @@ public class FileConsumer extends 
GenericFileConsumer<File> implements ResumeAwa
                     = asGenericFile(endpointPath, file, 
getEndpoint().getCharset(), getEndpoint().isProbeContentType());
 
             if (resumeStrategy != null) {
-                ResumeAdapter adapter = resumeStrategy.getAdapter();
-                LOG.trace("Checking the resume adapter: {}", adapter);
-                if (adapter instanceof FileOffsetResumeAdapter) {
-                    LOG.trace("The resume adapter is for offsets: {}", 
adapter);
-                    ((FileOffsetResumeAdapter) adapter).setResumePayload(gf);
-                    adapter.resume();
-                }
+                final ResumeAdapter adapter = setupResumeStrategy(gf);
 
                 if (adapter instanceof DirectoryEntriesResumeAdapter) {
                     LOG.trace("Running the resume process for file {}", file);
@@ -122,36 +116,64 @@ public class FileConsumer extends 
GenericFileConsumer<File> implements ResumeAwa
                 }
             }
 
-            if (file.isDirectory()) {
-                if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() 
&& isValidFile(gf, true, files)) {
-                    boolean canPollMore = pollDirectory(file, fileList, depth);
-                    if (!canPollMore) {
-                        return false;
-                    }
-                }
-            } else {
-                // Windows can report false to a file on a share so regard it
-                // always as a file (if it is not a directory)
-                if (depth >= endpoint.minDepth && isValidFile(gf, false, 
files)) {
-                    LOG.trace("Adding valid file: {}", file);
-                    // matched file so add
-                    if (extendedAttributes != null) {
-                        Path path = file.toPath();
-                        Map<String, Object> allAttributes = new HashMap<>();
-                        for (String attribute : extendedAttributes) {
-                            readAttributes(file, path, allAttributes, 
attribute);
-                        }
-
-                        gf.setExtendedAttributes(allAttributes);
-                    }
+            if (processEntry(fileList, depth, file, gf, files)) {
+                return false;
+            }
+        }
+
+        return true;
+    }
 
-                    fileList.add(gf);
+    private boolean processEntry(List<GenericFile<File>> fileList, int depth, 
File file, GenericFile<File> gf, File[] files) {
+        if (file.isDirectory()) {
+            return processDirectoryEntry(fileList, depth, file, gf, files);
+        } else {
+            processFileEntry(fileList, depth, file, gf, files);
+
+        }
+        return false;
+    }
+
+    private void processFileEntry(List<GenericFile<File>> fileList, int depth, 
File file, GenericFile<File> gf, File[] files) {
+        // Windows can report false to a file on a share so regard it
+        // always as a file (if it is not a directory)
+        if (depth >= endpoint.minDepth && isValidFile(gf, false, files)) {
+            LOG.trace("Adding valid file: {}", file);
+            // matched file so add
+            if (extendedAttributes != null) {
+                Path path = file.toPath();
+                Map<String, Object> allAttributes = new HashMap<>();
+                for (String attribute : extendedAttributes) {
+                    readAttributes(file, path, allAttributes, attribute);
                 }
 
+                gf.setExtendedAttributes(allAttributes);
             }
+
+            fileList.add(gf);
         }
+    }
 
-        return true;
+    private boolean processDirectoryEntry(
+            List<GenericFile<File>> fileList, int depth, File file, 
GenericFile<File> gf, File[] files) {
+        if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && 
isValidFile(gf, true, files)) {
+            boolean canPollMore = pollDirectory(file, fileList, depth);
+            if (!canPollMore) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private ResumeAdapter setupResumeStrategy(GenericFile<File> gf) {
+        ResumeAdapter adapter = resumeStrategy.getAdapter();
+        LOG.trace("Checking the resume adapter: {}", adapter);
+        if (adapter instanceof FileOffsetResumeAdapter) {
+            LOG.trace("The resume adapter is for offsets: {}", adapter);
+            ((FileOffsetResumeAdapter) adapter).setResumePayload(gf);
+            adapter.resume();
+        }
+        return adapter;
     }
 
     @Override
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 4de58f0d6cc..eb07f3d5e02 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
@@ -102,16 +102,7 @@ public class FileEndpoint extends 
GenericFileEndpoint<File> {
 
         // auto create starting directory if needed
         if (!file.exists() && !file.isDirectory()) {
-            if (isAutoCreate()) {
-                LOG.debug("Creating non existing starting directory: {}", 
file);
-                boolean absolute = FileUtil.isAbsolute(file);
-                boolean created = operations.buildDirectory(file.getPath(), 
absolute);
-                if (!created) {
-                    LOG.warn("Cannot auto create starting directory: {}", 
file);
-                }
-            } else if (isStartingDirectoryMustExist()) {
-                throw new FileNotFoundException("Starting directory does not 
exist: " + file);
-            }
+            tryCreateDirectory();
         }
         if (!isStartingDirectoryMustExist() && 
isStartingDirectoryMustHaveAccess()) {
             throw new IllegalArgumentException(
@@ -140,13 +131,7 @@ public class FileEndpoint extends 
GenericFileEndpoint<File> {
         }
 
         if (ObjectHelper.isNotEmpty(getReadLock())) {
-            // check if its a valid
-            String valid = 
"none,markerFile,fileLock,rename,changed,idempotent,idempotent-changed,idempotent-rename";
-            String[] arr = valid.split(",");
-            boolean matched = Arrays.stream(arr).anyMatch(n -> 
n.equals(getReadLock()));
-            if (!matched) {
-                throw new IllegalArgumentException("ReadLock invalid: " + 
getReadLock() + ", must be one of: " + valid);
-            }
+            readLockCheck();
         }
 
         // set max messages per poll
@@ -157,6 +142,33 @@ public class FileEndpoint extends 
GenericFileEndpoint<File> {
         return result;
     }
 
+    private void readLockCheck() {
+        // check if its a valid
+        String valid = 
"none,markerFile,fileLock,rename,changed,idempotent,idempotent-changed,idempotent-rename";
+        String[] arr = valid.split(",");
+        boolean matched = Arrays.stream(arr).anyMatch(n -> 
n.equals(getReadLock()));
+        if (!matched) {
+            throw new IllegalArgumentException("ReadLock invalid: " + 
getReadLock() + ", must be one of: " + valid);
+        }
+    }
+
+    private void tryCreateDirectory() throws FileNotFoundException {
+        if (isAutoCreate()) {
+            doCreateStartDirectory();
+        } else if (isStartingDirectoryMustExist()) {
+            throw new FileNotFoundException("Starting directory does not 
exist: " + file);
+        }
+    }
+
+    private void doCreateStartDirectory() {
+        LOG.debug("Creating non existing starting directory: {}", file);
+        boolean absolute = FileUtil.isAbsolute(file);
+        boolean created = operations.buildDirectory(file.getPath(), absolute);
+        if (!created) {
+            LOG.warn("Cannot auto create starting directory: {}", file);
+        }
+    }
+
     @Override
     public PollingConsumer createPollingConsumer() throws Exception {
         ObjectHelper.notNull(operations, PARAM_OPERATIONS);
diff --git 
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFile.java
 
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFile.java
index cc8d919129a..5d2c4027bbe 100644
--- 
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFile.java
+++ 
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFile.java
@@ -164,14 +164,8 @@ public class GenericFile<T> implements WrappedFile<T> {
                 message.setHeader(FileConstants.FILE_EXTENDED_ATTRIBUTES, 
extendedAttributes);
             }
 
-            if ((isProbeContentTypeFromEndpoint || probeContentType) && file 
instanceof File) {
-                File f = (File) file;
-                Path path = f.toPath();
-                try {
-                    message.setHeader(FileConstants.FILE_CONTENT_TYPE, 
Files.probeContentType(path));
-                } catch (Exception e) {
-                    // just ignore the exception
-                }
+            if (isProbeContentType(isProbeContentTypeFromEndpoint)) {
+                probeContentType(message);
             }
 
             if (isAbsolute()) {
@@ -196,6 +190,20 @@ public class GenericFile<T> implements WrappedFile<T> {
         }
     }
 
+    private boolean isProbeContentType(boolean isProbeContentTypeFromEndpoint) 
{
+        return (isProbeContentTypeFromEndpoint || probeContentType) && file 
instanceof File;
+    }
+
+    private void probeContentType(GenericFileMessage<T> message) {
+        File f = (File) file;
+        Path path = f.toPath();
+        try {
+            message.setHeader(FileConstants.FILE_CONTENT_TYPE, 
Files.probeContentType(path));
+        } catch (Exception e) {
+            // just ignore the exception
+        }
+    }
+
     protected boolean isAbsolute(String name) {
         return FileUtil.isAbsolute(new File(name));
     }
@@ -225,15 +233,7 @@ public class GenericFile<T> implements WrappedFile<T> {
             // for relative then we should avoid having the endpoint path
             // duplicated so clip it
             if (ObjectHelper.isNotEmpty(newEndpointPath) && 
newFileName.startsWith(newEndpointPath)) {
-                // clip starting endpoint in case it was added
-                // use File.separatorChar as the normalizePath uses this as 
path
-                // separator so we should use the same
-                // in this logic here
-                if 
(newEndpointPath.endsWith(String.valueOf(File.separatorChar))) {
-                    newFileName = StringHelper.after(newFileName, 
newEndpointPath);
-                } else {
-                    newFileName = StringHelper.after(newFileName, 
newEndpointPath + File.separatorChar);
-                }
+                newFileName = clipFileName(newEndpointPath, newFileName);
 
                 // reconstruct file with clipped name
                 file = new File(newFileName);
@@ -264,13 +264,30 @@ public class GenericFile<T> implements WrappedFile<T> {
         }
 
         if (LOG.isTraceEnabled()) {
-            LOG.trace("FileNameOnly: {}", getFileNameOnly());
-            LOG.trace("FileName: {}", getFileName());
-            LOG.trace("Absolute: {}", isAbsolute());
-            LOG.trace("Relative path: {}", getRelativeFilePath());
-            LOG.trace("Absolute path: {}", getAbsoluteFilePath());
-            LOG.trace("Name changed to: {}", this);
+            logFileProperties();
+        }
+    }
+
+    private void logFileProperties() {
+        LOG.trace("FileNameOnly: {}", getFileNameOnly());
+        LOG.trace("FileName: {}", getFileName());
+        LOG.trace("Absolute: {}", isAbsolute());
+        LOG.trace("Relative path: {}", getRelativeFilePath());
+        LOG.trace("Absolute path: {}", getAbsoluteFilePath());
+        LOG.trace("Name changed to: {}", this);
+    }
+
+    private static String clipFileName(String newEndpointPath, String 
newFileName) {
+        // clip starting endpoint in case it was added
+        // use File.separatorChar as the normalizePath uses this as path
+        // separator so we should use the same
+        // in this logic here
+        if (newEndpointPath.endsWith(String.valueOf(File.separatorChar))) {
+            newFileName = StringHelper.after(newFileName, newEndpointPath);
+        } else {
+            newFileName = StringHelper.after(newFileName, newEndpointPath + 
File.separatorChar);
         }
+        return newFileName;
     }
 
     public String getRelativeFilePath() {

Reply via email to