Search in sources :

Example 1 with CTSPersistentStore

use of org.forgerock.openam.cts.CTSPersistentStore in project OpenAM by OpenRock.

the class SessionCount method getSessionsFromRepository.

private static Map<String, Long> getSessionsFromRepository(String uuid) throws Exception {
    CTSPersistentStore repo = sessionService.getRepository();
    try {
        // Filter and Query the CTS
        TokenFilter filter = new TokenFilterBuilder().returnAttribute(SessionTokenField.SESSION_ID.getField()).returnAttribute(CoreTokenField.EXPIRY_DATE).and().withAttribute(CoreTokenField.USER_ID, uuid).build();
        Collection<PartialToken> partialTokens = repo.attributeQuery(filter);
        if (debug.messageEnabled()) {
            debug.message(MessageFormat.format("getSessionsFromRepository query success:\n" + "Query: {0}\n" + "Count: {1}", filter, partialTokens.size()));
        }
        // Populate the return Map from the query results.
        Map<String, Long> sessions = new HashMap<String, Long>();
        for (PartialToken partialToken : partialTokens) {
            // Session ID
            String sessionId = partialToken.getValue(SessionTokenField.SESSION_ID.getField());
            // Expiration Date converted to Unix Time
            Calendar timestamp = partialToken.getValue(CoreTokenField.EXPIRY_DATE);
            long unixTime = TimeUtils.toUnixTime(timestamp);
            sessions.put(sessionId, unixTime);
        }
        if (debug.messageEnabled()) {
            debug.message(MessageFormat.format("getSessionsFromRepository query results:\n" + "{0}", sessions));
        }
        return sessions;
    } catch (Exception e) {
        debug.error("SessionCount.getSessionsFromRepository: " + "Session repository is not available", e);
        throw e;
    }
}
Also used : CTSPersistentStore(org.forgerock.openam.cts.CTSPersistentStore) PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) HashMap(java.util.HashMap) Calendar(java.util.Calendar) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) SessionException(com.iplanet.dpro.session.SessionException) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter)

Aggregations

SessionException (com.iplanet.dpro.session.SessionException)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 CTSPersistentStore (org.forgerock.openam.cts.CTSPersistentStore)1 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)1 TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)1 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)1