Search in sources :

Example 21 with IPortletEntityId

use of org.apereo.portal.portlet.om.IPortletEntityId in project uPortal by Jasig.

the class GuestPortletEntityPreferencesImpl method storeSessionPreferences.

@SuppressWarnings("unchecked")
protected void storeSessionPreferences(IPortletEntityId portletEntityId, HttpServletRequest httpServletRequest, Map<String, IPortletPreference> preferences) {
    final HttpSession session = httpServletRequest.getSession();
    Map<IPortletEntityId, Map<String, IPortletPreference>> portletPreferences;
    // Sync on the session to ensure other threads aren't creating the Map at the same time
    synchronized (session) {
        portletPreferences = (Map<IPortletEntityId, Map<String, IPortletPreference>>) session.getAttribute(PORTLET_PREFERENCES_MAP_ATTRIBUTE);
        if (portletPreferences == null) {
            portletPreferences = new ConcurrentHashMap<IPortletEntityId, Map<String, IPortletPreference>>();
            session.setAttribute(PORTLET_PREFERENCES_MAP_ATTRIBUTE, portletPreferences);
        }
    }
    portletPreferences.put(portletEntityId, preferences);
}
Also used : HttpSession(javax.servlet.http.HttpSession) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId)

Example 22 with IPortletEntityId

use of org.apereo.portal.portlet.om.IPortletEntityId in project uPortal by Jasig.

the class PortletEventCoordinatationService method resolvePortletEvents.

