Search in sources :

Example 16 with IPortletEntity

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;
}
Also used : IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) CacheControl(javax.portlet.CacheControl) PortletDefinition(org.apache.pluto.container.om.portlet.PortletDefinition)

Example 17 with IPortletEntity

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;
}
Also used : WindowState(javax.portlet.WindowState) IPortletPreference(org.apereo.portal.portlet.om.IPortletPreference) IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) Map(java.util.Map) ConcurrentMap(java.util.concurrent.ConcurrentMap) PortletPreferenceImpl(org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl)

Example 18 with IPortletEntity

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;
}
Also used : Function(com.google.common.base.Function) IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId)

Example 19 with IPortletEntity

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);
            }
        }
    }
}
Also used : IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) HibernateOptimisticLockingFailureException(org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId)

Example 20 with IPortletEntity

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;
}
Also used : IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition)

Aggregations

IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)63 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)32 IPortletEntityId (org.apereo.portal.portlet.om.IPortletEntityId)25 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)24 IPortletDefinitionId (org.apereo.portal.portlet.om.IPortletDefinitionId)19 IUserInstance (org.apereo.portal.user.IUserInstance)12 Test (org.junit.Test)11 IPortletPreference (org.apereo.portal.portlet.om.IPortletPreference)10 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)10 List (java.util.List)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 PortletPreferenceImpl (org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl)9 IPortletWindowId (org.apereo.portal.portlet.om.IPortletWindowId)9 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)9 IPerson (org.apereo.portal.security.IPerson)7 PortletApplicationDefinition (org.apache.pluto.container.om.portlet.PortletApplicationDefinition)5 IUserPreferencesManager (org.apereo.portal.IUserPreferencesManager)5 IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)5 ArrayList (java.util.ArrayList)4 Callable (java.util.concurrent.Callable)4