Example usage for org.apache.wicket ThreadContext getSession

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

Introduction

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

Prototype

public static Session getSession() 

Source Link

Usage

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

License:Apache License

/**
 * reuse an existing session if possible
 *///from w  w  w.ja  v  a2  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.evolveum.midpoint.web.security.MidPointAuthWebSession.java

License:Apache License

private void auditEvent(Authentication authentication, String username, OperationResultStatus status) {
    MidPointPrincipal principal = SecurityUtils.getPrincipalUser(authentication);
    PrismObject<UserType> user = principal != null ? principal.getUser().asPrismObject() : null;

    Task task = taskManager.createTaskInstance();
    task.setOwner(user);/* w  w w  .j  a v a2 s . c o m*/
    task.setChannel(SchemaConstants.CHANNEL_GUI_USER_URI);

    AuditEventRecord record = new AuditEventRecord(AuditEventType.CREATE_SESSION, AuditEventStage.REQUEST);
    record.setInitiator(user);
    record.setParameter(username);

    record.setChannel(SchemaConstants.CHANNEL_GUI_USER_URI);
    Url url = RequestCycle.get().getRequest().getUrl();
    record.setHostIdentifier(url.getHost());
    record.setTimestamp(System.currentTimeMillis());

    Session session = ThreadContext.getSession();
    if (session != null) {
        record.setSessionIdentifier(session.getId());
    }

    record.setOutcome(status);

    auditService.audit(record, task);
}

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 a  v  a 2s  .  com
    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.jabylon.rest.ui.wicket.JabylonApplication.java

License:Open Source License

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);
    /*//www .ja  v a2 s .  c o m
     * 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);
                }
            }
        }
    }
}

From source file:org.ujorm.hotels.services.impl.AuthServiceImpl.java

License:Apache License

/** Return a Session or {@code null} if no session was found. */
private Session getThreadSession() {
    return ThreadContext.getSession();
}