@Override
public void resolvePortletEvents(HttpServletRequest request, PortletEventQueue portletEventQueue) {
    final Queue<QueuedEvent> events = portletEventQueue.getUnresolvedEvents();
    // Skip all processing if there are no new events.
    if (events.isEmpty()) {
        return;
    }
    // Get all the portlets the user is subscribed to
    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final IUserPreferencesManager preferencesManager = userInstance.getPreferencesManager();
    final IUserLayoutManager userLayoutManager = preferencesManager.getUserLayoutManager();
    // Make a local copy so we can remove data from it
    final Set<String> allLayoutNodeIds = new LinkedHashSet<String>(userLayoutManager.getAllSubscribedChannels());
    final Map<String, IPortletEntity> portletEntityCache = new LinkedHashMap<String, IPortletEntity>();
    while (!events.isEmpty()) {
        final QueuedEvent queuedEvent = events.poll();
        if (queuedEvent == null) {
            // no more queued events, done resolving
            return;
        }
        final IPortletWindowId sourceWindowId = queuedEvent.getPortletWindowId();
        final Event event = queuedEvent.getEvent();
        final boolean globalEvent = isGlobalEvent(request, sourceWindowId, event);
        final Set<IPortletDefinition> portletDefinitions = new LinkedHashSet<IPortletDefinition>();
        if (globalEvent) {
            portletDefinitions.addAll(this.portletDefinitionRegistry.getAllPortletDefinitions());
        }
        // Check each subscription to see what events it is registered to see
        for (final Iterator<String> layoutNodeIdItr = allLayoutNodeIds.iterator(); layoutNodeIdItr.hasNext(); ) {
            final String layoutNodeId = layoutNodeIdItr.next();
            IPortletEntity portletEntity = portletEntityCache.get(layoutNodeId);
            if (portletEntity == null) {
                portletEntity = this.portletEntityRegistry.getOrCreatePortletEntity(request, userInstance, layoutNodeId);
                // remove it (see UP-3378)
                if (portletEntity == null) {
                    layoutNodeIdItr.remove();
                    continue;
                }
                final IPortletDefinitionId portletDefinitionId = portletEntity.getPortletDefinitionId();
                final PortletDefinition portletDescriptor = this.portletDefinitionRegistry.getParentPortletDescriptor(portletDefinitionId);
                if (portletDescriptor == null) {
                    // Missconfigured portlet, remove it from the list so we don't check again
                    // and ignore it
                    layoutNodeIdItr.remove();
                    continue;
                }
                final List<? extends EventDefinitionReference> supportedProcessingEvents = portletDescriptor.getSupportedProcessingEvents();
                // they are not checked again
                if (supportedProcessingEvents == null || supportedProcessingEvents.size() == 0) {
                    layoutNodeIdItr.remove();
                    continue;
                }
                portletEntityCache.put(layoutNodeId, portletEntity);
            }
            final IPortletDefinition portletDefinition = portletEntity.getPortletDefinition();
            final IPortletDefinitionId portletDefinitionId = portletDefinition.getPortletDefinitionId();
            if (this.supportsEvent(event, portletDefinitionId)) {
                this.logger.debug("{} supports event {}", portletDefinition, event);
                // If this is the default portlet entity remove the definition from the all defs
                // set to avoid duplicate processing
                final IPortletEntity defaultPortletEntity = this.portletEntityRegistry.getOrCreateDefaultPortletEntity(request, portletDefinitionId);
                if (defaultPortletEntity.equals(portletEntity)) {
                    portletDefinitions.remove(portletDefinition);
                }
                // Is this portlet permitted to receive events?  (Or is it
                // disablePortletEvents=true?)
                IPortletDefinitionParameter disablePortletEvents = portletDefinition.getParameter(PortletExecutionManager.DISABLE_PORTLET_EVENTS_PARAMETER);
                if (disablePortletEvents != null && Boolean.parseBoolean(disablePortletEvents.getValue())) {
                    logger.info("Ignoring portlet events for portlet '{}' because they have been disabled.", portletDefinition.getFName());
                    continue;
                }
                final IPortletEntityId portletEntityId = portletEntity.getPortletEntityId();
                final Set<IPortletWindow> portletWindows = this.portletWindowRegistry.getAllPortletWindowsForEntity(request, portletEntityId);
                for (final IPortletWindow portletWindow : portletWindows) {
                    this.logger.debug("{} resolved target {}", event, portletWindow);
                    final IPortletWindowId portletWindowId = portletWindow.getPortletWindowId();
                    final Event unmarshalledEvent = this.unmarshall(portletWindow, event);
                    portletEventQueue.offerEvent(portletWindowId, new QueuedEvent(sourceWindowId, unmarshalledEvent));
                }
            } else {
                portletDefinitions.remove(portletDefinition);
            }
        }
        if (!portletDefinitions.isEmpty()) {
            final IPerson user = userInstance.getPerson();
            final EntityIdentifier ei = user.getEntityIdentifier();
            final IAuthorizationPrincipal ap = AuthorizationServiceFacade.instance().newPrincipal(ei.getKey(), ei.getType());
            // targeting
            for (final IPortletDefinition portletDefinition : portletDefinitions) {
                // Is this portlet permitted to receive events?  (Or is it
                // disablePortletEvents=true?)
                IPortletDefinitionParameter disablePortletEvents = portletDefinition.getParameter(PortletExecutionManager.DISABLE_PORTLET_EVENTS_PARAMETER);
                if (disablePortletEvents != null && Boolean.parseBoolean(disablePortletEvents.getValue())) {
                    logger.info("Ignoring portlet events for portlet '{}' because they have been disabled.", portletDefinition.getFName());
                    continue;
                }
                final IPortletDefinitionId portletDefinitionId = portletDefinition.getPortletDefinitionId();
                // Check if the user can render the portlet definition before doing event tests
                if (ap.canRender(portletDefinitionId.getStringId())) {
                    if (this.supportsEvent(event, portletDefinitionId)) {
                        this.logger.debug("{} supports event {}", portletDefinition, event);
                        final IPortletEntity portletEntity = this.portletEntityRegistry.getOrCreateDefaultPortletEntity(request, portletDefinitionId);
                        final IPortletEntityId portletEntityId = portletEntity.getPortletEntityId();
                        final Set<IPortletWindow> portletWindows = this.portletWindowRegistry.getAllPortletWindowsForEntity(request, portletEntityId);
                        for (final IPortletWindow portletWindow : portletWindows) {
                            this.logger.debug("{} resolved target {}", event, portletWindow);
                            final IPortletWindowId portletWindowId = portletWindow.getPortletWindowId();
                            final Event unmarshalledEvent = this.unmarshall(portletWindow, event);
                            portletEventQueue.offerEvent(portletWindowId, new QueuedEvent(sourceWindowId, unmarshalledEvent));
                        }
                    }
                }
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EntityIdentifier(org.apereo.portal.EntityIdentifier) IPortletWindow(org.apereo.portal.portlet.om.IPortletWindow) LinkedHashMap(java.util.LinkedHashMap) IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) IPortletDefinitionParameter(org.apereo.portal.portlet.om.IPortletDefinitionParameter) IAuthorizationPrincipal(org.apereo.portal.security.IAuthorizationPrincipal) IUserPreferencesManager(org.apereo.portal.IUserPreferencesManager) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) PortletDefinition(org.apache.pluto.container.om.portlet.PortletDefinition) IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) Event(javax.portlet.Event) IPortletWindowId(org.apereo.portal.portlet.om.IPortletWindowId)

Example 23 with IPortletEntityId

use of org.apereo.portal.portlet.om.IPortletEntityId in project uPortal by Jasig.

the class PortletEntityCache method storeIfAbsentEntity.

public T storeIfAbsentEntity(T entity) {
    final IPortletEntityId portletEntityId = entity.getPortletEntityId();
    // Check if the entity already exists (uses a read lock)
    T existingEntity = this.getEntity(portletEntityId);
    if (existingEntity != null) {
        return existingEntity;
    }
    writeLock.lock();
    try {
        // Check again inside the write lock
        existingEntity = this.entitiesById.get(portletEntityId);
        if (existingEntity != null) {
            return existingEntity;
        }
        this.storeEntity(entity);
    } finally {
        writeLock.unlock();
    }
    return entity;
}
Also used : IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId)

