mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-12 15:56:00 +08:00
Replace the old placeholder system with the new one (%PLACEHOLDER% -> ${PLACEHOLDER})
This commit is contained in:
parent
971007b589
commit
c37b0c8941
@ -72,24 +72,7 @@ public abstract class RawData {
|
|||||||
* @param value Any value the placeholder should be replaced with.
|
* @param value Any value the placeholder should be replaced with.
|
||||||
*/
|
*/
|
||||||
public void addValue(String placeholder, Serializable value) {
|
public void addValue(String placeholder, Serializable value) {
|
||||||
replaceMap.put(addPlaceholderSigns(placeholder), value.toString());
|
replaceMap.put(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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -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.
|
* @return Value the placeholder should be replaced with or null.
|
||||||
*/
|
*/
|
||||||
public String get(String key) {
|
public String get(String key) {
|
||||||
return replaceMap.get(addPlaceholderSigns(key));
|
return replaceMap.get(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -269,7 +269,7 @@ public class WebServer {
|
|||||||
private String readPOSTRequest(HttpExchange exchange) throws IOException {
|
private String readPOSTRequest(HttpExchange exchange) throws IOException {
|
||||||
try (InputStream in = exchange.getRequestBody()) {
|
try (InputStream in = exchange.getRequestBody()) {
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
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)) {
|
for (int n = in.read(buf); n > 0; n = in.read(buf)) {
|
||||||
out.write(buf, 0, n);
|
out.write(buf, 0, n);
|
||||||
}
|
}
|
||||||
|
@ -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.BadRequestResponse;
|
||||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.SuccessResponse;
|
import main.java.com.djrapitops.plan.ui.webserver.response.api.SuccessResponse;
|
||||||
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI;
|
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 org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -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.html.Html;
|
||||||
import main.java.com.djrapitops.plan.ui.webserver.WebServer;
|
import main.java.com.djrapitops.plan.ui.webserver.WebServer;
|
||||||
import main.java.com.djrapitops.plan.utilities.file.FileUtil;
|
import main.java.com.djrapitops.plan.utilities.file.FileUtil;
|
||||||
|
import org.apache.commons.lang.text.StrSubstitutor;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -40,14 +41,9 @@ public class HtmlUtils {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String replacePlaceholders(String html, Map<String, Serializable> replaceMap) {
|
public static String replacePlaceholders(String html, Map<String, Serializable> replaceMap) {
|
||||||
for (Map.Entry<String, Serializable> entrySet : replaceMap.entrySet()) {
|
StrSubstitutor sub = new StrSubstitutor(replaceMap);
|
||||||
String placeholder = entrySet.getKey();
|
|
||||||
String replacer = entrySet.getValue().toString();
|
|
||||||
|
|
||||||
html = html.replace(placeholder, replacer);
|
return sub.replace(html);
|
||||||
}
|
|
||||||
|
|
||||||
return html;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,6 +11,14 @@ import java.util.Map;
|
|||||||
* @author Fuzzlemann
|
* @author Fuzzlemann
|
||||||
*/
|
*/
|
||||||
public class WebAPIManager {
|
public class WebAPIManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor used to hide the public constructor
|
||||||
|
*/
|
||||||
|
private WebAPIManager() {
|
||||||
|
throw new IllegalStateException("Utility class");
|
||||||
|
}
|
||||||
|
|
||||||
private static Map<String, WebAPI> registry = new HashMap<>();
|
private static Map<String, WebAPI> registry = new HashMap<>();
|
||||||
|
|
||||||
public static void registerNewAPI(String method, WebAPI api) {
|
public static void registerNewAPI(String method, WebAPI api) {
|
||||||
|
@ -5,21 +5,21 @@
|
|||||||
*/
|
*/
|
||||||
package test.java.main.java.com.djrapitops.plan.utilities;
|
package test.java.main.java.com.djrapitops.plan.utilities;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
import main.java.com.djrapitops.plan.utilities.HtmlUtils;
|
import main.java.com.djrapitops.plan.utilities.HtmlUtils;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
import org.powermock.modules.junit4.PowerMockRunner;
|
import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
|
import test.java.utils.RandomData;
|
||||||
import test.java.utils.TestInit;
|
import test.java.utils.TestInit;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.assertFalse;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rsl1122
|
* @author Rsl1122
|
||||||
@ -28,62 +28,50 @@ import static org.junit.Assert.assertTrue;
|
|||||||
@PrepareForTest(JavaPlugin.class)
|
@PrepareForTest(JavaPlugin.class)
|
||||||
public class HtmlUtilsTest {
|
public class HtmlUtilsTest {
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public HtmlUtilsTest() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Before
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetHtmlStringFromResource() throws Exception {
|
public void testGetHtmlStringFromResource() throws Exception {
|
||||||
TestInit.init();
|
TestInit.init();
|
||||||
|
|
||||||
String fileName = "player.html";
|
String fileName = "player.html";
|
||||||
String result = HtmlUtils.getStringFromResource(fileName);
|
String result = HtmlUtils.getStringFromResource(fileName);
|
||||||
assertTrue("Result empty", !result.isEmpty());
|
|
||||||
|
assertFalse("Result empty", result.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testReplacePlaceholders() {
|
public void testReplacePlaceholders() {
|
||||||
String html = "%test%";
|
String randomString = RandomData.randomString(100);
|
||||||
Map<String, Serializable> replaceMap = new HashMap<>();
|
String randomIdentifier = RandomData.randomString(5);
|
||||||
replaceMap.put("%test%", "Success");
|
|
||||||
String expResult = "Success";
|
String html = "${" + randomIdentifier + "}" + randomString;
|
||||||
|
|
||||||
|
Map<String, Serializable> replaceMap = ImmutableMap.of(randomIdentifier, "Success");
|
||||||
|
|
||||||
|
String expResult = "Success" + randomString;
|
||||||
String result = HtmlUtils.replacePlaceholders(html, replaceMap);
|
String result = HtmlUtils.replacePlaceholders(html, replaceMap);
|
||||||
|
|
||||||
assertEquals(expResult, result);
|
assertEquals(expResult, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testReplacePlaceholdersBackslash() {
|
public void testReplacePlaceholdersBackslash() {
|
||||||
Map<String, Serializable> replace = new HashMap<>();
|
String randomIdentifier = RandomData.randomString(5);
|
||||||
replace.put("%test%", "/\\");
|
|
||||||
String result = HtmlUtils.replacePlaceholders("%test% alright %test%", replace);
|
Map<String, Serializable> replace = ImmutableMap.of(randomIdentifier, "/\\");
|
||||||
String exp = "/\\ alright /\\";
|
|
||||||
assertEquals(result, exp);
|
String expResult = "/\\ alright /\\";
|
||||||
|
String result = HtmlUtils.replacePlaceholders("${" + randomIdentifier + "} alright ${" + randomIdentifier + "}", replace);
|
||||||
|
|
||||||
|
assertEquals(result, expResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void testRemoveXSS() {
|
public void testRemoveXSS() {
|
||||||
String xss = "<script></script><!--";
|
String randomString = RandomData.randomString(10);
|
||||||
boolean passed = HtmlUtils.removeXSS(xss).length() < xss.length();
|
|
||||||
assertEquals(true, passed);
|
String xss = "<script>" + randomString + "</script><!--";
|
||||||
|
String result = HtmlUtils.removeXSS(xss);
|
||||||
|
|
||||||
|
assertEquals(randomString, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user