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

changeset 71
dca186d3911f
parent 70
821c4950b619
child 73
672b5003cafe
equal deleted inserted replaced
70:821c4950b619 71:dca186d3911f
228 * The fragment must be located in the dynamic fragments folder. 228 * The fragment must be located in the dynamic fragments folder.
229 * 229 *
230 * @param req the servlet request object 230 * @param req the servlet request object
231 * @param fragmentName the name of the fragment 231 * @param fragmentName the name of the fragment
232 * @see Constants#DYN_FRAGMENT_PATH_PREFIX 232 * @see Constants#DYN_FRAGMENT_PATH_PREFIX
233 * @see Constants#REQ_ATTR_FRAGMENT
233 */ 234 */
234 protected void setDynamicFragment(HttpServletRequest req, String fragmentName) { 235 protected void setDynamicFragment(HttpServletRequest req, String fragmentName) {
235 req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName)); 236 req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName));
237 }
238
239 /**
240 * Sets the breadcrumbs menu.
241 *
242 * @param req the servlet request object
243 * @param breadcrumbs the menu entries for the breadcrumbs menu
244 * @see Constants#REQ_ATTR_BREADCRUMBS
245 */
246 protected void setBreadcrumbs(HttpServletRequest req, List<MenuEntry> breadcrumbs) {
247 req.setAttribute(Constants.REQ_ATTR_BREADCRUMBS, breadcrumbs);
236 } 248 }
237 249
238 /** 250 /**
239 * @param req the servlet request object 251 * @param req the servlet request object
240 * @param location the location where to redirect 252 * @param location the location where to redirect
266 /** 278 /**
267 * Obtains a request parameter of the specified type. 279 * Obtains a request parameter of the specified type.
268 * The specified type must have a single-argument constructor accepting a string to perform conversion. 280 * The specified type must have a single-argument constructor accepting a string to perform conversion.
269 * The constructor of the specified type may throw an exception on conversion failures. 281 * The constructor of the specified type may throw an exception on conversion failures.
270 * 282 *
271 * @param req the servlet request object 283 * @param req the servlet request object
272 * @param clazz the class object of the expected type 284 * @param clazz the class object of the expected type
273 * @param name the name of the parameter 285 * @param name the name of the parameter
274 * @param <T> the expected type 286 * @param <T> the expected type
275 * @return the parameter value or an empty optional, if no parameter with the specified name was found 287 * @return the parameter value or an empty optional, if no parameter with the specified name was found
276 */ 288 */
277 protected<T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) { 289 protected <T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
278 final String paramValue = req.getParameter(name); 290 final String paramValue = req.getParameter(name);
279 if (paramValue == null) return Optional.empty(); 291 if (paramValue == null) return Optional.empty();
280 if (clazz.equals(String.class)) return Optional.of((T)paramValue); 292 if (clazz.equals(String.class)) return Optional.of((T) paramValue);
281 try { 293 try {
282 final Constructor<T> ctor = clazz.getConstructor(String.class); 294 final Constructor<T> ctor = clazz.getConstructor(String.class);
283 return Optional.of(ctor.newInstance(paramValue)); 295 return Optional.of(ctor.newInstance(paramValue));
284 } catch (ReflectiveOperationException e) { 296 } catch (ReflectiveOperationException e) {
285 throw new RuntimeException(e); 297 throw new RuntimeException(e);
288 } 300 }
289 301
290 /** 302 /**
291 * Tries to look up an entity with a key obtained from a request parameter. 303 * Tries to look up an entity with a key obtained from a request parameter.
292 * 304 *
293 * @param req the servlet request object 305 * @param req the servlet request object
294 * @param clazz the class representing the type of the request parameter 306 * @param clazz the class representing the type of the request parameter
295 * @param name the name of the request parameter 307 * @param name the name of the request parameter
296 * @param find the find function (typically a DAO function) 308 * @param find the find function (typically a DAO function)
297 * @param <T> the type of the request parameter 309 * @param <T> the type of the request parameter
298 * @param <R> the type of the looked up entity 310 * @param <R> the type of the looked up entity
299 * @return the retrieved entity or an empty optional if there is no such entity or the request parameter was missing 311 * @return the retrieved entity or an empty optional if there is no such entity or the request parameter was missing
300 * @throws SQLException if the find function throws an exception 312 * @throws SQLException if the find function throws an exception
301 */ 313 */
302 protected<T,R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, SQLFindFunction<? super T, ? extends R> find) throws SQLException { 314 protected <T, R> Optional<R> findByParameter(HttpServletRequest req, Class<T> clazz, String name, SQLFindFunction<? super T, ? extends R> find) throws SQLException {
303 final var param = getParameter(req, clazz, name); 315 final var param = getParameter(req, clazz, name);
304 if (param.isPresent()) { 316 if (param.isPresent()) {
305 return Optional.ofNullable(find.apply(param.get())); 317 return Optional.ofNullable(find.apply(param.get()));
306 } else { 318 } else {
307 return Optional.empty(); 319 return Optional.empty();
309 } 321 }
310 322
311 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) 323 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp)
312 throws IOException, ServletException { 324 throws IOException, ServletException {
313 325
314 req.setAttribute(Constants.REQ_ATTR_MENU, getModuleManager().getMainMenu()); 326 final var mainMenu = new ArrayList<MenuEntry>(getModuleManager().getMainMenu());
327 for (var entry : mainMenu) {
328 if (Functions.fullPath(req).startsWith("/" + entry.getPathName())) {
329 entry.setActive(true);
330 }
331 }
332 req.setAttribute(Constants.REQ_ATTR_MENU, mainMenu);
315 req.getRequestDispatcher(SITE_JSP).forward(req, resp); 333 req.getRequestDispatcher(SITE_JSP).forward(req, resp);
316 } 334 }
317 335
318 private String sanitizeRequestPath(HttpServletRequest req) { 336 private String sanitizeRequestPath(HttpServletRequest req) {
319 return Optional.ofNullable(req.getPathInfo()).orElse("/"); 337 return Optional.ofNullable(req.getPathInfo()).orElse("/");

mercurial