Example usage for org.apache.wicket ThreadContext setSession

List of usage examples for org.apache.wicket ThreadContext setSession

Introduction

In this page you can find the example usage for org.apache.wicket ThreadContext setSession.

Prototype

public static void setSession(Session session) 

Source Link

Document

Binds the session to current thread.

Usage

From source file:at.molindo.wicketutils.utils.MockUtils.java

License:Apache License

/**
 * reuse an existing session if possible
 *///from w  w w.j  ava  2 s.  co 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.evolveum.midpoint.web.component.SecurityContextAwareCallable.java

License:Apache License

protected void setupContext(Application application, Session session) {
    if (!Application.exists() && application != null) {
        ThreadContext.setApplication(application);
    }//w  ww  .j  av  a 2s.co  m
    if (!Session.exists() && session != null) {
        ThreadContext.setSession(session);
    }
}

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   ww w .j  a v a 2 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 void destroyApplication() {
    WebApplicationContext ctx = getWebApplicationContext(
            ((WebApplication) ensureApplication()).getServletContext());
    ((XmlWebApplicationContext) ctx).destroy();
    ThreadContext.setApplication(null);/*from   www .ja v a2  s  .  c  om*/
    ThreadContext.setRequestCycle(null);
    ThreadContext.setSession(null);
}

From source file:org.efaps.ui.wicket.background.JobRunnable.java

License:Apache License

@Override
public void run() {
    try {//w w w .  j a v a  2s .  c o m
        ThreadContext.setApplication(this.application);
        ThreadContext.setSession(this.session);
        final Context context = Context.begin(this.bridge.getJobContext().getUserName(),
                this.bridge.getJobContext().getLocale(), null, null, null, Context.Inheritance.Local);
        context.setCompany(Company.get(this.bridge.getJobContext().getCompanyUUID()));
        this.job.execute(this.bridge);
        Context.commit(true);
    } catch (final EFapsException e) {
        LOG.debug("run", e);
    } finally {
        ThreadContext.detach();
    }
}

From source file:org.hippoecm.frontend.plugins.standards.datetime.DateTimePrinterTest.java

License:Apache License

@Before
public void before() throws Exception {
    UserSession session = EasyMock.createNiceMock(UserSession.class);
    EasyMock.expect(session.getLocale()).andAnswer(() -> locale).anyTimes();
    EasyMock.expect(session.getTimeZone()).andAnswer(() -> timeZone).anyTimes();
    EasyMock.replay(session);//from   w ww. j  a  v a 2s. c o m
    ThreadContext.setSession(session);

    cal70 = Calendar.getInstance();
    cal70.setTimeInMillis(0);
    date70 = cal70.getTime();
    instant70 = cal70.toInstant();
}

From source file:org.hippoecm.frontend.plugins.standards.util.ByteSizeFormatterTest.java

License:Apache License

@Before
public void before() throws Exception {
    Session session = EasyMock.createNiceMock(Session.class);
    EasyMock.expect(session.getLocale()).andReturn(Locale.FRANCE).anyTimes();
    EasyMock.replay(session);//from w  w w.  j a  v a2 s  .c  om
    ThreadContext.setSession(session);
}

From source file:org.hippoecm.frontend.plugins.standards.util.ByteSizeFormatterTest.java

License:Apache License

@Test
public void testSizeFormattingInUSALocale() throws Exception {
    Session session = EasyMock.createNiceMock(Session.class);
    EasyMock.expect(session.getLocale()).andReturn(Locale.US).anyTimes();
    EasyMock.replay(session);/*from   w w w. j a  v a 2  s  .  c  om*/
    ThreadContext.setSession(session);

    long size = 1024 + 512 + 32; // 1.5.. (with more fractions, but rounded to 1.5) KB
    ByteSizeFormatter formatter = new ByteSizeFormatter(1);
    assertEquals("1.5 KB", formatter.format(size));
}

From source file:org.hippoecm.frontend.plugins.standards.util.ByteSizeFormatterTest.java

License:Apache License

@Test
public void testSizeFormattingInFranceLocale() throws Exception {
    Session session = EasyMock.createNiceMock(Session.class);
    EasyMock.expect(session.getLocale()).andReturn(Locale.FRANCE).anyTimes();
    EasyMock.replay(session);//ww w  . j  a v  a  2 s.  com
    ThreadContext.setSession(session);

    long size = 1024 + 512 + 32; // 1.5.. (with more fractions, but rounded to 1.5) KB
    ByteSizeFormatter formatter = new ByteSizeFormatter(1);
    assertEquals("1,5 KB", formatter.format(size));
}

From source file:org.jabylon.rest.ui.wicket.JabylonApplication.java

License:Open Source License

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//  w w  w  .j a  v a2 s. co m
protected void init() {
    super.init();
    mount(new ResouceAwareMountedMapper("/", StartupPage.class)); //$NON-NLS-1$
    getRequestCycleSettings().setResponseRequestEncoding("UTF-8");
    getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
    OSGiInjector injector = new OSGiInjector(this);
    getBehaviorInstantiationListeners().add(injector);
    getResourceSettings().getStringResourceLoaders().add(new OSGiAwareBundleStringResourceLoader());
    getApplicationSettings().setInternalErrorPage(CustomInternalErrorPage.class);
    getComponentInstantiationListeners().add(injector);
    getSecuritySettings().setAuthorizationStrategy(new PermissionBasedAuthorizationStrategy());
    getAjaxRequestTargetListeners().add(new AjaxFeedbackListener());
    final BundleContext bundleContext = Activator.getDefault().getContext();

    pageTracker = new ServiceTracker(bundleContext, PageProvider.class, new ServiceTrackerCustomizer() {

        @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 '{}'", //$NON-NLS-1$
                        service, pathObject);
            }
            return service;
        }

        @Override
        public void modifiedService(ServiceReference arg0, Object arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void removedService(ServiceReference ref, Object service) {
            Object pathObject = ref.getProperty(PageProvider.MOUNT_PATH_PROPERTY);
            if (pathObject instanceof String) {
                String path = (String) pathObject;
                internalUmount(path);
            }
        }

    });
    pageTracker.open();
}