Html locale replacement code

This commit is contained in:
Rsl1122 2018-07-29 11:11:33 +03:00
parent d33f305bb2
commit ca93523f14
4 changed files with 29 additions and 4 deletions

View File

@ -63,4 +63,18 @@ public class Locale extends HashMap<Lang, Message> {
public String[] getArray(Lang key, Serializable... values) {
return get(key).toArray(values);
}
public String replaceMatchingLanguage(String from) {
if (isEmpty()) {
return from;
}
for (Entry<Lang, Message> entry : entrySet()) {
String defaultValue = entry.getKey().getDefault();
String replacement = entry.getValue().toString();
from = from.replace(defaultValue, replacement);
}
return from;
}
}

View File

@ -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> 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:");

View File

@ -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<Locale> getLocaleSupplier() {
return locale;
}
}

View File

@ -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) {