Search in sources :

Example 46 with UserSession

use of org.olat.core.util.UserSession in project openolat by klemens.

the class UserSessionManager method getUserSessionIfAlreadySet.

/**
 * Return the UserSession of the given request if it is already set or null otherwise
 * @param hreq
 * @return
 */
public UserSession getUserSessionIfAlreadySet(HttpServletRequest hreq) {
    HttpSession session = hreq.getSession(false);
    if (session == null) {
        return null;
    }
    UserSession us = (UserSession) session.getAttribute(USERSESSIONKEY);
    setHttpSessionTimeout(session, us);
    return us;
}
Also used : HttpSession(javax.servlet.http.HttpSession) UserSession(org.olat.core.util.UserSession)

Example 47 with UserSession

use of org.olat.core.util.UserSession in project openolat by klemens.

the class RestApiLoginFilter method followToken.

private void followToken(String token, HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpSession session = request.getSession(true);
    session.setMaxInactiveInterval(TOKEN_BASED_SESSION_TIMEOUT);
    UserSession uress = CoreSpringFactory.getImpl(UserSessionManager.class).getUserSession(session);
    if (uress != null) {
        UserRequest ureq = null;
        try {
            // upon creation URL is checked for
            String requestURI = request.getRequestURI();
            ureq = new UserRequestImpl(requestURI, request, response);
        } catch (Exception e) {
            response.sendError(500);
            return;
        }
        request.setAttribute(RestSecurityHelper.SEC_USER_REQUEST, ureq);
        RestSecurityBean securityBean = (RestSecurityBean) CoreSpringFactory.getBean(RestSecurityBean.class);
        Identity identity = securityBean.getIdentity(token);
        int loginStatus = AuthHelper.doHeadlessLogin(identity, BaseSecurityModule.getDefaultAuthProviderIdentifier(), ureq, true);
        if (loginStatus == AuthHelper.LOGIN_OK) {
            String renewedToken = securityBean.renewToken(token);
            if (renewedToken != null) {
                response.setHeader(RestSecurityHelper.SEC_TOKEN, renewedToken);
                synchronized (uress) {
                    chain.doFilter(request, response);
                }
            } else
                response.sendError(401);
        } else
            response.sendError(401);
    } else
        response.sendError(401);
}
Also used : UserSessionManager(org.olat.core.util.session.UserSessionManager) HttpSession(javax.servlet.http.HttpSession) UserSession(org.olat.core.util.UserSession) Identity(org.olat.core.id.Identity) UserRequest(org.olat.core.gui.UserRequest) UserRequestImpl(org.olat.core.gui.UserRequestImpl) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 48 with UserSession

use of org.olat.core.util.UserSession in project openolat by klemens.

the class OpenOLATStatisticsWebService method getSessionsVO.

