[cxf] branch master updated: getBytes uses the platform charset by default. (#624)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[cxf] branch master updated: getBytes uses the platform charset by default. (#624)

coheigea
Administrator
This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new 543521a  getBytes uses the platform charset by default. (#624)
543521a is described below

commit 543521ab051cb4f199faeab5765d001920bbbba2
Author: Adrian Leonhard <[hidden email]>
AuthorDate: Tue Jan 14 13:35:05 2020 +0100

    getBytes uses the platform charset by default. (#624)
   
    * getBytes uses the platform charset by default.
   
    As far as I can see, the only place where the schemas are loaded into the cache is loadSchemasIntoCache, where they are extracted from an XML Document. As such, they won't have an associated encoding. The XMLSource defaults to UTF-8, so this blows up if you have special characters and, for example, windows-1252 as the platform charset.
   
    * Alphabet hard
   
    * Update WadlGenerator.java
   
    https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/
   
    * Remove BOS import
---
 .../java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java   | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
index 3573a14..1827e81 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
@@ -19,7 +19,6 @@
 package org.apache.cxf.jaxrs.model.wadl;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
@@ -32,6 +31,7 @@ import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
 import java.net.URI;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -1978,7 +1978,7 @@ public class WadlGenerator implements ContainerRequestFilter {
             this.theSchemas = new LinkedList<>();
             // we'll need to do the proper schema caching eventually
             for (String s : schemas) {
-                XMLSource source = new XMLSource(new ByteArrayInputStream(s.getBytes()));
+                XMLSource source = new XMLSource(new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)));
                 source.setBuffering();
                 Map<String, String> locs = getLocationsMap(source, "import", links, ui);
                 locs.putAll(getLocationsMap(source, "include", links, ui));
@@ -2026,17 +2026,16 @@ public class WadlGenerator implements ContainerRequestFilter {
         }
 
         private String transformSchema(String schema, Map<String, String> locs) {
-            ByteArrayOutputStream bos = new ByteArrayOutputStream();
-            SchemaConverter sc = new SchemaConverter(StaxUtils.createXMLStreamWriter(bos), locs);
+            StringWriter sw = new StringWriter();
+            SchemaConverter sc = new SchemaConverter(StaxUtils.createXMLStreamWriter(sw), locs);
             try {
                 StaxUtils.copy(new StreamSource(new StringReader(schema)), sc);
                 sc.flush();
                 sc.close();
-                return bos.toString();
+                return sw.toString();
             } catch (Exception ex) {
                 return schema;
             }
-
         }
 
         @Override