Search in sources :

Example 1 with CacheControl

use of javax.portlet.CacheControl in project uPortal by Jasig.

the class PortletCacheControlServiceImplTest method testDetermineCacheScopePortletDefinitionPrivate.

@Test
public void testDetermineCacheScopePortletDefinitionPrivate() {
    MockHttpServletRequest httpRequest = new MockHttpServletRequest();
    MockPortletWindowId portletWindowId = new MockPortletWindowId("123");
    MockPortletDefinitionId portletDefinitionId = new MockPortletDefinitionId(789);
    PortletDefinition portletDefinition = mock(PortletDefinition.class);
    when(portletDefinition.getCacheScope()).thenReturn("private");
    when(portletWindowRegistry.getPortletWindow(httpRequest, portletWindowId)).thenReturn(portletWindow);
    when(portletWindow.getPortletWindowId()).thenReturn(portletWindowId);
    when(portletWindow.getPortletEntity()).thenReturn(portletEntity);
    when(portletWindow.getWindowState()).thenReturn(WindowState.NORMAL);
    when(portletWindow.getPortletMode()).thenReturn(PortletMode.VIEW);
    when(portletEntity.getPortletDefinitionId()).thenReturn(portletDefinitionId);
    when(portletDefinitionRegistry.getParentPortletDescriptor(portletDefinitionId)).thenReturn(portletDefinition);
    when(urlSyntaxProvider.getPortalRequestInfo(httpRequest)).thenReturn(portalRequestInfo);
    cacheControlService.setPortletWindowRegistry(portletWindowRegistry);
    cacheControlService.setPortletDefinitionRegistry(portletDefinitionRegistry);
    final CacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult> cacheState = cacheControlService.getPortletRenderState(httpRequest, portletWindowId);
    final CacheControl cacheControl = cacheState.getCacheControl();
    assertFalse(cacheControl.isPublicScope());
}
Also used : MockPortletDefinitionId(org.apereo.portal.mock.portlet.om.MockPortletDefinitionId) PortletRenderResult(org.apereo.portal.portlet.rendering.PortletRenderResult) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockPortletWindowId(org.apereo.portal.mock.portlet.om.MockPortletWindowId) CacheControl(javax.portlet.CacheControl) PortletDefinition(org.apache.pluto.container.om.portlet.PortletDefinition) Test(org.junit.Test)

Example 2 with CacheControl

use of javax.portlet.CacheControl in project uPortal by Jasig.

the class PortletCacheControlServiceImplTest method testDetermineCacheScopePortletDefinitionPublic.

@Test
public void testDetermineCacheScopePortletDefinitionPublic() {
    MockHttpServletRequest httpRequest = new MockHttpServletRequest();
    MockPortletWindowId portletWindowId = new MockPortletWindowId("123");
    MockPortletDefinitionId portletDefinitionId = new MockPortletDefinitionId(789);
    when(portletDescriptor.getCacheScope()).thenReturn(MimeResponse.PUBLIC_SCOPE);
    when(portletWindowRegistry.getPortletWindow(httpRequest, portletWindowId)).thenReturn(portletWindow);
    when(portletWindow.getPortletWindowId()).thenReturn(portletWindowId);
    when(portletWindow.getPortletEntity()).thenReturn(portletEntity);
    when(portletWindow.getWindowState()).thenReturn(WindowState.NORMAL);
    when(portletWindow.getPortletMode()).thenReturn(PortletMode.VIEW);
    when(portletEntity.getPortletDefinitionId()).thenReturn(portletDefinitionId);
    when(portletDefinitionRegistry.getParentPortletDescriptor(portletDefinitionId)).thenReturn(portletDescriptor);
    when(urlSyntaxProvider.getPortalRequestInfo(httpRequest)).thenReturn(portalRequestInfo);
    cacheControlService.setPortletWindowRegistry(portletWindowRegistry);
    cacheControlService.setPortletDefinitionRegistry(portletDefinitionRegistry);
    final CacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult> cacheState = cacheControlService.getPortletRenderState(httpRequest, portletWindowId);
    final CacheControl cacheControl = cacheState.getCacheControl();
    assertTrue(cacheControl.isPublicScope());
}
Also used : MockPortletDefinitionId(org.apereo.portal.mock.portlet.om.MockPortletDefinitionId) PortletRenderResult(org.apereo.portal.portlet.rendering.PortletRenderResult) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockPortletWindowId(org.apereo.portal.mock.portlet.om.MockPortletWindowId) CacheControl(javax.portlet.CacheControl) Test(org.junit.Test)

Example 3 with CacheControl

use of javax.portlet.CacheControl in project uPortal by Jasig.

the class PortletCacheControlServiceImplTest method testGetCacheControlDataExistsInPrivateCache.

@Test
public void testGetCacheControlDataExistsInPrivateCache() {
    // mock 2 requests, have to share sessionId for private cache
    MockHttpSession mockSession = new MockHttpSession();
    MockHttpServletRequest httpRequest = new MockHttpServletRequest();
    httpRequest.setSession(mockSession);
    MockHttpServletRequest nextHttpRequest = new MockHttpServletRequest();
    nextHttpRequest.setSession(mockSession);
    MockPortletWindowId portletWindowId = new MockPortletWindowId("123");
    MockPortletDefinitionId portletDefinitionId = new MockPortletDefinitionId(789);
    // use private cache
    when(portletDescriptor.getCacheScope()).thenReturn("private");
    when(portletDescriptor.getExpirationCache()).thenReturn(300);
    when(portletWindowRegistry.getPortletWindow(httpRequest, portletWindowId)).thenReturn(portletWindow);
    when(portletWindowRegistry.getPortletWindow(nextHttpRequest, portletWindowId)).thenReturn(portletWindow);
    when(portletWindow.getPortletWindowId()).thenReturn(portletWindowId);
    when(portletWindow.getPortletEntity()).thenReturn(portletEntity);
    when(portletWindow.getWindowState()).thenReturn(WindowState.NORMAL);
    when(portletWindow.getPortletMode()).thenReturn(PortletMode.VIEW);
    when(portletEntity.getPortletDefinitionId()).thenReturn(portletDefinitionId);
    when(portletDefinitionRegistry.getParentPortletDescriptor(portletDefinitionId)).thenReturn(portletDescriptor);
    when(urlSyntaxProvider.getPortalRequestInfo(httpRequest)).thenReturn(portalRequestInfo);
    final CacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult> cacheState = cacheControlService.getPortletRenderState(httpRequest, portletWindowId);
    final CacheControl cacheControl = cacheState.getCacheControl();
    assertFalse(cacheControl.isPublicScope());
    cacheControl.setETag("123456");
    final PortletRenderResult portletResult = new PortletRenderResult("title", null, 0, 1);
    final String content = "<p>Cached content</p>";
    final CachedPortletData<PortletRenderResult> cachedPortletData = new CachedPortletData<PortletRenderResult>(portletResult, content, null, null, cacheControl.isPublicScope(), cacheControl.getETag(), -2);
    cacheControlService.cachePortletRenderOutput(portletWindowId, nextHttpRequest, cacheState, cachedPortletData);
    when(urlSyntaxProvider.getPortalRequestInfo(nextHttpRequest)).thenReturn(portalRequestInfo);
    final CacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult> afterCacheState = cacheControlService.getPortletRenderState(nextHttpRequest, portletWindowId);
    // retrieve cachecontrol again, and return should have etag set
    // note using 'nextHttpRequest'
    Assert.assertEquals("123456", afterCacheState.getCacheControl().getETag());
}
Also used : MockPortletDefinitionId(org.apereo.portal.mock.portlet.om.MockPortletDefinitionId) PortletRenderResult(org.apereo.portal.portlet.rendering.PortletRenderResult) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockPortletWindowId(org.apereo.portal.mock.portlet.om.MockPortletWindowId) CacheControl(javax.portlet.CacheControl) Test(org.junit.Test)

Example 4 with CacheControl

use of javax.portlet.CacheControl in project uPortal by Jasig.

the class PortletCacheControlServiceImpl method cachePortletOutput.

private <D extends CachedPortletResultHolder<T>, T extends Serializable> void cachePortletOutput(IPortletWindowId portletWindowId, HttpServletRequest httpRequest, CacheState<D, T> cacheState, D cachedPortletData, Ehcache publicOutputCache, Ehcache privateOutputCache) {
    final IPortletWindow portletWindow = this.portletWindowRegistry.getPortletWindow(httpRequest, portletWindowId);
    final CacheControl cacheControl = cacheState.getCacheControl();
    if (cacheControl.isPublicScope()) {
        final PublicPortletCacheKey publicCacheKey = cacheState.getPublicPortletCacheKey();
        this.cacheElement(publicOutputCache, publicCacheKey, cachedPortletData, cacheControl);
        logger.debug("Cached public data under key {} for {}", publicCacheKey, portletWindow);
    } else {
        PrivatePortletCacheKey privateCacheKey = cacheState.getPrivatePortletCacheKey();
        // portlet's response is now privately scoped
        if (privateCacheKey == null) {
            final HttpSession session = httpRequest.getSession();
            final String sessionId = session.getId();
            final IPortletEntityId entityId = portletWindow.getPortletEntityId();
            final PublicPortletCacheKey publicCacheKey = cacheState.getPublicPortletCacheKey();
            privateCacheKey = new PrivatePortletCacheKey(sessionId, portletWindowId, entityId, publicCacheKey);
        }
        this.cacheElement(privateOutputCache, privateCacheKey, cachedPortletData, cacheControl);
        logger.debug("Cached private data under key {} for {}", privateCacheKey, portletWindow);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) CacheControl(javax.portlet.CacheControl) IPortletWindow(org.apereo.portal.portlet.om.IPortletWindow) IPortletEntityId(org.apereo.portal.portlet.om.IPortletEntityId)

Example 5 with CacheControl

use of javax.portlet.CacheControl 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)

Aggregations

CacheControl (javax.portlet.CacheControl)26 Test (org.junit.Test)22 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)22 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)16 CachedPortletData (org.apereo.portal.portlet.container.cache.CachedPortletData)14 CachedPortletResourceData (org.apereo.portal.portlet.container.cache.CachedPortletResourceData)11 PortletHttpServletRequestWrapper (org.apereo.portal.utils.web.PortletHttpServletRequestWrapper)11 MockPortletDefinitionId (org.apereo.portal.mock.portlet.om.MockPortletDefinitionId)6 MockPortletWindowId (org.apereo.portal.mock.portlet.om.MockPortletWindowId)6 PortletRenderResult (org.apereo.portal.portlet.rendering.PortletRenderResult)5 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)4 PortletHttpServletResponseWrapper (org.apereo.portal.utils.web.PortletHttpServletResponseWrapper)4 IOException (java.io.IOException)2 PortletException (javax.portlet.PortletException)2 PortletContainerException (org.apache.pluto.container.PortletContainerException)2 PortletDefinition (org.apache.pluto.container.om.portlet.PortletDefinition)2 PortletDispatchException (org.apereo.portal.portlet.PortletDispatchException)2 HeaderSettingCacheControl (org.apereo.portal.portlet.container.cache.HeaderSettingCacheControl)2 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)2 List (java.util.List)1