List of usage examples for org.apache.wicket ThreadContext setRequestCycle
public static void setRequestCycle(RequestCycle requestCycle)
From source file:at.molindo.wicketutils.utils.MockUtils.java
License:Apache License
/** * reuse an existing session if possible *//*from ww w. ja va 2 s . c o m*/ public static <V> V withRequest(WebApplication webApplication, IMockRequestCallback<V> callback) { Session oldSession = ThreadContext.exists() ? ThreadContext.getSession() : null; ThreadContext oldContext = ThreadContext.detach(); try { ThreadContext.setApplication(webApplication); ThreadContext.setSession(oldSession); // mock http session ServletContext context = webApplication.getServletContext(); MockHttpSession httpSession = new MockHttpSession(context); // mock servlet request MockServletRequest servletRequest = new MockServletRequest(webApplication, httpSession, context); callback.configure(new MockRequest(servletRequest)); servletRequest.setDefaultHeaders(); // mock response MockHttpServletResponse servletResponse = new MockHttpServletResponse(servletRequest); // mock web request final WebRequest request = VisibilityHelper.newWebRequest(webApplication, servletRequest, "/"); // mock web response final WebResponse response = VisibilityHelper.newWebResponse(webApplication, request, servletResponse); // create ThreadContext.setRequestCycle(webApplication.createRequestCycle(request, response)); return callback.call(); } finally { Session newSession = ThreadContext.getSession(); ThreadContext.restore(oldContext); if (oldSession == null && newSession != null && !newSession.isTemporary()) { // reuse session if a new one was created ThreadContext.setSession(newSession); } } }
From source file:com.mastfrog.acteur.wicket.WicketActeur.java
License:Open Source License
@Inject WicketActeur(HttpEvent evt, Application application, PathFactory pf, Charset charset, WicketConfig config, ByteBufAllocator alloc, Settings settings, ReentrantScope scope) { RequestAdapter request = new RequestAdapter(evt, config.locale(), charset, settings); ResponseAdapter response = new ResponseAdapter(response(), charset, alloc, pf); try (QuietAutoCloseable closeScope = scope.enter(request, response, response())) { ThreadContext.setApplication(application); RequestCycle requestCycle = application.createRequestCycle(request, response); ThreadContext.setRequestCycle(requestCycle); boolean processed = requestCycle.processRequestAndDetach(); if (!processed) { reject();/*from w w w . ja v a 2s .c om*/ } else { HttpResponseStatus status = response.status(); reply(status == null ? OK : status); } } finally { ThreadContext.detach(); } }
From source file:com.norconex.commons.wicket.ThreadContextMocker.java
License:Apache License
/** * Invoke this method in the new thread before doing something with Wicket. *//*from w w w . j a va2 s . c o m*/ public void mock() { ThreadContext.setApplication(application); ThreadContext.setSession(session); final MockServletContext context = new MockServletContext(application, SystemUtils.JAVA_IO_TMPDIR); ThreadContext.setRequestCycle( application.createRequestCycle(new MockWebRequest(Url.parse("http://localhost/mock")) { @Override public Object getContainerRequest() { return new MockHttpServletRequest(application, new MockHttpSession(context), context); } }, new MockWebResponse())); }
From source file:org.apache.openmeetings.db.util.ApplicationHelper.java
License:Apache License
public static IApplication ensureApplication(Long langId) { IApplication a = null;//from w ww .j ava2 s . co m if (Application.exists()) { a = (IApplication) Application.get(); } else { WebApplication app = (WebApplication) Application.get(wicketApplicationName); LabelDao.initLanguageMap(); if (app == null) { try { app = (WebApplication) LabelDao.getAppClass().newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { log.error("Failed to create Application"); return null; } app.setServletContext(new MockServletContext(app, null)); app.setName(wicketApplicationName); ServletContext sc = app.getServletContext(); OMContextListener omcl = new OMContextListener(); omcl.contextInitialized(new ServletContextEvent(sc)); XmlWebApplicationContext xmlContext = new XmlWebApplicationContext(); xmlContext.setConfigLocation("classpath:openmeetings-applicationContext.xml"); xmlContext.setServletContext(sc); xmlContext.refresh(); sc.setAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, xmlContext); app.setConfigurationType(RuntimeConfigurationType.DEPLOYMENT); ThreadContext.setApplication(app); app.initApplication(); } else { ThreadContext.setApplication(app); } a = (IApplication) Application.get(wicketApplicationName); } if (ThreadContext.getRequestCycle() == null) { ServletWebRequest req = new ServletWebRequest(new MockHttpServletRequest((Application) a, new MockHttpSession(a.getServletContext()), a.getServletContext()), ""); RequestCycleContext rctx = new RequestCycleContext(req, new MockWebResponse(), a.getRootRequestMapper(), a.getExceptionMapperProvider().get()); ThreadContext.setRequestCycle(new RequestCycle(rctx)); } if (ThreadContext.getSession() == null) { WebSession s = WebSession.get(); if (langId > 0) { ((IWebSession) s).setLanguage(langId); } } return a; }
From source file:org.apache.openmeetings.db.util.ApplicationHelper.java
License:Apache License
public static void destroyApplication() { WebApplicationContext ctx = getWebApplicationContext( ((WebApplication) ensureApplication()).getServletContext()); ((XmlWebApplicationContext) ctx).destroy(); ThreadContext.setApplication(null);//from w w w . j av a 2 s. c o m ThreadContext.setRequestCycle(null); ThreadContext.setSession(null); }