com.hp.autonomy.hod.caching.AbstractHodCacheResolver Maven / Gradle / Ivy
Show all versions of hod-spring-caching Show documentation
/*
* Copyright 2014-2015 Hewlett-Packard Development Company, L.P.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
*/
package com.hp.autonomy.hod.caching;
import com.hp.autonomy.hod.sso.HodAuthentication;
import com.hp.autonomy.hod.sso.HodAuthenticationPrincipal;
import org.springframework.cache.interceptor.AbstractCacheResolver;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
/**
* CacheResolver that prefixes cache names with the HP Haven OnDemand domain and the HP Haven OnDemand application in
* the current security context. This allows caching to be used across multiple domains without interference.
*
* The domain and application will be joined with colons, so cache name must not contain the ":" character if
* {@link HodCacheNameResolver#getOriginalName(String)} is to be used
*/
abstract class AbstractHodCacheResolver extends AbstractCacheResolver {
@Override
protected Collection getCacheNames(final CacheOperationInvocationContext> context) {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof HodAuthentication)) {
throw new IllegalStateException("There is no HOD authentication token in the security context holder");
}
final HodAuthenticationPrincipal principal = ((HodAuthentication) authentication).getPrincipal();
final Set contextCacheNames = context.getOperation().getCacheNames();
final Collection resolvedCacheNames = contextCacheNames.stream().map(cacheName -> resolveName(cacheName, principal)).collect(Collectors.toCollection(HashSet::new));
return resolvedCacheNames;
}
/**
* Resolve the cache for the given name, qualified by the HOD application.
*
* @param cacheName The original cache name
* @param principal HoD authentication principal retrieved from context
* @return The cache name qualified with the application
*/
protected abstract String resolveName(final String cacheName, final HodAuthenticationPrincipal principal);
}