fixes minor issues that were reported by default inspection

Tue, 12 May 2020 22:03:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 12 May 2020 22:03:00 +0200
changeset 39
e722861558bb
parent 38
cf85ef18f231
child 40
276ef00a336d

fixes minor issues that were reported by default inspection

src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/dao/AbstractDao.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/LanguageModule.java file | annotate | diff | comparison | revisions
src/main/webapp/WEB-INF/dynamic_fragments/error.jsp file | annotate | diff | comparison | revisions
src/main/webapp/WEB-INF/dynamic_fragments/language.jsp file | annotate | diff | comparison | revisions
src/main/webapp/WEB-INF/jsp/html_full.jsp file | annotate | diff | comparison | revisions
src/main/webapp/lightpit.css file | annotate | diff | comparison | revisions
--- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Mon May 11 19:09:06 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Tue May 12 22:03:00 2020 +0200
@@ -60,12 +60,6 @@
      */
     private LightPITModule.ELProxy moduleInfo = null;
 
-
-    @FunctionalInterface
-    private interface HandlerMethod {
-        ResponseType apply(HttpServletRequest request, HttpServletResponse response, DataAccessObjects dao) throws IOException, SQLException;
-    }
-
     /**
      * Invocation mapping gathered from the {@link RequestMapping} annotations.
      * <p>
@@ -75,7 +69,7 @@
      * The reason for this is the different handling of empty paths in
      * {@link HttpServletRequest#getPathInfo()}.
      */
-    private final Map<HttpMethod, Map<String, HandlerMethod>> mappings = new HashMap<>();
+    private final Map<HttpMethod, Map<String, Method>> mappings = new HashMap<>();
 
     /**
      * Gives implementing modules access to the {@link ModuleManager}.
@@ -95,12 +89,10 @@
      */
     private DataAccessObjects createDataAccessObjects(Connection connection) throws SQLException {
         final var df = (DatabaseFacade) getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME);
-        switch (df.getSQLDialect()) {
-            case Postgres:
-                return new PGDataAccessObjects(connection);
-            default:
-                throw new AssertionError("Non-exhaustive switch - this is a bug.");
+        if (df.getSQLDialect() == DatabaseFacade.Dialect.Postgres) {
+            return new PGDataAccessObjects(connection);
         }
+        throw new AssertionError("Non-exhaustive if-else - this is a bug.");
     }
 
     private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
@@ -160,9 +152,9 @@
 
                         final String requestPath = "/" + mapping.get().requestPath();
 
-                        if (mappings.computeIfAbsent(mapping.get().method(), k -> new HashMap<>()).
-                                putIfAbsent(requestPath,
-                                        (req, resp, dao) -> invokeMapping(method, req, resp, dao)) != null) {
+                        if (mappings
+                                .computeIfAbsent(mapping.get().method(), k -> new HashMap<>())
+                                .putIfAbsent(requestPath, method) != null) {
                             LOG.warn("{} {} has multiple mappings",
                                     mapping.get().method(),
                                     mapping.get().requestPath()
@@ -232,10 +224,10 @@
         req.getRequestDispatcher(HTML_FULL_DISPATCHER).forward(req, resp);
     }
 
-    private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) {
-        return Optional.ofNullable(mappings.get(method)).map(
-                (rm) -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse("/"))
-        );
+    private Optional<Method> findMapping(HttpMethod method, HttpServletRequest req) {
+        return Optional.ofNullable(mappings.get(method))
+                .map(rm -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse("/"))
+                );
     }
 
     private void forwardAsSpecified(ResponseType type, HttpServletRequest req, HttpServletResponse resp)
@@ -277,15 +269,24 @@
         final var db = (DatabaseFacade) req.getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME);
         try (final var connection = db.getDataSource().getConnection()) {
             final var dao = createDataAccessObjects(connection);
-            // call the handler, if available, or send an HTTP 404 error
-            final var mapping = findMapping(method, req);
-            if (mapping.isPresent()) {
-                forwardAsSpecified(mapping.get().apply(req, resp, dao), req, resp);
-            } else {
-                resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+            try {
+                connection.setAutoCommit(false);
+                // call the handler, if available, or send an HTTP 404 error
+                final var mapping = findMapping(method, req);
+                if (mapping.isPresent()) {
+                    forwardAsSpecified(invokeMapping(mapping.get(), req, resp, dao), req, resp);
+                } else {
+                    resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+                }
+                connection.commit();
+            } catch (SQLException ex) {
+                LOG.warn("Database transaction failed (Code {}): {}", ex.getErrorCode(), ex.getMessage());
+                LOG.debug("Details: ", ex);
+                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unhandled Transaction Error - Code:" + ex.getErrorCode());
+                connection.rollback();
             }
         } catch (SQLException ex) {
-            LOG.error("Database exception (Code {}): {}", ex.getErrorCode(), ex.getMessage());
+            LOG.error("Severe Database Exception (Code {}): {}", ex.getErrorCode(), ex.getMessage());
             LOG.debug("Details: ", ex);
             resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database Error - Code:" + ex.getErrorCode());
         }
--- a/src/main/java/de/uapcore/lightpit/dao/AbstractDao.java	Mon May 11 19:09:06 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/dao/AbstractDao.java	Tue May 12 22:03:00 2020 +0200
@@ -74,7 +74,7 @@
         }
     }
 
