Author: davsclaus
Date: Thu Apr 26 06:51:07 2012
New Revision: 1330670
URL: http://svn.apache.org/viewvc?rev=1330670&view=rev
Log:
CAMEL-5222: The file consumer should use the charset encoding when writing the
file if configured.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java
- copied, changed from r1330211,
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConvertBodyToTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeCharsetTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerMoveFailureOnCompletionTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
Thu Apr 26 06:51:07 2012
@@ -78,7 +78,7 @@ public class FileConsumer extends Generi
}
// creates a generic file
- GenericFile<File> gf = asGenericFile(endpointPath, file);
+ GenericFile<File> gf = asGenericFile(endpointPath, file,
getEndpoint().getCharset());
if (file.isDirectory()) {
if (endpoint.isRecursive() && isValidFile(gf, true) && depth <
endpoint.getMaxDepth()) {
@@ -116,11 +116,12 @@ public class FileConsumer extends Generi
* @param file the source file
* @return wrapped as a GenericFile
*/
- public static GenericFile<File> asGenericFile(String endpointPath, File
file) {
+ public static GenericFile<File> asGenericFile(String endpointPath, File
file, String charset) {
GenericFile<File> answer = new GenericFile<File>();
// use file specific binding
answer.setBinding(new FileBinding());
+ answer.setCharset(charset);
answer.setEndpointPath(endpointPath);
answer.setFile(file);
answer.setFileNameOnly(file.getName());
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
Thu Apr 26 06:51:07 2012
@@ -102,7 +102,7 @@ public class FileEndpoint extends Generi
}
public Exchange createExchange(GenericFile<File> file) {
- Exchange exchange = createExchange();
+ Exchange exchange = createExchange();
if (file != null) {
file.bindToExchange(exchange);
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
Thu Apr 26 06:51:07 2012
@@ -178,11 +178,8 @@ public class FileOperations implements G
// 3. write stream to file
try {
- // determine charset, exchange property overrides endpoint
configuration
- String charset = IOHelper.getCharsetName(exchange, false);
- if (charset == null) {
- charset = endpoint.getCharset();
- }
+ // is there an explicit charset configured we must write the file
as
+ String charset = endpoint.getCharset();
// we can optimize and use file based if no charset must be used,
and the input body is a file
File source = null;
@@ -279,7 +276,7 @@ public class FileOperations implements G
FileChannel out = null;
try {
out = prepareOutputFileChannel(target, out);
- LOG.trace("Using FileChannel to transfer from: {} to: {}", in,
out);
+ LOG.debug("Using FileChannel to write file: {}", target);
long size = in.size();
long position = 0;
while (position < size) {
@@ -297,7 +294,7 @@ public class FileOperations implements G
FileChannel out = null;
try {
out = prepareOutputFileChannel(target, out);
- LOG.trace("Using InputStream to transfer from: {} to: {}", in,
out);
+ LOG.debug("Using InputStream to write file: {}", target);
int size = endpoint.getBufferSize();
byte[] buffer = new byte[size];
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
@@ -321,7 +318,7 @@ public class FileOperations implements G
boolean append = endpoint.getFileExist() == GenericFileExist.Append;
Writer out = IOConverter.toWriter(target, append, charset);
try {
- LOG.trace("Using Reader to transfer from: {} to: {} with charset:
{}", new Object[]{in, out, charset});
+ LOG.debug("Using Reader to write file: {} with charset: {}",
target, charset);
int size = endpoint.getBufferSize();
IOHelper.copy(in, out, size);
} finally {
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
Thu Apr 26 06:51:07 2012
@@ -45,6 +45,7 @@ public class GenericFile<T> implements W
private GenericFileBinding<T> binding;
private boolean absolute;
private boolean directory;
+ private String charset;
public char getFileSeparator() {
return File.separatorChar;
@@ -76,6 +77,7 @@ public class GenericFile<T> implements W
result.setFile(source.getFile());
result.setBody(source.getBody());
result.setBinding(source.getBinding());
+ result.setCharset(source.getCharset());
copyFromPopulateAdditional(source, result);
return result;
@@ -253,9 +255,14 @@ public class GenericFile<T> implements W
this.lastModified = lastModified;
}
- /* (non-Javadoc)
- * @see org.apache.camel.component.file.WrappedFile#getFile()
- */
+ public String getCharset() {
+ return charset;
+ }
+
+ public void setCharset(String charset) {
+ this.charset = charset;
+ }
+
@Override
public T getFile() {
return file;
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
Thu Apr 26 06:51:07 2012
@@ -16,9 +16,11 @@
*/
package org.apache.camel.component.file;
+import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.Reader;
import java.io.Serializable;
import org.apache.camel.Converter;
@@ -26,7 +28,10 @@ import org.apache.camel.Exchange;
import org.apache.camel.FallbackConverter;
import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.TypeConverter;
+import org.apache.camel.converter.IOConverter;
import org.apache.camel.spi.TypeConverterRegistry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* A set of converter methods for working with generic file types
@@ -34,12 +39,16 @@ import org.apache.camel.spi.TypeConverte
@Converter
public final class GenericFileConverter {
+ private static final Logger LOG =
LoggerFactory.getLogger(GenericFileConverter.class);
+
private GenericFileConverter() {
// Helper Class
}
@FallbackConverter
- public static Object convertTo(Class<?> type, Exchange exchange, Object
value, TypeConverterRegistry registry) {
+ public static Object convertTo(Class<?> type, Exchange exchange, Object
value, TypeConverterRegistry registry)
+ throws IOException, NoTypeConversionAvailableException {
+
// use a fallback type converter so we can convert the embedded body
if the value is GenericFile
if (GenericFile.class.isAssignableFrom(value.getClass())) {
@@ -55,6 +64,36 @@ public final class GenericFileConverter
TypeConverter tc = registry.lookup(type, from);
if (tc != null) {
Object body = file.getBody();
+ // if its a file and we have a charset then use a reader to
ensure we read the content using the given charset
+ // this is a bit complicated, but a file consumer can be
configured with an explicit charset, which means
+ // we should read the file content with that given charset,
and ignore any other charset properties
+
+ // if the desired type is InputStream or Reader we can use the
optimized methods
+ if (Reader.class.isAssignableFrom(type)) {
+ Reader reader = genericFileToReader(file, exchange);
+ if (reader != null) {
+ return reader;
+ }
+ }
+ if (InputStream.class.isAssignableFrom(type)) {
+ InputStream is = genericFileToInputStream(file, exchange);
+ if (is != null) {
+ return is;
+ }
+ }
+
+ // okay if the file has a charset configured then we must try
to load the file using that charset
+ // which mean we have to use the Reader first, and then
convert from there
+ if (body instanceof File && file.getCharset() != null) {
+ Reader reader = genericFileToReader(file, exchange);
+ // we dont want a reader back, so use the type converter
registry to find a suitable converter
+ TypeConverter readerTc = registry.lookup(type,
Reader.class);
+ if (readerTc != null) {
+ // use the reader based type converter
+ return readerTc.convertTo(type, exchange, reader);
+ }
+ }
+ // fallback and use the type suitable type converter
return tc.convertTo(type, exchange, body);
}
}
@@ -64,15 +103,22 @@ public final class GenericFileConverter
@Converter
public static InputStream genericFileToInputStream(GenericFile<?> file,
Exchange exchange) throws IOException, NoTypeConversionAvailableException {
- if (exchange != null) {
- if (file.getFile() instanceof java.io.File) {
- // prefer to use a file input stream if its a java.io.File
(must use type converter to take care of encoding)
- File f = (File) file.getFile();
- InputStream is =
exchange.getContext().getTypeConverter().convertTo(InputStream.class, exchange,
f);
- if (is != null) {
- return is;
+ if (file.getFile() instanceof File) {
+ // prefer to use a file input stream if its a java.io.File
+ File f = (File) file.getFile();
+ // the file must exists
+ if (f.exists()) {
+ // read the file using the specified charset
+ String charset = file.getCharset();
+ if (charset != null) {
+ LOG.debug("Read file {} with charset {}", f,
file.getCharset());
+ } else {
+ LOG.debug("Read file {} (no charset)", f);
}
+ return IOConverter.toInputStream(f, charset);
}
+ }
+ if (exchange != null) {
// otherwise ensure the body is loaded as we want the input stream
of the body
file.getBinding().loadContent(exchange, file);
return
exchange.getContext().getTypeConverter().convertTo(InputStream.class, exchange,
file.getBody());
@@ -83,9 +129,14 @@ public final class GenericFileConverter
}
@Converter
- public static String genericFileToString(GenericFile<?> file, Exchange
exchange) throws IOException {
+ public static String genericFileToString(GenericFile<?> file, Exchange
exchange) throws IOException, NoTypeConversionAvailableException {
+ // use reader first as it supports the file charset
+ BufferedReader reader = genericFileToReader(file, exchange);
+ if (reader != null) {
+ return IOConverter.toString(reader);
+ }
if (exchange != null) {
- // ensure the body is loaded as we do not want a toString of
java.io.File handle returned, but the file content
+ // otherwise ensure the body is loaded as we want the content of
the body
file.getBinding().loadContent(exchange, file);
return
exchange.getContext().getTypeConverter().convertTo(String.class, exchange,
file.getBody());
} else {
@@ -95,14 +146,36 @@ public final class GenericFileConverter
}
@Converter
- public static Serializable genericFileToSerializable(GenericFile<?> file,
Exchange exchange) throws IOException {
+ public static Serializable genericFileToSerializable(GenericFile<?> file,
Exchange exchange) throws IOException, NoTypeConversionAvailableException {
if (exchange != null) {
- // ensure the body is loaded as we do not want a java.io.File
handle returned, but the file content
- file.getBinding().loadContent(exchange, file);
- return
exchange.getContext().getTypeConverter().convertTo(Serializable.class,
exchange, file.getBody());
- } else {
- // should revert to fallback converter if we don't have an exchange
- return null;
+ // load the file using input stream
+ InputStream is = genericFileToInputStream(file, exchange);
+ if (is != null) {
+ return
exchange.getContext().getTypeConverter().convertTo(Serializable.class,
exchange, is);
+ }
}
+ // should revert to fallback converter if we don't have an exchange
+ return null;
+ }
+
+ private static BufferedReader genericFileToReader(GenericFile<?> file,
Exchange exchange) throws IOException, NoTypeConversionAvailableException {
+ if (file.getFile() instanceof File) {
+ // prefer to use a file input stream if its a java.io.File
+ File f = (File) file.getFile();
+ // the file must exists
+ if (!f.exists()) {
+ return null;
+ }
+ // and use the charset if the file was explicit configured with a
charset
+ String charset = file.getCharset();
+ if (charset != null) {
+ LOG.debug("Read file {} with charset {}", f,
file.getCharset());
+ return IOConverter.toReader(f, charset);
+ } else {
+ LOG.debug("Read file {} (no charset)", f);
+ return IOConverter.toReader(f, exchange);
+ }
+ }
+ return null;
}
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
Thu Apr 26 06:51:07 2012
@@ -73,17 +73,35 @@ public final class IOConverter {
return IOHelper.buffered(new FileInputStream(file));
}
+ public static InputStream toInputStream(File file, String charset) throws
IOException {
+ if (charset != null) {
+ final BufferedReader reader = toReader(file, charset);
+ return new InputStream() {
+ @Override
+ public int read() throws IOException {
+ return reader.read();
+ }
+ };
+ } else {
+ return IOHelper.buffered(new FileInputStream(file));
+ }
+ }
+
/**
* @deprecated will be removed in Camel 3.0. Use the method which has 2
parameters.
*/
@Deprecated
public static BufferedReader toReader(File file) throws IOException {
- return toReader(file, null);
+ return toReader(file, (String) null);
}
@Converter
public static BufferedReader toReader(File file, Exchange exchange) throws
IOException {
- return IOHelper.buffered(new EncodingFileReader(file,
IOHelper.getCharsetName(exchange)));
+ return toReader(file, IOHelper.getCharsetName(exchange));
+ }
+
+ public static BufferedReader toReader(File file, String charset) throws
IOException {
+ return IOHelper.buffered(new EncodingFileReader(file, charset));
}
@Converter
@@ -285,7 +303,8 @@ public final class IOConverter {
@Converter
public static byte[] toByteArray(BufferedReader reader, Exchange exchange)
throws IOException {
- return toByteArray(toString(reader), exchange);
+ String s = toString(reader);
+ return toByteArray(s, exchange);
}
/**
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ConvertBodyProcessor.java
Thu Apr 26 06:51:07 2012
@@ -60,6 +60,8 @@ public class ConvertBodyProcessor extend
}
if (charset != null) {
+ // override existing charset with configured charset as that is
what the user
+ // have explicit configured and expects to be used
exchange.setProperty(Exchange.CHARSET_NAME, charset);
}
// use mandatory conversion
@@ -75,6 +77,12 @@ public class ConvertBodyProcessor extend
} else {
exchange.setIn(msg);
}
+
+ // remove charset when we are done as we should not propagate that,
+ // as that can lead to double converting later on
+ if (charset != null) {
+ exchange.removeProperty(Exchange.CHARSET_NAME);
+ }
}
public Class<?> getType() {
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
Thu Apr 26 06:51:07 2012
@@ -98,7 +98,7 @@ public class FileConfigureTest extends C
assertDirectoryEquals("For uri: " + endpointUri + " the file is
not equal", expectedPath, path);
file = new File(expectedPath +
(expectedPath.endsWith(File.separator) ? "" : File.separator) + EXPECT_FILE);
- GenericFile<File> consumedFile =
FileConsumer.asGenericFile(endpoint.getFile().getPath(), file);
+ GenericFile<File> consumedFile =
FileConsumer.asGenericFile(endpoint.getFile().getPath(), file, null);
assertEquals(EXPECT_FILE, consumedFile.getRelativeFilePath());
}
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeCharsetTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeCharsetTest.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeCharsetTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumeCharsetTest.java
Thu Apr 26 06:51:07 2012
@@ -24,7 +24,7 @@ import org.apache.camel.builder.RouteBui
import org.apache.camel.component.mock.MockEndpoint;
/**
- * Unit test for consuming the same filename only.
+ *
*/
public class FileConsumeCharsetTest extends ContextTestSupport {
@@ -51,7 +51,9 @@ public class FileConsumeCharsetTest exte
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
-
from("file://target/files/?fileName=report.txt&delete=true&charset=UTF-8").convertBodyTo(String.class).to("mock:result");
+
from("file://target/files/?fileName=report.txt&delete=true&charset=UTF-8")
+ .convertBodyTo(String.class)
+ .to("mock:result");
}
};
}
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerMoveFailureOnCompletionTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerMoveFailureOnCompletionTest.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerMoveFailureOnCompletionTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerMoveFailureOnCompletionTest.java
Thu Apr 26 06:51:07 2012
@@ -38,7 +38,7 @@ public class FileConsumerMoveFailureOnCo
mock.expectedMessageCount(0);
mock.expectedFileExists("target/failed/error/bye-error.txt", "Kabom");
- getMockEndpoint("mock:failed").expectedBodiesReceived("Kabom");
+ getMockEndpoint("mock:failed").expectedMessageCount(1);
template.sendBodyAndHeader("file://target/failed", "Kabom",
Exchange.FILE_NAME, "bye.txt");
@@ -51,7 +51,7 @@ public class FileConsumerMoveFailureOnCo
mock.expectedFileExists("target/failed/.camel/hello.txt", "Hello
World");
mock.expectedFileExists("target/failed/error/bye-error.txt", "Kabom");
- getMockEndpoint("mock:failed").expectedBodiesReceived("Kabom");
+ getMockEndpoint("mock:failed").expectedMessageCount(1);
template.sendBodyAndHeader("file://target/failed", "Hello World",
Exchange.FILE_NAME, "hello.txt");
template.sendBodyAndHeader("file://target/failed", "Kabom",
Exchange.FILE_NAME, "bye.txt");
Copied:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java
(from r1330211,
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOTest.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOTest.java&r1=1330211&r2=1330670&rev=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java
Thu Apr 26 06:51:07 2012
@@ -29,7 +29,7 @@ import org.apache.camel.util.IOHelper;
/**
*
*/
-public class FileProducerCharsetUTFtoISOTest extends ContextTestSupport {
+public class FileProducerCharsetUTFtoISOConfiguredTest extends
ContextTestSupport {
private byte[] utf;
private byte[] iso;
@@ -89,7 +89,7 @@ public class FileProducerCharsetUTFtoISO
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("file:target/charset/input?noop=true")
+ from("file:target/charset/input?charset=utf-8&noop=true")
.to("file:target/charset/?fileName=output.txt&charset=iso-8859-1");
}
};
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConvertBodyToTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConvertBodyToTest.java?rev=1330670&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConvertBodyToTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConvertBodyToTest.java
Thu Apr 26 06:51:07 2012
@@ -0,0 +1,108 @@
+/**
+ * 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.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.util.IOHelper;
+
+/**
+ *
+ */
+public class FileProducerCharsetUTFtoISOConvertBodyToTest extends
ContextTestSupport {
+
+ private byte[] utf;
+ private byte[] iso;
+
+ @Override
+ protected void setUp() throws Exception {
+ // use utf-8 as original payload with 00e6 which is a danish ae letter
+ utf = "ABC\u00e6".getBytes("utf-8");
+ iso = "ABC\u00e6".getBytes("iso-8859-1");
+
+ deleteDirectory("target/charset");
+ createDirectory("target/charset/input");
+
+ log.debug("utf: {}", new String(utf, Charset.forName("utf-8")));
+ log.debug("iso: {}", new String(iso, Charset.forName("iso-8859-1")));
+
+ for (byte b : utf) {
+ log.debug("utf byte: {}", b);
+ }
+ for (byte b : iso) {
+ log.debug("iso byte: {}", b);
+ }
+
+ // write the byte array to a file using plain API
+ FileOutputStream fos = new
FileOutputStream("target/charset/input/input.txt");
+ fos.write(utf);
+ fos.close();
+
+ super.setUp();
+ }
+
+ public void testFileProducerCharsetUTFtoISOConvertBodyTo() throws
Exception {
+ oneExchangeDone.matchesMockWaitTime();
+
+ File file = new File("target/charset/output.txt").getAbsoluteFile();
+ assertTrue("File should exist", file.exists());
+
+ InputStream fis = IOHelper.buffered(new FileInputStream(file));
+ byte[] buffer = new byte[100];
+
+ int len = fis.read(buffer);
+ assertTrue("Should read data: " + len, len != -1);
+ byte[] data = new byte[len];
+ System.arraycopy(buffer, 0, data, 0, len);
+ fis.close();
+
+
+ for (byte b : data) {
+ log.info("loaded byte: {}", b);
+ }
+
+ // data should be in iso, where the danish ae is -26
+ assertEquals(4, data.length);
+ assertEquals(65, data[0]);
+ assertEquals(66, data[1]);
+ assertEquals(67, data[2]);
+ assertEquals(-26, data[3]);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // the input file is in utf-8
+ from("file:target/charset/input?noop=true&charset=utf-8")
+ // now convert the input file from utf-8 to iso-8859-1
+ .convertBodyTo(byte[].class, "iso-8859-1")
+ // and write the file using that encoding
+ .setProperty(Exchange.CHARSET_NAME,
header("someCharsetHeader"))
+ .to("file:target/charset/?fileName=output.txt");
+ }
+ };
+ }
+}
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
Thu Apr 26 06:51:07 2012
@@ -165,7 +165,7 @@ public class FileLanguageTest extends La
// get the file handle
file = new File("target/filelanguage/test/hello.txt");
- GenericFile<File> gf =
FileConsumer.asGenericFile("target/filelanguage", file);
+ GenericFile<File> gf =
FileConsumer.asGenericFile("target/filelanguage", file, null);
FileEndpoint endpoint = getMandatoryEndpoint(uri, FileEndpoint.class);
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java?rev=1330670&r1=1330669&r2=1330670&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ConvertBodyTest.java
Thu Apr 26 06:51:07 2012
@@ -52,7 +52,8 @@ public class ConvertBodyTest extends Con
});
getMockEndpoint("mock:foo").expectedMessageCount(1);
-
getMockEndpoint("mock:foo").expectedPropertyReceived(Exchange.CHARSET_NAME,
"iso-8859-1");
+ // do not propagate charset to avoid side effects with double
conversion etc
+
getMockEndpoint("mock:foo").message(0).property(Exchange.CHARSET_NAME).isNull();
template.sendBody("direct:foo", "Hello World");