From d945bd512a5d2036d23acec82977106efc8d7f74 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Mon, 10 Nov 2014 18:19:57 +0100 Subject: [PATCH] add: 403-page, custom 404-message --- src/org/cacert/gigi/Gigi.java | 2 ++ src/org/cacert/gigi/Launcher.java | 1 + .../cacert/gigi/pages/admin/TTPAdminPage.java | 6 +++-- .../cacert/gigi/pages/error/AccessDenied.java | 26 +++++++++++++++++++ .../gigi/pages/error/AccessDenied.templ | 1 + .../cacert/gigi/pages/error/PageNotFound.java | 11 +++++++- .../gigi/pages/error/PageNotFound.templ | 2 +- 7 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/org/cacert/gigi/pages/error/AccessDenied.java create mode 100644 src/org/cacert/gigi/pages/error/AccessDenied.templ diff --git a/src/org/cacert/gigi/Gigi.java b/src/org/cacert/gigi/Gigi.java index ff1b9693..692eb3ed 100644 --- a/src/org/cacert/gigi/Gigi.java +++ b/src/org/cacert/gigi/Gigi.java @@ -42,6 +42,7 @@ import org.cacert.gigi.pages.account.certs.Certificates; import org.cacert.gigi.pages.account.domain.DomainOverview; import org.cacert.gigi.pages.account.mail.MailOverview; import org.cacert.gigi.pages.admin.TTPAdminPage; +import org.cacert.gigi.pages.error.AccessDenied; import org.cacert.gigi.pages.error.PageNotFound; import org.cacert.gigi.pages.main.RegisterPage; import org.cacert.gigi.pages.orga.CreateOrgPage; @@ -95,6 +96,7 @@ public class Gigi extends HttpServlet { @Override public void init() throws ServletException { if ( !firstInstanceInited) { + putPage("/denied", new AccessDenied(), null); putPage("/error", new PageNotFound(), null); putPage("/login", new LoginPage("Password Login"), "CAcert.org"); getMenu("CAcert.org").addItem(new SimpleMenuItem("https://" + ServerConstants.getSecureHostNamePort() + "/login", "Certificate Login") { diff --git a/src/org/cacert/gigi/Launcher.java b/src/org/cacert/gigi/Launcher.java index 0490d1b1..7448151a 100644 --- a/src/org/cacert/gigi/Launcher.java +++ b/src/org/cacert/gigi/Launcher.java @@ -205,6 +205,7 @@ public class Launcher { servlet.addServlet(webAppServlet, "/*"); ErrorPageErrorHandler epeh = new ErrorPageErrorHandler(); epeh.addErrorPage(404, "/error"); + epeh.addErrorPage(403, "/denied"); servlet.setErrorHandler(epeh); HandlerList hl = new HandlerList(); diff --git a/src/org/cacert/gigi/pages/admin/TTPAdminPage.java b/src/org/cacert/gigi/pages/admin/TTPAdminPage.java index 9c7d48b6..cc9a536d 100644 --- a/src/org/cacert/gigi/pages/admin/TTPAdminPage.java +++ b/src/org/cacert/gigi/pages/admin/TTPAdminPage.java @@ -14,6 +14,7 @@ import org.cacert.gigi.localisation.Language; import org.cacert.gigi.output.Form; import org.cacert.gigi.output.template.IterableDataset; import org.cacert.gigi.pages.Page; +import org.cacert.gigi.pages.error.PageNotFound; public class TTPAdminPage extends Page { @@ -40,10 +41,11 @@ public class TTPAdminPage extends Page { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { String path = req.getPathInfo(); - if (path != null && path.length() > PATH.length()) { + if (path != null && path.length() > PATH.length() + 1) { int id = Integer.parseInt(path.substring(1 + PATH.length())); User u = User.getById(id); - if ( !u.isInGroup(TTP_APPLICANT)) { + if (u == null || !u.isInGroup(TTP_APPLICANT)) { + req.setAttribute(PageNotFound.MESSAGE_ATTRIBUTE, "The TTP-request is not available anymore."); resp.sendError(404); return; } diff --git a/src/org/cacert/gigi/pages/error/AccessDenied.java b/src/org/cacert/gigi/pages/error/AccessDenied.java new file mode 100644 index 00000000..60c48bf2 --- /dev/null +++ b/src/org/cacert/gigi/pages/error/AccessDenied.java @@ -0,0 +1,26 @@ +package org.cacert.gigi.pages.error; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.cacert.gigi.pages.Page; + +public class AccessDenied extends Page { + + public AccessDenied() { + super("Access denied"); + } + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { + getDefaultTemplate().output(resp.getWriter(), Page.getLanguage(req), null); + } + + @Override + public boolean needsLogin() { + return false; + } + +} diff --git a/src/org/cacert/gigi/pages/error/AccessDenied.templ b/src/org/cacert/gigi/pages/error/AccessDenied.templ new file mode 100644 index 00000000..69069ca9 --- /dev/null +++ b/src/org/cacert/gigi/pages/error/AccessDenied.templ @@ -0,0 +1 @@ +

diff --git a/src/org/cacert/gigi/pages/error/PageNotFound.java b/src/org/cacert/gigi/pages/error/PageNotFound.java index e6ba0640..ffc107f5 100644 --- a/src/org/cacert/gigi/pages/error/PageNotFound.java +++ b/src/org/cacert/gigi/pages/error/PageNotFound.java @@ -1,6 +1,7 @@ package org.cacert.gigi.pages.error; import java.io.IOException; +import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -9,13 +10,21 @@ import org.cacert.gigi.pages.Page; public class PageNotFound extends Page { + public static final String MESSAGE_ATTRIBUTE = "message-Str"; + public PageNotFound() { super("File not found!"); } @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { - getDefaultTemplate().output(resp.getWriter(), Page.getLanguage(req), null); + HashMap vars = new HashMap<>(); + Object customMessage = req.getAttribute(MESSAGE_ATTRIBUTE); + if (customMessage == null) { + customMessage = getLanguage(req).getTranslation("Due to recent site changes bookmarks may no longer be valid, please update your bookmarks."); + } + vars.put("message", customMessage); + getDefaultTemplate().output(resp.getWriter(), Page.getLanguage(req), vars); } @Override diff --git a/src/org/cacert/gigi/pages/error/PageNotFound.templ b/src/org/cacert/gigi/pages/error/PageNotFound.templ index 5ecd8867..1c1b95c1 100644 --- a/src/org/cacert/gigi/pages/error/PageNotFound.templ +++ b/src/org/cacert/gigi/pages/error/PageNotFound.templ @@ -1 +1 @@ -

+

-- 2.39.2