From dacd73d365b9e144ecf9259dd17e3a8dc9072fe3 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 6 Jan 2015 10:33:20 +0100 Subject: [PATCH] Fix quality flaws --- .../org/sonar/process/monitor/StreamGobbler.java | 3 ++- .../java/org/sonar/process/ConfigurationUtils.java | 9 ++++++--- .../server/debt/DebtModelPluginRepository.java | 5 +++-- .../sonar/server/debt/DebtModelXMLExporter.java | 7 +++---- .../server/qualityprofile/QProfileCopier.java | 7 ++++--- .../org/sonar/server/rule/ws/CreateAction.java | 3 ++- .../sonar/server/startup/GeneratePluginIndex.java | 3 ++- .../java/org/sonar/server/ws/ServletResponse.java | 7 ++++--- .../java/org/sonar/server/ws/WebServiceEngine.java | 3 ++- .../java/org/sonar/colorizer/HtmlDecorator.java | 3 ++- .../sonar/core/graph/graphson/GraphsonUtil.java | 5 +++-- .../main/java/org/sonar/api/utils/XpathParser.java | 14 +++++++------- .../sonar/test/i18n/BundleSynchronizedMatcher.java | 9 +++++++-- 13 files changed, 47 insertions(+), 31 deletions(-) diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/StreamGobbler.java b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/StreamGobbler.java index 2115884b189..35c03564abf 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/StreamGobbler.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/process/monitor/StreamGobbler.java @@ -19,6 +19,7 @@ */ package org.sonar.process.monitor; +import org.apache.commons.io.Charsets; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,7 +50,7 @@ class StreamGobbler extends Thread { @Override public void run() { - BufferedReader br = new BufferedReader(new InputStreamReader(is)); + BufferedReader br = new BufferedReader(new InputStreamReader(is, Charsets.UTF_8)); try { String line; while ((line = br.readLine()) != null) { diff --git a/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java b/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java index b4f86457555..458cf031722 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java +++ b/server/sonar-process/src/main/java/org/sonar/process/ConfigurationUtils.java @@ -19,12 +19,15 @@ */ package org.sonar.process; +import org.apache.commons.io.Charsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.text.StrSubstitutor; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.util.Enumeration; import java.util.Map; import java.util.Properties; @@ -55,9 +58,9 @@ public final class ConfigurationUtils { File propertyFile = new File(args[0]); Properties properties = new Properties(); - FileReader reader = null; + Reader reader = null; try { - reader = new FileReader(propertyFile); + reader = new InputStreamReader(new FileInputStream(propertyFile), Charsets.UTF_8); properties.load(reader); } catch (Exception e) { throw new IllegalStateException("Could not read properties from file: " + args[0], e); diff --git a/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelPluginRepository.java b/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelPluginRepository.java index a770b93fa4d..b0e1cb6bd20 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelPluginRepository.java +++ b/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelPluginRepository.java @@ -22,6 +22,7 @@ package org.sonar.server.debt; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Maps; +import org.apache.commons.io.Charsets; import org.picocontainer.Startable; import org.sonar.api.Plugin; import org.sonar.api.ServerExtension; @@ -123,11 +124,11 @@ public class DebtModelPluginRepository implements ServerExtension, Startable { public Reader createReaderForXMLFile(String pluginKey) { ClassLoader classLoader = contributingPluginKeyToClassLoader.get(pluginKey); String xmlFilePath = getXMLFilePath(pluginKey); - return new InputStreamReader(classLoader.getResourceAsStream(xmlFilePath)); + return new InputStreamReader(classLoader.getResourceAsStream(xmlFilePath), Charsets.UTF_8); } @VisibleForTesting - Map getContributingPluginKeyToClassLoader(){ + Map getContributingPluginKeyToClassLoader() { return contributingPluginKeyToClassLoader; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java b/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java index e5b794a9994..929648174e1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java @@ -24,6 +24,7 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; import com.google.common.collect.Ordering; +import org.apache.commons.io.Charsets; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; @@ -165,12 +166,10 @@ public class DebtModelXMLExporter implements ServerComponent { serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT); - Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); + Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8)))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); - return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()); - } catch (TransformerConfigurationException ignored) { - // Ignore, raw XML will be returned + return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), Charsets.UTF_8); } catch (TransformerException ignored) { // Ignore, raw XML will be returned } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java index 4be8be03ae1..c92f17703b0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileCopier.java @@ -19,6 +19,7 @@ */ package org.sonar.server.qualityprofile; +import org.apache.commons.io.Charsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -87,14 +88,14 @@ public class QProfileCopier implements ServerComponent { } if (fromProfile.getName().equals(toProfileName.getName())) { throw new IllegalArgumentException(String.format("Source and target profiles are equal: %s", - fromProfile.getName(), toProfileName.getName())); + fromProfile.getName())); } } private void backup(String profileKey, File backupFile) { Writer writer = null; try { - writer = new OutputStreamWriter(FileUtils.openOutputStream(backupFile)); + writer = new OutputStreamWriter(FileUtils.openOutputStream(backupFile), Charsets.UTF_8); backuper.backup(profileKey, writer); } catch (IOException e) { throw new IllegalStateException("Fail to open temporary backup file: " + backupFile, e); @@ -106,7 +107,7 @@ public class QProfileCopier implements ServerComponent { private void restore(File backupFile, QProfileName profileName) { Reader reader = null; try { - reader = new InputStreamReader(FileUtils.openInputStream(backupFile)); + reader = new InputStreamReader(FileUtils.openInputStream(backupFile), Charsets.UTF_8); backuper.restore(reader, profileName); } catch (IOException e) { throw new IllegalStateException("Fail to create temporary backup file: " + backupFile, e); diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java index 0c823713fb5..cf48eba28d8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/CreateAction.java @@ -20,6 +20,7 @@ package org.sonar.server.rule.ws; import com.google.common.base.Strings; +import org.apache.commons.io.Charsets; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; @@ -167,7 +168,7 @@ public class CreateAction implements RequestHandler { Response.Stream stream = response.stream(); stream.setStatus(409); stream.setMediaType(MimeTypes.JSON); - JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output())).beginObject().name("rule"); + JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output(), Charsets.UTF_8)).beginObject().name("rule"); mapping.write(rule, json, null /* TODO replace by SearchOptions immutable constant */); json.endObject().close(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java b/server/sonar-server/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java index bb9e08077df..d79adb553c2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/startup/GeneratePluginIndex.java @@ -31,6 +31,7 @@ import org.sonar.server.platform.DefaultServerFileSystem; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; /** * @since 2.11 @@ -51,7 +52,7 @@ public final class GeneratePluginIndex { void writeIndex(File indexFile) throws IOException { FileUtils.forceMkdir(indexFile.getParentFile()); - FileWriter writer = new FileWriter(indexFile, false); + Writer writer = new FileWriter(indexFile, false); try { for (PluginMetadata metadata : repository.getMetadata()) { writer.append(RemotePlugin.create((DefaultPluginMetadata) metadata).marshal()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java b/server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java index e1907701971..98676d441fa 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java @@ -19,6 +19,7 @@ */ package org.sonar.server.ws; +import com.google.common.base.Charsets; import org.sonar.api.server.ws.Response; import org.sonar.api.utils.text.JsonWriter; import org.sonar.api.utils.text.XmlWriter; @@ -64,7 +65,7 @@ public class ServletResponse implements Response { } public String outputAsString() { - return output.toString(); + return new String(output.toByteArray(), Charsets.UTF_8); } public ServletStream reset() { @@ -78,13 +79,13 @@ public class ServletResponse implements Response { @Override public JsonWriter newJsonWriter() { stream.setMediaType(MimeTypes.JSON); - return JsonWriter.of(new OutputStreamWriter(stream.output())); + return JsonWriter.of(new OutputStreamWriter(stream.output(), Charsets.UTF_8)); } @Override public XmlWriter newXmlWriter() { stream.setMediaType(MimeTypes.XML); - return XmlWriter.of(new OutputStreamWriter(stream.output())); + return XmlWriter.of(new OutputStreamWriter(stream.output(), Charsets.UTF_8)); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java b/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java index 498af7da455..54cf3e39911 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java @@ -19,6 +19,7 @@ */ package org.sonar.server.ws; +import com.google.common.base.Charsets; import org.picocontainer.Startable; import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; @@ -120,7 +121,7 @@ public class WebServiceEngine implements ServerComponent, Startable { stream.reset(); stream.setStatus(status); stream.setMediaType(MimeTypes.JSON); - JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output())); + JsonWriter json = JsonWriter.of(new OutputStreamWriter(stream.output(), Charsets.UTF_8)); try { json.beginObject(); diff --git a/sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlDecorator.java b/sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlDecorator.java index e55dc91718e..35f1d896d34 100644 --- a/sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlDecorator.java +++ b/sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlDecorator.java @@ -21,6 +21,7 @@ package org.sonar.colorizer; import com.google.common.io.ByteStreams; import com.google.common.io.Closeables; +import org.apache.commons.io.Charsets; import org.sonar.channel.CodeReader; import java.io.IOException; @@ -98,7 +99,7 @@ public class HtmlDecorator extends Tokenizer { InputStream input = null; try { input = HtmlRenderer.class.getResourceAsStream(CSS_PATH); - return new String(ByteStreams.toByteArray(input)); + return new String(ByteStreams.toByteArray(input), Charsets.UTF_8); } catch (IOException e) { throw new SynhtaxHighlightingException("SonarQube Colorizer CSS file not found: " + CSS_PATH, e); diff --git a/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java index 4f43f45dfaf..61631162061 100644 --- a/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java +++ b/sonar-core/src/main/java/org/sonar/core/graph/graphson/GraphsonUtil.java @@ -23,6 +23,7 @@ import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Element; import com.tinkerpop.blueprints.Vertex; +import org.apache.commons.io.Charsets; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -548,7 +549,7 @@ class GraphsonUtil { * Creates a vertex from GraphSON using settings supplied in the constructor. */ Vertex vertexFromJson(InputStream json) throws ParseException, IOException { - return this.vertexFromJson((JSONObject) parser.parse(new InputStreamReader(json))); + return this.vertexFromJson((JSONObject) parser.parse(new InputStreamReader(json, Charsets.UTF_8))); } /** @@ -562,7 +563,7 @@ class GraphsonUtil { * Creates an edge from GraphSON using settings supplied in the constructor. */ Edge edgeFromJson(InputStream json, Vertex out, Vertex in) throws IOException, ParseException { - return this.edgeFromJson((JSONObject) parser.parse(new InputStreamReader(json)), out, in); + return this.edgeFromJson((JSONObject) parser.parse(new InputStreamReader(json, Charsets.UTF_8)), out, in); } /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/XpathParser.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/XpathParser.java index c69d076d549..0b9547e0e0e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/XpathParser.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/XpathParser.java @@ -19,6 +19,7 @@ */ package org.sonar.api.utils; +import org.apache.commons.io.Charsets; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,6 +43,7 @@ import javax.xml.xpath.XPathFactory; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; @@ -96,7 +98,7 @@ public class XpathParser { BufferedReader buffer = null; try { - buffer = new BufferedReader(new FileReader(file)); + buffer = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8)); parse(buffer); } catch (IOException e) { @@ -110,7 +112,7 @@ public class XpathParser { public void parse(InputStream stream) { BufferedReader buffer = null; try { - buffer = new BufferedReader(new InputStreamReader(stream)); + buffer = new BufferedReader(new InputStreamReader(stream, Charsets.UTF_8)); parse(buffer); } catch (IOException e) { @@ -127,14 +129,12 @@ public class XpathParser { public void parse(String xml) { try { - xml = fixUnicodeChar(xml); - doc = builder.parse(new ByteArrayInputStream(xml.getBytes())); + String fixedXml = fixUnicodeChar(xml); + doc = builder.parse(new ByteArrayInputStream(fixedXml.getBytes(Charsets.UTF_8))); XPathFactory factory = XPathFactory.newInstance(); xpath = factory.newXPath(); - } catch (SAXException e) { - throw new XmlParserException(CAN_NOT_PARSE_XML + xml, e); - } catch (IOException e) { + } catch (IOException | SAXException e) { throw new XmlParserException(CAN_NOT_PARSE_XML + xml, e); } } diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java index 6e6737c4045..fdb16924b63 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java @@ -19,14 +19,19 @@ */ package org.sonar.test.i18n; +import org.apache.commons.io.Charsets; import org.apache.commons.io.IOUtils; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; import java.util.Map; import java.util.Properties; import java.util.SortedMap; @@ -118,9 +123,9 @@ public class BundleSynchronizedMatcher extends BaseMatcher { dumpFile.delete(); } dumpFile.getParentFile().mkdirs(); - FileWriter writer = null; + Writer writer = null; try { - writer = new FileWriter(dumpFile); + writer = new OutputStreamWriter(new FileOutputStream(dumpFile), Charsets.UTF_8); writer.write(details); } catch (IOException e) { throw new IllegalStateException("Unable to write the report to 'target/l10n/" + bundleName + ".report.txt'", e); -- 2.39.5