Fancier Error Responses

This commit is contained in:
Rsl1122 2017-08-28 18:47:20 +03:00
parent 5b2a8de68a
commit 8e16e4ca73
8 changed files with 136 additions and 39 deletions

View File

@ -56,7 +56,6 @@ public class WebServer {
private boolean usingHttps = false;
/**
*
* @param plugin
*/
public WebServer(IPlan plugin) {
@ -429,14 +428,10 @@ public class WebServer {
private Response forbiddenResponse(int permLevel, int required) {
return PageCache.loadPage("forbidden", () -> {
ForbiddenResponse response403 = new ForbiddenResponse();
String content = "<h1>403 Forbidden - Access Denied</h1>"
+ "<p>Unauthorized User.<br>"
return new ForbiddenResponse("Unauthorized User.<br>"
+ "Make sure your user has the correct access level.<br>"
+ "This page requires permission level of " + required + ",<br>"
+ "This user has permission level of " + permLevel + "</p>";
response403.setContent(content);
return response403;
+ "This user has permission level of " + permLevel);
});
}
@ -488,16 +483,12 @@ public class WebServer {
}
private Response notFoundResponse() {
String error = "<h1>404 Not Found</h1>"
+ "<p>Make sure you're accessing a link given by a command, Examples:</p>"
+ "<p>" + getProtocol() + HtmlUtils.getInspectUrl("<player>") + " or<br>"
+ getProtocol() + HtmlUtils.getServerAnalysisUrl() + "</p>";
return PageCache.loadPage("notFound: " + error, () -> {
Response response404 = new NotFoundResponse();
response404.setContent(error);
return response404;
});
String error = "404 Not Found";
return PageCache.loadPage("notFound: " + error, () ->
new NotFoundResponse("Make sure you're accessing a link given by a command, Examples:</p>"
+ "<p>" + getProtocol() + ":" + HtmlUtils.getInspectUrl("<player>") + " or<br>"
+ getProtocol() + ":" + HtmlUtils.getServerAnalysisUrl())
);
}
/**
@ -557,6 +548,6 @@ public class WebServer {
}
public String getAccessAddress() {
return getProtocol()+":/"+ HtmlUtils.getIP();
return getProtocol() + ":/" + HtmlUtils.getIP();
}
}

View File

@ -0,0 +1,51 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.webserver.response;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.systems.webserver.theme.Theme;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.file.FileUtil;
import org.apache.commons.lang3.text.StrSubstitutor;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
/**
* Represents generic HTTP Error response that has the page style in it.
*
* @author Rsl1122
*/
public class ErrorResponse extends Response {
private String title;
private String paragraph;
public ErrorResponse() {
try {
setContent(Theme.replaceColors(FileUtil.getStringFromResource("error.html")));
} catch (FileNotFoundException e) {
Log.toLog(this.getClass().getName(), e);
}
}
protected void replacePlaceholders() {
Map<String, String> placeHolders = new HashMap<>();
placeHolders.put("title", title);
placeHolders.put("paragraph", paragraph);
placeHolders.put("version", MiscUtils.getPlanVersion());
setContent(StrSubstitutor.replace(getContent(), placeHolders));
}
public void setTitle(String title) {
this.title = title;
}
public void setParagraph(String paragraph) {
this.paragraph = paragraph;
}
}

View File

@ -4,9 +4,16 @@ package main.java.com.djrapitops.plan.systems.webserver.response;
* @author Rsl1122
* @since 3.5.2
*/
public class ForbiddenResponse extends Response {
public class ForbiddenResponse extends ErrorResponse {
public ForbiddenResponse() {
super.setHeader("HTTP/1.1 403 Forbidden");
super.setTitle("403 Forbidden - Access Denied");
}
public ForbiddenResponse(String msg) {
super.setHeader("HTTP/1.1 404 Not Found");
super.setTitle("403 Forbidden - Access Denied");
super.setParagraph(msg);
super.replacePlaceholders();
}
}

View File

@ -6,26 +6,25 @@ import main.java.com.djrapitops.plan.utilities.html.Html;
* @author Rsl1122
* @since 3.5.2
*/
public class InternalErrorResponse extends Response {
public class InternalErrorResponse extends ErrorResponse {
public InternalErrorResponse(Throwable e, String cause) {
StringBuilder content = new StringBuilder();
super.setHeader("HTTP/1.1 500 Internal Error");
content.append("<h1>500 Internal Error occurred</h1>");
content.append("<p>Please report this issue here: </p>");
content.append(Html.LINK.parse("https://github.com/Rsl1122/Plan-PlayerAnalytics/issues", "Issues"));
content.append("<p>");
content.append(e).append(" | ").append(cause);
super.setTitle("500 Internal Error occurred");
StringBuilder paragraph = new StringBuilder();
paragraph.append("Please report this issue here: ");
paragraph.append(Html.LINK.parse("https://github.com/Rsl1122/Plan-PlayerAnalytics/issues", "Issues"));
paragraph.append("<br><br>");
paragraph.append(e).append(" | ").append(cause);
for (StackTraceElement element : e.getStackTrace()) {
content.append("<br>");
content.append(" ").append(element);
paragraph.append("<br>");
paragraph.append(" ").append(element);
}
content.append("</p>");
super.setContent(content.toString());
super.setParagraph(paragraph.toString());
super.replacePlaceholders();
}
}

View File

@ -4,15 +4,19 @@ package main.java.com.djrapitops.plan.systems.webserver.response;
* @author Rsl1122
* @since 3.5.2
*/
public class NotFoundResponse extends Response {
public class NotFoundResponse extends ErrorResponse {
public NotFoundResponse() {
super.setHeader("HTTP/1.1 404 Not Found");
super.setContent("<h1>404 Not Found</h1><p>Page does not exist.</p>");
super.setTitle("404 Not Found");
super.setParagraph("Page does not exist.");
super.replacePlaceholders();
}
public NotFoundResponse(String msg) {
super.setHeader("HTTP/1.1 404 Not Found");
super.setContent("<h1>404 Not Found</h1><p>" + msg + "</p>");
super.setTitle("404 Not Found");
super.setParagraph(msg);
super.replacePlaceholders();
}
}

View File

@ -4,15 +4,17 @@ package main.java.com.djrapitops.plan.systems.webserver.response;
* @author Rsl1122
* @since 3.5.2
*/
public class PromptAuthorizationResponse extends Response {
public class PromptAuthorizationResponse extends ErrorResponse {
public PromptAuthorizationResponse() {
super.setHeader("HTTP/1.1 401 Access Denied\r\n"
+ "WWW-Authenticate: Basic realm=\"/\";");
super.setContent("<h1>401 Unauthorized</h1><p>Authentication Failed.<br>"
super.setTitle("401 Unauthorized");
super.setParagraph("Authentication Failed.<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.</p>");
+ "<br>If you have forgotten your password, ask a staff member to delete your old user and re-register.");
super.replacePlaceholders();
}
}

View File

@ -3,8 +3,10 @@ package main.java.com.djrapitops.plan.utilities;
import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.command.CommandUtils;
import com.djrapitops.plugin.command.ISender;
import com.djrapitops.plugin.utilities.Compatibility;
import main.java.com.djrapitops.plan.Permissions;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.bungee.PlanBungee;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
@ -124,4 +126,12 @@ public class MiscUtils {
}
}
}
public static String getPlanVersion() {
if (Compatibility.isBukkitAvailable()) {
return Plan.getInstance().getDescription().getVersion();
} else {
return PlanBungee.getInstance().getDescription().getVersion();
}
}
}

View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
<meta name="description" content="Player Analysis window">
<meta name="author" content="Rsl1122">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="https://puu.sh/tK0KL/6aa2ba141b.ico" type="image/x-icon">
<link rel="stylesheet" href="./main.css">
<link href="https://fonts.googleapis.com/css?family=Quicksand:300,400" rel="stylesheet">
<script src="https://use.fontawesome.com/df48eb908b.js"></script>
</head>
<body>
<header>
<div>
<img class="right" src="https://puu.sh/tJZUb/c2e0ab220f.png" alt="Player Analytics | Analysis">
<p class="right">Player Analytics v.${version}</p>
</div>
</header>
<div class="content">
<div class="row">
<div class="column">
<div class="box-header">
<h2><i class="fa fa-exclamation-circle"></i> ${title}</h2>
</div>
<div class="box" style="height: 75%;">
<p>${paragraph}</p>
</div>
</div>
</div>
</div>
</body>
</html>