diff --git a/Plan/src/main/java/com/djrapitops/plan/data/analysis/RawData.java b/Plan/src/main/java/com/djrapitops/plan/data/analysis/RawData.java index fd0b485d4..71aebb51c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/analysis/RawData.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/analysis/RawData.java @@ -72,24 +72,7 @@ public abstract class RawData { * @param value Any value the placeholder should be replaced with. */ public void addValue(String placeholder, Serializable value) { - replaceMap.put(addPlaceholderSigns(placeholder), value.toString()); - } - - private String addPlaceholderSigns(String placeholder) { - StringBuilder newPlaceholder = new StringBuilder(); - - if (placeholder.charAt(0) != '%') { - newPlaceholder.append("%"); - } - - newPlaceholder.append(placeholder); - int lastIndex = placeholder.length() - 1; - - if (placeholder.charAt(lastIndex) != '%') { - newPlaceholder.append("%"); - } - - return newPlaceholder.toString(); + replaceMap.put(placeholder, value.toString()); } /** @@ -102,12 +85,12 @@ public abstract class RawData { } /** - * Used to get the value for a placeholder with or without the % symbols. + * Used to get the value for a placeholder without the placeholder prefix and suffix. * - * @param key placeholder with or without % symbols. + * @param key placeholder without the prefix and suffix * @return Value the placeholder should be replaced with or null. */ public String get(String key) { - return replaceMap.get(addPlaceholderSigns(key)); + return replaceMap.get(key); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java index da7d0236b..7e2a9626c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java @@ -269,7 +269,7 @@ public class WebServer { private String readPOSTRequest(HttpExchange exchange) throws IOException { try (InputStream in = exchange.getRequestBody()) { ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte buf[] = new byte[4096]; + byte[] buf = new byte[4096]; for (int n = in.read(buf); n > 0; n = in.read(buf)) { out.write(buf, 0, n); } diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/api/bukkit/ConfigureWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/api/bukkit/ConfigureWebAPI.java index 562a01d9e..57e3964e3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/api/bukkit/ConfigureWebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/api/bukkit/ConfigureWebAPI.java @@ -10,6 +10,7 @@ import main.java.com.djrapitops.plan.ui.webserver.response.Response; import main.java.com.djrapitops.plan.ui.webserver.response.api.BadRequestResponse; import main.java.com.djrapitops.plan.ui.webserver.response.api.SuccessResponse; import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI; +import org.apache.commons.lang3.text.translate.CharSequenceTranslator; import org.bukkit.configuration.file.FileConfiguration; import java.util.Map; diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java index 71953505b..10bf9a1f0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java @@ -7,6 +7,7 @@ import main.java.com.djrapitops.plan.locale.Msg; import main.java.com.djrapitops.plan.ui.html.Html; import main.java.com.djrapitops.plan.ui.webserver.WebServer; import main.java.com.djrapitops.plan.utilities.file.FileUtil; +import org.apache.commons.lang.text.StrSubstitutor; import java.io.FileNotFoundException; import java.io.Serializable; @@ -40,14 +41,9 @@ public class HtmlUtils { * @return */ public static String replacePlaceholders(String html, Map replaceMap) { - for (Map.Entry entrySet : replaceMap.entrySet()) { - String placeholder = entrySet.getKey(); - String replacer = entrySet.getValue().toString(); + StrSubstitutor sub = new StrSubstitutor(replaceMap); - html = html.replace(placeholder, replacer); - } - - return html; + return sub.replace(html); } /** diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/webserver/api/WebAPIManager.java b/Plan/src/main/java/com/djrapitops/plan/utilities/webserver/api/WebAPIManager.java index e72429808..ad258ccd4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/webserver/api/WebAPIManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/webserver/api/WebAPIManager.java @@ -11,6 +11,14 @@ import java.util.Map; * @author Fuzzlemann */ public class WebAPIManager { + + /** + * Constructor used to hide the public constructor + */ + private WebAPIManager() { + throw new IllegalStateException("Utility class"); + } + private static Map registry = new HashMap<>(); public static void registerNewAPI(String method, WebAPI api) { diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java index 930622cde..c8ce3d0e4 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/utilities/HtmlUtilsTest.java @@ -5,21 +5,21 @@ */ package test.java.main.java.com.djrapitops.plan.utilities; +import com.google.common.collect.ImmutableMap; import main.java.com.djrapitops.plan.utilities.HtmlUtils; import org.bukkit.plugin.java.JavaPlugin; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import test.java.utils.RandomData; import test.java.utils.TestInit; import java.io.Serializable; -import java.util.HashMap; import java.util.Map; +import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; /** * @author Rsl1122 @@ -28,62 +28,50 @@ import static org.junit.Assert.assertTrue; @PrepareForTest(JavaPlugin.class) public class HtmlUtilsTest { - /** - * - */ - public HtmlUtilsTest() { - } - - /** - * - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws Exception - */ @Test public void testGetHtmlStringFromResource() throws Exception { TestInit.init(); + String fileName = "player.html"; String result = HtmlUtils.getStringFromResource(fileName); - assertTrue("Result empty", !result.isEmpty()); + + assertFalse("Result empty", result.isEmpty()); } - /** - * - */ @Test public void testReplacePlaceholders() { - String html = "%test%"; - Map replaceMap = new HashMap<>(); - replaceMap.put("%test%", "Success"); - String expResult = "Success"; + String randomString = RandomData.randomString(100); + String randomIdentifier = RandomData.randomString(5); + + String html = "${" + randomIdentifier + "}" + randomString; + + Map replaceMap = ImmutableMap.of(randomIdentifier, "Success"); + + String expResult = "Success" + randomString; String result = HtmlUtils.replacePlaceholders(html, replaceMap); + assertEquals(expResult, result); } - /** - * - */ @Test public void testReplacePlaceholdersBackslash() { - Map replace = new HashMap<>(); - replace.put("%test%", "/\\"); - String result = HtmlUtils.replacePlaceholders("%test% alright %test%", replace); - String exp = "/\\ alright /\\"; - assertEquals(result, exp); + String randomIdentifier = RandomData.randomString(5); + + Map replace = ImmutableMap.of(randomIdentifier, "/\\"); + + String expResult = "/\\ alright /\\"; + String result = HtmlUtils.replacePlaceholders("${" + randomIdentifier + "} alright ${" + randomIdentifier + "}", replace); + + assertEquals(result, expResult); } - /** - * - */ @Test public void testRemoveXSS() { - String xss = "