/** * Provides a method to directly get the <code>ServletRequestScope</code>, * to be able to set and clear its request. */ default ServletRequestScope getServletRequestScope() { return singleton( () -> new ServletRequestScope() ); }
/** * Provides a method to directly get the <code>HttpSessionScope</code>, * to be able to set and clear its session. */ default HttpSessionScope getHttpSessionScope() { return singleton( () -> new HttpSessionScope() ); }
@Override default Scope getRequestScope() { return makeScopeSerializable( () -> getServletRequestScope() ); } }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { getServletRequestScope().setStorage(request); if (request instanceof HttpServletRequest) { HttpServletRequest httpRequest = (HttpServletRequest) request; getHttpSessionScope().setStorage(httpRequest.getSession(true)); } try { chain.doFilter(request, response); } finally { getServletRequestScope().clearStorage(); getHttpSessionScope().clearStorage(); } } }).addMappingForUrlPatterns(null, false, "/*");
@Override public void onEndRequest(RequestCycle requestCycle) { getHttpSessionScope().clearStorage(); getServletRequestScope().clearStorage(); } });
@Override public void onBeginRequest(RequestCycle requestCycle) { if (requestCycle.getRequest() != null && requestCycle.getRequest() instanceof ServletWebRequest) { ServletRequest request = ((ServletWebRequest) requestCycle.getRequest()).getContainerRequest(); getServletRequestScope().setStorage(request); // Force creation of session if (request instanceof HttpServletRequest) { application.getSessionStore().getSessionId(requestCycle.getRequest(), true); Session session = application.fetchCreateAndSetSession(requestCycle); if (session == null) { throw new WicketRuntimeException("Could not create session, which is necessary for JayWire session scope."); } getHttpSessionScope().setStorage(((HttpServletRequest) request).getSession()); } } }
@Override default Scope getSessionScope() { return makeScopeSerializable( () -> getHttpSessionScope() ); } }
/** * Convenience method to produce request scope suppliers easily. Equals * <code>getRequestScope().apply(<supplier>)</code>. */ default <T> Supplier<T> requestScope(Factory<T> supplier) { return getRequestScope().apply(supplier); } }
/** * Convenience method to produce session scope suppliers easily. Equals * <code>getSessionScope().apply(<supplier>)</code>. */ default <T> Supplier<T> sessionScope(Factory<T> supplier) { return getSessionScope().apply(supplier); } }
@Override @SuppressWarnings("unchecked") public <T> T get(Factory<T> factory) { S storage = storageThreadLocal.get(); if (storage == null) { throw new IllegalStateException("there was no storage object while trying to get scoped object"); } T object = (T) retrievalFunction.apply(storage, factory.getKind()); if (object == null) { object = factory.get(); storageFunction.accept(storage, factory.getKind(), object); } return object; }
public void addRoutes() { Spark.before((request, response) -> { getServletRequestScope().setStorage(request.raw()); getHttpSessionScope().setStorage(request.session(true).raw()); }); Spark.after((request, response) -> { getServletRequestScope().clearStorage(); getHttpSessionScope().clearStorage(); }); } }
/** * Provides a method to directly get the <code>ServletRequestScope</code>, * to be able to set and clear its request. */ default ServletRequestScope getServletRequestScope() { return singleton( () -> new ServletRequestScope() ); }
/** * Provides a method to directly get the <code>HttpSessionScope</code>, * to be able to set and clear its session. */ default HttpSessionScope getHttpSessionScope() { return singleton( () -> new HttpSessionScope() ); }
@Override default Scope getRequestScope() { return makeScopeSerializable( () -> getServletRequestScope() ); } }
@Override default Scope getSessionScope() { return makeScopeSerializable( () -> getHttpSessionScope() ); } }
/** * Convenience method to produce request scope suppliers easily. Equals * <code>getRequestScope().apply(<supplier>)</code>. */ default <T> Supplier<T> requestScope(Factory<T> supplier) { return getRequestScope().apply(supplier); } }
/** * Convenience method to produce session scope suppliers easily. Equals * <code>getSessionScope().apply(<supplier>)</code>. */ default <T> Supplier<T> sessionScope(Factory<T> supplier) { return getSessionScope().apply(supplier); } }
@Override @SuppressWarnings("unchecked") public <T> T get(Factory<T> factory) { S storage = storageThreadLocal.get(); if (storage == null) { throw new IllegalStateException("there was no storage object while trying to get scoped object"); } T object = (T) retrievalFunction.apply(storage, factory.getKind()); if (object == null) { object = factory.get(); storageFunction.accept(storage, factory.getKind(), object); } return object; }