mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-08-21 20:22:45 +02:00
[Debt] Delegated Exception logs to ResponseFactory
ErrorResponse caught an IOException for reading a file from the jar, but this error should be moved further up stream - thus catching it was delegated to ResponseFactory, where the error can be parsed into InternalErrorResponse and dealt with accordingly.
This commit is contained in:
parent
7f53fb4c80
commit
a48b660a05
|
@ -8,12 +8,11 @@ import com.djrapitops.plan.api.exceptions.connection.NotFoundException;
|
|||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.info.request.InfoRequest;
|
||||
import com.djrapitops.plan.system.locale.lang.ErrorPageLang;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.pages.PageHandler;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.BadRequestResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
|
@ -35,16 +34,18 @@ public class InfoRequestPageHandler implements PageHandler {
|
|||
|
||||
private final Database database;
|
||||
private final ConnectionSystem connectionSystem;
|
||||
private final ResponseFactory responseFactory;
|
||||
private final PluginLogger logger;
|
||||
|
||||
@Inject
|
||||
public InfoRequestPageHandler(
|
||||
Database database,
|
||||
ConnectionSystem connectionSystem,
|
||||
PluginLogger logger
|
||||
ResponseFactory responseFactory, PluginLogger logger
|
||||
) {
|
||||
this.database = database;
|
||||
this.connectionSystem = connectionSystem;
|
||||
this.responseFactory = responseFactory;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
@ -54,7 +55,7 @@ public class InfoRequestPageHandler implements PageHandler {
|
|||
|
||||
try {
|
||||
if (target.isEmpty()) {
|
||||
return new NotFoundResponse(request.getLocale().getString(ErrorPageLang.UNKNOWN_PAGE_404));
|
||||
return responseFactory.pageNotFound404();
|
||||
}
|
||||
|
||||
if (!request.getRequestMethod().equals("POST")) {
|
||||
|
|
|
@ -10,7 +10,7 @@ import com.djrapitops.plan.api.exceptions.database.DBException;
|
|||
import com.djrapitops.plan.system.info.InfoSystem;
|
||||
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.utilities.html.pages.PageFactory;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
|
@ -25,6 +25,7 @@ import java.util.UUID;
|
|||
public class GenerateInspectPageRequest extends InfoRequestWithVariables implements GenerateRequest {
|
||||
|
||||
private final InfoRequestFactory infoRequestFactory;
|
||||
private final ResponseFactory responseFactory;
|
||||
private final PageFactory pageFactory;
|
||||
private final InfoSystem infoSystem;
|
||||
|
||||
|
@ -32,21 +33,23 @@ public class GenerateInspectPageRequest extends InfoRequestWithVariables impleme
|
|||
|
||||
GenerateInspectPageRequest(
|
||||
InfoRequestFactory infoRequestFactory,
|
||||
PageFactory pageFactory,
|
||||
ResponseFactory responseFactory, PageFactory pageFactory,
|
||||
InfoSystem infoSystem
|
||||
) {
|
||||
this.infoRequestFactory = infoRequestFactory;
|
||||
this.responseFactory = responseFactory;
|
||||
this.pageFactory = pageFactory;
|
||||
this.infoSystem = infoSystem;
|
||||
}
|
||||
|
||||
public GenerateInspectPageRequest(
|
||||
GenerateInspectPageRequest(
|
||||
UUID uuid,
|
||||
InfoRequestFactory infoRequestFactory,
|
||||
PageFactory pageFactory,
|
||||
ResponseFactory responseFactory, PageFactory pageFactory,
|
||||
InfoSystem infoSystem
|
||||
) {
|
||||
this.infoRequestFactory = infoRequestFactory;
|
||||
this.responseFactory = responseFactory;
|
||||
this.pageFactory = pageFactory;
|
||||
this.infoSystem = infoSystem;
|
||||
|
||||
|
@ -75,7 +78,7 @@ public class GenerateInspectPageRequest extends InfoRequestWithVariables impleme
|
|||
html = getHtml(uuid);
|
||||
infoSystem.getConnectionSystem().sendWideInfoRequest(infoRequestFactory.generateInspectPluginsTabRequest(uuid));
|
||||
} catch (NotFoundException e) {
|
||||
html = new NotFoundResponse(e.getMessage()).getContent();
|
||||
html = responseFactory.notFound404(e.getMessage()).getContent();
|
||||
}
|
||||
infoSystem.sendRequest(infoRequestFactory.cacheInspectPageRequest(uuid, html));
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import com.djrapitops.plan.system.info.connection.ConnectionSystem;
|
|||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
import com.djrapitops.plan.system.processing.Processing;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.utilities.file.export.HtmlExport;
|
||||
import com.djrapitops.plan.utilities.html.pages.PageFactory;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
|
@ -31,6 +32,7 @@ public class InfoRequestFactory {
|
|||
private final Lazy<ConnectionSystem> connectionSystem;
|
||||
private final Lazy<ServerInfo> serverInfo;
|
||||
private final Lazy<InfoRequestFactory> infoRequestFactory;
|
||||
private final Lazy<ResponseFactory> responseFactory;
|
||||
private final Lazy<PageFactory> pageFactory;
|
||||
private final Lazy<HtmlExport> htmlExport;
|
||||
private final Lazy<PluginLogger> logger;
|
||||
|
@ -45,6 +47,7 @@ public class InfoRequestFactory {
|
|||
Lazy<ConnectionSystem> connectionSystem,
|
||||
Lazy<ServerInfo> serverInfo,
|
||||
Lazy<InfoRequestFactory> infoRequestFactory,
|
||||
Lazy<ResponseFactory> responseFactory,
|
||||
Lazy<PageFactory> pageFactory,
|
||||
Lazy<HtmlExport> htmlExport,
|
||||
Lazy<PluginLogger> logger,
|
||||
|
@ -57,6 +60,7 @@ public class InfoRequestFactory {
|
|||
this.connectionSystem = connectionSystem;
|
||||
this.serverInfo = serverInfo;
|
||||
this.infoRequestFactory = infoRequestFactory;
|
||||
this.responseFactory = responseFactory;
|
||||
this.pageFactory = pageFactory;
|
||||
this.htmlExport = htmlExport;
|
||||
this.logger = logger;
|
||||
|
@ -84,7 +88,7 @@ public class InfoRequestFactory {
|
|||
}
|
||||
|
||||
public GenerateRequest generateInspectPageRequest(UUID uuid) {
|
||||
return new GenerateInspectPageRequest(uuid, this, pageFactory.get(), infoSystem.get());
|
||||
return new GenerateInspectPageRequest(uuid, this, responseFactory.get(), pageFactory.get(), infoSystem.get());
|
||||
}
|
||||
|
||||
public GenerateInspectPluginsTabRequest generateInspectPluginsTabRequest(UUID uuid) {
|
||||
|
|
|
@ -6,6 +6,7 @@ import com.djrapitops.plan.system.info.connection.ConnectionSystem;
|
|||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||
import com.djrapitops.plan.system.processing.Processing;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.utilities.file.export.HtmlExport;
|
||||
import com.djrapitops.plan.utilities.html.pages.PageFactory;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
|
@ -30,6 +31,7 @@ public class InfoRequestHandlerFactory {
|
|||
private final Lazy<ConnectionSystem> connectionSystem;
|
||||
private final Lazy<ServerInfo> serverInfo;
|
||||
private final Lazy<InfoRequestFactory> infoRequestFactory;
|
||||
private final Lazy<ResponseFactory> responseFactory;
|
||||
private final Lazy<PageFactory> pageFactory;
|
||||
private final Lazy<HtmlExport> htmlExport;
|
||||
private final Lazy<PluginLogger> logger;
|
||||
|
@ -44,6 +46,7 @@ public class InfoRequestHandlerFactory {
|
|||
Lazy<ConnectionSystem> connectionSystem,
|
||||
Lazy<ServerInfo> serverInfo,
|
||||
Lazy<InfoRequestFactory> infoRequestFactory,
|
||||
Lazy<ResponseFactory> responseFactory,
|
||||
Lazy<PageFactory> pageFactory,
|
||||
Lazy<HtmlExport> htmlExport,
|
||||
Lazy<PluginLogger> logger,
|
||||
|
@ -56,6 +59,7 @@ public class InfoRequestHandlerFactory {
|
|||
this.connectionSystem = connectionSystem;
|
||||
this.serverInfo = serverInfo;
|
||||
this.infoRequestFactory = infoRequestFactory;
|
||||
this.responseFactory = responseFactory;
|
||||
this.pageFactory = pageFactory;
|
||||
this.htmlExport = htmlExport;
|
||||
this.logger = logger;
|
||||
|
@ -87,7 +91,7 @@ public class InfoRequestHandlerFactory {
|
|||
}
|
||||
|
||||
GenerateRequest generateInspectPageRequest() {
|
||||
return new GenerateInspectPageRequest(infoRequestFactory.get(), pageFactory.get(), infoSystem.get());
|
||||
return new GenerateInspectPageRequest(infoRequestFactory.get(), responseFactory.get(), pageFactory.get(), infoSystem.get());
|
||||
}
|
||||
|
||||
GenerateRequest generateInspectPluginsTabRequest() {
|
||||
|
|
|
@ -11,11 +11,9 @@ import com.djrapitops.plan.system.webserver.auth.Authentication;
|
|||
import com.djrapitops.plan.system.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.system.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.system.webserver.pages.*;
|
||||
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
|
||||
import com.djrapitops.plan.system.webserver.response.PromptAuthorizationResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.*;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.BadRequestResponse;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
import dagger.Lazy;
|
||||
|
@ -35,8 +33,6 @@ import java.util.Optional;
|
|||
@Singleton
|
||||
public class ResponseHandler extends TreePageHandler {
|
||||
|
||||
private final ResponseFactory responseFactory;
|
||||
|
||||
private final DebugPageHandler debugPageHandler;
|
||||
private final PlayersPageHandler playersPageHandler;
|
||||
private final PlayerPageHandler playerPageHandler;
|
||||
|
@ -59,8 +55,8 @@ public class ResponseHandler extends TreePageHandler {
|
|||
|
||||
ErrorHandler errorHandler
|
||||
) {
|
||||
super(responseFactory);
|
||||
this.webServer = webServer;
|
||||
this.responseFactory = responseFactory;
|
||||
this.debugPageHandler = debugPageHandler;
|
||||
this.playersPageHandler = playersPageHandler;
|
||||
this.playerPageHandler = playerPageHandler;
|
||||
|
@ -79,7 +75,7 @@ public class ResponseHandler extends TreePageHandler {
|
|||
registerPage("server", serverPageHandler);
|
||||
|
||||
if (webServer.get().isAuthRequired()) {
|
||||
registerPage("", new RootPageHandler());
|
||||
registerPage("", new RootPageHandler(responseFactory));
|
||||
} else {
|
||||
registerPage("", responseFactory.redirectResponse("/server"), 5);
|
||||
}
|
||||
|
@ -96,26 +92,26 @@ public class ResponseHandler extends TreePageHandler {
|
|||
try {
|
||||
return getResponse(request, targetString, target);
|
||||
} catch (NoServersException | NotFoundException e) {
|
||||
return new NotFoundResponse(e.getMessage());
|
||||
return responseFactory.notFound404(e.getMessage());
|
||||
} catch (WebUserAuthException e) {
|
||||
return PromptAuthorizationResponse.getBasicAuthResponse(e);
|
||||
return responseFactory.basicAuthFail(e);
|
||||
} catch (ForbiddenException e) {
|
||||
return new ForbiddenResponse(e.getMessage());
|
||||
return responseFactory.forbidden403(e.getMessage());
|
||||
} catch (BadRequestException e) {
|
||||
return new BadRequestResponse(e.getMessage());
|
||||
} catch (UnauthorizedServerException e) {
|
||||
return new UnauthorizedServerResponse(e.getMessage());
|
||||
return responseFactory.unauthorizedServer(e.getMessage());
|
||||
} catch (GatewayException e) {
|
||||
return new GatewayErrorResponse(e.getMessage());
|
||||
return responseFactory.gatewayError504(e.getMessage());
|
||||
} catch (InternalErrorException e) {
|
||||
if (e.getCause() != null) {
|
||||
return new InternalErrorResponse(request.getTarget(), e.getCause());
|
||||
return responseFactory.internalErrorResponse(e.getCause(), request.getTarget());
|
||||
} else {
|
||||
return new InternalErrorResponse(request.getTarget(), e);
|
||||
return responseFactory.internalErrorResponse(e, request.getTarget());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
return new InternalErrorResponse(request.getTarget(), e);
|
||||
return responseFactory.internalErrorResponse(e, request.getTarget());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,9 +130,9 @@ public class ResponseHandler extends TreePageHandler {
|
|||
authentication = request.getAuth();
|
||||
if (!authentication.isPresent()) {
|
||||
if (webServer.get().isUsingHTTPS()) {
|
||||
return DefaultResponses.BASIC_AUTH.get();
|
||||
return responseFactory.basicAuth();
|
||||
} else {
|
||||
return DefaultResponses.FORBIDDEN.get();
|
||||
return responseFactory.forbidden403();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -148,7 +144,7 @@ public class ResponseHandler extends TreePageHandler {
|
|||
if (!isAuthRequired || isAuthorized) {
|
||||
return pageHandler.getResponse(request, target);
|
||||
}
|
||||
return DefaultResponses.FORBIDDEN.get();
|
||||
return responseFactory.forbidden403();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@ import com.djrapitops.plan.system.webserver.cache.PageId;
|
|||
import com.djrapitops.plan.system.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.pages.InspectPageResponse;
|
||||
import com.djrapitops.plan.utilities.uuid.UUIDUtility;
|
||||
|
||||
|
@ -76,9 +75,9 @@ public class PlayerPageHandler implements PageHandler {
|
|||
return responseFactory.playerNotFound404();
|
||||
}
|
||||
} catch (NoServersException e) {
|
||||
ResponseCache.loadResponse(PageId.PLAYER.of(uuid), () -> new NotFoundResponse(e.getMessage()));
|
||||
ResponseCache.loadResponse(PageId.PLAYER.of(uuid), () -> responseFactory.notFound404(e.getMessage()));
|
||||
}
|
||||
return InspectPageResponse.getRefreshing();
|
||||
return responseFactory.serverNotFound404();
|
||||
}
|
||||
|
||||
private Response playerResponseOrNotFound(UUID uuid) throws WebException {
|
||||
|
|
|
@ -8,9 +8,9 @@ import com.djrapitops.plan.api.exceptions.connection.WebException;
|
|||
import com.djrapitops.plan.data.WebUser;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
|
||||
import com.djrapitops.plan.system.webserver.response.RedirectResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
@ -24,11 +24,17 @@ import java.util.Optional;
|
|||
*/
|
||||
public class RootPageHandler implements PageHandler {
|
||||
|
||||
private final ResponseFactory responseFactory;
|
||||
|
||||
public RootPageHandler(ResponseFactory responseFactory) {
|
||||
this.responseFactory = responseFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response getResponse(Request request, List<String> target) throws WebException {
|
||||
Optional<Authentication> auth = request.getAuth();
|
||||
if (!auth.isPresent()) {
|
||||
return DefaultResponses.BASIC_AUTH.get();
|
||||
return responseFactory.basicAuth();
|
||||
}
|
||||
|
||||
WebUser webUser = auth.get().getWebUser();
|
||||
|
@ -42,7 +48,7 @@ public class RootPageHandler implements PageHandler {
|
|||
case 2:
|
||||
return new RedirectResponse("/player/" + webUser.getName());
|
||||
default:
|
||||
return DefaultResponses.FORBIDDEN.get();
|
||||
return responseFactory.forbidden403();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,11 +6,10 @@ package com.djrapitops.plan.system.webserver.pages;
|
|||
|
||||
import com.djrapitops.plan.api.exceptions.WebUserAuthException;
|
||||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.system.locale.lang.ErrorPageLang;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -23,9 +22,12 @@ import java.util.Map;
|
|||
*/
|
||||
public abstract class TreePageHandler implements PageHandler {
|
||||
|
||||
protected final ResponseFactory responseFactory;
|
||||
|
||||
private Map<String, PageHandler> pages;
|
||||
|
||||
public TreePageHandler() {
|
||||
public TreePageHandler(ResponseFactory responseFactory) {
|
||||
this.responseFactory = responseFactory;
|
||||
pages = new HashMap<>();
|
||||
}
|
||||
|
||||
|
@ -52,7 +54,7 @@ public abstract class TreePageHandler implements PageHandler {
|
|||
PageHandler pageHandler = getPageHandler(target);
|
||||
return pageHandler != null
|
||||
? pageHandler.getResponse(request, target)
|
||||
: new NotFoundResponse(request.getLocale().getString(ErrorPageLang.UNKNOWN_PAGE_404));
|
||||
: responseFactory.pageNotFound404();
|
||||
}
|
||||
|
||||
public PageHandler getPageHandler(List<String> target) {
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package com.djrapitops.plan.system.webserver.response;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public class CSSResponse extends FileResponse {
|
||||
|
||||
public CSSResponse(String fileName) {
|
||||
public CSSResponse(String fileName) throws IOException {
|
||||
super(format(fileName));
|
||||
super.setType(ResponseType.CSS);
|
||||
setContent(getContent());
|
||||
|
|
|
@ -4,18 +4,13 @@
|
|||
*/
|
||||
package com.djrapitops.plan.system.webserver.response;
|
||||
|
||||
import com.djrapitops.plan.system.webserver.response.errors.ForbiddenResponse;
|
||||
|
||||
/**
|
||||
* Enum containing default responses that don't need to be cached because they're always the same.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public enum DefaultResponses {
|
||||
BASIC_AUTH(PromptAuthorizationResponse.getBasicAuthResponse()),
|
||||
SUCCESS(new TextResponse("Success")),
|
||||
FORBIDDEN(new ForbiddenResponse("Your user is not authorized to view this page.<br>"
|
||||
+ "If you believe this is an error contact staff to change your access level."));
|
||||
SUCCESS(new TextResponse("Success"));
|
||||
|
||||
private final Response response;
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
package com.djrapitops.plan.system.webserver.response;
|
||||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -23,13 +22,9 @@ public class FileResponse extends Response {
|
|||
// TODO
|
||||
private PlanFiles planFiles;
|
||||
|
||||
public FileResponse(String fileName) {
|
||||
public FileResponse(String fileName) throws IOException {
|
||||
super.setHeader("HTTP/1.1 200 OK");
|
||||
try {
|
||||
super.setContent(planFiles.readCustomizableResourceFlat(fileName));
|
||||
} catch (IOException e) {
|
||||
super.setContent(new NotFoundResponse(fileName + " was not found inside the .jar or /plugins/Plan/ folder").getContent());
|
||||
}
|
||||
super.setContent(planFiles.readCustomizableResourceFlat(fileName));
|
||||
}
|
||||
|
||||
public static String format(String fileName) {
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package com.djrapitops.plan.system.webserver.response;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public class JavaScriptResponse extends FileResponse {
|
||||
|
||||
JavaScriptResponse(String fileName) {
|
||||
JavaScriptResponse(String fileName) throws IOException {
|
||||
super(format(fileName));
|
||||
super.setType(ResponseType.JAVASCRIPT);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package com.djrapitops.plan.system.webserver.response;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.WebUserAuthException;
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plan.system.webserver.auth.FailReason;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -19,12 +21,13 @@ public class PromptAuthorizationResponse extends ErrorResponse {
|
|||
+ "- 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() {
|
||||
private PromptAuthorizationResponse(String version, PlanFiles files) throws IOException {
|
||||
super(version, files);
|
||||
super.setTitle(Icon.called("lock").build() + " 401 Unauthorized");
|
||||
}
|
||||
|
||||
public static PromptAuthorizationResponse getBasicAuthResponse() {
|
||||
PromptAuthorizationResponse response = new PromptAuthorizationResponse();
|
||||
public static PromptAuthorizationResponse getBasicAuthResponse(String version, PlanFiles files) throws IOException {
|
||||
PromptAuthorizationResponse response = new PromptAuthorizationResponse(version, files);
|
||||
response.setHeader("HTTP/1.1 401 Access Denied\r\n"
|
||||
+ "WWW-Authenticate: Basic realm=\"/\";");
|
||||
|
||||
|
@ -33,8 +36,8 @@ public class PromptAuthorizationResponse extends ErrorResponse {
|
|||
return response;
|
||||
}
|
||||
|
||||
public static PromptAuthorizationResponse getBasicAuthResponse(WebUserAuthException e) {
|
||||
PromptAuthorizationResponse response = new PromptAuthorizationResponse();
|
||||
public static PromptAuthorizationResponse getBasicAuthResponse(WebUserAuthException e, String version, PlanFiles files) throws IOException {
|
||||
PromptAuthorizationResponse response = new PromptAuthorizationResponse(version, files);
|
||||
response.setHeader("HTTP/1.1 401 Access Denied\r\n"
|
||||
+ "WWW-Authenticate: Basic realm=\"/\";");
|
||||
|
||||
|
|
|
@ -90,6 +90,7 @@ public abstract class Response {
|
|||
exchange.sendResponseHeaders(getCode(), 0);
|
||||
|
||||
String sentContent = getContent();
|
||||
// TODO Smell
|
||||
if (!(this instanceof JavaScriptResponse)) {
|
||||
sentContent = locale.replaceMatchingLanguage(sentContent);
|
||||
}
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
package com.djrapitops.plan.system.webserver.response;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.ParseException;
|
||||
import com.djrapitops.plan.api.exceptions.WebUserAuthException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
import com.djrapitops.plan.system.locale.lang.ErrorPageLang;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.InternalErrorResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.*;
|
||||
import com.djrapitops.plan.system.webserver.response.pages.*;
|
||||
import com.djrapitops.plan.utilities.html.pages.PageFactory;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import javax.inject.Singleton;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
|
@ -22,30 +26,56 @@ import java.util.UUID;
|
|||
@Singleton
|
||||
public class ResponseFactory {
|
||||
|
||||
private final String version;
|
||||
private final PlanFiles files;
|
||||
private final PageFactory pageFactory;
|
||||
private final Locale locale;
|
||||
private final Database database;
|
||||
private final ErrorHandler errorHandler;
|
||||
|
||||
@Inject
|
||||
public ResponseFactory(
|
||||
@Named("currentVersion") String version,
|
||||
PlanFiles files,
|
||||
PageFactory pageFactory,
|
||||
Locale locale,
|
||||
Database database
|
||||
Database database,
|
||||
ErrorHandler errorHandler
|
||||
) {
|
||||
this.version = version;
|
||||
this.files = files;
|
||||
this.pageFactory = pageFactory;
|
||||
this.locale = locale;
|
||||
this.database = database;
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
public Response debugPageResponse() {
|
||||
return new DebugPageResponse(pageFactory.debugPage());
|
||||
try {
|
||||
return new DebugPageResponse(pageFactory.debugPage(), version, files);
|
||||
} catch (IOException e) {
|
||||
return internalErrorResponse(e, "Failed to parse debug page");
|
||||
}
|
||||
}
|
||||
|
||||
public Response playersPageResponse() {
|
||||
try {
|
||||
return new PlayersPageResponse(pageFactory.playersPage());
|
||||
} catch (ParseException e) {
|
||||
return new InternalErrorResponse("Failed to parse players page", e);
|
||||
return internalErrorResponse(e, "Failed to parse players page");
|
||||
}
|
||||
}
|
||||
|
||||
public ErrorResponse internalErrorResponse(Throwable e, String s) {
|
||||
try {
|
||||
errorHandler.log(L.WARN, this.getClass(), e);
|
||||
return new InternalErrorResponse(s, e, version, files);
|
||||
} catch (IOException improperRestartException) {
|
||||
return new ErrorResponse(
|
||||
"Error occurred: " + e.toString() +
|
||||
", additional error occurred when attempting to send error page to user: " +
|
||||
improperRestartException.toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,7 +83,7 @@ public class ResponseFactory {
|
|||
try {
|
||||
return new NetworkPageResponse(pageFactory.networkPage());
|
||||
} catch (ParseException e) {
|
||||
return new InternalErrorResponse("Failed to parse network page", e);
|
||||
return internalErrorResponse(e, "Failed to parse network page");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,11 +96,19 @@ public class ResponseFactory {
|
|||
}
|
||||
|
||||
public Response javaScriptResponse(String fileName) {
|
||||
return new JavaScriptResponse(fileName);
|
||||
try {
|
||||
return new JavaScriptResponse(fileName);
|
||||
} catch (IOException e) {
|
||||
return notFound404("JS File not found from jar: " + fileName + ", " + e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public Response cssResponse(String fileName) {
|
||||
return new CSSResponse(fileName);
|
||||
try {
|
||||
return new CSSResponse(fileName);
|
||||
} catch (IOException e) {
|
||||
return notFound404("CSS File not found from jar: " + fileName + ", " + e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public Response redirectResponse(String location) {
|
||||
|
@ -78,18 +116,71 @@ public class ResponseFactory {
|
|||
}
|
||||
|
||||
public ErrorResponse pageNotFound404() {
|
||||
return new NotFoundResponse(locale.getString(ErrorPageLang.UNKNOWN_PAGE_404));
|
||||
return notFound404(locale.getString(ErrorPageLang.UNKNOWN_PAGE_404));
|
||||
}
|
||||
|
||||
public ErrorResponse uuidNotFound404() {
|
||||
return new NotFoundResponse(locale.getString(ErrorPageLang.UUID_404));
|
||||
return notFound404(locale.getString(ErrorPageLang.UUID_404));
|
||||
}
|
||||
|
||||
public ErrorResponse playerNotFound404() {
|
||||
return new NotFoundResponse(locale.getString(ErrorPageLang.NOT_PLAYED_404));
|
||||
return notFound404(locale.getString(ErrorPageLang.NOT_PLAYED_404));
|
||||
}
|
||||
|
||||
public ErrorResponse serverNotFound404() {
|
||||
return new NotFoundResponse(locale.getString(ErrorPageLang.NO_SERVERS_404));
|
||||
return notFound404(locale.getString(ErrorPageLang.NO_SERVERS_404));
|
||||
}
|
||||
|
||||
public ErrorResponse notFound404(String message) {
|
||||
try {
|
||||
return new NotFoundResponse(message, version, files);
|
||||
} catch (IOException e) {
|
||||
return internalErrorResponse(e, "Failed to parse 404 page");
|
||||
}
|
||||
}
|
||||
|
||||
public ErrorResponse basicAuthFail(WebUserAuthException e) {
|
||||
try {
|
||||
return PromptAuthorizationResponse.getBasicAuthResponse(e, version, files);
|
||||
} catch (IOException e1) {
|
||||
return internalErrorResponse(e, "Failed to parse PromptAuthorizationResponse");
|
||||
}
|
||||
}
|
||||
|
||||
public ErrorResponse forbidden403() {
|
||||
return forbidden403("Your user is not authorized to view this page.<br>"
|
||||
+ "If you believe this is an error contact staff to change your access level.");
|
||||
}
|
||||
|
||||
public ErrorResponse forbidden403(String message) {
|
||||
try {
|
||||
return new ForbiddenResponse(message, version, files);
|
||||
} catch (IOException e) {
|
||||
return internalErrorResponse(e, "Failed to parse ForbiddenResponse");
|
||||
}
|
||||
}
|
||||
|
||||
public ErrorResponse unauthorizedServer(String message) {
|
||||
try {
|
||||
return new UnauthorizedServerResponse(message, version, files);
|
||||
} catch (IOException e) {
|
||||
return internalErrorResponse(e, "Failed to parse UnauthorizedServerResponse");
|
||||
}
|
||||
}
|
||||
|
||||
public ErrorResponse gatewayError504(String message) {
|
||||
try {
|
||||
return new GatewayErrorResponse(message, version, files);
|
||||
} catch (IOException e) {
|
||||
return internalErrorResponse(e, "Failed to parse GatewayErrorResponse");
|
||||
}
|
||||
}
|
||||
|
||||
public ErrorResponse basicAuth() {
|
||||
try {
|
||||
return PromptAuthorizationResponse.getBasicAuthResponse(version, files);
|
||||
} catch (IOException e) {
|
||||
return internalErrorResponse(e, "Failed to parse PromptAuthorizationResponse");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,8 +6,6 @@ package com.djrapitops.plan.system.webserver.response.errors;
|
|||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
import org.apache.commons.text.StringSubstitutor;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -24,17 +22,15 @@ public class ErrorResponse extends Response {
|
|||
private String title;
|
||||
private String paragraph;
|
||||
|
||||
// TODO
|
||||
private String version;
|
||||
private PlanFiles planFiles;
|
||||
private ErrorHandler errorHandler;
|
||||
|
||||
public ErrorResponse() {
|
||||
try {
|
||||
setContent(planFiles.readCustomizableResourceFlat("web/error.html"));
|
||||
} catch (IOException e) {
|
||||
errorHandler.log(L.WARN, this.getClass(), e);
|
||||
}
|
||||
public ErrorResponse(String version, PlanFiles planFiles) throws IOException {
|
||||
this.version = version;
|
||||
setContent(planFiles.readCustomizableResourceFlat("web/error.html"));
|
||||
}
|
||||
|
||||
public ErrorResponse(String message) {
|
||||
setContent(message);
|
||||
}
|
||||
|
||||
public void replacePlaceholders() {
|
||||
|
|
|
@ -1,15 +1,18 @@
|
|||
package com.djrapitops.plan.system.webserver.response.errors;
|
||||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plan.utilities.html.icon.Family;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public class ForbiddenResponse extends ErrorResponse {
|
||||
|
||||
public ForbiddenResponse(String msg) {
|
||||
public ForbiddenResponse(String msg, String version, PlanFiles files) throws IOException {
|
||||
super(version, files);
|
||||
super.setHeader("HTTP/1.1 403 Forbidden");
|
||||
super.setTitle(Icon.called("hand-paper").of(Family.REGULAR) + " 403 Forbidden - Access Denied");
|
||||
super.setParagraph(msg);
|
||||
|
|
|
@ -4,6 +4,10 @@
|
|||
*/
|
||||
package com.djrapitops.plan.system.webserver.response.errors;
|
||||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* ErrorResponse for GatewayException.
|
||||
*
|
||||
|
@ -11,7 +15,8 @@ package com.djrapitops.plan.system.webserver.response.errors;
|
|||
*/
|
||||
public class GatewayErrorResponse extends ErrorResponse {
|
||||
|
||||
public GatewayErrorResponse(String message) {
|
||||
public GatewayErrorResponse(String message, String version, PlanFiles files) throws IOException {
|
||||
super(version, files);
|
||||
super.setHeader("HTTP/1.1 504 Gateway Error");
|
||||
super.setTitle("Failed to Connect (Gateway Error)");
|
||||
super.setParagraph(message);
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
package com.djrapitops.plan.system.webserver.response.errors;
|
||||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plan.utilities.html.Html;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public class InternalErrorResponse extends ErrorResponse {
|
||||
|
||||
public InternalErrorResponse(String cause, Throwable e) {
|
||||
public InternalErrorResponse(String cause, Throwable e, String version, PlanFiles files) throws IOException {
|
||||
super(version, files);
|
||||
super.setHeader("HTTP/1.1 500 Internal Error");
|
||||
|
||||
super.setTitle(Icon.called("bug") + " 500 Internal Error occurred");
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
package com.djrapitops.plan.system.webserver.response.errors;
|
||||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Generic 404 response.
|
||||
*
|
||||
* @author Rsl1122
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public class NotFoundResponse extends ErrorResponse {
|
||||
|
||||
public NotFoundResponse() {
|
||||
super.setHeader("HTTP/1.1 404 Not Found");
|
||||
super.setTitle(Icon.called("map-signs") + " 404 Not Found");
|
||||
super.setParagraph("Page does not exist.");
|
||||
super.replacePlaceholders();
|
||||
}
|
||||
|
||||
public NotFoundResponse(String msg) {
|
||||
public NotFoundResponse(String msg, String version, PlanFiles files) throws IOException {
|
||||
super(version, files);
|
||||
super.setHeader("HTTP/1.1 404 Not Found");
|
||||
super.setTitle(Icon.called("map-signs") + " 404 Not Found");
|
||||
super.setParagraph(msg);
|
||||
super.replacePlaceholders();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,14 +4,18 @@
|
|||
*/
|
||||
package com.djrapitops.plan.system.webserver.response.errors;
|
||||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Response when Server is not found in database when attempting to InfoRequest.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class UnauthorizedServerResponse extends ErrorResponse {
|
||||
|
||||
public UnauthorizedServerResponse(String message) {
|
||||
public UnauthorizedServerResponse(String message, String version, PlanFiles files) throws IOException {
|
||||
super(version, files);
|
||||
super.setHeader("HTTP/1.1 412 Unauthorized");
|
||||
super.setTitle("Unauthorized Server");
|
||||
super.setParagraph(message);
|
||||
|
|
|
@ -8,7 +8,7 @@ import com.djrapitops.plan.system.processing.Processing;
|
|||
import com.djrapitops.plan.system.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.system.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.utilities.html.pages.AnalysisPage;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
|
||||
|
@ -23,11 +23,12 @@ public class AnalysisPageResponse extends Response {
|
|||
// TODO Split responsibility so that this method does not call system to refresh and also render a refresh page.
|
||||
@Deprecated
|
||||
public static AnalysisPageResponse refreshNow(UUID serverUUID, Processing processing, InfoSystem infoSystem) {
|
||||
ResponseFactory responseFactory = null; // TODO
|
||||
processing.submitNonCritical(() -> {
|
||||
try {
|
||||
infoSystem.generateAnalysisPage(serverUUID);
|
||||
} catch (NoServersException | ConnectionFailException e) {
|
||||
ResponseCache.cacheResponse(PageId.SERVER.of(serverUUID), () -> new NotFoundResponse(e.getMessage()));
|
||||
ResponseCache.cacheResponse(PageId.SERVER.of(serverUUID), () -> responseFactory.notFound404(e.getMessage()));
|
||||
} catch (WebException e) {
|
||||
Log.toLog(AnalysisPageResponse.class.getName(), e);
|
||||
}
|
||||
|
|
|
@ -4,10 +4,13 @@
|
|||
*/
|
||||
package com.djrapitops.plan.system.webserver.response.pages;
|
||||
|
||||
import com.djrapitops.plan.system.file.PlanFiles;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse;
|
||||
import com.djrapitops.plan.utilities.html.icon.Icon;
|
||||
import com.djrapitops.plan.utilities.html.pages.DebugPage;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* WebServer response for /debug-page used for easing issue reporting.
|
||||
*
|
||||
|
@ -15,7 +18,8 @@ import com.djrapitops.plan.utilities.html.pages.DebugPage;
|
|||
*/
|
||||
public class DebugPageResponse extends ErrorResponse {
|
||||
|
||||
public DebugPageResponse(DebugPage debugPage) {
|
||||
public DebugPageResponse(DebugPage debugPage, String version, PlanFiles files) throws IOException {
|
||||
super(version, files);
|
||||
super.setHeader("HTTP/1.1 200 OK");
|
||||
super.setTitle(Icon.called("bug") + " Debug Information");
|
||||
super.setParagraph(debugPage.toHtml());
|
||||
|
|
|
@ -3,7 +3,6 @@ package com.djrapitops.plan.system.webserver.response.pages;
|
|||
import com.djrapitops.plan.system.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.system.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.pages.parts.InspectPagePluginsContent;
|
||||
import org.apache.commons.text.StringSubstitutor;
|
||||
|
||||
|
@ -40,12 +39,4 @@ public class InspectPageResponse extends Response {
|
|||
private String[] getCalculating() {
|
||||
return new String[]{"<li><i class=\"fa fa-spin fa-refresh\"></i><a> Calculating...</a></li>", ""};
|
||||
}
|
||||
|
||||
public static InspectPageResponse getRefreshing() {
|
||||
ErrorResponse refreshPage = new ErrorResponse();
|
||||
refreshPage.setTitle("Player page request is being processed..");
|
||||
refreshPage.setParagraph("<meta http-equiv=\"refresh\" content=\"2\" /><i class=\"fa fa-refresh fa-spin\" aria-hidden=\"true\"></i> Page will refresh automatically..");
|
||||
refreshPage.replacePlaceholders();
|
||||
return new InspectPageResponse(null, refreshPage.getContent());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,13 @@ public class AnalysisPage implements Page {
|
|||
}
|
||||
|
||||
public static String getRefreshingHtml() {
|
||||
ErrorResponse refreshPage = new ErrorResponse();
|
||||
// TODO Work this out
|
||||
ErrorResponse refreshPage = null;
|
||||
try {
|
||||
refreshPage = new ErrorResponse("", null);
|
||||
} catch (IOException e) {
|
||||
|
||||
}
|
||||
refreshPage.setTitle("Analysis is being refreshed..");
|
||||
refreshPage.setParagraph("<meta http-equiv=\"refresh\" content=\"5\" /><i class=\"fa fa-refresh fa-spin\" aria-hidden=\"true\"></i> Analysis is being run, refresh the page after a few seconds.. (F5)");
|
||||
refreshPage.replacePlaceholders();
|
||||
|
|
Loading…
Reference in New Issue
Block a user