CAMEL-8357 Add JDK 7 Mime/Content Type Support to Camel File endpoint
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a4c11942 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a4c11942 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a4c11942 Branch: refs/heads/master Commit: a4c11942f71b12d2da1e1df0aaaa0d5345f49d24 Parents: 4fcf62b Author: Willem Jiang <willem.ji...@gmail.com> Authored: Sun Feb 15 12:27:05 2015 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Sun Feb 15 12:27:05 2015 +0800 ---------------------------------------------------------------------- .../main/java/org/apache/camel/Exchange.java | 1 + .../camel/component/file/GenericFile.java | 13 ++++++++ .../component/file/GenericFileMessageTest.java | 14 +++++++++ .../component/file/MyFileTypeDetector.java | 31 ++++++++++++++++++++ .../services/java.nio.file.spi.FileTypeDetector | 18 ++++++++++++ 5 files changed, 77 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a4c11942/camel-core/src/main/java/org/apache/camel/Exchange.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/Exchange.java b/camel-core/src/main/java/org/apache/camel/Exchange.java index a9dbf18..b773e93 100644 --- a/camel-core/src/main/java/org/apache/camel/Exchange.java +++ b/camel-core/src/main/java/org/apache/camel/Exchange.java @@ -117,6 +117,7 @@ public interface Exchange { String FAILURE_ENDPOINT = "CamelFailureEndpoint"; String FAILURE_ROUTE_ID = "CamelFailureRouteId"; String FILTER_NON_XML_CHARS = "CamelFilterNonXmlChars"; + String FILE_CONTENT_TYPE = "CamelFileContentType"; String FILE_LOCAL_WORK_PATH = "CamelFileLocalWorkPath"; String FILE_NAME = "CamelFileName"; String FILE_NAME_ONLY = "CamelFileNameOnly"; http://git-wip-us.apache.org/repos/asf/camel/blob/a4c11942/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java index 5c36320..0394fd8 100644 --- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java +++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java @@ -17,6 +17,8 @@ package org.apache.camel.component.file; import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Map; import org.apache.camel.Exchange; @@ -131,6 +133,17 @@ public class GenericFile<T> implements WrappedFile<T> { message.setHeader(Exchange.FILE_NAME_CONSUMED, getFileName()); message.setHeader("CamelFileAbsolute", isAbsolute()); message.setHeader("CamelFileAbsolutePath", getAbsoluteFilePath()); + + if (file instanceof File) { + File f = (File) file; + Path path = f.toPath(); + try { + message.setHeader(Exchange.FILE_CONTENT_TYPE, Files.probeContentType(path)); + } catch (Exception ex) { + // just ignore the exception + System.out.println(ex); + } + } if (isAbsolute()) { message.setHeader(Exchange.FILE_PATH, getAbsoluteFilePath()); http://git-wip-us.apache.org/repos/asf/camel/blob/a4c11942/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java b/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java index 3b79bf7..5382622 100644 --- a/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/file/GenericFileMessageTest.java @@ -19,6 +19,7 @@ package org.apache.camel.component.file; import java.io.File; import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; import org.apache.camel.util.FileUtil; public class GenericFileMessageTest extends ContextTestSupport { @@ -32,5 +33,18 @@ public class GenericFileMessageTest extends ContextTestSupport { file.setFile(new File("target/test.txt")); message = new GenericFileMessage<File>(file); assertEquals(FileUtil.isWindows() ? "target\\test.txt" : "target/test.txt", message.toString()); + + } + + public void testGenericFileContentType() throws Exception { + GenericFileMessage<File> message = new GenericFileMessage<File>(); + + GenericFile<File> file = new GenericFile<File>(); + file.setEndpointPath("target"); + file.setFileName("target"); + file.setFile(new File("target/camel-core-test.log")); + message = new GenericFileMessage<File>(file); + file.populateHeaders(message); + assertEquals("Get a wrong file content type", "txt", message.getHeader(Exchange.FILE_CONTENT_TYPE)); } } http://git-wip-us.apache.org/repos/asf/camel/blob/a4c11942/camel-core/src/test/java/org/apache/camel/component/file/MyFileTypeDetector.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/file/MyFileTypeDetector.java b/camel-core/src/test/java/org/apache/camel/component/file/MyFileTypeDetector.java new file mode 100644 index 0000000..b53dd3c --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/file/MyFileTypeDetector.java @@ -0,0 +1,31 @@ +/** + * 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 java.io.IOException; +import java.nio.file.Path; +import java.nio.file.spi.FileTypeDetector; + +public class MyFileTypeDetector extends FileTypeDetector { + + @Override + public String probeContentType(Path path) throws IOException { + // Just return a content-type without checking the path + return "txt"; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a4c11942/camel-core/src/test/resources/META-INF/services/java.nio.file.spi.FileTypeDetector ---------------------------------------------------------------------- diff --git a/camel-core/src/test/resources/META-INF/services/java.nio.file.spi.FileTypeDetector b/camel-core/src/test/resources/META-INF/services/java.nio.file.spi.FileTypeDetector new file mode 100644 index 0000000..f27d463 --- /dev/null +++ b/camel-core/src/test/resources/META-INF/services/java.nio.file.spi.FileTypeDetector @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.camel.component.file.MyFileTypeDetector \ No newline at end of file