Removed unused code

This commit is contained in:
Rsl1122 2020-03-01 21:24:48 +02:00 committed by Risto Lahtela
parent 1ab5f3473d
commit b5d07de18b
74 changed files with 167 additions and 1592 deletions

View File

@ -1,3 +1,19 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.web.resolver.request;
import java.util.Map;

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.delivery.web.resolver.request;
import com.djrapitops.plan.delivery.web.resolver.Resolver;
import org.apache.commons.lang3.StringUtils;
import java.util.Map;
import java.util.Optional;
@ -55,7 +56,7 @@ public final class Request {
public Request(String method, String target, WebUser user, Map<String, String> headers) {
this.method = method;
if (target.contains("?")) {
String[] halves = target.split("\\?", 2);
String[] halves = StringUtils.split(target, "?", 2);
this.path = new URIPath(halves[0]);
this.query = new URIQuery(halves[1]);
} else {

View File

@ -14,9 +14,8 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.web.resolver;
package com.djrapitops.plan.delivery.web.resolver.request;
import com.djrapitops.plan.delivery.web.resolver.request.URIPath;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.commands.subcommands;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.delivery.webserver.WebServer;
import com.djrapitops.plan.exceptions.database.DBOpException;
import com.djrapitops.plan.processing.Processing;
@ -129,7 +129,7 @@ public class RegisterCommand extends CommandNode {
Verify.isTrue(userName.length() <= 100, () -> new IllegalArgumentException("Username can only be 100 characters long."));
int permLevel = Integer.parseInt(args[2]);
String passHash = PassEncryptUtil.createHash(args[0]);
registerUser(new WebUser_old(userName, passHash, permLevel), sender);
registerUser(new WebUser(userName, passHash, permLevel), sender);
}
private void playerRegister(String[] args, Sender sender) throws PassEncryptUtil.CannotPerformOperationException {
@ -138,7 +138,7 @@ public class RegisterCommand extends CommandNode {
String user = sender.getName();
String pass = PassEncryptUtil.createHash(args[0]);
int permLvl = getPermissionLevel(sender);
registerUser(new WebUser_old(user, pass, permLvl), sender);
registerUser(new WebUser(user, pass, permLvl), sender);
} else if (sender.hasPermission(Permissions.MANAGE_WEB.getPermission())) {
consoleRegister(args, sender, notEnoughArgsMsg);
} else {
@ -162,7 +162,7 @@ public class RegisterCommand extends CommandNode {
return 100;
}
private void registerUser(WebUser_old webUser, Sender sender) {
private void registerUser(WebUser webUser, Sender sender) {
processing.submitCritical(() -> {
String userName = webUser.getName();
try {

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.commands.subcommands.webuser;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.Permissions;
import com.djrapitops.plan.settings.locale.Locale;
@ -85,12 +85,12 @@ public class WebCheckCommand extends CommandNode {
processing.submitNonCritical(() -> {
try {
Database db = dbSystem.getDatabase();
Optional<WebUser_old> found = db.query(WebUserQueries.fetchWebUser(user));
Optional<WebUser> found = db.query(WebUserQueries.fetchWebUser(user));
if (!found.isPresent()) {
sender.sendMessage(locale.getString(CommandLang.FAIL_WEB_USER_NOT_EXISTS));
return;
}
WebUser_old info = found.get();
WebUser info = found.get();
sender.sendMessage(locale.getString(CommandLang.WEB_USER_LIST, info.getName(), info.getPermLevel()));
} catch (Exception e) {
errorHandler.log(L.ERROR, this.getClass(), e);

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.commands.subcommands.webuser;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.Permissions;
import com.djrapitops.plan.settings.locale.Locale;
@ -86,7 +86,7 @@ public class WebDeleteCommand extends CommandNode {
processing.submitNonCritical(() -> {
try {
Database db = dbSystem.getDatabase();
Optional<WebUser_old> found = db.query(WebUserQueries.fetchWebUser(user));
Optional<WebUser> found = db.query(WebUserQueries.fetchWebUser(user));
if (!found.isPresent()) {
sender.sendMessage("§c[Plan] User Doesn't exist.");
return;

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.commands.subcommands.webuser;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.Permissions;
import com.djrapitops.plan.settings.locale.Locale;
@ -76,9 +76,9 @@ public class WebListUsersCommand extends CommandNode {
processing.submitNonCritical(() -> {
try {
List<WebUser_old> users = dbSystem.getDatabase().query(WebUserQueries.fetchAllPlanWebUsers());
List<WebUser> users = dbSystem.getDatabase().query(WebUserQueries.fetchAllPlanWebUsers());
sender.sendMessage(locale.getString(CommandLang.HEADER_WEB_USERS, users.size()));
for (WebUser_old user : users) {
for (WebUser user : users) {
sender.sendMessage(locale.getString(CommandLang.WEB_USER_LIST, user.getName(), user.getPermLevel()));
}
sender.sendMessage(">");

View File

@ -16,8 +16,6 @@
*/
package com.djrapitops.plan.delivery.domain;
import com.djrapitops.plan.delivery.web.resolver.request.WebUser;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -28,13 +26,13 @@ import java.util.Objects;
* @author Rsl1122
*/
@Deprecated
public class WebUser_old {
public class WebUser {
private final String user;
private final String saltedPassHash;
private final int permLevel;
public WebUser_old(String user, String saltedPassHash, int permLevel) {
public WebUser(String user, String saltedPassHash, int permLevel) {
this.user = user;
this.saltedPassHash = saltedPassHash;
this.permLevel = permLevel;
@ -56,7 +54,7 @@ public class WebUser_old {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WebUser_old webUser = (WebUser_old) o;
WebUser webUser = (WebUser) o;
return permLevel == webUser.permLevel &&
Objects.equals(user, webUser.user) &&
Objects.equals(saltedPassHash, webUser.saltedPassHash);
@ -67,7 +65,7 @@ public class WebUser_old {
return Objects.hash(user, saltedPassHash, permLevel);
}
public WebUser toNewWebUser() {
public com.djrapitops.plan.delivery.web.resolver.request.WebUser toNewWebUser() {
List<String> permissions = new ArrayList<>();
if (permLevel <= 0) {
permissions.add("page.network");
@ -82,7 +80,7 @@ public class WebUser_old {
if (permLevel <= 2) {
permissions.add("page.player.self");
}
return new WebUser(
return new com.djrapitops.plan.delivery.web.resolver.request.WebUser(
user, permissions.toArray(new String[0])
);
}

View File

@ -18,7 +18,10 @@ package com.djrapitops.plan.delivery.domain.container;
import com.djrapitops.plan.delivery.domain.keys.Key;
import com.djrapitops.plan.delivery.formatting.Formatter;
import com.djrapitops.plan.utilities.java.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
@ -33,6 +36,41 @@ import java.util.function.Supplier;
*/
public interface DataContainer {
default Map<String, Object> mapToNormalMap() {
Map<String, Object> values = new HashMap<>();
getMap().forEach((key, value) ->
{
if (value instanceof DataContainer) {
value = ((DataContainer) value).mapToNormalMap();
}
if (value instanceof Map) {
value = handleMap((Map<?, ?>) value);
}
if (value instanceof List) {
value = handleList((List<?>) value);
}
values.put(key.getKeyName(), value);
}
);
return values;
}
default List<?> handleList(List<?> list) {
if (list.stream().findAny().orElse(null) instanceof DataContainer) {
return Lists.map(list, obj -> ((DataContainer) obj).mapToNormalMap());
}
return list;
}
default Map<?, ?> handleMap(Map<?, ?> map) {
if (map.values().stream().findAny().orElse(null) instanceof DataContainer) {
Map<Object, Object> newMap = new HashMap<>();
map.forEach((key, value) -> newMap.put(key, ((DataContainer) value).mapToNormalMap()));
return newMap;
}
return map;
}
/**
* Place your data inside the container.
* <p>

View File

@ -20,7 +20,7 @@ import com.djrapitops.plan.delivery.rendering.pages.Page;
import com.djrapitops.plan.delivery.rendering.pages.PageFactory;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver;
import com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver;
import com.djrapitops.plan.exceptions.connection.NotFoundException;
import com.djrapitops.plan.exceptions.connection.WebException;
import com.djrapitops.plan.identification.Server;

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.delivery.export;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.delivery.webserver.ResponseFactory;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.queries.PlayerFetchQueries;

View File

@ -20,7 +20,7 @@ import com.djrapitops.plan.delivery.rendering.pages.Page;
import com.djrapitops.plan.delivery.rendering.pages.PageFactory;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver;
import com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver;
import com.djrapitops.plan.exceptions.connection.NotFoundException;
import com.djrapitops.plan.exceptions.connection.WebException;
import com.djrapitops.plan.settings.locale.Locale;

View File

@ -20,7 +20,7 @@ import com.djrapitops.plan.delivery.rendering.pages.Page;
import com.djrapitops.plan.delivery.rendering.pages.PageFactory;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver;
import com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver;
import com.djrapitops.plan.exceptions.connection.NotFoundException;
import com.djrapitops.plan.exceptions.connection.WebException;
import com.djrapitops.plan.identification.ServerInfo;

View File

@ -20,7 +20,7 @@ import com.djrapitops.plan.delivery.rendering.pages.Page;
import com.djrapitops.plan.delivery.rendering.pages.PageFactory;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver;
import com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver;
import com.djrapitops.plan.exceptions.connection.NotFoundException;
import com.djrapitops.plan.exceptions.connection.WebException;
import com.djrapitops.plan.identification.Server;

View File

@ -71,10 +71,10 @@ public class WorldPie extends PieWithDrilldown {
return drilldowns;
}
private List<List> createGMTimesForWorld(GMTimes gmTimes) {
List<List> data = new ArrayList<>();
private List<List<Object>> createGMTimesForWorld(GMTimes gmTimes) {
List<List<Object>> data = new ArrayList<>();
for (Map.Entry<String, Long> gmEntry : gmTimes.getTimes().entrySet()) {
List gmList = Arrays.asList(gmEntry.getKey(), gmEntry.getValue());
List<Object> gmList = Arrays.asList(gmEntry.getKey(), gmEntry.getValue());
data.add(gmList);
}
return data;

View File

@ -19,7 +19,6 @@ package com.djrapitops.plan.delivery.webserver;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.webserver.auth.Authentication;
import com.djrapitops.plan.delivery.webserver.auth.BasicAuthentication;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.PluginSettings;
import com.djrapitops.plan.settings.config.paths.WebserverSettings;

View File

@ -25,7 +25,6 @@ import com.djrapitops.plan.exceptions.WebUserAuthException;
import com.djrapitops.plan.settings.locale.Locale;
import com.sun.net.httpserver.HttpExchange;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
@ -84,10 +83,6 @@ public class RequestInternal {
return new URIQuery(requestURI.getQuery());
}
public InputStream getRequestBody() {
return exchange.getRequestBody();
}
@Override
public String toString() {
return "Request:" + requestMethod + " " + requestURI.getPath();
@ -101,11 +96,6 @@ public class RequestInternal {
return locale;
}
@Deprecated
public RequestTarget getRequestTarget() {
return new RequestTarget(requestURI);
}
public Request toAPIRequest() throws WebUserAuthException {
return new Request(
requestMethod,
@ -118,7 +108,7 @@ public class RequestInternal {
private WebUser getWebUser() throws WebUserAuthException {
Optional<Authentication> auth = getAuth();
return auth.isPresent() ? auth.get().getWebUser().toNewWebUser() : null;
return auth.map(authentication -> authentication.getWebUser().toNewWebUser()).orElse(null);
}
private Map<String, String> getRequestHeaders() {

View File

@ -1,96 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver;
import org.apache.commons.lang3.StringUtils;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
/**
* Represents URI of a requested resource.
*
* @author Rsl1122
*/
@Deprecated
public class RequestTarget {
private final String resourceString;
private final List<String> resource;
private final Map<String, String> parameters;
public RequestTarget(URI targetURI) {
resourceString = targetURI.getPath();
resource = Arrays.stream(StringUtils.split(resourceString, '/'))
.filter(part -> !part.isEmpty()).collect(Collectors.toList());
parameters = new TreeMap<>();
parseParameters(targetURI.getQuery());
}
private void parseParameters(String parameterString) {
if (parameterString == null || parameterString.isEmpty()) {
return;
}
String[] keysAndValues = StringUtils.split(parameterString, '&');
for (String kv : keysAndValues) {
if (kv.isEmpty()) {
continue;
}
String[] keyAndValue = StringUtils.split(kv, "=", 2);
if (keyAndValue.length >= 2) {
parameters.put(keyAndValue[0], keyAndValue[1]);
}
}
}
public boolean isEmpty() {
return resource.isEmpty();
}
public int size() {
return resource.size();
}
public String get(int index) {
return resource.get(index);
}
public void removeFirst() {
if (!isEmpty()) {
resource.remove(0);
}
}
public boolean endsWith(String suffix) {
return resourceString.endsWith(suffix);
}
public boolean endsWithAny(String... suffixes) {
return StringUtils.endsWithAny(resourceString, suffixes);
}
public Optional<String> getParameter(String key) {
return Optional.ofNullable(parameters.get(key));
}
public String getResourceString() {
return resourceString;
}
}

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
package com.djrapitops.plan.delivery.webserver;
import com.djrapitops.plan.delivery.domain.container.PlayerContainer;
import com.djrapitops.plan.delivery.rendering.html.icon.Family;
@ -24,10 +24,6 @@ import com.djrapitops.plan.delivery.rendering.pages.PageFactory;
import com.djrapitops.plan.delivery.web.resolver.MimeType;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.webserver.auth.FailReason;
import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse;
import com.djrapitops.plan.delivery.webserver.response.errors.InternalErrorResponse;
import com.djrapitops.plan.delivery.webserver.response.errors.NotFoundResponse;
import com.djrapitops.plan.delivery.webserver.response.pages.RawDataResponse;
import com.djrapitops.plan.exceptions.WebUserAuthException;
import com.djrapitops.plan.exceptions.connection.NotFoundException;
import com.djrapitops.plan.settings.locale.Locale;
@ -135,19 +131,6 @@ public class ResponseFactory {
.build();
}
@Deprecated
public ErrorResponse internalErrorResponse_old(Throwable e, String s) {
try {
return new InternalErrorResponse(s, e, versionCheckSystem, files);
} catch (IOException improperRestartException) {
return new ErrorResponse(
"Error occurred: " + e.toString() +
", additional error occurred when attempting to send error page to user: " +
improperRestartException.toString()
);
}
}
public Response internalErrorResponse(Throwable e, String s) {
return forInternalError(e, s);
}
@ -178,7 +161,7 @@ public class ResponseFactory {
PlayerContainer player = dbSystem.getDatabase().query(ContainerFetchQueries.fetchPlayerContainer(playerUUID));
return Response.builder()
.setMimeType(MimeType.JSON)
.setJSONContent(RawDataResponse.mapToNormalMap(player))
.setJSONContent(player.mapToNormalMap())
.build();
}
@ -258,11 +241,6 @@ public class ResponseFactory {
}
}
@Deprecated
public ErrorResponse pageNotFound404_old() {
return notFound404_old(locale.getString(ErrorPageLang.UNKNOWN_PAGE_404));
}
public Response pageNotFound404() {
return notFound404(locale.getString(ErrorPageLang.UNKNOWN_PAGE_404));
}
@ -275,15 +253,6 @@ public class ResponseFactory {
return notFound404(locale.getString(ErrorPageLang.NOT_PLAYED_404));
}
@Deprecated
public ErrorResponse notFound404_old(String message) {
try {
return new NotFoundResponse(message, versionCheckSystem, files);
} catch (IOException e) {
return internalErrorResponse_old(e, "Failed to generate 404 page");
}
}
public Response notFound404(String message) {
try {
return Response.builder()

View File

@ -23,10 +23,8 @@ import com.djrapitops.plan.delivery.web.resolver.Resolver;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.webserver.auth.Authentication;
import com.djrapitops.plan.delivery.webserver.pages.*;
import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver;
import com.djrapitops.plan.delivery.webserver.response.OptionsResponse;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.delivery.webserver.resolver.*;
import com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver;
import com.djrapitops.plan.exceptions.WebUserAuthException;
import com.djrapitops.plan.exceptions.connection.BadRequestException;
import com.djrapitops.plan.exceptions.connection.ForbiddenException;
@ -109,7 +107,7 @@ public class ResponseResolver {
}
public NoAuthResolver noAuthResolverFor(Response response) {
return (request) -> Optional.of(response);
return request -> Optional.of(response);
}
public Response getResponse(RequestInternal request) {
@ -130,8 +128,9 @@ public class ResponseResolver {
}
private Response tryToGetResponse(RequestInternal internalRequest) throws WebException {
if ("OPTIONS" .equalsIgnoreCase(internalRequest.getRequestMethod())) {
return new OptionsResponse().toNewResponse();
if ("OPTIONS".equalsIgnoreCase(internalRequest.getRequestMethod())) {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
return Response.builder().setStatus(204).setContent(new byte[0]).build();
}
Optional<Authentication> authentication = internalRequest.getAuth();

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.delivery.webserver.auth;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.exceptions.WebUserAuthException;
/**
@ -26,6 +26,6 @@ import com.djrapitops.plan.exceptions.WebUserAuthException;
*/
public interface Authentication {
WebUser_old getWebUser() throws WebUserAuthException;
WebUser getWebUser() throws WebUserAuthException;
}

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.delivery.webserver.auth;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.exceptions.PassEncryptException;
import com.djrapitops.plan.exceptions.WebUserAuthException;
import com.djrapitops.plan.exceptions.database.DBOpException;
@ -45,7 +45,7 @@ public class BasicAuthentication implements Authentication {
}
@Override
public WebUser_old getWebUser() throws WebUserAuthException {
public WebUser getWebUser() throws WebUserAuthException {
String decoded = Base64Util.decode(authenticationString);
String[] userInfo = StringUtils.split(decoded, ':');
@ -62,7 +62,7 @@ public class BasicAuthentication implements Authentication {
}
try {
WebUser_old webUser = database.query(WebUserQueries.fetchWebUser(user))
WebUser webUser = database.query(WebUserQueries.fetchWebUser(user))
.orElseThrow(() -> new WebUserAuthException(FailReason.USER_DOES_NOT_EXIST, user));
boolean correctPass = PassEncryptUtil.verifyPassword(passwordRaw, webUser.getSaltedPassHash());

View File

@ -18,7 +18,7 @@ package com.djrapitops.plan.delivery.webserver.cache;
import com.djrapitops.plan.delivery.web.resolver.MimeType;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONResolver;
import com.djrapitops.plan.delivery.webserver.resolver.json.RootJSONResolver;
import com.djrapitops.plan.storage.file.ResourceCache;
import com.djrapitops.plugin.task.AbsRunnable;
import com.github.benmanes.caffeine.cache.Cache;
@ -72,17 +72,17 @@ public class JSONCache {
return new String(found, StandardCharsets.UTF_8);
}
public static <T> Response getOrCache(DataID dataID, Supplier<T> jsonResponseSupplier) {
public static <T> Response getOrCache(DataID dataID, Supplier<T> objectSupplier) {
return getOrCache(dataID.name(), () -> Response.builder()
.setMimeType(MimeType.JSON)
.setJSONContent(jsonResponseSupplier.get())
.setJSONContent(objectSupplier.get())
.build());
}
public static <T> Response getOrCache(DataID dataID, UUID serverUUID, Supplier<T> jsonResponseSupplier) {
public static <T> Response getOrCache(DataID dataID, UUID serverUUID, Supplier<T> objectSupplier) {
return getOrCache(dataID.of(serverUUID), () -> Response.builder()
.setMimeType(MimeType.JSON)
.setJSONContent(jsonResponseSupplier.get())
.setJSONContent(objectSupplier.get())
.build());
}

View File

@ -1,110 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages;
import com.djrapitops.plan.delivery.webserver.RequestInternal;
import com.djrapitops.plan.delivery.webserver.RequestTarget;
import com.djrapitops.plan.delivery.webserver.auth.Authentication;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.delivery.webserver.response.Response_old;
import com.djrapitops.plan.exceptions.WebUserAuthException;
import com.djrapitops.plan.exceptions.connection.WebException;
import java.util.HashMap;
import java.util.Map;
/**
* Extended {@link PageResolver} to implement layered resolution of addresses.
* <p>
* Allows resolving /example/foo and /example/bar with different PageResolvers as if the address being resolved
* was at the root of the address (/example/foo - FooPageResolver sees /foo).
* <p>
* Tree-like pattern for URL resolution.
*
* @author Rsl1122
*/
@Deprecated
public abstract class CompositePageResolver implements PageResolver {
protected final ResponseFactory responseFactory;
private final Map<String, PageResolver> pages;
public CompositePageResolver(ResponseFactory responseFactory) {
this.responseFactory = responseFactory;
pages = new HashMap<>();
}
@Deprecated
public void registerPage(String targetPage, PageResolver resolver) {
pages.put(targetPage, resolver);
}
@Deprecated
public void registerPage(String targetPage, PageResolver resolver, int requiredPerm) {
pages.put(targetPage, new PageResolver() {
@Override
public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException {
return resolver.resolve(request, target);
}
@Override
public boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException {
return auth.getWebUser().getPermLevel() <= requiredPerm;
}
});
}
@Deprecated
public void registerPage(String targetPage, Response_old response, int requiredPerm) {
pages.put(targetPage, new PageResolver() {
@Override
public Response_old resolve(RequestInternal request, RequestTarget target) {
return response;
}
@Override
public boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException {
return auth.getWebUser().getPermLevel() <= requiredPerm;
}
});
}
@Override
@Deprecated
public Response_old resolve(RequestInternal request, RequestTarget target) throws WebException {
PageResolver pageResolver = getPageResolver(target);
return pageResolver != null
? pageResolver.resolve(request, target)
: responseFactory.pageNotFound404_old();
}
@Deprecated
public PageResolver getPageResolver(RequestTarget target) {
if (target.isEmpty()) {
return pages.get("");
}
String targetPage = target.get(0);
target.removeFirst();
return pages.get(targetPage);
}
@Deprecated
public PageResolver getPageResolver(String targetPage) {
return pages.get(targetPage);
}
}

View File

@ -1,48 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages;
import com.djrapitops.plan.delivery.webserver.RequestInternal;
import com.djrapitops.plan.delivery.webserver.RequestTarget;
import com.djrapitops.plan.delivery.webserver.auth.Authentication;
import com.djrapitops.plan.delivery.webserver.response.Response_old;
import com.djrapitops.plan.exceptions.WebUserAuthException;
import com.djrapitops.plan.exceptions.connection.WebException;
/**
* Used for Response resolution and authorization.
*
* @author Rsl1122
* @see CompositePageResolver for larger depth resolution than 1.
*/
@Deprecated
public interface PageResolver {
/**
* Resolve the request to a response.
*
* @param request Request in case it is useful for choosing page.
* @param target Rest of the target coordinates after this page has been solved.
* @return Appropriate response.
*/
Response_old resolve(RequestInternal request, RequestTarget target) throws WebException;
default boolean isAuthorized(Authentication auth, RequestTarget target) throws WebUserAuthException {
return true;
}
}

View File

@ -14,12 +14,12 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages;
package com.djrapitops.plan.delivery.webserver.resolver;
import com.djrapitops.plan.delivery.web.resolver.Resolver;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.delivery.webserver.ResponseFactory;
import javax.inject.Inject;
import javax.inject.Singleton;

View File

@ -14,14 +14,14 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages;
package com.djrapitops.plan.delivery.webserver.resolver;
import com.djrapitops.plan.delivery.web.resolver.Resolver;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.web.resolver.request.URIPath;
import com.djrapitops.plan.delivery.web.resolver.request.WebUser;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.delivery.webserver.ResponseFactory;
import com.djrapitops.plan.identification.UUIDUtility;
import javax.inject.Inject;

View File

@ -14,12 +14,12 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages;
package com.djrapitops.plan.delivery.webserver.resolver;
import com.djrapitops.plan.delivery.web.resolver.Resolver;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.delivery.webserver.ResponseFactory;
import javax.inject.Inject;
import javax.inject.Singleton;

View File

@ -14,15 +14,15 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages;
package com.djrapitops.plan.delivery.webserver.resolver;
import com.djrapitops.plan.delivery.rendering.html.Html;
import com.djrapitops.plan.delivery.web.resolver.NoAuthResolver;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.web.resolver.request.WebUser;
import com.djrapitops.plan.delivery.webserver.ResponseFactory;
import com.djrapitops.plan.delivery.webserver.WebServer;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
import dagger.Lazy;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages;
package com.djrapitops.plan.delivery.webserver.resolver;
import com.djrapitops.plan.delivery.rendering.html.Html;
import com.djrapitops.plan.delivery.web.resolver.Resolver;
@ -22,8 +22,8 @@ import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.web.resolver.request.URIPath;
import com.djrapitops.plan.delivery.web.resolver.request.WebUser;
import com.djrapitops.plan.delivery.webserver.ResponseFactory;
import com.djrapitops.plan.delivery.webserver.WebServer;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.storage.database.DBSystem;
@ -65,8 +65,8 @@ public class ServerPageResolver implements Resolver {
public boolean canAccess(Request request) {
String firstPart = request.getPath().getPart(0).orElse("");
WebUser permissions = request.getUser().orElse(new WebUser(""));
boolean forServerPage = firstPart.equalsIgnoreCase("server") && permissions.hasPermission("page.server");
boolean forNetworkPage = firstPart.equalsIgnoreCase("network") && permissions.hasPermission("page.network");
boolean forServerPage = "server".equalsIgnoreCase(firstPart) && permissions.hasPermission("page.server");
boolean forNetworkPage = "network".equalsIgnoreCase(firstPart) && permissions.hasPermission("page.network");
return forServerPage || forNetworkPage;
}

View File

@ -14,12 +14,12 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages;
package com.djrapitops.plan.delivery.webserver.resolver;
import com.djrapitops.plan.delivery.web.resolver.NoAuthResolver;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.delivery.webserver.ResponseFactory;
import org.apache.commons.lang3.StringUtils;
import javax.inject.Inject;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages.json;
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.rendering.json.graphs.GraphJSONCreator;
import com.djrapitops.plan.delivery.web.resolver.Resolver;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages.json;
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.rendering.json.JSONFactory;
import com.djrapitops.plan.delivery.rendering.json.network.NetworkOverviewJSONCreator;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages.json;
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.rendering.json.network.NetworkTabJSONCreator;
import com.djrapitops.plan.delivery.web.resolver.Resolver;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages.json;
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.rendering.json.PlayerJSONCreator;
import com.djrapitops.plan.delivery.web.resolver.MimeType;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages.json;
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.rendering.json.JSONFactory;
import com.djrapitops.plan.delivery.web.resolver.Resolver;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages.json;
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.rendering.json.JSONFactory;
import com.djrapitops.plan.delivery.web.resolver.Resolver;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages.json;
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.rendering.json.*;
import com.djrapitops.plan.delivery.web.resolver.CompositeResolver;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages.json;
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.rendering.json.ServerTabJSONCreator;
import com.djrapitops.plan.delivery.web.resolver.Resolver;

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.pages.json;
package com.djrapitops.plan.delivery.webserver.resolver.json;
import com.djrapitops.plan.delivery.rendering.json.JSONFactory;
import com.djrapitops.plan.delivery.web.resolver.Resolver;

View File

@ -1,60 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* {@link Response_old} for raw bytes.
*
* @author Rsl1122
*/
public class ByteResponse extends Response_old {
private final PlanFiles files;
private final String fileName;
public ByteResponse(ResponseType type, String fileName, PlanFiles files) {
super(type);
this.fileName = fileName;
this.files = files;
setHeader("HTTP/1.1 200 OK");
}
@Override
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
responseHeaders.set("Accept-Ranges", "bytes");
exchange.sendResponseHeaders(getCode(), 0);
try (OutputStream out = exchange.getResponseBody();
InputStream bis = files.getCustomizableResourceOrDefault(fileName).asInputStream()) {
byte[] buffer = new byte[2048];
int count;
while ((count = bis.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
}
}
}

View File

@ -1,42 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
/**
* @author Rsl1122
*/
public class CSSResponse extends FileResponse {
public CSSResponse(String fileName, PlanFiles files) throws IOException {
super(format(fileName), files);
super.setType(ResponseType.CSS);
setContent(getContent());
}
@Override
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
fixThemeColors(theme);
super.send(exchange, locale, theme);
}
}

View File

@ -1,55 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plugin.utilities.Verify;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
/**
* Response class for returning file contents.
* <p>
* Created to remove copy-paste.
*
* @author Rsl1122
*/
public class FileResponse extends Response_old {
public FileResponse(String fileName, PlanFiles files) throws IOException {
super.setHeader("HTTP/1.1 200 OK");
super.setContent(files.getCustomizableResourceOrDefault(fileName).asString());
}
public static String format(String fileName) {
String[] split = StringUtils.split(fileName, '/');
int i;
for (i = 0; i < split.length; i++) {
String s = split[i];
if (Verify.equalsOne(s, "css", "js", "vendor", "img")) {
break;
}
}
StringBuilder b = new StringBuilder("web");
for (int j = i; j < split.length; j++) {
String s = split[j];
b.append('/').append(s);
}
return b.toString();
}
}

View File

@ -1,34 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.storage.file.PlanFiles;
import java.io.IOException;
/**
* @author Rsl1122
*/
public class JavaScriptResponse extends FileResponse {
JavaScriptResponse(String fileName, PlanFiles files, Locale locale) throws IOException {
super(format(fileName), files);
super.translate(locale::replaceLanguageInJavascript);
super.setType(ResponseType.JAVASCRIPT);
}
}

View File

@ -1,30 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
/**
* Response for an OPTIONS request.
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
*
* @author Rsl1122
*/
public class OptionsResponse extends Response_old {
public OptionsResponse() {
setHeader("HTTP/1.1 204 No Content");
}
}

View File

@ -1,102 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
import com.djrapitops.plan.delivery.rendering.html.icon.Icon;
import com.djrapitops.plan.delivery.webserver.auth.FailReason;
import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse;
import com.djrapitops.plan.exceptions.WebUserAuthException;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.version.VersionCheckSystem;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Rsl1122
*/
public class PromptAuthorizationResponse extends ErrorResponse {
private static final String TIPS = "<br>- Ensure you have registered a user with <b>/plan register</b><br>"
+ "- Check that the username and password are correct<br>"
+ "- Username and password are case-sensitive<br>"
+ "<br>If you have forgotten your password, ask a staff member to delete your old user and re-register.";
private PromptAuthorizationResponse(VersionCheckSystem versionCheckSystem, PlanFiles files) throws IOException {
super(versionCheckSystem, files);
super.setTitle(Icon.called("lock").build() + " 401 Unauthorized");
}
public static PromptAuthorizationResponse getBasicAuthResponse(VersionCheckSystem versionCheckSystem, PlanFiles files) throws IOException {
PromptAuthorizationResponse response = new PromptAuthorizationResponse(versionCheckSystem, files);
response.setHeader("HTTP/1.1 401 Access Denied\r\n"
+ "WWW-Authenticate: Basic realm=\"Plan WebUser (/plan register)\"");
response.setParagraph("Authentication Failed." + TIPS);
response.replacePlaceholders();
return response;
}
public static PromptAuthorizationResponse getBasicAuthResponse(WebUserAuthException e, VersionCheckSystem versionCheckSystem, PlanFiles files) throws IOException {
PromptAuthorizationResponse response = new PromptAuthorizationResponse(versionCheckSystem, files);
FailReason failReason = e.getFailReason();
String reason = failReason.getReason();
if (failReason == FailReason.ERROR) {
StringBuilder errorBuilder = new StringBuilder("</p><pre>");
for (String line : getStackTrace(e.getCause())) {
errorBuilder.append(line);
}
errorBuilder.append("</pre>");
reason += errorBuilder.toString();
}
response.setHeader("HTTP/1.1 401 Access Denied\r\n"
+ "WWW-Authenticate: Basic realm=\"" + failReason.getReason() + "\"");
response.setParagraph("Authentication Failed.</p><p><b>Reason: " + reason + "</b></p><p>" + TIPS);
response.replacePlaceholders();
return response;
}
/**
* Gets lines for stack trace recursively.
*
* @param throwable Throwable element
* @return lines of stack trace.
*/
private static List<String> getStackTrace(Throwable throwable) {
List<String> stackTrace = new ArrayList<>();
stackTrace.add(throwable.toString());
for (StackTraceElement element : throwable.getStackTrace()) {
stackTrace.add(" " + element.toString());
}
Throwable cause = throwable.getCause();
if (cause != null) {
List<String> causeTrace = getStackTrace(cause);
if (!causeTrace.isEmpty()) {
causeTrace.set(0, "Caused by: " + causeTrace.get(0));
stackTrace.addAll(causeTrace);
}
}
return stackTrace;
}
}

View File

@ -1,42 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
/**
* @author Rsl1122
*/
public class RedirectResponse extends Response_old {
private final String direct;
public RedirectResponse(String direct) {
super.setHeader("HTTP/1.1 302 Found");
this.direct = direct;
}
@Override
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
responseHeaders.set("Location", direct);
super.send(exchange, locale, theme);
}
}

View File

@ -1,46 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
/**
* Enum for HTTP content-type response header Strings.
*
* @author Rsl1122
*/
public enum ResponseType {
HTML("text/html; charset=utf-8"),
CSS("text/css"),
JSON("application/json"),
JAVASCRIPT("application/javascript"),
IMAGE("image/gif"),
X_ICON("image/x-icon"),
FONT_TTF("application/x-font-ttf"),
FONT_WOFF("application/font-woff"),
FONT_WOFF2("application/font-woff2"),
FONT_EOT("application/vnd.ms-fontobject"),
FONT_BYTESTREAM("application/octet-stream");
private final String type;
ResponseType(String type) {
this.type = type;
}
public String get() {
return type;
}
}

View File

@ -1,159 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
import com.djrapitops.plan.delivery.web.resolver.Response;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.zip.GZIPOutputStream;
/**
* @author Rsl1122
*/
@Deprecated
public abstract class Response_old {
private String type;
private String header;
private String content;
protected Headers responseHeaders;
public Response_old(ResponseType type) {
this.type = type.get();
}
/**
* Default Response constructor that defaults ResponseType to HTML.
*/
public Response_old() {
this(ResponseType.HTML);
}
protected String getHeader() {
return header;
}
public Optional<String> getHeader(String called) {
if (header != null) {
for (String header : StringUtils.split(header, "\r\n")) {
if (called == null) {
return Optional.of(header);
}
if (StringUtils.startsWith(header, called)) {
return Optional.of(StringUtils.split(header, ':')[1].trim());
}
}
}
return Optional.empty();
}
public void setHeader(String header) {
this.header = header;
}
public String getResponse() {
return header + "\r\n"
+ "Content-Type: " + type + ";\r\n"
+ "Content-Length: " + content.length() + "\r\n"
+ "\r\n"
+ content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getCode() {
return getHeader(null).map(h -> Integer.parseInt(StringUtils.split(h, ' ')[1])).orElse(500);
}
public Response toNewResponse() {
return Response.builder()
.setStatus(getCode())
.setMimeType(type)
.setContent(content)
.build();
}
@Override
public int hashCode() {
return Objects.hash(header, content);
}
protected void setType(ResponseType type) {
this.type = type.get();
}
public void setResponseHeaders(Headers responseHeaders) {
this.responseHeaders = responseHeaders;
}
protected void translate(Function<String, String> translator) {
content = translator.apply(content);
}
protected void fixThemeColors(Theme theme) {
content = theme.replaceThemeColors(content);
}
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
responseHeaders.set("Content-Type", type);
responseHeaders.set("Content-Encoding", "gzip");
// TODO handle case of ByteResponse when "Accept-Ranges", "bytes" is set
exchange.sendResponseHeaders(getCode(), 0);
try (
GZIPOutputStream out = new GZIPOutputStream(exchange.getResponseBody());
ByteArrayInputStream bis = new ByteArrayInputStream((content != null ? content : "").getBytes(StandardCharsets.UTF_8))
) {
byte[] buffer = new byte[2048];
int count;
while ((count = bis.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
}
}
@Override
public String toString() {
return header + " | " + getResponse();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response_old response = (Response_old) o;
return Objects.equals(header, response.header) &&
Objects.equals(content, response.content);
}
}

View File

@ -1,30 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response;
/**
* Response for raw text.
*
* @author Rsl1122
*/
public class TextResponse extends Response_old {
public TextResponse(String content) {
setHeader("HTTP/1.1 200 OK");
setContent(content);
}
}

View File

@ -1,46 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.data;
import com.djrapitops.plan.delivery.webserver.response.ResponseType;
import com.djrapitops.plan.delivery.webserver.response.Response_old;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
/**
* Generic JSON response implemented using Gson.
* <p>
* Returns a JSON version of the given object.
*
* @author Rsl1122
*/
public class JSONResponse extends Response_old {
public JSONResponse(Object object) {
this(new Gson().toJson(object));
}
public JSONResponse(JsonElement json) {
this(json.getAsString());
}
public JSONResponse(String jsonString) {
super(ResponseType.JSON);
super.setHeader("HTTP/1.1 200 OK");
super.setContent(jsonString);
}
}

View File

@ -1,30 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.errors;
import com.djrapitops.plan.delivery.webserver.response.Response_old;
/**
* @author Fuzzlemann
*/
public class BadRequestResponse extends Response_old {
public BadRequestResponse(String error) {
super.setHeader("HTTP/1.1 400 Bad Request " + error);
super.setContent("400 Bad Request: " + error);
}
}

View File

@ -1,97 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.errors;
import com.djrapitops.plan.delivery.rendering.html.Contributors;
import com.djrapitops.plan.delivery.webserver.response.pages.PageResponse;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.version.VersionCheckSystem;
import com.sun.net.httpserver.HttpExchange;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* Represents generic HTTP Error response that has the page style in it.
*
* @author Rsl1122
*/
public class ErrorResponse extends PageResponse {
private String title;
private String paragraph;
private VersionCheckSystem versionCheckSystem;
public ErrorResponse(VersionCheckSystem versionCheckSystem, PlanFiles files) throws IOException {
this.versionCheckSystem = versionCheckSystem;
setContent(files.getCustomizableResourceOrDefault("web/error.html").asString());
}
public ErrorResponse(String message) {
setContent(message);
}
public void replacePlaceholders() {
Map<String, String> placeholders = new HashMap<>();
placeholders.put("title", title);
String[] split = StringUtils.split(title, ">", 3);
placeholders.put("titleText", split.length == 3 ? split[2] : title);
placeholders.put("paragraph", paragraph);
placeholders.put("version", versionCheckSystem.getUpdateButton().orElse(versionCheckSystem.getCurrentVersionButton()));
placeholders.put("updateModal", versionCheckSystem.getUpdateModal());
placeholders.put("contributors", Contributors.generateContributorHtml());
setContent(StringSubstitutor.replace(getContent(), placeholders));
}
public void setTitle(String title) {
this.title = title;
}
public void setParagraph(String paragraph) {
this.paragraph = paragraph;
}
@Override
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
translate(locale::replaceLanguageInHtml);
fixThemeColors(theme);
super.send(exchange, locale, theme);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ErrorResponse)) return false;
if (!super.equals(o)) return false;
ErrorResponse that = (ErrorResponse) o;
return Objects.equals(title, that.title) &&
Objects.equals(paragraph, that.paragraph);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), title, paragraph);
}
}

View File

@ -1,37 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.errors;
import com.djrapitops.plan.delivery.rendering.html.icon.Family;
import com.djrapitops.plan.delivery.rendering.html.icon.Icon;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.version.VersionCheckSystem;
import java.io.IOException;
/**
* @author Rsl1122
*/
public class ForbiddenResponse extends ErrorResponse {
public ForbiddenResponse(String msg, VersionCheckSystem versionCheckSystem, PlanFiles files) throws IOException {
super(versionCheckSystem, files);
super.setHeader("HTTP/1.1 403 Forbidden");
super.setTitle(Icon.called("hand-paper").of(Family.REGULAR) + " 403 Forbidden - Access Denied");
super.setParagraph(msg);
super.replacePlaceholders();
}
}

View File

@ -1,67 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.errors;
import com.djrapitops.plan.delivery.rendering.html.Html;
import com.djrapitops.plan.delivery.rendering.html.icon.Icon;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.version.VersionCheckSystem;
import java.io.IOException;
/**
* @author Rsl1122
*/
public class InternalErrorResponse extends ErrorResponse {
public InternalErrorResponse(String cause, Throwable e, VersionCheckSystem versionCheckSystem, PlanFiles files) throws IOException {
super(versionCheckSystem, files);
super.setHeader("HTTP/1.1 500 Internal Error");
super.setTitle(Icon.called("bug") + " 500 Internal Error occurred");
StringBuilder paragraph = new StringBuilder();
paragraph.append("Please report this issue here: ");
paragraph.append(Html.LINK.create("https://github.com/Rsl1122/Plan-PlayerAnalytics/issues", "Issues"));
paragraph.append("<br><br><pre>");
paragraph.append(e).append(" | ").append(cause);
for (StackTraceElement element : e.getStackTrace()) {
paragraph.append("<br>");
paragraph.append(" ").append(element);
}
if (e.getCause() != null) {
appendCause(e.getCause(), paragraph);
}
paragraph.append("</pre>");
super.setParagraph(paragraph.toString());
super.replacePlaceholders();
}
private void appendCause(Throwable cause, StringBuilder paragraph) {
paragraph.append("<br>Caused by: ").append(cause);
for (StackTraceElement element : cause.getStackTrace()) {
paragraph.append("<br>");
paragraph.append(" ").append(element);
}
if (cause.getCause() != null) {
appendCause(cause.getCause(), paragraph);
}
}
}

View File

@ -1,40 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.errors;
import com.djrapitops.plan.delivery.rendering.html.icon.Icon;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.version.VersionCheckSystem;
import java.io.IOException;
/**
* Generic 404 response.
*
* @author Rsl1122
*/
public class NotFoundResponse extends ErrorResponse {
public NotFoundResponse(String msg, VersionCheckSystem versionCheckSystem, PlanFiles files) throws IOException {
super(versionCheckSystem, files);
super.setHeader("HTTP/1.1 404 Not Found");
super.setTitle(Icon.called("map-signs") + " 404 Not Found");
super.setParagraph(msg);
super.replacePlaceholders();
}
}

View File

@ -1,42 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.pages;
import com.djrapitops.plan.delivery.rendering.html.icon.Icon;
import com.djrapitops.plan.delivery.rendering.pages.DebugPage;
import com.djrapitops.plan.delivery.webserver.response.errors.ErrorResponse;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.version.VersionCheckSystem;
import java.io.IOException;
/**
* WebServer response for /debug-page used for easing issue reporting.
*
* @author Rsl1122
*/
public class DebugPageResponse extends ErrorResponse {
public DebugPageResponse(DebugPage debugPage, VersionCheckSystem versionCheckSystem, PlanFiles files) throws IOException {
super(versionCheckSystem, files);
super.setHeader("HTTP/1.1 200 OK");
super.setTitle(Icon.called("bug") + " Debug Information");
super.setParagraph(debugPage.toHtml());
replacePlaceholders();
}
}

View File

@ -1,66 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.pages;
import com.djrapitops.plan.delivery.rendering.pages.Page;
import com.djrapitops.plan.delivery.webserver.response.ResponseType;
import com.djrapitops.plan.delivery.webserver.response.Response_old;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
/**
* Response for all HTML Page responses.
*
* @author Rsl1122
*/
public class PageResponse extends Response_old {
private static final HtmlCompressor HTML_COMPRESSOR = new HtmlCompressor();
static {
HTML_COMPRESSOR.setRemoveIntertagSpaces(true);
}
public PageResponse(ResponseType type) {
super(type);
}
public PageResponse(Page page) {
this(ResponseType.HTML);
super.setHeader("HTTP/1.1 200 OK");
setContent(page.toHtml());
}
public PageResponse() {
}
@Override
public void send(HttpExchange exchange, Locale locale, Theme theme) throws IOException {
translate(locale::replaceLanguageInHtml);
fixThemeColors(theme);
super.send(exchange, locale, theme);
}
@Override
public void setContent(String content) {
super.setContent(HTML_COMPRESSOR.compress(content));
}
}

View File

@ -1,30 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.pages;
import com.djrapitops.plan.delivery.rendering.pages.PlayersPage;
/**
* @author Rsl1122
*/
public class PlayersPageResponse extends PageResponse {
public PlayersPageResponse(PlayersPage playersPage) {
setHeader("HTTP/1.1 200 OK");
setContent(playersPage.toHtml());
}
}

View File

@ -1,75 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.pages;
import com.djrapitops.plan.delivery.domain.container.DataContainer;
import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse;
import com.djrapitops.plan.utilities.java.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Response for sending raw data as JSON when it is inside a DataContainer.
* <p>
* This transform class is required to remove Key-Supplier object pollution in the resulting JSON, as well as to remove
* the effects of the caching layer.
*
* @author Rsl1122
*/
public class RawDataResponse extends JSONResponse {
public RawDataResponse(DataContainer dataContainer) {
super(mapToNormalMap(dataContainer));
}
public static Map<String, Object> mapToNormalMap(DataContainer player) {
Map<String, Object> values = new HashMap<>();
player.getMap().forEach((key, value) ->
{
if (value instanceof DataContainer) {
value = mapToNormalMap((DataContainer) value);
}
if (value instanceof Map) {
value = handleMap((Map<?, ?>) value);
}
if (value instanceof List) {
value = handleList((List<?>) value);
}
values.put(key.getKeyName(), value);
}
);
return values;
}
private static List<?> handleList(List<?> list) {
if (list.stream().findAny().orElse(null) instanceof DataContainer) {
return Lists.map(list, obj -> mapToNormalMap((DataContainer) obj));
}
return list;
}
private static Map<?, ?> handleMap(Map<?, ?> map) {
if (map.values().stream().findAny().orElse(null) instanceof DataContainer) {
Map<Object, Object> newMap = new HashMap<>();
map.forEach((key, value) -> newMap.put(key, mapToNormalMap((DataContainer) value)));
return newMap;
}
return map;
}
}

View File

@ -1,31 +0,0 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.delivery.webserver.response.pages;
import com.djrapitops.plan.delivery.domain.container.PlayerContainer;
/**
* Raw Data JSON response for a Player.
*
* @author Rsl1122
*/
public class RawPlayerDataResponse extends RawDataResponse {
public RawPlayerDataResponse(PlayerContainer playerContainer) {
super(playerContainer);
}
}

View File

@ -17,7 +17,6 @@
package com.djrapitops.plan.gathering.cache;
import com.djrapitops.plan.SubSystem;
import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.gathering.geolocation.GeolocationCache;
import javax.inject.Inject;
@ -47,7 +46,7 @@ public class CacheSystem implements SubSystem {
}
@Override
public void enable() throws EnableException {
public void enable() {
nicknameCache.enable();
geolocationCache.enable();
}

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.storage.database.queries;
import com.djrapitops.plan.delivery.domain.Nickname;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.delivery.domain.keys.SessionKeys;
import com.djrapitops.plan.gathering.domain.*;
import com.djrapitops.plan.identification.Server;
@ -119,7 +119,7 @@ public class LargeStoreQueries {
* @param users Collection of Plan WebUsers.
* @return Executable, use inside a {@link com.djrapitops.plan.storage.database.transactions.Transaction}
*/
public static Executable storeAllPlanWebUsers(Collection<WebUser_old> users) {
public static Executable storeAllPlanWebUsers(Collection<WebUser> users) {
if (Verify.isEmpty(users)) {
return Executable.empty();
}
@ -127,7 +127,7 @@ public class LargeStoreQueries {
return new ExecBatchStatement(SecurityTable.INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (WebUser_old user : users) {
for (WebUser user : users) {
String userName = user.getName();
String pass = user.getSaltedPassHash();
int permLvl = user.getPermLevel();

View File

@ -16,7 +16,6 @@
*/
package com.djrapitops.plan.storage.database.queries.objects;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.gathering.domain.Ping;
import com.djrapitops.plan.storage.database.queries.Query;
import com.djrapitops.plan.storage.database.queries.QueryAllStatement;
@ -33,7 +32,7 @@ import java.util.*;
import static com.djrapitops.plan.storage.database.sql.building.Sql.*;
/**
* Queries for {@link WebUser_old} objects.
* Queries for {@link Ping} objects.
*
* @author Rsl1122
*/

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.storage.database.queries.objects;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.storage.database.queries.Query;
import com.djrapitops.plan.storage.database.queries.QueryAllStatement;
import com.djrapitops.plan.storage.database.queries.QueryStatement;
@ -32,7 +32,7 @@ import java.util.Optional;
import static com.djrapitops.plan.storage.database.sql.building.Sql.*;
/**
* Queries for {@link WebUser_old} objects.
* Queries for {@link WebUser} objects.
*
* @author Rsl1122
*/
@ -47,18 +47,18 @@ public class WebUserQueries {
*
* @return List of Plan WebUsers.
*/
public static Query<List<WebUser_old>> fetchAllPlanWebUsers() {
public static Query<List<WebUser>> fetchAllPlanWebUsers() {
String sql = SELECT + '*' + FROM + SecurityTable.TABLE_NAME + ORDER_BY + SecurityTable.PERMISSION_LEVEL + " ASC";
return new QueryAllStatement<List<WebUser_old>>(sql, 5000) {
return new QueryAllStatement<List<WebUser>>(sql, 5000) {
@Override
public List<WebUser_old> processResults(ResultSet set) throws SQLException {
List<WebUser_old> list = new ArrayList<>();
public List<WebUser> processResults(ResultSet set) throws SQLException {
List<WebUser> list = new ArrayList<>();
while (set.next()) {
String user = set.getString(SecurityTable.USERNAME);
String saltedPassHash = set.getString(SecurityTable.SALT_PASSWORD_HASH);
int permissionLevel = set.getInt(SecurityTable.PERMISSION_LEVEL);
WebUser_old info = new WebUser_old(user, saltedPassHash, permissionLevel);
WebUser info = new WebUser(user, saltedPassHash, permissionLevel);
list.add(info);
}
return list;
@ -66,21 +66,21 @@ public class WebUserQueries {
};
}
public static Query<Optional<WebUser_old>> fetchWebUser(String called) {
public static Query<Optional<WebUser>> fetchWebUser(String called) {
String sql = SELECT + '*' + FROM + SecurityTable.TABLE_NAME +
WHERE + SecurityTable.USERNAME + "=? LIMIT 1";
return new QueryStatement<Optional<WebUser_old>>(sql) {
return new QueryStatement<Optional<WebUser>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, called);
}
@Override
public Optional<WebUser_old> processResults(ResultSet set) throws SQLException {
public Optional<WebUser> processResults(ResultSet set) throws SQLException {
if (set.next()) {
String saltedPassHash = set.getString(SecurityTable.SALT_PASSWORD_HASH);
int permissionLevel = set.getInt(SecurityTable.PERMISSION_LEVEL);
return Optional.of(new WebUser_old(called, saltedPassHash, permissionLevel));
return Optional.of(new WebUser(called, saltedPassHash, permissionLevel));
}
return Optional.empty();
}

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.storage.database.transactions.commands;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.storage.database.sql.tables.SecurityTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Transaction;
@ -25,15 +25,15 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Transaction to save a new Plan {@link WebUser_old} to the database.
* Transaction to save a new Plan {@link WebUser} to the database.
*
* @author Rsl1122
*/
public class RegisterWebUserTransaction extends Transaction {
private final WebUser_old webUser;
private final WebUser webUser;
public RegisterWebUserTransaction(WebUser_old webUser) {
public RegisterWebUserTransaction(WebUser webUser) {
this.webUser = webUser;
}

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.storage.database.transactions.commands;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.storage.database.sql.tables.SecurityTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Transaction;
@ -28,7 +28,7 @@ import static com.djrapitops.plan.storage.database.sql.building.Sql.DELETE_FROM;
import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE;
/**
* Transaction to remove a Plan {@link WebUser_old} from the database.
* Transaction to remove a Plan {@link WebUser} from the database.
*
* @author Rsl1122
*/

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.utilities.comparators;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import java.util.Comparator;
@ -25,10 +25,10 @@ import java.util.Comparator;
*
* @author Rsl1122
*/
public class WebUserComparator implements Comparator<WebUser_old> {
public class WebUserComparator implements Comparator<WebUser> {
@Override
public int compare(WebUser_old o1, WebUser_old o2) {
public int compare(WebUser o1, WebUser o2) {
return Integer.compare(o2.getPermLevel(), o1.getPermLevel());
}

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.delivery.webserver;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.WebserverSettings;
import com.djrapitops.plan.storage.database.transactions.commands.RegisterWebUserTransaction;
@ -61,7 +61,7 @@ class JksHttpsServerTest implements HttpsServerTest {
system.enable();
WebUser_old webUser = new WebUser_old("test", PassEncryptUtil.createHash("testPass"), 0);
WebUser webUser = new WebUser("test", PassEncryptUtil.createHash("testPass"), 0);
system.getDatabaseSystem().getDatabase().executeTransaction(new RegisterWebUserTransaction(webUser));
}

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.delivery.webserver;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.changes.ConfigUpdater;
import com.djrapitops.plan.settings.config.paths.WebserverSettings;
@ -66,7 +66,7 @@ class Pkcs12HttpsServerTest implements HttpsServerTest {
system.enable();
WebUser_old webUser = new WebUser_old("test", PassEncryptUtil.createHash("testPass"), 0);
WebUser webUser = new WebUser("test", PassEncryptUtil.createHash("testPass"), 0);
system.getDatabaseSystem().getDatabase().executeTransaction(new RegisterWebUserTransaction(webUser));
}

View File

@ -16,7 +16,6 @@
*/
package com.djrapitops.plan.delivery.webserver.cache;
import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
@ -47,7 +46,7 @@ class JSONCacheTest {
@Test
void cachedByDataIDName() {
JSONCache.getOrCache(TEST_ID, () -> new JSONResponse(CACHED));
JSONCache.getOrCache(TEST_ID, () -> CACHED);
assertContains();
}
@ -77,7 +76,7 @@ class JSONCacheTest {
@Test
void cachedByServerUUID() {
JSONCache.getOrCache(TEST_ID, TEST_UUID, () -> new JSONResponse(CACHED));
JSONCache.getOrCache(TEST_ID, TEST_UUID, () -> CACHED);
assertContainsUUID();
}

View File

@ -21,7 +21,7 @@ import com.djrapitops.plan.data.element.TableContainer;
import com.djrapitops.plan.delivery.domain.DateObj;
import com.djrapitops.plan.delivery.domain.Nickname;
import com.djrapitops.plan.delivery.domain.TablePlayer;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.delivery.domain.container.PlayerContainer;
import com.djrapitops.plan.delivery.domain.container.ServerContainer;
import com.djrapitops.plan.delivery.domain.keys.Key;
@ -208,11 +208,11 @@ public interface DatabaseTest {
@Test
default void webUserIsRegistered() {
WebUser_old expected = new WebUser_old(TestConstants.PLAYER_ONE_NAME, "RandomGarbageBlah", 0);
WebUser expected = new WebUser(TestConstants.PLAYER_ONE_NAME, "RandomGarbageBlah", 0);
db().executeTransaction(new RegisterWebUserTransaction(expected));
commitTest();
Optional<WebUser_old> found = db().query(WebUserQueries.fetchWebUser(TestConstants.PLAYER_ONE_NAME));
Optional<WebUser> found = db().query(WebUserQueries.fetchWebUser(TestConstants.PLAYER_ONE_NAME));
assertTrue(found.isPresent());
assertEquals(expected, found.get());
}
@ -500,7 +500,7 @@ public interface DatabaseTest {
Collections.singletonList(new DateObj<>(System.currentTimeMillis(), r.nextInt())))
);
WebUser_old webUser = new WebUser_old(TestConstants.PLAYER_ONE_NAME, "RandomGarbageBlah", 0);
WebUser webUser = new WebUser(TestConstants.PLAYER_ONE_NAME, "RandomGarbageBlah", 0);
db().executeTransaction(new RegisterWebUserTransaction(webUser));
}

View File

@ -16,7 +16,7 @@
*/
package com.djrapitops.plan.utilities.comparators;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.delivery.domain.keys.SessionKeys;
import com.djrapitops.plan.delivery.rendering.json.graphs.line.Point;
import com.djrapitops.plan.gathering.domain.GeoInfo;
@ -83,14 +83,14 @@ class ComparatorTest {
@Test
void webUserComparator() throws PassEncryptUtil.CannotPerformOperationException {
List<WebUser_old> webUsers = RandomData.randomWebUsers();
List<WebUser> webUsers = RandomData.randomWebUsers();
List<Integer> expected = webUsers.stream().map(WebUser_old::getPermLevel)
List<Integer> expected = webUsers.stream().map(WebUser::getPermLevel)
.sorted(Integer::compare).collect(Collectors.toList());
Collections.reverse(expected);
webUsers.sort(new WebUserComparator());
List<Integer> result = Lists.map(webUsers, WebUser_old::getPermLevel);
List<Integer> result = Lists.map(webUsers, WebUser::getPermLevel);
assertEquals(expected, result);
}

View File

@ -16,19 +16,17 @@
*/
package utilities;
import com.djrapitops.plan.delivery.domain.WebUser_old;
import com.djrapitops.plan.delivery.domain.WebUser;
import com.djrapitops.plan.delivery.rendering.json.graphs.line.Point;
import com.djrapitops.plan.gathering.domain.GeoInfo;
import com.djrapitops.plan.gathering.domain.Session;
import com.djrapitops.plan.gathering.domain.TPS;
import com.djrapitops.plan.gathering.domain.UserInfo;
import com.djrapitops.plan.utilities.PassEncryptUtil;
import org.apache.commons.lang3.RandomStringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public class RandomData {
@ -51,10 +49,10 @@ public class RandomData {
return RandomStringUtils.randomAlphanumeric(size);
}
public static List<WebUser_old> randomWebUsers() throws PassEncryptUtil.CannotPerformOperationException {
List<WebUser_old> test = new ArrayList<>();
public static List<WebUser> randomWebUsers() throws PassEncryptUtil.CannotPerformOperationException {
List<WebUser> test = new ArrayList<>();
for (int i = 0; i < 20; i++) {
test.add(new WebUser_old(randomString(5), PassEncryptUtil.createHash(randomString(7)), r.nextInt()));
test.add(new WebUser(randomString(5), PassEncryptUtil.createHash(randomString(7)), r.nextInt()));
}
return test;
}
@ -89,20 +87,6 @@ public class RandomData {
return test;
}
public static <T extends Enum> T randomEnum(Class<T> clazz) {
int x = r.nextInt(clazz.getEnumConstants().length);
return clazz.getEnumConstants()[x];
}
public static List<UserInfo> randomUserData() {
List<UserInfo> test = new ArrayList<>();
for (int i = 0; i < 20; i++) {
UserInfo info = new UserInfo(UUID.randomUUID(), UUID.randomUUID(), r.nextLong(), r.nextBoolean(), r.nextBoolean());
test.add(info);
}
return test;
}
public static List<GeoInfo> randomGeoInfo() {
List<GeoInfo> test = new ArrayList<>();
for (int i = 0; i < 20; i++) {