fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)

Sat, 16 May 2020 15:45:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 May 2020 15:45:06 +0200
changeset 53
6a8498291606
parent 52
67a02e79b7a1
child 54
77e01cda5a40

fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)

also improves error pages in general

src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java file | annotate | diff | comparison | revisions
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/web.xml file | annotate | diff | comparison | revisions
src/main/webapp/error.css file | annotate | diff | comparison | revisions
     1.1 --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Sat May 16 15:11:07 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Sat May 16 15:45:06 2020 +0200
     1.3 @@ -324,13 +324,28 @@
     1.4          }
     1.5  
     1.6          // set some internal request attributes
     1.7 +        final String fullPath = Functions.fullPath(req);
     1.8          req.setAttribute(Constants.REQ_ATTR_BASE_HREF, Functions.baseHref(req));
     1.9 -        req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
    1.10 +        req.setAttribute(Constants.REQ_ATTR_PATH, fullPath);
    1.11          Optional.ofNullable(moduleInfo).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
    1.12  
    1.13 +        // if this is an error path, bypass the normal flow
    1.14 +        if (fullPath.startsWith("/error/")) {
    1.15 +            final var mapping = findMapping(method, req);
    1.16 +            if (mapping.isPresent()) {
    1.17 +                forwardAsSpecified(invokeMapping(mapping.get(), req, resp, null), req, resp);
    1.18 +            }
    1.19 +            return;
    1.20 +        }
    1.21 +
    1.22          // obtain a connection and create the data access objects
    1.23          final var db = (DatabaseFacade) req.getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME);
    1.24 -        try (final var connection = db.getDataSource().getConnection()) {
    1.25 +        final var ds = db.getDataSource();
    1.26 +        if (ds == null) {
    1.27 +            resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "JNDI DataSource lookup failed. See log for details.");
    1.28 +            return;
    1.29 +        }
    1.30 +        try (final var connection = ds.getConnection()) {
    1.31              final var dao = createDataAccessObjects(connection);
    1.32              try {
    1.33                  connection.setAutoCommit(false);
     2.1 --- a/src/main/java/de/uapcore/lightpit/modules/ErrorModule.java	Sat May 16 15:11:07 2020 +0200
     2.2 +++ b/src/main/java/de/uapcore/lightpit/modules/ErrorModule.java	Sat May 16 15:45:06 2020 +0200
     2.3 @@ -32,6 +32,7 @@
     2.4  
     2.5  import javax.servlet.annotation.WebServlet;
     2.6  import javax.servlet.http.HttpServletRequest;
     2.7 +import javax.servlet.http.HttpServletResponse;
     2.8  import java.util.Optional;
     2.9  
    2.10  /**
    2.11 @@ -49,13 +50,10 @@
    2.12  )
    2.13  public final class ErrorModule extends AbstractLightPITServlet {
    2.14  
    2.15 -    public static final String REQ_ATTR_ERROR_CODE = "errorCode";
    2.16      public static final String REQ_ATTR_RETURN_LINK = "returnLink";
    2.17  
    2.18 -    private ResponseType handle(HttpServletRequest req, int sc) {
    2.19 -
    2.20 -        req.setAttribute(REQ_ATTR_ERROR_CODE, sc);
    2.21 -
    2.22 +    @RequestMapping(requestPath = "generic", method = HttpMethod.GET)
    2.23 +    public ResponseType onError(HttpServletRequest req, HttpServletResponse resp) {
    2.24          Optional.ofNullable(req.getHeader("Referer")).ifPresent(
    2.25                  referer -> req.setAttribute(REQ_ATTR_RETURN_LINK, referer)
    2.26          );
    2.27 @@ -65,19 +63,4 @@
    2.28  
    2.29          return ResponseType.HTML;
    2.30      }
    2.31 -
    2.32 -    @RequestMapping(requestPath = "404.html", method = HttpMethod.GET)
    2.33 -    public ResponseType handle404(HttpServletRequest req) {
    2.34 -        return handle(req, 404);
    2.35 -    }
    2.36 -
    2.37 -    @RequestMapping(requestPath = "403.html", method = HttpMethod.GET)
    2.38 -    public ResponseType handle403(HttpServletRequest req) {
    2.39 -        return handle(req, 403);
    2.40 -    }
    2.41 -
    2.42 -    @RequestMapping(requestPath = "500.html", method = HttpMethod.GET)
    2.43 -    public ResponseType handle500(HttpServletRequest req) {
    2.44 -        return handle(req, 500);
    2.45 -    }
    2.46  }
     3.1 --- a/src/main/resources/localization/error.properties	Sat May 16 15:11:07 2020 +0200
     3.2 +++ b/src/main/resources/localization/error.properties	Sat May 16 15:45:06 2020 +0200
     3.3 @@ -26,10 +26,11 @@
     3.4  h1 = The requested page cannot be displayed.
     3.5  errorCode = Code
     3.6  
     3.7 -errorMessage = Message
     3.8 -errorMessage.404 = Page not found
     3.9 -errorMessage.403 = Access denied
    3.10 -errorMessage.500 = Internal error
    3.11 +errorMessage = Server Message
    3.12 +code.404 = Page not found
    3.13 +code.403 = Access denied
    3.14 +code.500 = Internal error
    3.15 +code.503 = Service unavailable
    3.16  
    3.17  errorTimestamp = Timestamp
    3.18  errorExceptionText = Internal Exception
     4.1 --- a/src/main/resources/localization/error_de.properties	Sat May 16 15:11:07 2020 +0200
     4.2 +++ b/src/main/resources/localization/error_de.properties	Sat May 16 15:45:06 2020 +0200
     4.3 @@ -26,10 +26,11 @@
     4.4  h1 = Die angeforderte Seite kann nicht angezeigt werden.
     4.5  errorCode = Fehlercode
     4.6  
     4.7 -errorMessage = Nachricht
     4.8 -errorMessage.404 = Seite nicht gefunden
     4.9 -errorMessage.403 = Zugriff verboten
    4.10 -errorMessage.500 = Interner Fehler
    4.11 +errorMessage = Server Nachricht
    4.12 +code.404 = Seite nicht gefunden
    4.13 +code.403 = Zugriff verboten
    4.14 +code.500 = Interner Fehler
    4.15 +code.503 = Dienst steht derzeit nicht zur Verfügung
    4.16  
    4.17  errorTimestamp = Zeitstempel
    4.18  errorExceptionText = Interne Ausnahme
     5.1 --- a/src/main/webapp/WEB-INF/dynamic_fragments/error.jsp	Sat May 16 15:11:07 2020 +0200
     5.2 +++ b/src/main/webapp/WEB-INF/dynamic_fragments/error.jsp	Sat May 16 15:45:06 2020 +0200
     5.3 @@ -32,7 +32,7 @@
     5.4  <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
     5.5  
     5.6  <c:set scope="page" var="baseHref" value="${requestScope[Constants.REQ_ATTR_BASE_HREF]}" />
     5.7 -<c:set scope="page" var="errorCode" value="${requestScope[ErrorModule.REQ_ATTR_ERROR_CODE]}"/>
     5.8 +<c:set scope="page" var="errorCode" value="${requestScope['javax.servlet.error.status_code']}"/>
     5.9  <c:set scope="page" var="returnLink" value="${requestScope[ErrorModule.REQ_ATTR_RETURN_LINK]}"/>
    5.10  
    5.11  <div id="error-page">
    5.12 @@ -40,11 +40,11 @@
    5.13      <table>
    5.14          <tr>
    5.15              <th><fmt:message key="errorCode"/>:</th>
    5.16 -            <td>${errorCode}</td>
    5.17 +            <td>${errorCode} - <fmt:message key="code.${errorCode}" /></td>
    5.18          </tr>
    5.19          <tr>
    5.20              <th><fmt:message key="errorMessage" />:</th>
    5.21 -            <td><fmt:message key="errorMessage.${errorCode}" /></td>
    5.22 +            <td><c:out value="${requestScope['javax.servlet.error.message']}"/></td>
    5.23          </tr>
    5.24          <tr>
    5.25              <th><fmt:message key="errorTimestamp" />:</th>
     6.1 --- a/src/main/webapp/WEB-INF/web.xml	Sat May 16 15:11:07 2020 +0200
     6.2 +++ b/src/main/webapp/WEB-INF/web.xml	Sat May 16 15:45:06 2020 +0200
     6.3 @@ -17,15 +17,6 @@
     6.4          <lookup-name>java:/jdbc/lightpit/app</lookup-name>
     6.5      </resource-ref>
     6.6      <error-page>
     6.7 -        <error-code>404</error-code>
     6.8 -        <location>/error/404.html</location>
     6.9 -    </error-page>
    6.10 -    <error-page>
    6.11 -        <error-code>403</error-code>
    6.12 -        <location>/error/403.html</location>
    6.13 -    </error-page>
    6.14 -    <error-page>
    6.15 -        <error-code>500</error-code>
    6.16 -        <location>/error/500.html</location>
    6.17 +        <location>/error/generic</location>
    6.18      </error-page>
    6.19  </web-app>
     7.1 --- a/src/main/webapp/error.css	Sat May 16 15:11:07 2020 +0200
     7.2 +++ b/src/main/webapp/error.css	Sat May 16 15:45:06 2020 +0200
     7.3 @@ -48,6 +48,7 @@
     7.4  
     7.5  #error-page table th {
     7.6      text-align: right;
     7.7 +    white-space: nowrap;
     7.8  }
     7.9  
    7.10  #error-page table td {

mercurial