From ca93523f1489ecc7baf63acb6c633f13a6023270 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 29 Jul 2018 11:11:33 +0300 Subject: [PATCH] Html locale replacement code --- .../com/djrapitops/plan/system/locale/Locale.java | 14 ++++++++++++++ .../plan/system/webserver/RequestHandler.java | 6 +++++- .../plan/system/webserver/WebServer.java | 6 +++++- .../plan/system/webserver/response/Response.java | 7 +++++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/system/locale/Locale.java b/Plan/src/main/java/com/djrapitops/plan/system/locale/Locale.java index 6a6fc1a68..dfaf19d12 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/locale/Locale.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/locale/Locale.java @@ -63,4 +63,18 @@ public class Locale extends HashMap { public String[] getArray(Lang key, Serializable... values) { return get(key).toArray(values); } + + public String replaceMatchingLanguage(String from) { + if (isEmpty()) { + return from; + } + for (Entry entry : entrySet()) { + String defaultValue = entry.getKey().getDefault(); + String replacement = entry.getValue().toString(); + + from = from.replace(defaultValue, replacement); + } + + return from; + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/RequestHandler.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/RequestHandler.java index 6b76559b1..2f7dfe14c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/RequestHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/RequestHandler.java @@ -4,6 +4,7 @@ */ package com.djrapitops.plan.system.webserver; +import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.webserver.auth.Authentication; import com.djrapitops.plan.system.webserver.auth.BasicAuthentication; @@ -17,6 +18,7 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import java.util.List; +import java.util.function.Supplier; /** * HttpHandler for WebServer request management. @@ -25,10 +27,12 @@ import java.util.List; */ public class RequestHandler implements HttpHandler { + private final Supplier locale; private final ResponseHandler responseHandler; RequestHandler(WebServer webServer) { responseHandler = new ResponseHandler(webServer); + locale = webServer.getLocaleSupplier(); } @Override @@ -49,7 +53,7 @@ public class RequestHandler implements HttpHandler { } response.setResponseHeaders(responseHeaders); - response.send(exchange); + response.send(exchange, locale.get()); } catch (Exception e) { if (Settings.DEV_MODE.isTrue()) { Log.warn("THIS ERROR IS ONLY LOGGED IN DEV MODE:"); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServer.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServer.java index 16ff99c35..c8d90b4e3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServer.java @@ -141,7 +141,7 @@ public class WebServer implements SubSystem { throw new IllegalStateException("Certificate with Alias: " + alias + " was not found in the Keystore."); } - Log.info("Found Certificate: " + cert.getType()); + Log.info("Certificate: " + cert.getType()); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); keyManagerFactory.init(keystore, keypass); @@ -228,4 +228,8 @@ public class WebServer implements SubSystem { public ResponseHandler getResponseHandler() { return responseHandler; } + + Supplier getLocaleSupplier() { + return locale; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/Response.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/Response.java index 728642226..0a9aac06d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/Response.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/Response.java @@ -1,5 +1,6 @@ package com.djrapitops.plan.system.webserver.response; +import com.djrapitops.plan.system.locale.Locale; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; @@ -81,13 +82,15 @@ public abstract class Response { this.responseHeaders = responseHeaders; } - public void send(HttpExchange exchange) throws IOException { + public void send(HttpExchange exchange, Locale locale) throws IOException { responseHeaders.set("Content-Type", type); responseHeaders.set("Content-Encoding", "gzip"); exchange.sendResponseHeaders(getCode(), 0); + String content = locale.replaceMatchingLanguage(getContent()); + try (GZIPOutputStream out = new GZIPOutputStream(exchange.getResponseBody()); - ByteArrayInputStream bis = new ByteArrayInputStream(getContent().getBytes())) { + ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes())) { byte[] buffer = new byte[2048]; int count; while ((count = bis.read(buffer)) != -1) {