-    protected final <T> void setForeignKeyOrNull(PreparedStatement stmt, int index, T instance, Function<T, Integer> keyGetter) throws SQLException {
+    protected final void setForeignKeyOrNull(PreparedStatement stmt, int index, Object instance, Function<Object, Integer> keyGetter) throws SQLException {
         Integer key = Optional.ofNullable(instance).map(keyGetter).orElse(null);
         if (key == null) {
             stmt.setNull(index, Types.INTEGER);
--- a/src/main/java/de/uapcore/lightpit/modules/LanguageModule.java	Mon May 11 19:09:06 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/LanguageModule.java	Tue May 12 22:03:00 2020 +0200
@@ -68,13 +68,13 @@
                     }
                     languages.add(locale);
                 } catch (IllformedLocaleException ex) {
-                    LOG.warn("Specified lanaguge {} in context parameter cannot be mapped to an existing locale - skipping.", lang);
+                    LOG.warn("Specified language {} in context parameter cannot be mapped to an existing locale - skipping.", lang);
                 }
             }
 
         } else {
             languages.add(Locale.ENGLISH);
-            LOG.warn("Context parameter 'available-languges' not found. Only english will be available.");
+            LOG.warn("Context parameter 'available-languages' not found. Only english will be available.");
         }
     }
 
--- a/src/main/webapp/WEB-INF/dynamic_fragments/error.jsp	Mon May 11 19:09:06 2020 +0200
+++ b/src/main/webapp/WEB-INF/dynamic_fragments/error.jsp	Tue May 12 22:03:00 2020 +0200
@@ -24,18 +24,20 @@
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 --%>
-<%@page pageEncoding="UTF-8" session="true" %>
+<%@page pageEncoding="UTF-8" %>
 <%@page import="de.uapcore.lightpit.modules.ErrorModule" %>
 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 
+<jsp:useBean id="exception" scope="request" type="java.lang.Throwable"/>
+
 <c:set scope="page" var="errorCode" value="${requestScope[ErrorModule.REQ_ATTR_ERROR_CODE]}"/>
 
 <div id="error-page">
     <h1><fmt:message key="h1"/></h1>
     <table>
         <tr>
-            <th><fmt:message key="errorCode" />:</th>
+            <th><fmt:message key="errorCode"/>:</th>
             <td>${errorCode}</td>
         </tr>
         <tr>
--- a/src/main/webapp/WEB-INF/dynamic_fragments/language.jsp	Mon May 11 19:09:06 2020 +0200
+++ b/src/main/webapp/WEB-INF/dynamic_fragments/language.jsp	Tue May 12 22:03:00 2020 +0200
@@ -24,20 +24,25 @@
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 --%>
-<%@page pageEncoding="UTF-8" session="true" %>
+<%@page pageEncoding="UTF-8" %>
 <%@page import="de.uapcore.lightpit.Constants" %>
 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 
-<c:set scope="page" var="currentLanguage" value="${sessionScope[Constants.SESSION_ATTR_LANGUAGE]}" />
+<jsp:useBean id="languages" type="java.util.List<java.util.Locale>" scope="request"/>
+<jsp:useBean id="browserLanguage" type="java.util.Locale" scope="request"/>
 
-<form method="POST"id="lang-selector">
+<c:set scope="page" var="currentLanguage" value="${sessionScope[Constants.SESSION_ATTR_LANGUAGE]}"/>
+
+<form method="POST" id="lang-selector">
     <c:forEach items="${languages}" var="l">
         <label>
             <input type="radio" name="language" value="${l.language}"
                    <c:if test="${l.language eq currentLanguage.language}">checked</c:if>/>
-            ${l.displayLanguage}
-            (${l.getDisplayLanguage(currentLanguage)}<c:if test="${not empty browserLanguage and l.language eq browserLanguage.language}"><c:set var="browserLanguagePresent" value="true"/>&nbsp;-&nbsp;<fmt:message key="browserLanguage"/></c:if>)
+                ${l.displayLanguage}
+            (${l.getDisplayLanguage(currentLanguage)}
+            <c:if test="${not empty browserLanguage and l.language eq browserLanguage.language}"><c:set
+                    var="browserLanguagePresent" value="true"/>&nbsp;-&nbsp;<fmt:message key="browserLanguage"/></c:if>)
         </label>
     </c:forEach>
     <c:if test="${not browserLanguagePresent}">
--- a/src/main/webapp/WEB-INF/jsp/html_full.jsp	Mon May 11 19:09:06 2020 +0200
+++ b/src/main/webapp/WEB-INF/jsp/html_full.jsp	Tue May 12 22:03:00 2020 +0200
@@ -24,7 +24,7 @@
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 --%>
-<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" session="true" %>
+<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %>
 <%@page import="de.uapcore.lightpit.Constants" %>
 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
--- a/src/main/webapp/lightpit.css	Mon May 11 19:09:06 2020 +0200
+++ b/src/main/webapp/lightpit.css	Tue May 12 22:03:00 2020 +0200
@@ -60,8 +60,7 @@
 
 #subMenu {
     background: #f7f7ff;
-
-    border-image: linear-gradient(to right, #606060, rgba(60,60,60,.25));
+    border-image-source: linear-gradient(to right, #606060, rgba(60, 60, 60, .25));
     border-image-slice: 1;
     border-top-style: solid;
     border-top-width: 1pt;

mercurial