src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java

Mon, 01 Jun 2020 14:46:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 01 Jun 2020 14:46:58 +0200
changeset 86
0a658e53177c
parent 83
24a3596b8f98
child 96
b7b685f31e39
permissions
-rw-r--r--

improves issue overview and adds progress information

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2018 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    29 package de.uapcore.lightpit;
    31 import de.uapcore.lightpit.dao.DataAccessObjects;
    32 import de.uapcore.lightpit.dao.postgres.PGDataAccessObjects;
    33 import org.slf4j.Logger;
    34 import org.slf4j.LoggerFactory;
    36 import javax.servlet.ServletException;
    37 import javax.servlet.http.HttpServlet;
    38 import javax.servlet.http.HttpServletRequest;
    39 import javax.servlet.http.HttpServletResponse;
    40 import javax.servlet.http.HttpSession;
    41 import java.io.IOException;
    42 import java.lang.reflect.*;
    43 import java.sql.Connection;
    44 import java.sql.SQLException;
    45 import java.util.*;
    46 import java.util.function.Function;
    48 /**
    49  * A special implementation of a HTTPServlet which is focused on implementing
    50  * the necessary functionality for LightPIT pages.
    51  */
    52 public abstract class AbstractLightPITServlet extends HttpServlet {
    54     private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class);
    56     private static final String SITE_JSP = Functions.jspPath("site");
    59     @FunctionalInterface
    60     protected interface SQLFindFunction<K, T> {
    61         T apply(K key) throws SQLException;
    63         default <V> SQLFindFunction<V, T> compose(Function<? super V, ? extends K> before) throws SQLException {
    64             Objects.requireNonNull(before);
    65             return (v) -> this.apply(before.apply(v));
    66         }
    68         default <V> SQLFindFunction<K, V> andThen(Function<? super T, ? extends V> after) throws SQLException {
    69             Objects.requireNonNull(after);
    70             return (t) -> after.apply(this.apply(t));
    71         }
    73         static <K> Function<K, K> identity() {
    74             return (t) -> t;
    75         }
    76     }
    78     /**
    79      * Invocation mapping gathered from the {@link RequestMapping} annotations.
    80      * <p>
    81      * Paths in this map must always start with a leading slash, although
    82      * the specification in the annotation must not start with a leading slash.
    83      * <p>
    84      * The reason for this is the different handling of empty paths in
    85      * {@link HttpServletRequest#getPathInfo()}.
    86      */
    87     private final Map<HttpMethod, Map<String, Method>> mappings = new HashMap<>();
    89     /**
    90      * Returns the name of the resource bundle associated with this servlet.
    91      *
    92      * @return the resource bundle base name
    93      */
    94     protected abstract String getResourceBundleName();
    97     /**
    98      * Creates a set of data access objects for the specified connection.
    99      *
   100      * @param connection the SQL connection
   101      * @return a set of data access objects
   102      */
   103     private DataAccessObjects createDataAccessObjects(Connection connection) throws SQLException {
   104         final var df = (DatabaseFacade) getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME);
   105         if (df.getSQLDialect() == DatabaseFacade.Dialect.Postgres) {
   106             return new PGDataAccessObjects(connection);
   107         }
   108         throw new AssertionError("Non-exhaustive if-else - this is a bug.");
   109     }
   111     private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
   112         try {
   113             LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName());
   114             final var paramTypes = method.getParameterTypes();
   115             final var paramValues = new Object[paramTypes.length];
   116             for (int i = 0; i < paramTypes.length; i++) {
   117                 if (paramTypes[i].isAssignableFrom(HttpServletRequest.class)) {
   118                     paramValues[i] = req;
   119                 } else if (paramTypes[i].isAssignableFrom(HttpServletResponse.class)) {
   120                     paramValues[i] = resp;
   121                 }
   122                 if (paramTypes[i].isAssignableFrom(DataAccessObjects.class)) {
   123                     paramValues[i] = dao;
   124                 }
   125             }
   126             return (ResponseType) method.invoke(this, paramValues);
   127         } catch (InvocationTargetException ex) {
   128             LOG.error("invocation of method {}::{} failed: {}",
   129                     method.getDeclaringClass().getName(), method.getName(), ex.getTargetException().getMessage());
   130             LOG.debug("Details: ", ex.getTargetException());
   131             resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getTargetException().getMessage());
   132             return ResponseType.NONE;
   133         } catch (ReflectiveOperationException | ClassCastException ex) {
   134             LOG.error("invocation of method {}::{} failed: {}",
   135                     method.getDeclaringClass().getName(), method.getName(), ex.getMessage());
   136             LOG.debug("Details: ", ex);
   137             resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
   138             return ResponseType.NONE;
   139         }
   140     }
   142     @Override
   143     public void init() throws ServletException {
   144         scanForRequestMappings();
   146         LOG.trace("{} initialized", getServletName());
   147     }
   149     private void scanForRequestMappings() {
   150         try {
   151             Method[] methods = getClass().getDeclaredMethods();
   152             for (Method method : methods) {
   153                 Optional<RequestMapping> mapping = Optional.ofNullable(method.getAnnotation(RequestMapping.class));
   154                 if (mapping.isPresent()) {
   155                     if (!Modifier.isPublic(method.getModifiers())) {
   156                         LOG.warn("{} is annotated with {} but is not public",
   157                                 method.getName(), RequestMapping.class.getSimpleName()
   158                         );
   159                         continue;
   160                     }
   161                     if (Modifier.isAbstract(method.getModifiers())) {
   162                         LOG.warn("{} is annotated with {} but is abstract",
   163                                 method.getName(), RequestMapping.class.getSimpleName()
   164                         );
   165                         continue;
   166                     }
   167                     if (!ResponseType.class.isAssignableFrom(method.getReturnType())) {
   168                         LOG.warn("{} is annotated with {} but has the wrong return type - 'ResponseType' required",
   169                                 method.getName(), RequestMapping.class.getSimpleName()
   170                         );
   171                         continue;
   172                     }
   174                     boolean paramsInjectible = true;
   175                     for (var param : method.getParameterTypes()) {
   176                         paramsInjectible &= HttpServletRequest.class.isAssignableFrom(param)
   177                                 || HttpServletResponse.class.isAssignableFrom(param)
   178                                 || DataAccessObjects.class.isAssignableFrom(param);
   179                     }
   180                     if (paramsInjectible) {
   181                         String requestPath = "/" + mapping.get().requestPath();
   183                         if (mappings
   184                                 .computeIfAbsent(mapping.get().method(), k -> new HashMap<>())
   185                                 .putIfAbsent(requestPath, method) != null) {
   186                             LOG.warn("{} {} has multiple mappings",
   187                                     mapping.get().method(),
   188                                     mapping.get().requestPath()
   189                             );
   190                         }
   192                         LOG.debug("{} {} maps to {}::{}",
   193                                 mapping.get().method(),
   194                                 requestPath,
   195                                 getClass().getSimpleName(),
   196                                 method.getName()
   197                         );
   198                     } else {
   199                         LOG.warn("{} is annotated with {} but has the wrong parameters - only HttpServletRequest. HttpServletResponse, and DataAccessObjects are allowed",
   200                                 method.getName(), RequestMapping.class.getSimpleName()
   201                         );
   202                     }
   203                 }
   204             }
   205         } catch (SecurityException ex) {
   206             LOG.error("Scan for request mappings on declared methods failed.", ex);
   207         }
   208     }
   210     @Override
   211     public void destroy() {
   212         mappings.clear();
   213         LOG.trace("{} destroyed", getServletName());
   214     }
   216     /**
   217      * Sets the name of the content page.
   218      * <p>
   219      * It is sufficient to specify the name without any extension. The extension
   220      * is added automatically if not specified.
   221      *
   222      * @param req      the servlet request object
   223      * @param pageName the name of the content page
   224      * @see Constants#REQ_ATTR_CONTENT_PAGE
   225      */
   226     protected void setContentPage(HttpServletRequest req, String pageName) {
   227         req.setAttribute(Constants.REQ_ATTR_CONTENT_PAGE, Functions.jspPath(pageName));
   228     }
   230     /**
   231      * Sets the breadcrumbs menu.
   232      *
   233      * @param req         the servlet request object
   234      * @param breadcrumbs the menu entries for the breadcrumbs menu
   235      * @see Constants#REQ_ATTR_BREADCRUMBS
   236      */
   237     protected void setBreadcrumbs(HttpServletRequest req, List<MenuEntry> breadcrumbs) {
   238         req.setAttribute(Constants.REQ_ATTR_BREADCRUMBS, breadcrumbs);
   239     }
   241     /**
   242      * @param req      the servlet request object
   243      * @param location the location where to redirect
   244      * @see Constants#REQ_ATTR_REDIRECT_LOCATION
   245      */
   246     protected void setRedirectLocation(HttpServletRequest req, String location) {
   247         if (location.startsWith("./")) {
   248             location = location.replaceFirst("\\./", Functions.baseHref(req));
   249         }
   250         req.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, location);
   251     }
   253     /**
   254      * Specifies the name of an additional stylesheet used by the module.
   255      * <p>
   256      * Setting an additional stylesheet is optional, but quite common for HTML
   257      * output.
   258      * <p>
   259      * It is sufficient to specify the name without any extension. The extension
   260      * is added automatically if not specified.
   261      *
   262      * @param req        the servlet request object
   263      * @param stylesheet the name of the stylesheet
   264      */
   265     public void setStylesheet(HttpServletRequest req, String stylesheet) {
   266         req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css"));
   267     }
   269     /**
   270      * Sets the view model object.
   271      * The type must match the expected type in the JSP file.
   272      *
   273      * @param req       the servlet request object
   274      * @param viewModel the view model object
   275      */
   276     public void setViewModel(HttpServletRequest req, Object viewModel) {
   277         req.setAttribute(Constants.REQ_ATTR_VIEWMODEL, viewModel);
   278     }
   280     /**
   281      * Obtains a request parameter of the specified type.
   282      * The specified type must have a single-argument constructor accepting a string to perform conversion.
   283      * The constructor of the specified type may throw an exception on conversion failures.
   284      *
   285      * @param req   the servlet request object
   286      * @param clazz the class object of the expected type
   287      * @param name  the name of the parameter
   288      * @param <T>   the expected type
   289      * @return the parameter value or an empty optional, if no parameter with the specified name was found
   290      */
   291     protected <T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
   292         if (clazz.isArray()) {
   293             final String[] paramValues = req.getParameterValues(name);
   294             int len = paramValues == null ? 0 : paramValues.length;
   295             final var array = (T) Array.newInstance(clazz.getComponentType(), len);
   296             for (int i = 0; i < len; i++) {
   297                 try {
   298                     final Constructor<?> ctor = clazz.getComponentType().getConstructor(String.class);
   299                     Array.set(array, i, ctor.newInstance(paramValues[i]));
   300                 } catch (ReflectiveOperationException e) {
   301                     throw new RuntimeException(e);
   302                 }
   303             }
   304             return Optional.of(array);
   305         } else {
   306             final String paramValue = req.getParameter(name);
   307             if (paramValue == null) return Optional.empty();
   308             if (clazz.equals(Boolean.class)) {
   309                 if (paramValue.toLowerCase().equals("false") || paramValue.equals("0")) {
   310                     return Optional.of((T) Boolean.FALSE);
   311                 } else {
   312                     return Optional.of((T) Boolean.TRUE);
   313                 }
   314             }
   315             if (clazz.equals(String.class)) return Optional.of((T) paramValue);
   316             if (java.sql.Date.class.isAssignableFrom(clazz)) {
   317                 try {
   318                     return Optional.of((T) java.sql.Date.valueOf(paramValue));
   319                 } catch (IllegalArgumentException ex) {
   320                     return Optional.empty();
   321                 }
   322             }
   323             try {
   324                 final Constructor<T> ctor = clazz.getConstructor(String.class);
   325                 return Optional.of(ctor.newInstance(paramValue));
   326             } catch (ReflectiveOperationException e) {
   327                 throw new RuntimeException(e);
   328             }
   329         }
   330     }
   332     /**
   333      * Tries to look up an entity with a key obtained from a request parameter.
   334      *
   335      * @param req   the servlet request object
   336      * @param clazz the class representing the type of the request parameter
   337      * @param name  the name of the request parameter
   338      * @param find  the find function (typically a DAO function)
   339      * @param <T>   the type of the request parameter
   340      * @param <R>   the type of the looked up entity
   341      * @return the retrieved entity or an empty optional if there is no such entity or the request parameter was missing
   342      * @throws SQLException if the find function throws an exception
   343      */
   344     protected <T, R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, SQLFindFunction<? super T, ? extends R> find) throws SQLException {
   345         final var param = getParameter(req, clazz, name);
   346         if (param.isPresent()) {
   347             return Optional.ofNullable(find.apply(param.get()));
   348         } else {
   349             return Optional.empty();
   350         }
   351     }
   353     private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp)
   354             throws IOException, ServletException {
   356         final String lightpitBundle = "localization.lightpit";
   357         final var mainMenu = List.of(
   358                 new MenuEntry(new ResourceKey(lightpitBundle, "menu.projects"), "projects/"),
   359                 new MenuEntry(new ResourceKey(lightpitBundle, "menu.users"), "teams/"),
   360                 new MenuEntry(new ResourceKey(lightpitBundle, "menu.languages"), "language/")
   361         );
   362         for (var entry : mainMenu) {
   363             if (Functions.fullPath(req).startsWith("/" + entry.getPathName())) {
   364                 entry.setActive(true);
   365             }
   366         }
   367         req.setAttribute(Constants.REQ_ATTR_MENU, mainMenu);
   368         req.getRequestDispatcher(SITE_JSP).forward(req, resp);
   369     }
   371     private String sanitizeRequestPath(HttpServletRequest req) {
   372         return Optional.ofNullable(req.getPathInfo()).orElse("/");
   373     }
   375     private Optional<Method> findMapping(HttpMethod method, HttpServletRequest req) {
   376         return Optional.ofNullable(mappings.get(method)).map(rm -> rm.get(sanitizeRequestPath(req)));
   377     }
   379     private void forwardAsSpecified(ResponseType type, HttpServletRequest req, HttpServletResponse resp)
   380             throws ServletException, IOException {
   381         switch (type) {
   382             case NONE:
   383                 return;
   384             case HTML:
   385                 forwardToFullView(req, resp);
   386                 return;
   387             // TODO: implement remaining response types
   388             default:
   389                 throw new AssertionError("ResponseType switch is not exhaustive - this is a bug!");
   390         }
   391     }
   393     private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   395         // choose the requested language as session language (if available) or fall back to english, otherwise
   396         HttpSession session = req.getSession();
   397         if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) {
   398             Optional<List<String>> availableLanguages = Functions.availableLanguages(getServletContext()).map(Arrays::asList);
   399             Optional<Locale> reqLocale = Optional.of(req.getLocale());
   400             Locale sessionLocale = reqLocale.filter((rl) -> availableLanguages.map((al) -> al.contains(rl.getLanguage())).orElse(false)).orElse(Locale.ENGLISH);
   401             session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale);
   402             LOG.debug("Setting language for new session {}: {}", session.getId(), sessionLocale.getDisplayLanguage());
   403         } else {
   404             Locale sessionLocale = (Locale) session.getAttribute(Constants.SESSION_ATTR_LANGUAGE);
   405             resp.setLocale(sessionLocale);
   406             LOG.trace("Continuing session {} with language {}", session.getId(), sessionLocale);
   407         }
   409         // set some internal request attributes
   410         final String fullPath = Functions.fullPath(req);
   411         req.setAttribute(Constants.REQ_ATTR_BASE_HREF, Functions.baseHref(req));
   412         req.setAttribute(Constants.REQ_ATTR_PATH, fullPath);
   413         req.setAttribute(Constants.REQ_ATTR_RESOURCE_BUNDLE, getResourceBundleName());
   415         // if this is an error path, bypass the normal flow
   416         if (fullPath.startsWith("/error/")) {
   417             final var mapping = findMapping(method, req);
   418             if (mapping.isPresent()) {
   419                 forwardAsSpecified(invokeMapping(mapping.get(), req, resp, null), req, resp);
   420             }
   421             return;
   422         }
   424         // obtain a connection and create the data access objects
   425         final var db = (DatabaseFacade) req.getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME);
   426         final var ds = db.getDataSource();
   427         if (ds == null) {
   428             resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "JNDI DataSource lookup failed. See log for details.");
   429             return;
   430         }
   431         try (final var connection = ds.getConnection()) {
   432             final var dao = createDataAccessObjects(connection);
   433             try {
   434                 connection.setAutoCommit(false);
   435                 // call the handler, if available, or send an HTTP 404 error
   436                 final var mapping = findMapping(method, req);
   437                 if (mapping.isPresent()) {
   438                     forwardAsSpecified(invokeMapping(mapping.get(), req, resp, dao), req, resp);
   439                 } else {
   440                     resp.sendError(HttpServletResponse.SC_NOT_FOUND);
   441                 }
   442                 connection.commit();
   443             } catch (SQLException ex) {
   444                 LOG.warn("Database transaction failed (Code {}): {}", ex.getErrorCode(), ex.getMessage());
   445                 LOG.debug("Details: ", ex);
   446                 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unhandled Transaction Error - Code: " + ex.getErrorCode());
   447                 connection.rollback();
   448             }
   449         } catch (SQLException ex) {
   450             LOG.error("Severe Database Exception (Code {}): {}", ex.getErrorCode(), ex.getMessage());
   451             LOG.debug("Details: ", ex);
   452             resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database Error - Code: " + ex.getErrorCode());
   453         }
   454     }
   456     @Override
   457     protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
   458             throws ServletException, IOException {
   459         doProcess(HttpMethod.GET, req, resp);
   460     }
   462     @Override
   463     protected final void doPost(HttpServletRequest req, HttpServletResponse resp)
   464             throws ServletException, IOException {
   465         doProcess(HttpMethod.POST, req, resp);
   466     }
   467 }

mercurial