use of org.apereo.portal.utils.cache.CacheKey in project uPortal by Jasig.
the class CachingPipelineComponent method getEventReader.
/* (non-Javadoc)
* @see org.apereo.portal.rendering.PipelineComponent#getEventReader(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@SuppressWarnings("unchecked")
@Override
public final PipelineEventReader<R, E> getEventReader(HttpServletRequest request, HttpServletResponse response) {
if (Included.PLAIN == this.resourcesElementsProvider.getDefaultIncludedType()) {
this.logger.trace("{} - Resoure Aggregation Disabled, ignoring event cache and returning parent event reader directly", this.beanName);
return this.wrappedComponent.getEventReader(request, response);
}
// Get the key for this request from the target component and see if there is a cache entry
final CacheKey cacheKey = this.wrappedComponent.getCacheKey(request, response);
Element element = this.cache.get(cacheKey);
CachedEventReader<E> cachedEventReader = null;
if (element != null) {
cachedEventReader = (CachedEventReader<E>) element.getObjectValue();
}
// If there was a cached reader return it immediately
if (cachedEventReader == null) {
// No cached data for key, call target component to get events and an updated cache key
logger.debug("{} - No cached events found for key {}, calling parent", this.beanName, cacheKey);
final PipelineEventReader<R, E> pipelineEventReader = this.wrappedComponent.getEventReader(request, response);
// Copy the events from the reader into a buffer to be cached
final List<E> eventCache = new LinkedList<E>();
for (final E event : pipelineEventReader) {
// TODO add de-duplication logic here
eventCache.add(event);
}
final Map<String, String> outputProperties = pipelineEventReader.getOutputProperties();
cachedEventReader = new CachedEventReader<E>(eventCache, new LinkedHashMap<String, String>(outputProperties));
// Cache the buffer
element = new Element(cacheKey, cachedEventReader);
this.cache.put(element);
logger.debug("{} - Cached {} events for key {}", this.beanName, eventCache.size(), cacheKey);
} else {
logger.debug("{} - Found cached events for key {}", this.beanName, cacheKey);
}
final List<E> eventCache = cachedEventReader.getEventCache();
final Map<String, String> outputProperties = cachedEventReader.getOutputProperties();
final R eventReader = this.createEventReader(eventCache.listIterator());
return new PipelineEventReaderImpl<R, E>(eventReader, outputProperties);
}
use of org.apereo.portal.utils.cache.CacheKey in project uPortal by Jasig.
the class AuthorizationImpl method doesPrincipalHavePermission.
/**
* Answers if the owner has given the principal permission to perform the activity on the
* target, as evaluated by the policy. Params <code>policy</code>, <code>owner</code> and <code>
* activity</code> must be non-null.
*
* @return boolean
* @param principal IAuthorizationPrincipal
* @param owner java.lang.String
* @param activity java.lang.String
* @param target java.lang.String
* @exception AuthorizationException indicates authorization information could not be retrieved.
*/
@Override
@RequestCache
public boolean doesPrincipalHavePermission(IAuthorizationPrincipal principal, String owner, String activity, String target, IPermissionPolicy policy) throws AuthorizationException {
final CacheKeyBuilder<Serializable, Serializable> cacheKeyBuilder = CacheKey.builder(AuthorizationImpl.class.getName());
final String username = principal.getKey();
if (IPerson.class.equals(principal.getType())) {
cacheKeyBuilder.addTag(UsernameTaggedCacheEntryPurger.createCacheEntryTag(username));
}
cacheKeyBuilder.addAll(policy.getClass(), username, principal.getType(), owner, activity, target);
final CacheKey key = cacheKeyBuilder.build();
final Element element = this.doesPrincipalHavePermissionCache.get(key);
if (element != null) {
return (Boolean) element.getValue();
}
// fail closed
boolean rslt = false;
/*
* Convert to (strongly-typed) Java objects based on interfaces in
* o.j.p.permission before we make the actual check with IPermissionPolicy;
* parameters that communicate something of the nature of the things they
* represent helps us make the check(s) more intelligently. This objects
* were retro-fitted to IPermissionPolicy in uP 4.3; perhaps we should do
* the same to IAuthorizationService itself?
*/
final IPermissionOwner ipOwner = permissionOwnerDao.getPermissionOwner(owner);
final IPermissionActivity ipActivity = permissionOwnerDao.getPermissionActivity(owner, activity);
if (ipActivity != null) {
final IPermissionTargetProvider targetProvider = targetProviderRegistry.getTargetProvider(ipActivity.getTargetProviderKey());
final IPermissionTarget ipTarget = targetProvider.getTarget(target);
rslt = policy.doesPrincipalHavePermission(this, principal, ipOwner, ipActivity, ipTarget);
} else {
/*
* This circumstance means that a piece of the fundamental Permissions data expected by
* the code is missing in the database. It normally happens when a newer version of the
* uPortal code is run against an existing database, and a required data update was
* overlooked. This condition is not great, but probably not catastrophic; it means
* that no one will (or can) have the new permission. This method returns false.
*
* Administrators, however, have permission to do anything, including this unknown
* activity. It's most common in uPortal for only Administrators to have access to
* exotic activities, so in most cases this omission is a wash.
*
* We need to log a WARNing, but this method is invoked a lot, and we don't want to do
* it incessantly.
*/
final Long now = System.currentTimeMillis();
final String missingDataTrackerKey = owner + ":" + activity;
final Long lastLogMessageTime = missingDataLogTracker.get(missingDataTrackerKey);
if (lastLogMessageTime == null || lastLogMessageTime < now - MISSING_DATA_LOG_PERIOD_MILLIS) {
logger.warn("Activity '{}' is not defined for owner '{}'; only admins will be " + "able to access this function; this warning usually means that expected data " + "was not imported", activity, owner);
missingDataLogTracker.put(missingDataTrackerKey, now);
}
// This pass becomes a check for superuser (Portal Administrators)
rslt = doesPrincipalHavePermission(principal, IPermission.PORTAL_SYSTEM, IPermission.ALL_PERMISSIONS_ACTIVITY, IPermission.ALL_TARGET, policy);
}
this.doesPrincipalHavePermissionCache.put(new Element(key, rslt));
return rslt;
}
use of org.apereo.portal.utils.cache.CacheKey in project uPortal by Jasig.
the class RequestCacheAspect method cacheRequest.
@Around("anyPublicMethod() && @annotation(requestCache)")
public Object cacheRequest(ProceedingJoinPoint pjp, RequestCache requestCache) throws Throwable {
final long start = System.nanoTime();
final CacheKey cacheKey = createCacheKey(pjp, requestCache);
final HttpServletRequest currentPortalRequest;
try {
currentPortalRequest = this.portalRequestUtils.getCurrentPortalRequest();
} catch (IllegalStateException e) {
logger.trace("No current portal request, will not cache result of: {}", cacheKey);
// No current request, simply proceed
return pjp.proceed();
}
final CacheStatistics cacheStatistics = this.getCacheStatistics(pjp, requestCache);
// Check in the cache for a result
final ConcurrentMap<CacheKey, Object> cache = PortalWebUtils.getMapRequestAttribute(currentPortalRequest, CACHE_MAP);
Object result = cache.get(cacheKey);
// Return null if placeholder was cached
if (requestCache.cacheNull() && result == NULL_PLACEHOLDER) {
final long time = System.nanoTime() - start;
cacheStatistics.recordHit(time);
overallStats.recordHit(time);
logger.debug("Found cached null for invocation of: {}", cacheKey);
return null;
}
// Rethrow if exception was cached
if (requestCache.cacheException() && result instanceof ExceptionHolder) {
final long time = System.nanoTime() - start;
cacheStatistics.recordHit(time);
overallStats.recordHit(time);
logger.debug("Found cached exception for invocation of: {}", cacheKey);
throw ((ExceptionHolder) result).getThrowable();
}
// Return cached result
if (result != null) {
final long time = System.nanoTime() - start;
cacheStatistics.recordHit(time);
overallStats.recordHit(time);
logger.debug("Found cached result for invocation of: {}", cacheKey);
return result;
}
try {
// Execute the annotated method
result = pjp.proceed();
final long time = System.nanoTime() - start;
cacheStatistics.recordMissAndLoad(time);
overallStats.recordMissAndLoad(time);
if (result != null) {
// Cache the not-null result
cache.put(cacheKey, result);
logger.debug("Cached result for invocation of: {}", cacheKey);
} else if (requestCache.cacheNull()) {
// If caching nulls cache the placeholder
cache.put(cacheKey, NULL_PLACEHOLDER);
logger.debug("Cached null for invocation of: {}", cacheKey);
}
return result;
} catch (Throwable t) {
final long time = System.nanoTime() - start;
cacheStatistics.recordMissAndException(time);
overallStats.recordMissAndException(time);
if (requestCache.cacheException()) {
// If caching exceptions wrapp the exception and cache it
cache.put(cacheKey, new ExceptionHolder(t));
logger.debug("Cached exception for invocation of: {}", cacheKey);
}
throw t;
}
}
Aggregations