CAMEL-8191: sftp producer should support charset option as regular ftp producer does. Thanks to Volodymyr Sobotovych for the patch.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/834118dc Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/834118dc Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/834118dc Branch: refs/heads/camel-2.14.x Commit: 834118dce60d59c95f59198740bd1ccf81d27c2a Parents: e7638cd Author: Claus Ibsen <[email protected]> Authored: Mon Jan 26 14:46:57 2015 +0100 Committer: Claus Ibsen <[email protected]> Committed: Mon Jan 26 14:47:31 2015 +0100 ---------------------------------------------------------------------- .../component/file/remote/FtpConsumer.java | 8 +- .../component/file/remote/SftpConsumer.java | 7 +- .../component/file/remote/SftpOperations.java | 12 ++- .../sftp/SftpConsumerWithCharsetTest.java | 79 ++++++++++++++++++++ .../sftp/SftpProducerWithCharsetTest.java | 54 +++++++++++++ 5 files changed, 152 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/834118dc/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java index ba5d32c..f591caa 100644 --- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java +++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java @@ -127,7 +127,7 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> { } if (file.isDirectory()) { - RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file); + RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset()); if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && isValidFile(remote, true, files)) { // recursive scan and add the sub files and folders String subDirectory = file.getName(); @@ -138,7 +138,7 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> { } } } else if (file.isFile()) { - RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file); + RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset()); if (depth >= endpoint.getMinDepth() && isValidFile(remote, false, files)) { // matched file so add fileList.add(remote); @@ -186,9 +186,10 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> { return super.ignoreCannotRetrieveFile(name, exchange, cause); } - private RemoteFile<FTPFile> asRemoteFile(String absolutePath, FTPFile file) { + private RemoteFile<FTPFile> asRemoteFile(String absolutePath, FTPFile file, String charset) { RemoteFile<FTPFile> answer = new RemoteFile<FTPFile>(); + answer.setCharset(charset); answer.setEndpointPath(endpointPath); answer.setFile(file); answer.setFileNameOnly(file.getName()); @@ -221,7 +222,6 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> { // the file name should be the relative path answer.setFileName(answer.getRelativeFilePath()); - answer.setCharset(endpoint.getCharset()); return answer; } http://git-wip-us.apache.org/repos/asf/camel/blob/834118dc/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java index 0f2f536..29c7532 100644 --- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java +++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java @@ -110,7 +110,7 @@ public class SftpConsumer extends RemoteFileConsumer<ChannelSftp.LsEntry> { } if (file.getAttrs().isDir()) { - RemoteFile<ChannelSftp.LsEntry> remote = asRemoteFile(absolutePath, file); + RemoteFile<ChannelSftp.LsEntry> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset()); if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && isValidFile(remote, true, files)) { // recursive scan and add the sub files and folders String subDirectory = file.getFilename(); @@ -123,7 +123,7 @@ public class SftpConsumer extends RemoteFileConsumer<ChannelSftp.LsEntry> { // we cannot use file.getAttrs().isLink on Windows, so we dont invoke the method // just assuming its a file we should poll } else { - RemoteFile<ChannelSftp.LsEntry> remote = asRemoteFile(absolutePath, file); + RemoteFile<ChannelSftp.LsEntry> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset()); if (depth >= endpoint.getMinDepth() && isValidFile(remote, false, files)) { // matched file so add fileList.add(remote); @@ -159,9 +159,10 @@ public class SftpConsumer extends RemoteFileConsumer<ChannelSftp.LsEntry> { return super.ignoreCannotRetrieveFile(name, exchange, cause); } - private RemoteFile<ChannelSftp.LsEntry> asRemoteFile(String absolutePath, ChannelSftp.LsEntry file) { + private RemoteFile<ChannelSftp.LsEntry> asRemoteFile(String absolutePath, ChannelSftp.LsEntry file, String charset) { RemoteFile<ChannelSftp.LsEntry> answer = new RemoteFile<ChannelSftp.LsEntry>(); + answer.setCharset(charset); answer.setEndpointPath(endpointPath); answer.setFile(file); answer.setFileNameOnly(file.getFilename()); http://git-wip-us.apache.org/repos/asf/camel/blob/834118dc/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java ---------------------------------------------------------------------- diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java index daf6db6..2847e35 100644 --- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java +++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java @@ -825,7 +825,15 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry> try { if (is == null) { - is = exchange.getIn().getMandatoryBody(InputStream.class); + String charset = endpoint.getCharset(); + if (charset != null) { + // charset configured so we must convert to the desired + // charset so we can write with encoding + is = new ByteArrayInputStream(exchange.getIn().getMandatoryBody(String.class).getBytes(charset)); + LOG.trace("Using InputStream {} with charset {}.", is, charset); + } else { + is = exchange.getIn().getMandatoryBody(InputStream.class); + } } final StopWatch watch = new StopWatch(); @@ -859,6 +867,8 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry> throw new GenericFileOperationFailedException("Cannot store file: " + name, e); } catch (InvalidPayloadException e) { throw new GenericFileOperationFailedException("Cannot store file: " + name, e); + } catch (UnsupportedEncodingException e) { + throw new GenericFileOperationFailedException("Cannot store file: " + name, e); } finally { IOHelper.close(is, "store: " + name, LOG); } http://git-wip-us.apache.org/repos/asf/camel/blob/834118dc/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpConsumerWithCharsetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpConsumerWithCharsetTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpConsumerWithCharsetTest.java new file mode 100644 index 0000000..551e69f --- /dev/null +++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpConsumerWithCharsetTest.java @@ -0,0 +1,79 @@ +/** + * 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.remote.sftp; + +import java.io.File; +import java.io.IOException; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.file.FileComponent; +import org.apache.camel.component.file.remote.RemoteFile; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.commons.io.FileUtils; +import org.junit.Test; + +public class SftpConsumerWithCharsetTest extends SftpServerTestSupport { + + private static final String SAMPLE_FILE_NAME = String.format("sample-%s.txt", SftpConsumerWithCharsetTest.class.getSimpleName()); + private static final String SAMPLE_FILE_CHARSET = "iso-8859-1"; + private static final String SAMPLE_FILE_PAYLOAD = "\u00e6\u00f8\u00e5 \u00a9"; // danish ae oe aa and (c) sign + + @Test + public void testConsumeWithCharset() throws Exception { + if (!canTest()) { + return; + } + + // prepare sample file to be consumed by SFTP consumer + createSampleFile(); + + // Prepare expectations + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedBodiesReceived(SAMPLE_FILE_PAYLOAD); + + context.startRoute("foo"); + + // Check that expectations are satisfied + assertMockEndpointsSatisfied(); + + // Check that the proper charset was set in the internal object + Exchange exchange = mock.getExchanges().get(0); + RemoteFile<?> file = (RemoteFile<?>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE); + assertNotNull(file); + assertEquals(SAMPLE_FILE_CHARSET, file.getCharset()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR + "?username=admin&password=admin&charset=" + SAMPLE_FILE_CHARSET) + .routeId("foo").noAutoStartup() + .to("mock:result"); + } + }; + } + + private void createSampleFile() throws IOException { + File file = new File(FTP_ROOT_DIR + "/" + SAMPLE_FILE_NAME); + + FileUtils.write(file, SAMPLE_FILE_PAYLOAD, SAMPLE_FILE_CHARSET); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/834118dc/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerWithCharsetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerWithCharsetTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerWithCharsetTest.java new file mode 100644 index 0000000..89e97ff --- /dev/null +++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerWithCharsetTest.java @@ -0,0 +1,54 @@ +/** + * 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.remote.sftp; + +import java.io.File; + +import org.apache.camel.Exchange; +import org.apache.commons.io.FileUtils; +import org.junit.Test; + +public class SftpProducerWithCharsetTest extends SftpServerTestSupport { + + private static final String SAMPLE_FILE_NAME = String.format("sample-%s.txt", SftpProducerWithCharsetTest.class.getSimpleName()); + private static final String SAMPLE_FILE_CHARSET = "iso-8859-1"; + private static final String SAMPLE_FILE_PAYLOAD = "\u00e6\u00f8\u00e5 \u00a9"; // danish ae oe aa and (c) sign + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testProducerWithCharset() throws Exception { + if (!canTest()) { + return; + } + + template.sendBodyAndHeader(getSftpUri(), SAMPLE_FILE_PAYLOAD, Exchange.FILE_NAME, SAMPLE_FILE_NAME); + + File file = new File(FTP_ROOT_DIR + "/" + SAMPLE_FILE_NAME); + assertTrue("The uploaded file should exist", file.exists()); + + String storedPayload = FileUtils.readFileToString(file, SAMPLE_FILE_CHARSET); + assertEquals(SAMPLE_FILE_PAYLOAD, storedPayload); + } + + private String getSftpUri() { + return "sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR + "?username=admin&password=admin&charset=" + SAMPLE_FILE_CHARSET; + } +}
