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

changeset 33
fd8c40ff78c3
parent 29
27a0fdd7bca7
child 34
824d4042c857
--- a/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Sat May 09 14:58:41 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Sat May 09 15:19:21 2020 +0200
@@ -28,22 +28,18 @@
  */
 package de.uapcore.lightpit;
 
-import java.io.IOException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Optional;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.*;
 
 /**
  * A special implementation of a HTTPServlet which is focused on implementing
@@ -54,21 +50,21 @@
     private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class);
     
     private static final String HTML_FULL_DISPATCHER = Functions.jspPath("html_full");
-    
+
     /**
      * Store a reference to the annotation for quicker access.
      */
-    private Optional<LightPITModule> moduleInfo = Optional.empty();
+    private LightPITModule moduleInfo = null;
 
     /**
      * The EL proxy is necessary, because the EL resolver cannot handle annotation properties.
      */
-    private Optional<LightPITModule.ELProxy> moduleInfoELProxy = Optional.empty();
-    
-    
+    private LightPITModule.ELProxy moduleInfoELProxy = null;
+
+
     @FunctionalInterface
-    private static interface HandlerMethod {
-        ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException, ServletException;
+    private interface HandlerMethod {
+        ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException;
     }
     
     /**
@@ -84,22 +80,27 @@
 
     /**
      * Gives implementing modules access to the {@link ModuleManager}.
+     *
      * @return the module manager
      */
     protected final ModuleManager getModuleManager() {
         return (ModuleManager) getServletContext().getAttribute(ModuleManager.SC_ATTR_NAME);
     }
-    
+
+    public final LightPITModule getModuleInfo() {
+        return moduleInfo;
+    }
+
     /**
      * Gives implementing modules access to the {@link DatabaseFacade}.
+     *
      * @return the database facade
      */
     protected final DatabaseFacade getDatabaseFacade() {
         return (DatabaseFacade) getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME);
     }
-    
-    private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp)
-            throws IOException, ServletException {
+
+    private ResponseType invokeMapping(Method method, HttpServletRequest req, HttpServletResponse resp) throws IOException {
         try {
             LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName());
             return (ResponseType) method.invoke(this, req, resp);
@@ -112,13 +113,13 @@
 
     @Override
     public void init() throws ServletException {
-        moduleInfo = Optional.ofNullable(this.getClass().getAnnotation(LightPITModule.class));
-        moduleInfoELProxy = moduleInfo.map(LightPITModule.ELProxy::convert);
-        
-        if (moduleInfo.isPresent()) {
+        moduleInfo = this.getClass().getAnnotation(LightPITModule.class);
+        moduleInfoELProxy = moduleInfo == null ? null : LightPITModule.ELProxy.convert(moduleInfo);
+
+        if (moduleInfo != null) {
             scanForRequestMappings();
         }
-        
+
         LOG.trace("{} initialized", getServletName());
     }
 
@@ -270,7 +271,7 @@
         // set some internal request attributes
         req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
         req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName());
-        moduleInfoELProxy.ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
+        Optional.ofNullable(moduleInfoELProxy).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
         
         
         // call the handler, if available, or send an HTTP 404 error

mercurial