You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2018/07/04 07:00:31 UTC

[camel] branch camel-2.21.x updated: [CAMEL-12613] Use GenericFile's absolute path when updating file headers as it is updated when the underlying file is moved

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

acosentino pushed a commit to branch camel-2.21.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.21.x by this push:
     new 2447950  [CAMEL-12613] Use GenericFile's absolute path when updating file headers as it is updated when the underlying file is moved
2447950 is described below

commit 2447950d61651ed104cd05b34ba79c7677abcc09
Author: jpoth <po...@gmail.com>
AuthorDate: Mon Jul 2 16:10:04 2018 +0200

    [CAMEL-12613] Use GenericFile's absolute path when updating file headers as it is updated when the underlying file is moved
---
 .../apache/camel/component/file/FileConsumer.java  | 13 ++++-
 .../file/FileConsumerPreMoveLastModifiedTest.java  | 61 ++++++++++++++++++++++
 2 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java b/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
index aa3dcfa..cdbfa92 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
@@ -242,8 +242,12 @@ public class FileConsumer extends GenericFileConsumer<File> {
 
     @Override
     protected void updateFileHeaders(GenericFile<File> file, Message message) {
-        long length = file.getFile().length();
-        long modified = file.getFile().lastModified();
+        File upToDateFile = file.getFile();
+        if (fileHasMoved(file)) {
+            upToDateFile = new File(file.getAbsoluteFilePath());
+        }
+        long length = upToDateFile.length();
+        long modified = upToDateFile.lastModified();
         file.setFileLength(length);
         file.setLastModified(modified);
         if (length >= 0) {
@@ -258,4 +262,9 @@ public class FileConsumer extends GenericFileConsumer<File> {
     public FileEndpoint getEndpoint() {
         return (FileEndpoint) super.getEndpoint();
     }
+
+    private boolean fileHasMoved(GenericFile<File> file) {
+        // GenericFile's absolute path is always up to date whereas the underlying file is not
+        return !file.getFile().getAbsolutePath().equals(file.getAbsoluteFilePath());
+    }
 }
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveLastModifiedTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveLastModifiedTest.java
new file mode 100644
index 0000000..719145d
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveLastModifiedTest.java
@@ -0,0 +1,61 @@
+/**
+ * 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;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+public class FileConsumerPreMoveLastModifiedTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/premove");
+        super.setUp();
+    }
+
+    public void testPreMoveLastModified() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        template.sendBodyAndHeader("file://target/premove", "Hello World", Exchange.FILE_NAME, "hello.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file://target/premove?preMove=work/work-${file:name}&initialDelay=0&delay=10&keepLastModified=true")
+                        .process(new LastModifiedCheckerProcessor())
+                        .log("Got file ${file:name} modified=${file:modified}")
+                        .to("mock:result");
+            }
+        };
+    }
+
+    private static class LastModifiedCheckerProcessor implements Processor {
+
+        public void process(Exchange exchange) throws Exception {
+            assertTrue(exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Long.class) > 0L);
+            assertTrue(exchange.getIn().getHeader(Exchange.FILE_LENGTH, Long.class) > 0L);
+        }
+    }
+}