commit 24d9e5128c8d823a86d1ccc011f6d058e414c709
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Wed Feb 25 11:58:09 2015 +0100
Use Apache Common Lang's StringUtils.replace correctly.
When we switched from String.replaceAll to StringUtils.replaceEach for
performance reasons, we forgot to change the passed regular expression
into a simple string. That's why some of our replacements didn't work out
as expected.
Bug introduced in f660e24be80b2efed1423b9ebf912377ccea49d5.
---
src/main/java/org/torproject/onionoo/docs/DetailsDocument.java | 2 +-
src/main/java/org/torproject/onionoo/docs/DetailsStatus.java | 2 +-
src/main/java/org/torproject/onionoo/docs/DocumentStore.java | 2 +-
src/main/java/org/torproject/onionoo/server/ResponseBuilder.java | 3 ++-
4 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java b/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java
index 06b74cf..99a98cb 100644
--- a/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java
+++ b/src/main/java/org/torproject/onionoo/docs/DetailsDocument.java
@@ -22,7 +22,7 @@ public class DetailsDocument extends Document {
* '\'. */
private static String escapeJSON(String s) {
return StringUtils.replaceEach(StringEscapeUtils.escapeEcmaScript(s),
- new String[] { "\\\\'" }, new String[] { "'" });
+ new String[] { "\\'" }, new String[] { "'" });
}
private static String unescapeJSON(String s) {
return StringEscapeUtils.unescapeEcmaScript(StringUtils.replaceEach(s,
diff --git a/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java b/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java
index f38d7e8..ff951ce 100644
--- a/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java
+++ b/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java
@@ -22,7 +22,7 @@ public class DetailsStatus extends Document {
* '\'. */
private static String escapeJSON(String s) {
return StringUtils.replaceEach(StringEscapeUtils.escapeEcmaScript(s),
- new String[] { "\\\\'" }, new String[] { "'" });
+ new String[] { "\\'" }, new String[] { "'" });
}
private static String unescapeJSON(String s) {
return StringEscapeUtils.unescapeEcmaScript(StringUtils.replaceEach(s,
diff --git a/src/main/java/org/torproject/onionoo/docs/DocumentStore.java b/src/main/java/org/torproject/onionoo/docs/DocumentStore.java
index 974f4c1..69646f1 100644
--- a/src/main/java/org/torproject/onionoo/docs/DocumentStore.java
+++ b/src/main/java/org/torproject/onionoo/docs/DocumentStore.java
@@ -316,7 +316,7 @@ public class DocumentStore {
* this string, it escapes the \ to \\, hence writes \\u00F2. We
* need to undo this and change \\u00F2 back to \u00F2. */
documentString = StringUtils.replace(gson.toJson(document),
- "\\\\\\\\u", "\\\\u");
+ "\\\\u", "\\u");
/* Existing details statuses don't contain opening and closing curly
* brackets, so we should remove them from new details statuses,
* too. */
diff --git a/src/main/java/org/torproject/onionoo/server/ResponseBuilder.java b/src/main/java/org/torproject/onionoo/server/ResponseBuilder.java
index 371b79f..d1e7f23 100644
--- a/src/main/java/org/torproject/onionoo/server/ResponseBuilder.java
+++ b/src/main/java/org/torproject/onionoo/server/ResponseBuilder.java
@@ -6,6 +6,7 @@ import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
+import org.apache.commons.lang3.StringUtils;
import org.torproject.onionoo.docs.BandwidthDocument;
import org.torproject.onionoo.docs.ClientsDocument;
import org.torproject.onionoo.docs.DetailsDocument;
@@ -274,7 +275,7 @@ public class ResponseBuilder {
/* Whenever we provide Gson with a string containing an escaped
* non-ASCII character like \u00F2, it escapes the \ to \\, which
* we need to undo before including the string in a response. */
- return gson.toJson(dd).replaceAll("\\\\\\\\u", "\\\\u");
+ return StringUtils.replace(gson.toJson(dd), "\\\\u", "\\u");
} else {
// TODO We should probably log that we didn't find a details
// document that we expected to exist.