Example 24 with IPortletEntityId

use of org.apereo.portal.portlet.om.IPortletEntityId in project uPortal by Jasig.

the class PortletEntityRegistryImpl method getPortletEntityLock.

@Override
public Lock getPortletEntityLock(HttpServletRequest request, IPortletEntityId portletEntityId) {
    final ConcurrentMap<IPortletEntityId, Lock> lockMap = getPortletEntityLockMap(request);
    // See if the lock already exist, return if it does
    Lock lock = lockMap.get(portletEntityId);
    if (lock != null) {
        return lock;
    }
    // Create a new lock and do a putIfAbsent to avoid synchronizing but still get a single lock
    // instance for the session.
    lock = createPortletEntityLock();
    return ConcurrentMapUtils.putIfAbsent(lockMap, portletEntityId, lock);
}
Also used : IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 25 with IPortletEntityId

use of org.apereo.portal.portlet.om.IPortletEntityId in project uPortal by Jasig.

the class PortletEntityRegistryImpl method storePortletEntity.

/* (non-Javadoc)
     * @see org.apereo.portal.portlet.registry.IPortletEntityRegistry#storePortletEntity(org.apereo.portal.portlet.om.IPortletEntity)
     */
@Override
public void storePortletEntity(HttpServletRequest request, final IPortletEntity portletEntity) {
    Validate.notNull(portletEntity, "portletEntity can not be null");
    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final IPerson person = userInstance.getPerson();
    if (person.isGuest()) {
        // Never persist things for the guest user, just rely on in-memory storage
        return;
    }
    final IPortletEntityId wrapperPortletEntityId = portletEntity.getPortletEntityId();
    final Lock portletEntityLock = this.getPortletEntityLock(request, wrapperPortletEntityId);
    portletEntityLock.lock();
    try {
        final boolean shouldBePersisted = this.shouldBePersisted(portletEntity);
        if (portletEntity instanceof PersistentPortletEntityWrapper) {
            // Unwrap the persistent entity
            final IPortletEntity persistentEntity = ((PersistentPortletEntityWrapper) portletEntity).getPersistentEntity();
            // Already persistent entity that still has prefs
            if (shouldBePersisted) {
                try {
                    this.portletEntityDao.updatePortletEntity(persistentEntity);
                } catch (HibernateOptimisticLockingFailureException e) {
                    // Check if this exception is from the entity being deleted from under us.
                    final boolean exists = this.portletEntityDao.portletEntityExists(persistentEntity.getPortletEntityId());
                    if (!exists) {
                        this.logger.warn("The persistent portlet has already been deleted: " + persistentEntity + ". The passed entity should be persistent so a new persistent entity will be created");
                        this.deletePortletEntity(request, portletEntity, true);
                        this.createPersistentEntity(persistentEntity, wrapperPortletEntityId);
                    } else {
                        throw e;
                    }
                }
            } else // Already persistent entity that should not be, DELETE!
            {
                // Capture identifiers needed to recreate the entity as session persistent
                final IPortletDefinitionId portletDefinitionId = portletEntity.getPortletDefinitionId();
                final String layoutNodeId = portletEntity.getLayoutNodeId();
                final int userId = portletEntity.getUserId();
                // Delete the persistent entity
                this.deletePortletEntity(request, portletEntity, false);
                // Create a new entity and stick it in the cache
                this.getOrCreatePortletEntity(request, portletDefinitionId, layoutNodeId, userId);
            }
        } else if (portletEntity instanceof SessionPortletEntityImpl) {
            // There are preferences on the interim entity, create an store it
            if (shouldBePersisted) {
                // Remove the session scoped entity from the request and session caches
                this.deletePortletEntity(request, portletEntity, false);
                final IPortletEntity persistentEntity = createPersistentEntity(portletEntity, wrapperPortletEntityId);
                if (this.logger.isTraceEnabled()) {
                    this.logger.trace("Session scoped entity " + wrapperPortletEntityId + " should now be persistent. Deleted it from session cache and created persistent portlet entity " + persistentEntity.getPortletEntityId());
                }
            } else // Session scoped entity that is still session scoped,
            {
                // Look for a persistent entity and delete it
                final String channelSubscribeId = portletEntity.getLayoutNodeId();
                final int userId = portletEntity.getUserId();
                IPortletEntity existingPersistentEntity = this.portletEntityDao.getPortletEntity(channelSubscribeId, userId);
                if (existingPersistentEntity != null) {
                    final IPortletEntityId consistentPortletEntityId = this.createConsistentPortletEntityId(existingPersistentEntity);
                    existingPersistentEntity = new PersistentPortletEntityWrapper(existingPersistentEntity, consistentPortletEntityId);
                    this.logger.warn("A persistent portlet entity already exists: " + existingPersistentEntity + ". The passed entity has no preferences so the persistent version will be deleted");
                    this.deletePortletEntity(request, existingPersistentEntity, false);
                    // Add to request cache
                    final PortletEntityCache<IPortletEntity> portletEntityMap = this.getPortletEntityMap(request);
                    portletEntityMap.storeIfAbsentEntity(portletEntity);
                    // Add to session cache
                    final PortletEntityCache<PortletEntityData> portletEntityDataMap = this.getPortletEntityDataMap(request);
                    portletEntityDataMap.storeIfAbsentEntity(((SessionPortletEntityImpl) portletEntity).getPortletEntityData());
                }
            }
        } else {
            throw new IllegalArgumentException("Invalid portlet entity implementation passed: " + portletEntity.getClass());
        }
    } finally {
        portletEntityLock.unlock();
    }
}
Also used : HibernateOptimisticLockingFailureException(org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) IUserInstance(org.apereo.portal.user.IUserInstance) IPortletDefinitionId(org.apereo.portal.portlet.om.IPortletDefinitionId) IPerson(org.apereo.portal.security.IPerson) IPortletEntity(org.apereo.portal.portlet.om.IPortletEntity) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId)

Aggregations

IPortletEntityId (org.apereo.portal.portlet.om.IPortletEntityId)33 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)25 IPortletDefinitionId (org.apereo.portal.portlet.om.IPortletDefinitionId)14 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)10 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)9 Test (org.junit.Test)9 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 List (java.util.List)7 PortletPreferenceImpl (org.apereo.portal.portlet.dao.jpa.PortletPreferenceImpl)7 IPortletPreference (org.apereo.portal.portlet.om.IPortletPreference)7 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)6 IUserInstance (org.apereo.portal.user.IUserInstance)6 Callable (java.util.concurrent.Callable)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpSession (javax.servlet.http.HttpSession)4 IUserPreferencesManager (org.apereo.portal.IUserPreferencesManager)4 IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)4 IPortletWindowId (org.apereo.portal.portlet.om.IPortletWindowId)4 Map (java.util.Map)3 Lock (java.util.concurrent.locks.Lock)3