/** * */ private void newSession() { ThreadContext.setSession(null); // the following will create a new session and put it in the thread context session = Session.get(); }
/** * */ private void newSession() { ThreadContext.setSession(null); // the following will create a new session and put it in the thread context session = Session.get(); }
private void bindSession(ServletRequest request, WebApplication application) { // find wicket session and bind it to thread HttpSession httpSession = ((HttpServletRequest)request).getSession(false); Session session = getSession(httpSession, application); if (session == null) { if (logger.isDebugEnabled()) { logger.debug("could not set Wicket session: key " + sessionKey + " not found in http session for " + ((HttpServletRequest)request).getContextPath() + "," + request.getServerName() + ", or http session does not exist"); } } else { ThreadContext.setSession(session); } }
private void bindSession(ServletRequest request, WebApplication application) { // find wicket session and bind it to thread HttpSession httpSession = ((HttpServletRequest)request).getSession(false); Session session = getSession(httpSession, application); if (session == null) { if (logger.isDebugEnabled()) { logger.debug("could not set Wicket session: key " + sessionKey + " not found in http session for " + ((HttpServletRequest)request).getContextPath() + "," + request.getServerName() + ", or http session does not exist"); } } else { ThreadContext.setSession(session); } }
private void executeInNewThread(Runnable runnable) { OrienteerWebSession session = OrienteerWebSession.get(); OrienteerWebApplication app = OrienteerWebApplication.get(); RequestCycle requestCycle = RequestCycle.get(); new Thread(() -> { ThreadContext.setSession(session); ThreadContext.setApplication(app); ThreadContext.setRequestCycle(requestCycle); runnable.run(); }).start(); }
@Override public String call() throws Exception { ThreadContext.setApplication(application); ThreadContext.setSession(session); try { final ConnInstanceTO connector = connectorRestClient.read(key); return String.format("{ \"status\": \"%s\", \"target\": \"%s\"}", connectorRestClient.check(connector).getLeft() ? TopologyNode.Status.REACHABLE : TopologyNode.Status.UNREACHABLE, key); } catch (Exception e) { LOG.warn("Error checking connection for {}", key, e); return String.format("{ \"status\": \"%s\", \"target\": \"%s\"}", TopologyNode.Status.FAILURE, key); } finally { ThreadContext.detach(); } } }
@Override public String call() throws Exception { ThreadContext.setApplication(application); ThreadContext.setSession(session); try { final ResourceTO resource = resourceRestClient.read(key); return String.format("{ \"status\": \"%s\", \"target\": \"%s\"}", resourceRestClient.check(resource).getLeft() ? TopologyNode.Status.REACHABLE : TopologyNode.Status.UNREACHABLE, key); } catch (Exception e) { LOG.warn("Error checking connection for {}", key, e); return String.format("{ \"status\": \"%s\", \"target\": \"%s\"}", TopologyNode.Status.FAILURE, key); } finally { ThreadContext.detach(); } } }
@Override public Object addingService(ServiceReference ref) { PageProvider service = (PageProvider)bundleContext.getService(ref); Object pathObject = ref.getProperty(PageProvider.MOUNT_PATH_PROPERTY); if (pathObject instanceof String) { String path = (String) pathObject; Class pageClass = service.getPageClass(); if(pageClass==ResourcePage.class) { //workaround so wicket doesn't choke because the thread context isn't filled (wrong thread) ThreadContext.setApplication(JabylonApplication.this); ThreadContext.setSession(new WebSession(createFakeRequest(null))); //if the main page is ready, we can mount the rest of the pages initMainPages(); } logger.info("Mounting new page {} at {}", pageClass, path); //$NON-NLS-1$ mount(new ResouceAwareMountedMapper(path, pageClass)); } else { logger.warn("Ignored Page {} because it was registered with invalid path property '{}'", service, pathObject); //$NON-NLS-1$ } return service; }
@Override public CompletableFuture<Void> fetchMailsAsync(OMailSettings settings, String folderName, Consumer<Message> consumer) { OrienteerWebSession session = OrienteerWebSession.get(); OrienteerWebApplication app = OrienteerWebApplication.get(); RequestCycle requestCycle = RequestCycle.get(); return CompletableFuture.runAsync(() -> { ThreadContext.setSession(session); ThreadContext.setApplication(app); ThreadContext.setRequestCycle(requestCycle); try { fetchMails(settings, folderName, consumer); } catch (Exception ex) { LOG.error("Error during fetching mails: {}", settings, ex); } }); }
private void performTask(OSendMailTaskSessionRuntime runtime) { OrienteerWebSession session = OrienteerWebSession.get(); OrienteerWebApplication app = OrienteerWebApplication.get(); RequestCycle requestCycle = RequestCycle.get(); new Thread(() -> { ThreadContext.setSession(session); ThreadContext.setApplication(app); ThreadContext.setRequestCycle(requestCycle); DBClosure.sudoConsumer(db -> { try { sendMails(runtime); } catch (Exception ex) { LOG.error("Error occurred during perform task {}", OSendMailTask.this, ex); } finally { runtime.finish(); } }); }).start(); }
@Override public void run() { ThreadContext.setApplication(application); ThreadContext.setSession(session); try { timeoutHandlingConnectionChecker( new ConnectorChecker(key, this.application), connectorTestTimeout, connectors, runningConnCheck); } finally { ThreadContext.detach(); } } }
@Override public void run() { ThreadContext.setApplication(application); ThreadContext.setSession(session); try { timeoutHandlingConnectionChecker( new ResourceChecker(key, this.application), resourceTestTimeout, resources, runningResCheck); } finally { ThreadContext.detach(); } } }
private void internalUmount(String path){ //workaround so wicket doesn't choke because the thread context isn't filled (wrong thread) Application application = ThreadContext.getApplication(); if (application == null) ThreadContext.setApplication(JabylonApplication.this); if(ThreadContext.getSession()==null) ThreadContext.setSession(new WebSession(createFakeRequest(null))); // unmount(path); /* * umount seems to be greedy, e.g. a prefix match is enough. * That's troublesome because umount /settings/log will also umount /settings */ ICompoundRequestMapper rootRequestMapperAsCompound = getRootRequestMapperAsCompound(); if (rootRequestMapperAsCompound instanceof CompoundRequestMapper) { CompoundRequestMapper compound = (CompoundRequestMapper) rootRequestMapperAsCompound; Iterator<IRequestMapper> it = compound.iterator(); while (it.hasNext()) { IRequestMapper iRequestMapper = it.next(); if (iRequestMapper instanceof ResouceAwareMountedMapper) { ResouceAwareMountedMapper mapper = (ResouceAwareMountedMapper) iRequestMapper; if(path.equals(mapper.getMountPath())) { logger.info("Unmounting {}",path); getRootRequestMapperAsCompound().remove(mapper); } } } } }
/** * * @param requestCycle * @return Session */ public Session fetchCreateAndSetSession(final RequestCycle requestCycle) { Args.notNull(requestCycle, "requestCycle"); Session session = getSessionStore().lookup(requestCycle.getRequest()); if (session == null) { session = newSession(requestCycle.getRequest(), requestCycle.getResponse()); ThreadContext.setSession(session); internalGetPageManager().newSessionCreated(); sessionListeners.onCreated(session); } else { ThreadContext.setSession(session); } return session; }
/** * * @param requestCycle * @return Session */ public Session fetchCreateAndSetSession(final RequestCycle requestCycle) { Args.notNull(requestCycle, "requestCycle"); Session session = getSessionStore().lookup(requestCycle.getRequest()); if (session == null) { session = newSession(requestCycle.getRequest(), requestCycle.getResponse()); ThreadContext.setSession(session); internalGetPageManager().newSessionCreated(); sessionListeners.onCreated(session); } else { ThreadContext.setSession(session); } return session; }
/** {@inheritDoc} */ @Override public final void removedService(ServiceReference<PageMounter> reference, PageMounter mounter) { PageMounter pageMounter = mounter; List<? extends MountPointInfo> infos = pageMounter.getMountPoints(); for (MountPointInfo info : infos) { LOGGER.trace("Trying to mount {} with {}", info.getPath(), info.getPage().getName()); Application oldApp = ThreadContext.getApplication(); ThreadContext.setApplication(application); if (!Session.exists()) { Request request = new MockWebRequest(Url.parse(info.getPath())); ThreadContext.setSession(new WebSession(request)); } application.unmount(info.getPath()); ThreadContext.setApplication(oldApp); LOGGER.info("Unmounted {} with {}", info.getPath(), info.getPage().getName()); } super.removedService(reference, pageMounter); } }
/** {@inheritDoc} */ @Override public final void removedService(ServiceReference<PageMounter> reference, PageMounter mounter) { PageMounter pageMounter = mounter; List<? extends MountPointInfo> infos = pageMounter.getMountPoints(); for (MountPointInfo info : infos) { LOGGER.trace("Trying to mount {} with {}", info.getPath(), info.getPage().getName()); Application oldApp = ThreadContext.getApplication(); ThreadContext.setApplication(application); if (!Session.exists()) { Request request = new MockWebRequest(Url.parse(info.getPath())); ThreadContext.setSession(new WebSession(request)); } application.unmount(info.getPath()); ThreadContext.setApplication(oldApp); LOGGER.info("Unmounted {} with {}", info.getPath(), info.getPage().getName()); } super.removedService(reference, pageMounter); } }
/** * Checks existence of a <code>Session</code> associated with the current thread. * * @return {@code true} if {@link Session#get()} can return the instance of session, * {@code false} otherwise */ public static boolean exists() { Session session = ThreadContext.getSession(); if (session == null) { // no session is available via ThreadContext, so lookup in session store RequestCycle requestCycle = RequestCycle.get(); if (requestCycle != null) { session = Application.get().getSessionStore().lookup(requestCycle.getRequest()); if (session != null) { ThreadContext.setSession(session); } } } return session != null; }
/** * Checks existence of a <code>Session</code> associated with the current thread. * * @return {@code true} if {@link Session#get()} can return the instance of session, * {@code false} otherwise */ public static boolean exists() { Session session = ThreadContext.getSession(); if (session == null) { // no session is available via ThreadContext, so lookup in session store RequestCycle requestCycle = RequestCycle.get(); if (requestCycle != null) { session = Application.get().getSessionStore().lookup(requestCycle.getRequest()); if (session != null) { ThreadContext.setSession(session); } } } return session != null; }
/** * Invoke this method in the new thread before doing something with Wicket. */ 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()) ); } }