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

changeset 36
0f4f8f255c32
parent 34
824d4042c857
child 38
cf85ef18f231
equal deleted inserted replaced
35:4fa33bfa8fb9 36:0f4f8f255c32
50 private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class); 50 private static final Logger LOG = LoggerFactory.getLogger(AbstractLightPITServlet.class);
51 51
52 private static final String HTML_FULL_DISPATCHER = Functions.jspPath("html_full"); 52 private static final String HTML_FULL_DISPATCHER = Functions.jspPath("html_full");
53 53
54 /** 54 /**
55 * Store a reference to the annotation for quicker access.
56 */
57 private LightPITModule moduleInfo = null;
58
59 /**
60 * The EL proxy is necessary, because the EL resolver cannot handle annotation properties. 55 * The EL proxy is necessary, because the EL resolver cannot handle annotation properties.
61 */ 56 */
62 private LightPITModule.ELProxy moduleInfoELProxy = null; 57 private LightPITModule.ELProxy moduleInfo = null;
63 58
64 59
65 @FunctionalInterface 60 @FunctionalInterface
66 private interface HandlerMethod { 61 private interface HandlerMethod {
67 ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException; 62 ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException;
83 * 78 *
84 * @return the module manager 79 * @return the module manager
85 */ 80 */
86 protected final ModuleManager getModuleManager() { 81 protected final ModuleManager getModuleManager() {
87 return (ModuleManager) getServletContext().getAttribute(ModuleManager.SC_ATTR_NAME); 82 return (ModuleManager) getServletContext().getAttribute(ModuleManager.SC_ATTR_NAME);
88 }
89
90 /**
91 * Returns the annotated module information.
92 *
93 * @return the module annotation
94 */
95 public final LightPITModule getModuleInfo() {
96 return moduleInfo;
97 } 83 }
98 84
99 /** 85 /**
100 * Gives implementing modules access to the {@link DatabaseFacade}. 86 * Gives implementing modules access to the {@link DatabaseFacade}.
101 * 87 *
116 } 102 }
117 } 103 }
118 104
119 @Override 105 @Override
120 public void init() throws ServletException { 106 public void init() throws ServletException {
121 moduleInfo = this.getClass().getAnnotation(LightPITModule.class); 107 moduleInfo = Optional.ofNullable(this.getClass().getAnnotation(LightPITModule.class))
122 moduleInfoELProxy = moduleInfo == null ? null : LightPITModule.ELProxy.convert(moduleInfo); 108 .map(LightPITModule.ELProxy::new).orElse(null);
123 109
124 if (moduleInfo != null) { 110 if (moduleInfo != null) {
125 scanForRequestMappings(); 111 scanForRequestMappings();
126 } 112 }
127 113
226 } 212 }
227 213
228 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) 214 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp)
229 throws IOException, ServletException { 215 throws IOException, ServletException {
230 216
231 req.setAttribute(Constants.REQ_ATTR_MENU, getModuleManager().getMainMenu(getDatabaseFacade())); 217 req.setAttribute(Constants.REQ_ATTR_MENU, getModuleManager().getMainMenu());
232 req.getRequestDispatcher(HTML_FULL_DISPATCHER).forward(req, resp); 218 req.getRequestDispatcher(HTML_FULL_DISPATCHER).forward(req, resp);
233 } 219 }
234 220
235 private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) { 221 private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) {
236 return Optional.ofNullable(mappings.get(method)).map( 222 return Optional.ofNullable(mappings.get(method)).map(
252 } 238 }
253 } 239 }
254 240
255 private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp) 241 private void doProcess(HttpMethod method, HttpServletRequest req, HttpServletResponse resp)
256 throws ServletException, IOException { 242 throws ServletException, IOException {
257
258 // Synchronize module information with database
259 getModuleManager().syncWithDatabase(getDatabaseFacade());
260 243
261 // choose the requested language as session language (if available) or fall back to english, otherwise 244 // choose the requested language as session language (if available) or fall back to english, otherwise
262 HttpSession session = req.getSession(); 245 HttpSession session = req.getSession();
263 if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) { 246 if (session.getAttribute(Constants.SESSION_ATTR_LANGUAGE) == null) {
264 Optional<List<String>> availableLanguages = Functions.availableLanguages(getServletContext()).map(Arrays::asList); 247 Optional<List<String>> availableLanguages = Functions.availableLanguages(getServletContext()).map(Arrays::asList);
273 } 256 }
274 257
275 // set some internal request attributes 258 // set some internal request attributes
276 req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req)); 259 req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
277 req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName()); 260 req.setAttribute(Constants.REQ_ATTR_MODULE_CLASSNAME, this.getClass().getName());
278 Optional.ofNullable(moduleInfoELProxy).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy)); 261 Optional.ofNullable(moduleInfo).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
279 262
280 263
281 // call the handler, if available, or send an HTTP 404 error 264 // call the handler, if available, or send an HTTP 404 error
282 Optional<HandlerMethod> mapping = findMapping(method, req); 265 Optional<HandlerMethod> mapping = findMapping(method, req);
283 if (mapping.isPresent()) { 266 if (mapping.isPresent()) {

mercurial