You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/12/19 16:11:25 UTC

svn commit: r1423871 - in /camel/trunk/components/camel-ftp/src: main/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/

Author: ningjiang
Date: Wed Dec 19 15:11:24 2012
New Revision: 1423871

URL: http://svn.apache.org/viewvc?rev=1423871&view=rev
Log:
CAMEL-5881 FTP endpoints should support charset property like File endpoint does, with thanks to Preben

Added:
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerWithCharsetTest.java
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithCharsetTest.java
Modified:
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java?rev=1423871&r1=1423870&r2=1423871&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java (original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java Wed Dec 19 15:11:24 2012
@@ -184,6 +184,7 @@ public class FtpConsumer extends RemoteF
         // the file name should be the relative path
         answer.setFileName(answer.getRelativeFilePath());
 
+        answer.setCharset(endpoint.getCharset());
         return answer;
     }
 

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java?rev=1423871&r1=1423870&r2=1423871&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java (original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java Wed Dec 19 15:11:24 2012
@@ -516,7 +516,15 @@ public class FtpOperations implements Re
 
         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);
+                }
             }
             if (endpoint.getFileExist() == GenericFileExist.Append) {
                 log.trace("Client appendFile: {}", targetName);

Added: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerWithCharsetTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerWithCharsetTest.java?rev=1423871&view=auto
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerWithCharsetTest.java (added)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerWithCharsetTest.java Wed Dec 19 15:11:24 2012
@@ -0,0 +1,126 @@
+/**
+ * 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;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.FileComponent;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.IOHelper;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FtpConsumerWithCharsetTest extends FtpServerTestSupport {
+
+    private final String payload = "æøå ©";
+
+    private String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/upload?password=admin&charset=iso-8859-1";
+    }
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        byte[] iso = payload.getBytes("iso-8859-1");
+        byte[] utf = payload.getBytes("utf-8");
+
+        log.debug("iso: {}", new String(iso, Charset.forName("iso-8859-1")));
+        log.debug("utf: {}", new String(utf, Charset.forName("utf-8")));
+
+        for (byte b : iso) {
+            log.debug("iso byte: {}", b);
+        }
+
+        for (byte b : utf) {
+            log.debug("utf byte: {}", b);
+        }
+
+        prepareFtpServer();
+        // Check that the payload exists in upload and is in iso charset.ß
+        File file = new File(FTP_ROOT_DIR + "/upload/iso.txt");
+        assertTrue("The uploaded file should exists", file.exists());
+
+        // Lets also test byte wise
+        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();
+
+        // data should be in iso, where the danish ae is -26, oe is -8 aa is -27
+        // and copyright is -87
+        assertEquals(5, data.length);
+        assertEquals(-26, data[0]);
+        assertEquals(-8, data[1]);
+        assertEquals(-27, data[2]);
+        assertEquals(32, data[3]);
+        assertEquals(-87, data[4]);
+    }
+
+
+    @Test
+    public void testConsumerWithCharset() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived(payload);
+        assertMockEndpointsSatisfied();
+
+        Exchange exchange = mock.getExchanges().get(0);
+        RemoteFile<?> file = (RemoteFile<?>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
+        assertNotNull(file);
+        assertEquals("iso-8859-1", file.getCharset());
+        // The String will be encoded with UTF-8 by default
+        byte[] data = exchange.getIn().getBody(String.class).getBytes("UTF-8");
+        // data should be in iso, where the danish ae is -61 -90, oe is -61 -72
+        // aa is -61 -91
+        // and copyright is -62 -87
+        assertEquals(9, data.length);
+        assertEquals(-61, data[0]);
+        assertEquals(-90, data[1]);
+
+        assertEquals(-61, data[2]);
+        assertEquals(-72, data[3]);
+
+        assertEquals(-61, data[4]);
+        assertEquals(-91, data[5]);
+
+        assertEquals(32, data[6]);
+
+        assertEquals(-62, data[7]);
+        assertEquals(-87, data[8]);
+    }
+    
+    private void prepareFtpServer() throws Exception {
+        sendFile(getFtpUrl(), payload, "iso.txt");
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(getFtpUrl()).to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

Added: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithCharsetTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithCharsetTest.java?rev=1423871&view=auto
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithCharsetTest.java (added)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithCharsetTest.java Wed Dec 19 15:11:24 2012
@@ -0,0 +1,80 @@
+/**
+ * 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;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+
+import org.apache.camel.converter.IOConverter;
+import org.apache.camel.util.IOHelper;
+import org.junit.Test;
+
+public class FtpProducerFileWithCharsetTest extends FtpServerTestSupport {
+    private String payload = "æøå ©";
+
+    private String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/upload?charset=iso-8859-1&password=admin";
+    }
+
+    @Override
+    public void setUp() throws Exception {
+        byte[] utf = payload.getBytes("utf-8");
+        byte[] iso = payload.getBytes("iso-8859-1");
+
+        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);
+        }
+        super.setUp();
+    }
+
+    @Test
+    public void testProducerWithCharset() throws Exception {
+        sendFile(getFtpUrl(), payload, "charset/iso.txt");
+
+        File file = new File(FTP_ROOT_DIR + "/upload/charset/iso.txt");
+        assertTrue("The uploaded file should exists", file.exists());
+        String fileContent = new String(IOConverter.toByteArray(file), "iso-8859-1");
+        assertEquals(fileContent, payload);
+
+        // Lets also test byte wise
+        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();
+
+        // data should be in iso, where the danish ae is -26, oe is -8 aa is -27
+        // and copyright is -87
+        assertEquals(5, data.length);
+        assertEquals(-26, data[0]);
+        assertEquals(-8, data[1]);
+        assertEquals(-27, data[2]);
+        assertEquals(32, data[3]);
+        assertEquals(-87, data[4]);
+    }
+}
\ No newline at end of file



Re: svn commit: r1423871 - in /camel/trunk/components/camel-ftp/src: main/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/

Posted by Willem jiang <wi...@gmail.com>.
I just checked the camel maven pom, it already unify the encoding of the with the below properties
<!-- unify the encoding for all the modules -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

But it the change could avoid some complains from IDE which doesn't use UTF-8 encoding by default.

--  
Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem




On Thursday, December 20, 2012 at 4:56 AM, Preben.Asmussen wrote:

> Sorry my bad.  
> Just replace
> private String payload = "æøå ©";
> with
> // danish letter ae, oe, aa space and copyright
> private final String payload = "\u00e6\u00f8\u00e5 \u00a9";
>  
> in  
> org.apache.camel.component.file.remote.FtpProducerFileWithCharsetTest
> and
> org.apache.camel.component.file.remote.FtpConsumerWithCharsetTest
>  
> Actually i thought maven was set up to use utf-8 source code encoding
>  
> /Preben  
>  
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/Re-svn-commit-r1423871-in-camel-trunk-components-camel-ftp-src-main-java-org-apache-camel-component--tp5724380p5724398.html
> Sent from the Camel Development mailing list archive at Nabble.com (http://Nabble.com).




Re: svn commit: r1423871 - in /camel/trunk/components/camel-ftp/src: main/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/

Posted by "Preben.Asmussen" <pr...@dr.dk>.
Sorry my bad. 
Just replace
   private String payload = "æøå ©";
with
    // danish letter ae, oe, aa space and copyright
    private final String payload = "\u00e6\u00f8\u00e5 \u00a9";

in 
org.apache.camel.component.file.remote.FtpProducerFileWithCharsetTest
and
org.apache.camel.component.file.remote.FtpConsumerWithCharsetTest

Actually i thought maven was set up to use utf-8 source code encoding

/Preben 




--
View this message in context: http://camel.465427.n5.nabble.com/Re-svn-commit-r1423871-in-camel-trunk-components-camel-ftp-src-main-java-org-apache-camel-component--tp5724380p5724398.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: svn commit: r1423871 - in /camel/trunk/components/camel-ftp/src: main/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/

Posted by Claus Ibsen <cl...@gmail.com>.
Hi Willem

When you have foreign chars in source code, then you should unicode
escape them with \uFFFF.
Otherwise the code may not compile all over the world. eg the source
code should just be ASCII chars.



On Wed, Dec 19, 2012 at 4:11 PM,  <ni...@apache.org> wrote:
> private final String payload = "æøå ©";



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen