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);
}
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);
}
}
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);
}
}
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;
}
}
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();
}
Aggregations