protected SessionsVO getSessionsVO() {
    SessionsVO vo = new SessionsVO();
    SessionStatsManager sessionStatsManager = CoreSpringFactory.getImpl(SessionStatsManager.class);
    UserSessionManager sessionManager = CoreSpringFactory.getImpl(UserSessionManager.class);
    vo.setCount(sessionManager.getUserSessionsCnt());
    Set<UserSession> userSessions = sessionManager.getAuthenticatedUserSessions();
    int webdavcount = 0;
    int secureWebdavCount = 0;
    int authenticatedcount = 0;
    int secureAuthenticatedCount = 0;
    int restCount = 0;
    int secureRestCount = 0;
    for (UserSession usess : userSessions) {
        SessionInfo sessInfo = usess.getSessionInfo();
        if (sessInfo.isWebDAV()) {
            webdavcount++;
            if (sessInfo.isSecure()) {
                secureWebdavCount++;
            }
        } else if (sessInfo.isREST()) {
            restCount++;
            if (sessInfo.isSecure()) {
                secureRestCount++;
            }
        } else {
            authenticatedcount++;
            if (sessInfo.isSecure()) {
                secureAuthenticatedCount++;
            }
        }
    }
    vo.setAuthenticatedCount(authenticatedcount);
    vo.setSecureAuthenticatedCount(secureAuthenticatedCount);
    vo.setWebdavCount(webdavcount);
    vo.setSecureWebdavCount(secureWebdavCount);
    vo.setRestCount(restCount);
    vo.setSecureRestCount(secureRestCount);
    // Instant messaging
    vo.setInstantMessagingCount(-1);
    SessionsStats statsLastMinute = sessionStatsManager.getSessionsStatsLast(60);
    SessionsStats statsLast5Minutes = sessionStatsManager.getSessionsStatsLast(300);
    vo.setAuthenticatedClickCountLastMinute(statsLastMinute.getAuthenticatedClickCalls());
    vo.setAuthenticatedClickCountLastFiveMinutes(statsLast5Minutes.getAuthenticatedPollerCalls());
    vo.setAuthenticatedPollCountLastMinute(statsLastMinute.getAuthenticatedPollerCalls());
    vo.setAuthenticatedPollCountLastFiveMinutes(statsLast5Minutes.getAuthenticatedPollerCalls());
    vo.setRequestLastMinute(statsLastMinute.getRequests());
    vo.setRequestLastFiveMinutes(statsLast5Minutes.getRequests());
    vo.setConcurrentDispatchThreads(sessionStatsManager.getConcurrentCounter());
    return vo;
}
Also used : UserSessionManager(org.olat.core.util.session.UserSessionManager) SessionsStats(org.olat.admin.sysinfo.model.SessionsStats) SessionsVO(org.olat.restapi.system.vo.SessionsVO) UserSession(org.olat.core.util.UserSession) SessionInfo(org.olat.core.util.SessionInfo) SessionStatsManager(org.olat.admin.sysinfo.manager.SessionStatsManager)

Example 49 with UserSession

use of org.olat.core.util.UserSession in project openolat by klemens.

the class StatusWebservice method getSystemSummaryVO.

