use of org.apereo.portal.portlet.om.IPortletEntity in project uPortal by Jasig.
the class PortletCacheControlServiceImpl method getPortletState.
private <D extends CachedPortletResultHolder<T>, T extends Serializable> CacheState<D, T> getPortletState(HttpServletRequest request, IPortletWindow portletWindow, PublicPortletCacheKey publicCacheKey, Ehcache publicOutputCache, Ehcache privateOutputCache, boolean useHttpHeaders) {
//See if there is any cached data for the portlet header request
final CacheState<D, T> cacheState = this.<D, T>getPortletCacheState(request, portletWindow, publicCacheKey, publicOutputCache, privateOutputCache);
String etagHeader = null;
final D cachedPortletData = cacheState.getCachedPortletData();
if (cachedPortletData != null) {
if (useHttpHeaders) {
//Browser headers being used, check ETag and Last Modified
etagHeader = request.getHeader(IF_NONE_MATCH);
if (etagHeader != null && etagHeader.equals(cachedPortletData.getEtag())) {
//ETag is valid, mark the browser data as matching
cacheState.setBrowserDataMatches(true);
} else {
long ifModifiedSince = request.getDateHeader(IF_MODIFIED_SINCE);
if (ifModifiedSince >= 0 && cachedPortletData.getTimeStored() <= ifModifiedSince) {
//Cached content hasn't been modified since header date, mark the browser data as matching
cacheState.setBrowserDataMatches(true);
}
}
}
final long expirationTime = cachedPortletData.getExpirationTime();
if (expirationTime == -1 || expirationTime > System.currentTimeMillis()) {
//Cached data exists, see if it can be used with no additional work
//Cached data is not expired, check if browser data should be used
cacheState.setUseCachedData(true);
//Copy browser-data-matching flag to the user-browser-data flag
cacheState.setUseBrowserData(cacheState.isBrowserDataMatches());
//No browser side data to be used, return the cached data for replay
return cacheState;
}
}
//Build CacheControl structure
final CacheControl cacheControl = cacheState.getCacheControl();
//Get the portlet descriptor
final IPortletEntity entity = portletWindow.getPortletEntity();
final IPortletDefinitionId definitionId = entity.getPortletDefinitionId();
final PortletDefinition portletDescriptor = this.portletDefinitionRegistry.getParentPortletDescriptor(definitionId);
//Set the default scope
final String cacheScopeValue = portletDescriptor.getCacheScope();
if (MimeResponse.PUBLIC_SCOPE.equalsIgnoreCase(cacheScopeValue)) {
cacheControl.setPublicScope(true);
}
//Set the default expiration time
cacheControl.setExpirationTime(portletDescriptor.getExpirationCache());
// Use the request etag if it exists (implies useHttpHeaders==true)
if (etagHeader != null) {
cacheControl.setETag(etagHeader);
cacheState.setBrowserSetEtag(true);
} else // No browser-set etag, use the cached etag value if there is cached data
if (cachedPortletData != null) {
logger.debug("setting cacheControl.eTag from cached data to {}", cachedPortletData.getEtag());
cacheControl.setETag(cachedPortletData.getEtag());
}
return cacheState;
}
use of org.apereo.portal.portlet.om.IPortletEntity in project uPortal by Jasig.
the class PortletEntityRegistryImpl method createPersistentEntity.
protected IPortletEntity createPersistentEntity(final IPortletEntity portletEntity, final IPortletEntityId wrapperPortletEntityId) {
final IPortletDefinitionId portletDefinitionId = portletEntity.getPortletDefinitionId();
final String layoutNodeId = portletEntity.getLayoutNodeId();
final int userId = portletEntity.getUserId();
IPortletEntity persistentEntity = this.portletEntityDao.getPortletEntity(layoutNodeId, userId);
if (persistentEntity != null) {
this.logger.warn("A persistent portlet entity already exists: " + persistentEntity + ". The data from the passed in entity will be copied to the persistent entity: " + portletEntity);
} else {
persistentEntity = this.portletEntityDao.createPortletEntity(portletDefinitionId, layoutNodeId, userId);
}
//Copy over preferences to avoid modifying any part of the interim entity by reference
final List<IPortletPreference> existingPreferences = portletEntity.getPortletPreferences();
final List<IPortletPreference> persistentPreferences = persistentEntity.getPortletPreferences();
//Only do the copy if the List objects are not the same instance
if (persistentPreferences != existingPreferences) {
persistentPreferences.clear();
for (final IPortletPreference preference : existingPreferences) {
persistentPreferences.add(new PortletPreferenceImpl(preference));
}
}
//Copy over WindowStates
final Map<Long, WindowState> windowStates = portletEntity.getWindowStates();
for (Map.Entry<Long, WindowState> windowStateEntry : windowStates.entrySet()) {
final Long stylesheetDescriptorId = windowStateEntry.getKey();
final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.getStylesheetDescriptor(stylesheetDescriptorId);
final WindowState windowState = windowStateEntry.getValue();
persistentEntity.setWindowState(stylesheetDescriptor, windowState);
}
this.portletEntityDao.updatePortletEntity(persistentEntity);
return persistentEntity;
}
use of org.apereo.portal.portlet.om.IPortletEntity in project uPortal by Jasig.
the class PortletEntityRegistryImpl method getPortletEntity.
/** Lookup the portlet entity by layoutNodeId and userId */
protected IPortletEntity getPortletEntity(HttpServletRequest request, PortletEntityCache<IPortletEntity> portletEntityCache, IPortletEntityId portletEntityId, String layoutNodeId, int userId) {
IPortletEntity portletEntity;
//First look in the request map
if (portletEntityId != null) {
portletEntity = portletEntityCache.getEntity(portletEntityId);
} else {
portletEntity = portletEntityCache.getEntity(layoutNodeId, userId);
}
if (portletEntity != null) {
logger.trace("Found IPortletEntity {} in request cache", portletEntity.getPortletEntityId());
return portletEntity;
}
//Didn't find it, next look in the session map
final PortletEntityCache<PortletEntityData> portletEntityDataMap = this.getPortletEntityDataMap(request);
final PortletEntityData portletEntityData;
if (portletEntityId != null) {
portletEntityData = portletEntityDataMap.getEntity(portletEntityId);
} else {
portletEntityData = portletEntityDataMap.getEntity(layoutNodeId, userId);
}
if (portletEntityData != null) {
//Stick the entity wrapper in the request map (if it wasn't already added by another thread)
portletEntity = portletEntityCache.storeIfAbsentEntity(portletEntityData.getPortletEntityId(), new Function<IPortletEntityId, IPortletEntity>() {
@Override
public IPortletEntity apply(IPortletEntityId input) {
//Found a session stored entity, wrap it to make it a real IPortletEntity
logger.trace("Found PortletEntityData {} in session cache, caching wrapper in the request", portletEntityData.getPortletEntityId());
return wrapPortletEntityData(portletEntityData);
}
});
return portletEntity;
}
//Still didn't find it, look in the persistent store
if (portletEntityId != null) {
if (portletEntityId instanceof PortletEntityIdImpl) {
final PortletEntityIdImpl consistentPortletEntityId = (PortletEntityIdImpl) portletEntityId;
final String localLayoutNodeId = consistentPortletEntityId.getLayoutNodeId();
final int localUserId = consistentPortletEntityId.getUserId();
portletEntity = this.portletEntityDao.getPortletEntity(localLayoutNodeId, localUserId);
} else {
portletEntity = this.portletEntityDao.getPortletEntity(portletEntityId);
}
} else {
portletEntity = this.portletEntityDao.getPortletEntity(layoutNodeId, userId);
}
//Found a persistent entity, wrap it to make the id consistent between the persistent and session stored entities
if (portletEntity != null) {
final IPortletEntityId consistentPortletEntityId = this.createConsistentPortletEntityId(portletEntity);
final IPortletEntity anonPortletEntity = portletEntity;
//Stick the entity wrapper in the request map (if it wasn't already added by another thread)
portletEntity = portletEntityCache.storeIfAbsentEntity(consistentPortletEntityId, new Function<IPortletEntityId, IPortletEntity>() {
@Override
public IPortletEntity apply(IPortletEntityId input) {
logger.trace("Found persistent IPortletEntity {}, mapped id to {}, caching the wrapper in the request", anonPortletEntity.getPortletEntityId(), consistentPortletEntityId);
return new PersistentPortletEntityWrapper(anonPortletEntity, consistentPortletEntityId);
}
});
return portletEntity;
}
//Didn't find an entity, just return null
return null;
}
use of org.apereo.portal.portlet.om.IPortletEntity in project uPortal by Jasig.
the class PortletEntityRegistryImpl method deletePortletEntity.
/** Delete a portlet entity, removes it from the request, session and persistent stores */
protected void deletePortletEntity(HttpServletRequest request, IPortletEntity portletEntity, boolean cacheOnly) {
final IPortletEntityId portletEntityId = portletEntity.getPortletEntityId();
//Remove from request cache
final PortletEntityCache<IPortletEntity> portletEntityMap = this.getPortletEntityMap(request);
portletEntityMap.removeEntity(portletEntityId);
//Remove from session cache
final PortletEntityCache<PortletEntityData> portletEntityDataMap = this.getPortletEntityDataMap(request);
portletEntityDataMap.removeEntity(portletEntityId);
if (!cacheOnly && portletEntity instanceof PersistentPortletEntityWrapper) {
final IPortletEntity persistentEntity = ((PersistentPortletEntityWrapper) portletEntity).getPersistentEntity();
try {
this.portletEntityDao.deletePortletEntity(persistentEntity);
} catch (HibernateOptimisticLockingFailureException e) {
this.logger.warn("This persistent portlet has already been deleted: " + persistentEntity + ", trying to find and delete by layout node and user.");
final IPortletEntity existingPersistentEntity = this.portletEntityDao.getPortletEntity(persistentEntity.getLayoutNodeId(), persistentEntity.getUserId());
if (existingPersistentEntity != null) {
this.portletEntityDao.deletePortletEntity(existingPersistentEntity);
}
}
}
}
use of org.apereo.portal.portlet.om.IPortletEntity in project uPortal by Jasig.
the class PortletEntityRegistryImpl method getOrCreatePortletEntity.
/* (non-Javadoc)
* @see org.apereo.portal.portlet.registry.IPortletEntityRegistry#getOrCreatePortletEntity(org.apereo.portal.portlet.om.IPortletDefinitionId, java.lang.String, int)
*/
@Override
public IPortletEntity getOrCreatePortletEntity(HttpServletRequest request, IPortletDefinitionId portletDefinitionId, String layoutNodeId, int userId) {
final PortletEntityCache<IPortletEntity> portletEntityCache = getPortletEntityMap(request);
//Try just getting an existing entity first
IPortletEntity portletEntity = this.getPortletEntity(request, portletEntityCache, null, layoutNodeId, userId);
//Found an existing entity!
if (portletEntity != null) {
//Verify the definition IDs match, this is a MUST in the case where the subscribed portlet changes
final IPortletDefinition portletDefinition = portletEntity.getPortletDefinition();
if (portletDefinitionId.equals(portletDefinition.getPortletDefinitionId())) {
return portletEntity;
}
//Remove the entity if the definition IDs don't match
this.logger.warn("Found portlet entity '{}' is not the correct entity for portlet definition id: {}. The entity will be deleted and a new one created.", portletEntity, portletDefinitionId);
this.deletePortletEntity(request, portletEntity, false);
}
//Create the entity data object and store it in the session map (if not already there)
final PortletEntityCache<PortletEntityData> portletEntityDataMap = this.getPortletEntityDataMap(request);
final IPortletEntityId portletEntityId = this.createConsistentPortletEntityId(portletDefinitionId, layoutNodeId, userId);
PortletEntityData portletEntityData = new PortletEntityData(portletEntityId, portletDefinitionId, layoutNodeId, userId);
portletEntityData = portletEntityDataMap.storeIfAbsentEntity(portletEntityData);
portletEntity = wrapPortletEntityData(portletEntityData);
//Stick the wrapper in the request map
portletEntity = portletEntityCache.storeIfAbsentEntity(portletEntity);
return portletEntity;
}
Aggregations