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

changeset 47
57cfb94ab99f
parent 45
cc7f082c5ef3
child 53
6a8498291606
equal deleted inserted replaced
46:1574965c7dc7 47:57cfb94ab99f
37 import javax.servlet.http.HttpServlet; 37 import javax.servlet.http.HttpServlet;
38 import javax.servlet.http.HttpServletRequest; 38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse; 39 import javax.servlet.http.HttpServletResponse;
40 import javax.servlet.http.HttpSession; 40 import javax.servlet.http.HttpSession;
41 import java.io.IOException; 41 import java.io.IOException;
42 import java.lang.reflect.Constructor;
42 import java.lang.reflect.Method; 43 import java.lang.reflect.Method;
43 import java.lang.reflect.Modifier; 44 import java.lang.reflect.Modifier;
44 import java.sql.Connection; 45 import java.sql.Connection;
45 import java.sql.SQLException; 46 import java.sql.SQLException;
46 import java.util.*; 47 import java.util.*;
223 public void setDynamicFragment(HttpServletRequest req, String fragmentName) { 224 public void setDynamicFragment(HttpServletRequest req, String fragmentName) {
224 req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName)); 225 req.setAttribute(Constants.REQ_ATTR_FRAGMENT, Functions.dynFragmentPath(fragmentName));
225 } 226 }
226 227
227 /** 228 /**
229 * @param req the servlet request object
230 * @param location the location where to redirect
231 * @see Constants#REQ_ATTR_REDIRECT_LOCATION
232 */
233 public void setRedirectLocation(HttpServletRequest req, String location) {
234 if (location.startsWith("./")) {
235 location = location.replaceFirst("\\./", Functions.baseHref(req));
236 }
237 req.setAttribute(Constants.REQ_ATTR_REDIRECT_LOCATION, location);
238 }
239
240 /**
228 * Specifies the name of an additional stylesheet used by the module. 241 * Specifies the name of an additional stylesheet used by the module.
229 * <p> 242 * <p>
230 * Setting an additional stylesheet is optional, but quite common for HTML 243 * Setting an additional stylesheet is optional, but quite common for HTML
231 * output. 244 * output.
232 * <p> 245 * <p>
236 * @param req the servlet request object 249 * @param req the servlet request object
237 * @param stylesheet the name of the stylesheet 250 * @param stylesheet the name of the stylesheet
238 */ 251 */
239 public void setStylesheet(HttpServletRequest req, String stylesheet) { 252 public void setStylesheet(HttpServletRequest req, String stylesheet) {
240 req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css")); 253 req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css"));
254 }
255
256 /**
257 * Obtains a request parameter of the specified type.
258 * The specified type must have a single-argument constructor accepting a string to perform conversion.
259 * The constructor of the specified type may throw an exception on conversion failures.
260 *
261 * @param req the servlet request object
262 * @param clazz the class object of the expected type
263 * @param name the name of the parameter
264 * @param <T> the expected type
265 * @return the parameter value or an empty optional, if no parameter with the specified name was found
266 */
267 public<T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
268 final String paramValue = req.getParameter(name);
269 if (paramValue == null) return Optional.empty();
270 if (clazz.equals(String.class)) return Optional.of((T)paramValue);
271 try {
272 final Constructor<T> ctor = clazz.getConstructor(String.class);
273 return Optional.of(ctor.newInstance(paramValue));
274 } catch (ReflectiveOperationException e) {
275 throw new RuntimeException(e);
276 }
277
241 } 278 }
242 279
243 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp) 280 private void forwardToFullView(HttpServletRequest req, HttpServletResponse resp)
244 throws IOException, ServletException { 281 throws IOException, ServletException {
245 282
285 resp.setLocale(sessionLocale); 322 resp.setLocale(sessionLocale);
286 LOG.trace("Continuing session {} with language {}", session.getId(), sessionLocale); 323 LOG.trace("Continuing session {} with language {}", session.getId(), sessionLocale);
287 } 324 }
288 325
289 // set some internal request attributes 326 // set some internal request attributes
327 req.setAttribute(Constants.REQ_ATTR_BASE_HREF, Functions.baseHref(req));
290 req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req)); 328 req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
291 Optional.ofNullable(moduleInfo).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy)); 329 Optional.ofNullable(moduleInfo).ifPresent((proxy) -> req.setAttribute(Constants.REQ_ATTR_MODULE_INFO, proxy));
292 330
293 // obtain a connection and create the data access objects 331 // obtain a connection and create the data access objects
294 final var db = (DatabaseFacade) req.getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME); 332 final var db = (DatabaseFacade) req.getServletContext().getAttribute(DatabaseFacade.SC_ATTR_NAME);

mercurial