/**
 * Return the statistics about runtime: uptime, classes loaded, memory
 * summary, threads count...
 *
 * @response.representation.200.qname {http://www.example.com}runtimeVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The version of the instance
 * @response.representation.200.example {@link org.olat.restapi.system.vo.Examples#SAMPLE_RUNTIMEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @param request The HTTP request
 * @return The informations about runtime, uptime, classes loaded, memory summary...
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getSystemSummaryVO() {
    StatusVO stats = new StatusVO();
    // File
    try {
        long startFile = System.nanoTime();
        File infoFile = setInfoFiles("ping");
        WorkThreadInformations.unset();
        stats.setWriteFileInMilliseconds(CodeHelper.nanoToMilliTime(startFile));
        stats.setWriteFile(infoFile.exists());
        infoFile.delete();
    } catch (Exception e) {
        stats.setWriteFile(false);
        stats.setWriteFileInMilliseconds(-1l);
        log.error("", e);
    }
    // Datebase
    try {
        stats.setWriteDb(true);
        PropertyManager propertyManager = CoreSpringFactory.getImpl(PropertyManager.class);
        List<Property> props = propertyManager.findProperties((Identity) null, (BusinessGroup) null, PING_RESOURCE, PING_REF, PING_REF);
        if (props != null && props.size() > 0) {
            for (Property prop : props) {
                propertyManager.deleteProperty(prop);
            }
            DBFactory.getInstance().commit();
        }
        long startDB = System.nanoTime();
        Property prop = propertyManager.createPropertyInstance(null, null, PING_RESOURCE, PING_REF, PING_REF, 0f, 0l, "-", "-");
        DBFactory.getInstance().commit();
        stats.setWriteDbInMilliseconds(CodeHelper.nanoToMilliTime(startDB));
        propertyManager.deleteProperty(prop);
        DBFactory.getInstance().commit();
    } catch (Exception e) {
        stats.setWriteDb(false);
        stats.setWriteDbInMilliseconds(-1l);
        log.error("", e);
    }
    // Secure authenticated user
    UserSessionManager sessionManager = CoreSpringFactory.getImpl(UserSessionManager.class);
    Set<UserSession> userSessions = sessionManager.getAuthenticatedUserSessions();
    int secureAuthenticatedCount = 0;
    for (UserSession usess : userSessions) {
        SessionInfo sessInfo = usess.getSessionInfo();
        if (sessInfo.isWebDAV() || sessInfo.isREST()) {
        // 
        } else if (sessInfo.isSecure()) {
            secureAuthenticatedCount++;
        }
    }
    stats.setSecureAuthenticatedCount(secureAuthenticatedCount);
    // Concurrent dispatch threads
    SessionStatsManager sessionStatsManager = CoreSpringFactory.getImpl(SessionStatsManager.class);
    stats.setConcurrentDispatchThreads(sessionStatsManager.getConcurrentCounter());
    return Response.ok(stats).build();
}
Also used : PropertyManager(org.olat.properties.PropertyManager) SessionInfo(org.olat.core.util.SessionInfo) SessionStatsManager(org.olat.admin.sysinfo.manager.SessionStatsManager) IOException(java.io.IOException) UserSessionManager(org.olat.core.util.session.UserSessionManager) UserSession(org.olat.core.util.UserSession) File(java.io.File) Property(org.olat.properties.Property) StatusVO(org.olat.restapi.system.vo.StatusVO) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 50 with UserSession

use of org.olat.core.util.UserSession in project openolat by klemens.

the class MapperServiceTest method testGetMapper_serializable.

@Test
public void testGetMapper_serializable() {
    // create a mapper
    int initialNumOfMappers = mapperService.inMemoryCount();
    UserSession session = createUserSession();
    PersistentMapper mapper = new PersistentMapper(UUID.randomUUID().toString());
    MapperKey mapperKey = mapperService.register(session, mapper);
    dbInstance.commitAndCloseSession();
    // retrieve the mapper
    PersistentMapper reloadedMapper = (PersistentMapper) mapperService.getMapperById(session, mapperKey.getMapperId());
    Assert.assertNotNull(reloadedMapper);
    Assert.assertEquals(mapper.getKey(), reloadedMapper.getKey());
    Assert.assertFalse(initialNumOfMappers == mapperService.inMemoryCount());
    // remove in memory mappers
    mapperService.cleanUp(session.getSessionInfo().getSession().getId());
    Assert.assertEquals(initialNumOfMappers, mapperService.inMemoryCount());
    // reloaded episode 2
    PersistentMapper reloadedMapper2 = (PersistentMapper) mapperService.getMapperById(null, mapperKey.getMapperId());
    Assert.assertNotNull(reloadedMapper2);
    Assert.assertEquals(mapper.getKey(), reloadedMapper2.getKey());
}
Also used : UserSession(org.olat.core.util.UserSession) MapperKey(org.olat.core.dispatcher.mapper.manager.MapperKey) Test(org.junit.Test)

Aggregations

UserSession (org.olat.core.util.UserSession)146 UserSessionManager (org.olat.core.util.session.UserSessionManager)26 Identity (org.olat.core.id.Identity)22 Roles (org.olat.core.id.Roles)20 SessionInfo (org.olat.core.util.SessionInfo)20 HttpSession (javax.servlet.http.HttpSession)18 UserRequest (org.olat.core.gui.UserRequest)18 Test (org.junit.Test)16 MapperKey (org.olat.core.dispatcher.mapper.manager.MapperKey)16 UserRequestImpl (org.olat.core.gui.UserRequestImpl)16 ContextEntry (org.olat.core.id.context.ContextEntry)14 IOException (java.io.IOException)12 AssertException (org.olat.core.logging.AssertException)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 Window (org.olat.core.gui.components.Window)10 UnknownHostException (java.net.UnknownHostException)8 ArrayList (java.util.ArrayList)8 ChiefController (org.olat.core.gui.control.ChiefController)8 Preferences (org.olat.core.util.prefs.Preferences)8 InetAddress (java.net.InetAddress)6