fixes error pages and adds referer link to error page if it comes from the application

Wed, 13 May 2020 21:46:26 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 13 May 2020 21:46:26 +0200
changeset 46
1574965c7dc7
parent 45
cc7f082c5ef3
child 47
57cfb94ab99f

fixes error pages and adds referer link to error page if it comes from the application

src/main/java/de/uapcore/lightpit/modules/ErrorModule.java file | annotate | diff | comparison | revisions
src/main/resources/localization/error.properties file | annotate | diff | comparison | revisions
src/main/resources/localization/error_de.properties file | annotate | diff | comparison | revisions
src/main/webapp/WEB-INF/dynamic_fragments/error.jsp file | annotate | diff | comparison | revisions
src/main/webapp/WEB-INF/jsp/site.jsp file | annotate | diff | comparison | revisions
     1.1 --- a/src/main/java/de/uapcore/lightpit/modules/ErrorModule.java	Wed May 13 21:10:23 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/lightpit/modules/ErrorModule.java	Wed May 13 21:46:26 2020 +0200
     1.3 @@ -32,6 +32,7 @@
     1.4  
     1.5  import javax.servlet.annotation.WebServlet;
     1.6  import javax.servlet.http.HttpServletRequest;
     1.7 +import java.util.Optional;
     1.8  
     1.9  /**
    1.10   * Entry point for the application.
    1.11 @@ -49,10 +50,16 @@
    1.12  public final class ErrorModule extends AbstractLightPITServlet {
    1.13  
    1.14      public static final String REQ_ATTR_ERROR_CODE = "errorCode";
    1.15 +    public static final String REQ_ATTR_RETURN_LINK = "returnLink";
    1.16  
    1.17      private ResponseType handle(HttpServletRequest req, int sc) {
    1.18  
    1.19          req.setAttribute(REQ_ATTR_ERROR_CODE, sc);
    1.20 +
    1.21 +        Optional.ofNullable(req.getHeader("Referer")).ifPresent(
    1.22 +                referer -> req.setAttribute(REQ_ATTR_RETURN_LINK, referer)
    1.23 +        );
    1.24 +
    1.25          setStylesheet(req, "error");
    1.26          setDynamicFragment(req, "error");
    1.27  
     2.1 --- a/src/main/resources/localization/error.properties	Wed May 13 21:10:23 2020 +0200
     2.2 +++ b/src/main/resources/localization/error.properties	Wed May 13 21:46:26 2020 +0200
     2.3 @@ -33,3 +33,4 @@
     2.4  
     2.5  errorTimestamp = Timestamp
     2.6  errorExceptionText = Internal Exception
     2.7 +errorReturnLink = Return to
     3.1 --- a/src/main/resources/localization/error_de.properties	Wed May 13 21:10:23 2020 +0200
     3.2 +++ b/src/main/resources/localization/error_de.properties	Wed May 13 21:46:26 2020 +0200
     3.3 @@ -33,3 +33,4 @@
     3.4  
     3.5  errorTimestamp = Zeitstempel
     3.6  errorExceptionText = Interne Ausnahme
     3.7 +errorReturnLink = Kehre zurück zu
     4.1 --- a/src/main/webapp/WEB-INF/dynamic_fragments/error.jsp	Wed May 13 21:10:23 2020 +0200
     4.2 +++ b/src/main/webapp/WEB-INF/dynamic_fragments/error.jsp	Wed May 13 21:46:26 2020 +0200
     4.3 @@ -28,10 +28,10 @@
     4.4  <%@page import="de.uapcore.lightpit.modules.ErrorModule" %>
     4.5  <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     4.6  <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
     4.7 -
     4.8 -<jsp:useBean id="exception" scope="request" type="java.lang.Throwable"/>
     4.9 +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    4.10  
    4.11  <c:set scope="page" var="errorCode" value="${requestScope[ErrorModule.REQ_ATTR_ERROR_CODE]}"/>
    4.12 +<c:set scope="page" var="returnLink" value="${requestScope[ErrorModule.REQ_ATTR_RETURN_LINK]}"/>
    4.13  
    4.14  <div id="error-page">
    4.15      <h1><fmt:message key="h1"/></h1>
    4.16 @@ -48,11 +48,18 @@
    4.17              <th><fmt:message key="errorTimestamp" />:</th>
    4.18              <td><fmt:formatDate type="both" value="<%= new java.util.Date()%>"/></td>
    4.19          </tr>
    4.20 +        <%--@elvariable id="exception" type="java.lang.Exception"--%>
    4.21          <c:if test="${not empty exception}">
    4.22          <tr>
    4.23              <th><fmt:message key="errorExceptionText" />:</th>
    4.24              <td>${exception.class.name} - ${exception.message}</td>
    4.25          </tr>    
    4.26          </c:if>
    4.27 +        <c:if test="${fn:startsWith(returnLink, baseHref)}">
    4.28 +        <tr>
    4.29 +            <th><fmt:message key="errorReturnLink" />:</th>
    4.30 +            <td><a href="${returnLink}">${returnLink}</a></td>
    4.31 +        </tr>
    4.32 +        </c:if>
    4.33      </table>
    4.34  </div>
     5.1 --- a/src/main/webapp/WEB-INF/jsp/site.jsp	Wed May 13 21:10:23 2020 +0200
     5.2 +++ b/src/main/webapp/WEB-INF/jsp/site.jsp	Wed May 13 21:46:26 2020 +0200
     5.3 @@ -30,6 +30,9 @@
     5.4  <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
     5.5  <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
     5.6  
     5.7 +<%-- Make the base href easily available at request scope --%>
     5.8 +<c:set scope="request" var="baseHref" value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />
     5.9 +
    5.10  <%-- Define an alias for the request path --%>
    5.11  <c:set scope="page" var="requestPath" value="${requestScope[Constants.REQ_ATTR_PATH]}"/>
    5.12  
    5.13 @@ -56,7 +59,7 @@
    5.14  <!DOCTYPE html>
    5.15  <html>
    5.16      <head>
    5.17 -        <base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
    5.18 +        <base href="${baseHref}">
    5.19          <title>LightPIT -
    5.20              <fmt:bundle basename="${moduleInfo.bundleBaseName}">
    5.21                  <fmt:message key="${moduleInfo.titleKey}" />

mercurial