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

changeset 33
fd8c40ff78c3
parent 29
27a0fdd7bca7
child 34
824d4042c857
     1.1 --- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Sat May 09 14:58:41 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Sat May 09 15:19:21 2020 +0200
     1.3 @@ -28,22 +28,18 @@
     1.4   */
     1.5  package de.uapcore.lightpit;
     1.6  
     1.7 -import java.io.IOException;
     1.8 -import java.lang.reflect.Method;
     1.9 -import java.lang.reflect.Modifier;
    1.10 -import java.util.Arrays;
    1.11 -import java.util.HashMap;
    1.12 -import java.util.List;
    1.13 -import java.util.Locale;
    1.14 -import java.util.Map;
    1.15 -import java.util.Optional;
    1.16 +import org.slf4j.Logger;
    1.17 +import org.slf4j.LoggerFactory;
    1.18 +
    1.19  import javax.servlet.ServletException;
    1.20  import javax.servlet.http.HttpServlet;
    1.21  import javax.servlet.http.HttpServletRequest;
    1.22  import javax.servlet.http.HttpServletResponse;
    1.23  import javax.servlet.http.HttpSession;
    1.24 -import org.slf4j.Logger;
    1.25 -import org.slf4j.LoggerFactory;
    1.26 +import java.io.IOException;
    1.27 +import java.lang.reflect.Method;
    1.28 +import java.lang.reflect.Modifier;
    1.29 +import java.util.*;
    1.30  
    1.31  /**
    1.32   * A special implementation of a HTTPServlet which is focused on implementing
    1.33 @@ -54,21 +50,21 @@
    1.34      private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class);
    1.35      
    1.36      private static final String HTML_FULL_DISPATCHER = Functions.jspPath("html_full");
    1.37 -    
    1.38 +
    1.39      /**
    1.40       * Store a reference to the annotation for quicker access.
    1.41       */
    1.42 -    private Optional<LightPITModule> moduleInfo = Optional.empty();
    1.43 +    private LightPITModule moduleInfo = null;
    1.44  
    1.45      /**
    1.46       * The EL proxy is necessary, because the EL resolver cannot handle annotation properties.
    1.47       */
    1.48 -    private Optional<LightPITModule.ELProxy> moduleInfoELProxy = Optional.empty();
    1.49 -    
    1.50 -    
    1.51 +    private LightPITModule.ELProxy moduleInfoELProxy = null;
    1.52 +
    1.53 +
    1.54      @FunctionalInterface
    1.55 -    private static interface HandlerMethod {
    1.56 -        ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException, ServletException;
    1.57 +    private interface HandlerMethod {
    1.58 +        ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException;
    1.59      }
    1.60      
    1.61      /**
    1.62 @@ -84,22 +80,27 @@
    1.63  
    1.64      /**
    1.65       * Gives implementing modules access to the {@link ModuleManager}.
    1.66 +     *
    1.67       * @return the module manager
    1.68       */
    1.69      protected final ModuleManager getModuleManager() {
    1.70          return (ModuleManager) getServletContext().getAttribute(ModuleManager.SC_ATTR_NAME);
    1.71      }
    1.72 -    
    1.73 +
    1.74 +    public final LightPITModule getModuleInfo() {
    1.75 +        return moduleInfo;
    1.76 +    }
    1.77 +
    1.78      /**
    1.79       * Gives implementing modules access to the {@link DatabaseFacade}.
    1.80 +     *
    1.81       * @return the database facade
    1.82       */
    1.83      protected final DatabaseFacade getDatabaseFacade() {
    1.84          return (DatabaseFacade) getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME);
    1.85      }
    1.86 -    
    1.87 -    private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp)
    1.88 -            throws IOException, ServletException {
    1.89 +
    1.90 +    private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    1.91          try {
    1.92              LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName());
    1.93              return (ResponseType) method.invoke(this, req, resp);
    1.94 @@ -112,13 +113,13 @@
    1.95  
    1.96      @Override
    1.97      public void init() throws ServletException {
    1.98 -        moduleInfo = Optional.ofNullable(this.getClass().getAnnotation(LightPITModule.class));
    1.99 -        moduleInfoELProxy = moduleInfo.map(LightPITModule.ELProxy::convert);
   1.100 -        
   1.101 -        if (moduleInfo.isPresent()) {
   1.102 +        moduleInfo = this.getClass().getAnnotation(LightPITModule.class);
   1.103 +        moduleInfoELProxy = moduleInfo == null ? null : LightPITModule.ELProxy.convert(moduleInfo);
   1.104 +
   1.105 +        if (moduleInfo != null) {
   1.106              scanForRequestMappings();
   1.107          }
   1.108 -        
   1.109 +
   1.110          LOG.trace("{} initialized", getServletName());
   1.111      }
   1.112  
   1.113 @@ -270,7 +271,7 @@
   1.114          // set some internal request attributes
   1.115          req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
   1.116          req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName());
   1.117 -        moduleInfoELProxy.ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
   1.118 +        Optional.ofNullable(moduleInfoELProxy).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
   1.119          
   1.120          
   1.121          // call the handler, if available, or send an HTTP 404 error

mercurial