Replace the old placeholder system with the new one (%PLACEHOLDER% -> ${PLACEHOLDER})

This commit is contained in:
Fuzzlemann 2017-08-20 02:07:19 +02:00
parent 971007b589
commit c37b0c8941
6 changed files with 46 additions and 70 deletions

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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<String, Serializable> replaceMap) {
for (Map.Entry<String, Serializable> 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);
}
/**

View File

@ -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<String, WebAPI> registry = new HashMap<>();
public static void registerNewAPI(String method, WebAPI api) {

View File

@ -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<String, Serializable> 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<String, Serializable> replaceMap = ImmutableMap.of(randomIdentifier, "Success");
String expResult = "Success" + randomString;
String result = HtmlUtils.replacePlaceholders(html, replaceMap);
assertEquals(expResult, result);
}
/**
*
*/
@Test
public void testReplacePlaceholdersBackslash() {
Map<String, Serializable> 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<String, Serializable> replace = ImmutableMap.of(randomIdentifier, "/\\");
String expResult = "/\\ alright /\\";
String result = HtmlUtils.replacePlaceholders("${" + randomIdentifier + "} alright ${" + randomIdentifier + "}", replace);
assertEquals(result, expResult);
}
/**
*
*/
@Test
public void testRemoveXSS() {
String xss = "<script></script><!--";
boolean passed = HtmlUtils.removeXSS(xss).length() < xss.length();
assertEquals(true, passed);
String randomString = RandomData.randomString(10);
String xss = "<script>" + randomString + "</script><!--";
String result = HtmlUtils.removeXSS(xss);
assertEquals(randomString, result);
}
}