Repository: camel
Updated Branches:
  refs/heads/master 500da4886 -> 6c66df26f


CAMEL-11015 Encoding issues in camel-salesforce...

...-maven-plugin

This changes the way writers are created in the Mojo to explicitly set
the encoding to `UTF-8`.

Can be reproduced by setting the `LANG` environment variable to `en_US`
(on my system it was set to `en_US.UTF8`.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/6c66df26
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6c66df26
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6c66df26

Branch: refs/heads/master
Commit: 6c66df26fc3783d79da1acc3899bb67649af0682
Parents: 500da48
Author: Zoran Regvart <zregv...@apache.org>
Authored: Wed Mar 15 11:10:35 2017 +0100
Committer: Zoran Regvart <zregv...@apache.org>
Committed: Wed Mar 15 11:13:23 2017 +0100

----------------------------------------------------------------------
 .../src/it/simple-it/pom.xml                    |   9 --
 .../apache/camel/maven/CamelSalesforceMojo.java | 154 ++++++++-----------
 .../maven/CamelSalesforceMojoOutputTest.java    |  14 +-
 3 files changed, 69 insertions(+), 108 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/6c66df26/components/camel-salesforce/camel-salesforce-maven-plugin/src/it/simple-it/pom.xml
----------------------------------------------------------------------
diff --git 
a/components/camel-salesforce/camel-salesforce-maven-plugin/src/it/simple-it/pom.xml
 
b/components/camel-salesforce/camel-salesforce-maven-plugin/src/it/simple-it/pom.xml
index b025aad..934f895 100644
--- 
a/components/camel-salesforce/camel-salesforce-maven-plugin/src/it/simple-it/pom.xml
+++ 
b/components/camel-salesforce/camel-salesforce-maven-plugin/src/it/simple-it/pom.xml
@@ -66,15 +66,6 @@
                 <maxConnectionsPerAddress>10</maxConnectionsPerAddress>
                 <removeIdleDestinations>true</removeIdleDestinations>
               </httpClientProperties>
-              <sslContextParameters>
-                <clientParameters>
-                    <cipherSuitesFilter>
-                      <exclude>
-                        <exclude>SSL.*</exclude>
-                      </exclude>
-                    </cipherSuitesFilter>
-                </clientParameters>
-              </sslContextParameters>
               <includePattern>(.*__c)</includePattern>
               <!-- Salesforce login info -->
               <clientId>${clientId}</clientId>

http://git-wip-us.apache.org/repos/asf/camel/blob/6c66df26/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
----------------------------------------------------------------------
diff --git 
a/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
 
b/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
index 33aceba..38ec8ab 100644
--- 
a/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
+++ 
b/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
@@ -16,12 +16,14 @@
  */
 package org.apache.camel.maven;
 
-import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.lang.reflect.Field;
 import java.net.URI;
+import java.nio.charset.StandardCharsets;
 import java.security.GeneralSecurityException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -91,6 +93,8 @@ import org.eclipse.jetty.util.ssl.SslContextFactory;
 @Mojo(name = "generate", requiresProject = false, defaultPhase = 
LifecyclePhase.GENERATE_SOURCES)
 public class CamelSalesforceMojo extends AbstractMojo {
 
+    private static final String UTF_8 = "UTF-8";
+
     // default connect and call timeout
     protected static final int DEFAULT_TIMEOUT = 60000;
 
@@ -395,7 +399,11 @@ public class CamelSalesforceMojo extends AbstractMojo {
             // should we provide a flag to control timestamp generation?
             final String generatedDate = new Date().toString();
             for (SObjectDescription description : descriptions) {
-                processDescription(pkgDir, description, utility, 
generatedDate);
+                try {
+                    processDescription(pkgDir, description, utility, 
generatedDate);
+                } catch (IOException e) {
+                    throw new MojoExecutionException("Unable to generate 
source files for: " + description.getName(), e);
+                }
             }
             getLog().info(String.format("Successfully generated %s Java 
Classes", descriptions.size() * 2));
 
@@ -581,104 +589,64 @@ public class CamelSalesforceMojo extends AbstractMojo {
         return httpClient;
     }
 
-    void processDescription(File pkgDir, SObjectDescription description, 
GeneratorUtility utility, String generatedDate) throws MojoExecutionException {
+    void processDescription(File pkgDir, SObjectDescription description, 
GeneratorUtility utility, String generatedDate) throws IOException {
         // generate a source file for SObject
-        String fileName = description.getName() + JAVA_EXT;
-        BufferedWriter writer = null;
-        try {
-            File pojoFile = new File(pkgDir, fileName);
-            writer = new BufferedWriter(new FileWriter(pojoFile));
-
-            VelocityContext context = new VelocityContext();
-            context.put("packageName", packageName);
-            context.put("utility", utility);
-            context.put("esc", StringEscapeUtils.class);
-            context.put("desc", description);
-            context.put("generatedDate", generatedDate);
-            context.put("useStringsForPicklists", useStringsForPicklists);
-
-            Template pojoTemplate;
-            pojoTemplate = engine.getTemplate(SOBJECT_POJO_VM);
+        final VelocityContext context = new VelocityContext();
+        context.put("packageName", packageName);
+        context.put("utility", utility);
+        context.put("esc", StringEscapeUtils.class);
+        context.put("desc", description);
+        context.put("generatedDate", generatedDate);
+        context.put("useStringsForPicklists", useStringsForPicklists);
+
+        final String pojoFileName = description.getName() + JAVA_EXT;
+        final File pojoFile = new File(pkgDir, pojoFileName);
+        try (final Writer writer = new OutputStreamWriter(new 
FileOutputStream(pojoFile), StandardCharsets.UTF_8)) {
+            final Template pojoTemplate = engine.getTemplate(SOBJECT_POJO_VM, 
UTF_8);
             pojoTemplate.merge(context, writer);
-            // close pojoFile
-            writer.close();
-
-            if (useOptionals) {
-                fileName = description.getName() + "Optional" + JAVA_EXT;
-                pojoTemplate = engine.getTemplate(SOBJECT_POJO_OPTIONAL_VM);
-                pojoFile = new File(pkgDir, fileName);
-                writer = new BufferedWriter(new FileWriter(pojoFile));
-                pojoTemplate.merge(context, writer);
-                // close pojoFile
-                writer.close();
-            }
-            // write required Enumerations for any picklists
-            for (SObjectField field : description.getFields()) {
-                if (utility.isPicklist(field) || 
utility.isMultiSelectPicklist(field)) {
-                    String enumName = description.getName() + "_" + 
utility.enumTypeName(field.getName());
-                    fileName = enumName + JAVA_EXT;
-                    File enumFile = new File(pkgDir, fileName);
-                    writer = new BufferedWriter(new FileWriter(enumFile));
-
-                    context = new VelocityContext();
-                    context.put("packageName", packageName);
-                    context.put("utility", utility);
-                    context.put("esc", StringEscapeUtils.class);
-                    context.put("field", field);
-                    context.put("enumName", enumName);
-                    context.put("generatedDate", generatedDate);
-
-                    Template queryTemplate = 
engine.getTemplate(SOBJECT_PICKLIST_VM);
-                    queryTemplate.merge(context, writer);
-
-                    // close Enum file
-                    writer.close();
-                }
-            }
-
-            // write the QueryRecords class
-            fileName = "QueryRecords" + description.getName() + JAVA_EXT;
-            File queryFile = new File(pkgDir, fileName);
-            writer = new BufferedWriter(new FileWriter(queryFile));
-
-            context = new VelocityContext();
-            context.put("packageName", packageName);
-            context.put("desc", description);
-            context.put("generatedDate", generatedDate);
-
-            Template queryTemplate = 
engine.getTemplate(SOBJECT_QUERY_RECORDS_VM);
-            queryTemplate.merge(context, writer);
-
-            // close QueryRecords file
-            writer.close();
+        }
 
-            if (useOptionals) {
-                // write the QueryRecords Optional class
-                fileName = "QueryRecords" + description.getName() + "Optional" 
+ JAVA_EXT;
-                queryFile = new File(pkgDir, fileName);
-                writer = new BufferedWriter(new FileWriter(queryFile));
+        if (useOptionals) {
+            final String optionalFileName = description.getName() + "Optional" 
+ JAVA_EXT;
+            final File optionalFile = new File(pkgDir, optionalFileName);
+            try (final Writer writer = new OutputStreamWriter(new 
FileOutputStream(optionalFile), StandardCharsets.UTF_8)) {
+                final Template optionalTemplate = 
engine.getTemplate(SOBJECT_POJO_OPTIONAL_VM, UTF_8);
+                optionalTemplate.merge(context, writer);
+            }
+        }
 
-                context = new VelocityContext();
-                context.put("packageName", packageName);
-                context.put("desc", description);
-                context.put("generatedDate", generatedDate);
+        // write required Enumerations for any picklists
+        for (SObjectField field : description.getFields()) {
+            if (utility.isPicklist(field) || 
utility.isMultiSelectPicklist(field)) {
+                final String enumName = description.getName() + "_" + 
utility.enumTypeName(field.getName());
+                final String enumFileName = enumName + JAVA_EXT;
+                final File enumFile = new File(pkgDir, enumFileName);
 
-                queryTemplate = 
engine.getTemplate(SOBJECT_QUERY_RECORDS_OPTIONAL_VM);
-                queryTemplate.merge(context, writer);
+                context.put("field", field);
+                context.put("enumName", enumName);
+                final Template enumTemplate = 
engine.getTemplate(SOBJECT_PICKLIST_VM, UTF_8);
 
-                // close QueryRecords file
-                writer.close();
+                try (final Writer writer = new OutputStreamWriter(new 
FileOutputStream(enumFile), StandardCharsets.UTF_8)) {
+                    enumTemplate.merge(context, writer);
+                }
             }
+        }
 
-        } catch (Exception e) {
-            String msg = "Error creating " + fileName + ": " + e.getMessage();
-            throw new MojoExecutionException(msg, e);
-        } finally {
-            if (writer != null) {
-                try {
-                    writer.close();
-                } catch (IOException ignore) {
-                }
+        // write the QueryRecords class
+        final String queryRecordsFileName = "QueryRecords" + 
description.getName() + JAVA_EXT;
+        final File queryRecordsFile = new File(pkgDir, queryRecordsFileName);
+        final Template queryTemplate = 
engine.getTemplate(SOBJECT_QUERY_RECORDS_VM, UTF_8);
+        try (final Writer writer = new OutputStreamWriter(new 
FileOutputStream(queryRecordsFile), StandardCharsets.UTF_8)) {
+            queryTemplate.merge(context, writer);
+        }
+
+        if (useOptionals) {
+            // write the QueryRecords Optional class
+            final String queryRecordsOptionalFileName = "QueryRecords" + 
description.getName() + "Optional" + JAVA_EXT;
+            final File queryRecordsOptionalFile = new File(pkgDir, 
queryRecordsOptionalFileName);
+            final Template queryRecordsOptionalTemplate = 
engine.getTemplate(SOBJECT_QUERY_RECORDS_OPTIONAL_VM, UTF_8);
+            try (final Writer writer = new OutputStreamWriter(new 
FileOutputStream(queryRecordsOptionalFile), StandardCharsets.UTF_8)) {
+                queryRecordsOptionalTemplate.merge(context, writer);
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/6c66df26/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java
 
b/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java
index d7430a9..3728e1b 100644
--- 
a/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java
+++ 
b/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoOutputTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.maven;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -85,21 +86,22 @@ public class CamelSalesforceMojoOutputTest {
         mojo.processDescription(pkgDir, description, utility, FIXED_DATE);
 
         File generatedFile = new File(pkgDir, source);
-        String generatedContent = FileUtils.readFileToString(generatedFile);
+        String generatedContent = FileUtils.readFileToString(generatedFile, 
StandardCharsets.UTF_8);
 
         String expectedContent = IOUtils
-            
.toString(CamelSalesforceMojoOutputTest.class.getResource("/generated/" + 
source));
+            
.toString(CamelSalesforceMojoOutputTest.class.getResource("/generated/" + 
source), StandardCharsets.UTF_8);
 
         Assert.assertEquals(
-            "Geberated source file in " + source + " must be equal to the one 
present in test/resources",
+            "Generated source file in " + source + " must be equal to the one 
present in test/resources",
             generatedContent, expectedContent);
     }
 
     static SObjectDescription createSObjectDescription(String name) throws 
IOException {
-        InputStream inputStream = 
CamelSalesforceMojoOutputTest.class.getResourceAsStream("/" + name);
-        ObjectMapper mapper = JsonUtils.createObjectMapper();
+        try (InputStream inputStream = 
CamelSalesforceMojoOutputTest.class.getResourceAsStream("/" + name)) {
+            ObjectMapper mapper = JsonUtils.createObjectMapper();
 
-        return mapper.readValue(inputStream, SObjectDescription.class);
+            return mapper.readValue(inputStream, SObjectDescription.class);
+        }
     }
 
 }

Reply via email to