Search in sources :

Example 1 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder in project OpenAM by OpenRock.

the class CoreTokenAdapterTest method shouldAddTokenIDAsReturnFieldForDeleteOnQuery.

@Test
public void shouldAddTokenIDAsReturnFieldForDeleteOnQuery() throws CoreTokenException {
    // Given
    TokenFilter filter = new TokenFilterBuilder().build();
    // When
    adapter.deleteOnQuery(filter);
    // Then
    ArgumentCaptor<TokenFilter> captor = ArgumentCaptor.forClass(TokenFilter.class);
    verify(mockTaskDispatcher).partialQuery(captor.capture(), any(ResultHandler.class));
    TokenFilter capturedFilter = captor.getValue();
    assertThat(capturedFilter).isSameAs(filter);
    assertThat(capturedFilter.getReturnFields()).containsOnly(CoreTokenField.TOKEN_ID);
}
Also used : TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) ResultHandler(org.forgerock.openam.sm.datalayer.api.ResultHandler) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 2 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder in project OpenAM by OpenRock.

the class CTSPersistentStoreImpl method delete.

@Override
public int delete(Map<CoreTokenField, Object> query) throws DeleteFailedException {
    TokenFilterBuilder.FilterAttributeBuilder builder = new TokenFilterBuilder().returnAttribute(CoreTokenField.TOKEN_ID).and();
    for (Map.Entry<CoreTokenField, Object> entry : query.entrySet()) {
        CoreTokenField key = entry.getKey();
        Object value = entry.getValue();
        builder = builder.withAttribute(key, value);
    }
    TokenFilter filter = builder.build();
    Collection<String> failures = new ArrayList<String>();
    try {
        Collection<PartialToken> partialTokens = attributeQuery(filter);
        debug("Found {0} partial Tokens for deletion", Integer.toString(partialTokens.size()));
        for (PartialToken token : partialTokens) {
            String tokenId = token.getValue(CoreTokenField.TOKEN_ID);
            try {
                delete(tokenId);
            } catch (CoreTokenException e) {
                failures.add(tokenId);
            }
        }
        if (!failures.isEmpty()) {
            error("Failed to delete {0} tokens.\n{1}", Integer.toString(failures.size()), StringUtils.join(failures, ","));
        }
        return partialTokens.size() - failures.size();
    } catch (CoreTokenException e) {
        throw new DeleteFailedException("Failed to delete Tokens", e);
    }
}
Also used : PartialToken(org.forgerock.openam.sm.datalayer.api.query.PartialToken) ArrayList(java.util.ArrayList) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) DeleteFailedException(org.forgerock.openam.cts.exceptions.DeleteFailedException) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) Map(java.util.Map) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter)

Example 3 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder in project OpenAM by OpenRock.

the class SAML2CTSPersistentStore method retrieveSAML2TokensWithSecondaryKey.

/**
     *{@inheritDoc}
     */
@Override
public List<Object> retrieveSAML2TokensWithSecondaryKey(String secondaryKey) throws SAML2TokenRepositoryException {
    secondaryKey = tokenIdFactory.toSAMLSecondaryTokenId(secondaryKey);
    try {
        TokenFilter filter = new TokenFilterBuilder().withAttribute(SAMLTokenField.SECONDARY_KEY.getField(), secondaryKey).build();
        Collection<Token> tokens = persistentStore.query(filter);
        List<Object> results = new ArrayList<Object>(tokens.size());
        for (Token token : tokens) {
            SAMLToken samlToken = tokenAdapter.fromToken(token);
            results.add(samlToken.getToken());
        }
        return results;
    } catch (CoreTokenException e) {
        debug.error("SAML2CTSPersistentStore.retrieveSAML2TokensWithSecondaryKey(): failed to retrieve SAML2 " + "tokens using secondary key:" + secondaryKey, e);
        throw new SAML2TokenRepositoryException(e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) SAMLToken(org.forgerock.openam.cts.api.tokens.SAMLToken) Token(org.forgerock.openam.cts.api.tokens.Token) SAML2TokenRepositoryException(org.forgerock.openam.federation.saml2.SAML2TokenRepositoryException) SAMLToken(org.forgerock.openam.cts.api.tokens.SAMLToken) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter)

Example 4 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder 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)

Example 5 with TokenFilterBuilder

use of org.forgerock.openam.cts.api.filter.TokenFilterBuilder in project OpenAM by OpenRock.

the class JavaBeanAdapter method toTokenQuery.

/**
     * Use the bean mappings that have been parsed to turn a query keyed by bean property names into a query keyed by
     * token property names.
     * @param filter The query keyed by bean property names.
     * @return The transformed query keyed by token field names.
     */
public TokenFilter toTokenQuery(QueryFilter<String> filter) {
    TokenFilterBuilder builder = new TokenFilterBuilder();
    List<QueryFilter<CoreTokenField>> tokenFilter = new ArrayList<QueryFilter<CoreTokenField>>();
    tokenFilter.add(filter.accept(TOKEN_QUERY_TRANSLATOR, null));
    tokenFilter.add(QueryFilter.equalTo(CoreTokenField.TOKEN_TYPE, tokenType));
    return builder.withQuery(QueryFilter.and(tokenFilter)).build();
}
Also used : QueryFilter(org.forgerock.util.query.QueryFilter) ArrayList(java.util.ArrayList) TokenFilterBuilder(org.forgerock.openam.cts.api.filter.TokenFilterBuilder) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField)

Aggregations

TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)11 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)10 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)7 ArrayList (java.util.ArrayList)6 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)5 Test (org.testng.annotations.Test)5 CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)4 Token (org.forgerock.openam.cts.api.tokens.Token)3 QueryFilter (org.forgerock.util.query.QueryFilter)3 Calendar (java.util.Calendar)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 QueryBuilder (org.forgerock.openam.sm.datalayer.api.query.QueryBuilder)2 Connection (org.forgerock.opendj.ldap.Connection)2 Filter (org.forgerock.opendj.ldap.Filter)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 SessionException (com.iplanet.dpro.session.SessionException)1 SSOException (com.iplanet.sso.SSOException)1 IdRepoException (com.sun.identity.idm.IdRepoException)1 Map (java.util.Map)1