Search in sources :

Example 6 with CachedPortletResourceData

use of org.apereo.portal.portlet.container.cache.CachedPortletResourceData in project uPortal by Jasig.

the class PortletRendererImpl method doServeResource.

@Override
public long doServeResource(IPortletWindowId portletWindowId, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, PortletResourceOutputHandler portletOutputHandler) throws IOException {
    final CacheState<CachedPortletResourceData<Long>, Long> cacheState = this.portletCacheControlService.getPortletResourceState(httpServletRequest, portletWindowId);
    final IPortletWindow portletWindow = this.portletWindowRegistry.getPortletWindow(httpServletRequest, portletWindowId);
    enforceConfigPermission(httpServletRequest, portletWindow);
    if (cacheState.isUseBrowserData()) {
        logger.trace("doServeResource-Reusing browser data");
        return doResourceReplayBrowserContent(portletWindow, httpServletRequest, cacheState, portletOutputHandler);
    }
    if (cacheState.isUseCachedData()) {
        logger.trace("doServeResource-Reusing cached data");
        return doResourceReplayCachedContent(portletWindow, httpServletRequest, cacheState, portletOutputHandler, 0);
    }
    final int cacheSizeThreshold = this.portletCacheControlService.getCacheSizeThreshold();
    final CachingPortletResourceOutputHandler cachingPortletOutputHandler = new CachingPortletResourceOutputHandler(portletOutputHandler, cacheSizeThreshold);
    CacheControl cacheControl = cacheState.getCacheControl();
    //Wrap the cache control so it immediately sets the caching related response headers
    cacheControl = new HeaderSettingCacheControl(cacheControl, cachingPortletOutputHandler);
    //Setup the request and response
    httpServletRequest = this.setupPortletRequest(httpServletRequest);
    httpServletResponse = new PortletResourceHttpServletResponseWrapper(httpServletResponse, portletWindow, portletOutputHandler, cacheControl);
    httpServletRequest.setAttribute(IPortletRenderer.ATTRIBUTE__PORTLET_CACHE_CONTROL, cacheControl);
    httpServletRequest.setAttribute(IPortletRenderer.ATTRIBUTE__PORTLET_OUTPUT_HANDLER, cachingPortletOutputHandler);
    this.logger.debug("Executing resource request for window {}", portletWindow);
    final long start = System.nanoTime();
    try {
        this.portletContainer.doServeResource(portletWindow.getPlutoPortletWindow(), httpServletRequest, httpServletResponse);
    } catch (PortletException pe) {
        throw new PortletDispatchException("The portlet window '" + portletWindow + "' threw an exception while executing serveResource.", portletWindow, pe);
    } catch (PortletContainerException pce) {
        throw new PortletDispatchException("The portlet container threw an exception while executing serveResource on portlet window '" + portletWindow + "'.", portletWindow, pce);
    } catch (IOException ioe) {
        throw new PortletDispatchException("The portlet window '" + portletWindow + "' threw an exception while executing serveResource.", portletWindow, ioe);
    }
    final long executionTime = System.nanoTime() - start;
    //See if the portlet signaled to use the cached content
    final boolean useCachedContent = cacheControl.useCachedContent();
    if (useCachedContent) {
        final CachedPortletResourceData<Long> cachedPortletResourceData = cacheState.getCachedPortletData();
        if (cachedPortletResourceData != null) {
            //Update the expiration time and re-store in the cache
            final CachedPortletData<Long> cachedPortletData = cachedPortletResourceData.getCachedPortletData();
            cachedPortletData.updateExpirationTime(cacheControl.getExpirationTime());
            this.portletCacheControlService.cachePortletResourceOutput(portletWindowId, httpServletRequest, cacheState, cachedPortletResourceData);
        }
        if (cacheState.isBrowserSetEtag()) {
            logger.trace("doServeResource-useCachedContent, Reusing browser data");
            //Browser-side content matches, send a 304
            return doResourceReplayBrowserContent(portletWindow, httpServletRequest, cacheState, portletOutputHandler);
        }
        logger.trace("doServeResource-useCachedContent, Reusing cached data");
        return doResourceReplayCachedContent(portletWindow, httpServletRequest, cacheState, cachingPortletOutputHandler, executionTime);
    }
    publishResourceEvent(portletWindow, httpServletRequest, executionTime, false, false);
    if (cacheState != null) {
        boolean shouldCache = this.portletCacheControlService.shouldOutputBeCached(cacheControl);
        if (shouldCache) {
            final CachedPortletResourceData<Long> cachedPortletResourceData = cachingPortletOutputHandler.getCachedPortletResourceData(executionTime, cacheControl);
            if (cachedPortletResourceData != null) {
                this.portletCacheControlService.cachePortletResourceOutput(portletWindowId, httpServletRequest, cacheState, cachedPortletResourceData);
            }
        }
    }
    return executionTime;
}
Also used : PortletException(javax.portlet.PortletException) CachedPortletResourceData(org.apereo.portal.portlet.container.cache.CachedPortletResourceData) IOException(java.io.IOException) PortletContainerException(org.apache.pluto.container.PortletContainerException) IPortletWindow(org.apereo.portal.portlet.om.IPortletWindow) PortletDispatchException(org.apereo.portal.portlet.PortletDispatchException) CachingPortletResourceOutputHandler(org.apereo.portal.portlet.container.cache.CachingPortletResourceOutputHandler) HeaderSettingCacheControl(org.apereo.portal.portlet.container.cache.HeaderSettingCacheControl) CacheControl(javax.portlet.CacheControl) HeaderSettingCacheControl(org.apereo.portal.portlet.container.cache.HeaderSettingCacheControl)

Example 7 with CachedPortletResourceData

use of org.apereo.portal.portlet.container.cache.CachedPortletResourceData in project uPortal by Jasig.

the class PortletRendererImplTest method doServeResourceCachedContentExpirationMethodTest.

/**
     * Mimic workflow when cached portlet data using "expiration" method is available.
     *
     * @throws PortletContainerException
     * @throws IOException
     * @throws PortletException
     */
@Test
public void doServeResourceCachedContentExpirationMethodTest() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
    CacheControl cacheControl = cacheState.getCacheControl();
    cacheControl.setUseCachedContent(true);
    cacheControl.setExpirationTime(300);
    final String output = "{ \"hello\": \"world\" }";
    final CachedPortletData<Long> cachedPortletData = new CachedPortletData<Long>(1000l, output, null, "application/json", false, cacheControl.getETag(), cacheControl.getExpirationTime());
    final CachedPortletResourceData<Long> cachedPortletResourceData = new CachedPortletResourceData<Long>(cachedPortletData, Collections.EMPTY_MAP, null, null, null, null);
    final long expirationTime = System.currentTimeMillis() - 60000;
    cachedPortletData.updateExpirationTime(50000);
    cacheState.setCachedPortletData(cachedPortletResourceData);
    setupPortletExecutionMocks(request);
    when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
    ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
    portletRenderer.doServeResource(portletWindowId, request, response, handler);
    verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
    verify(portletCacheControlService, times(1)).getCacheSizeThreshold();
    verify(portletContainer, times(1)).doServeResource(eq(plutoPortletWindow), isA(PortletHttpServletRequestWrapper.class), isA(PortletResourceHttpServletResponseWrapper.class));
    verify(portletCacheControlService).cachePortletResourceOutput(eq(portletWindowId), isA(PortletHttpServletRequestWrapper.class), eq(cacheState), eq(cachedPortletResourceData));
    verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CachedPortletResourceData(org.apereo.portal.portlet.container.cache.CachedPortletResourceData) CachedPortletData(org.apereo.portal.portlet.container.cache.CachedPortletData) PortletHttpServletRequestWrapper(org.apereo.portal.utils.web.PortletHttpServletRequestWrapper) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with CachedPortletResourceData

use of org.apereo.portal.portlet.container.cache.CachedPortletResourceData in project uPortal by Jasig.

the class PortletRendererImplTest method doServeResourceCapture.

/**
     * No cached data exists, but mock a {@link CacheControl} that will trigger the
     * portletContainer#doServeResource, capture the output, and give to the portlet cachecontrol
     * service.
     *
     * @throws PortletException
     * @throws IOException
     * @throws PortletContainerException
     */
@Test
public void doServeResourceCapture() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("application/octet-stream");
    TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
    CacheControl cacheControl = cacheState.getCacheControl();
    cacheControl.setUseCachedContent(false);
    cacheControl.setExpirationTime(300);
    setupPortletExecutionMocks(request);
    when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
    when(portletCacheControlService.shouldOutputBeCached(cacheControl)).thenReturn(true);
    ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
    portletRenderer.doServeResource(portletWindowId, request, response, handler);
    verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
    verify(portletCacheControlService, times(1)).getCacheSizeThreshold();
    verify(portletContainer, times(1)).doServeResource(eq(plutoPortletWindow), isA(PortletHttpServletRequestWrapper.class), isA(PortletResourceHttpServletResponseWrapper.class));
    verify(portletCacheControlService, times(1)).shouldOutputBeCached(isA(CacheControl.class));
    verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
Also used : PortletHttpServletRequestWrapper(org.apereo.portal.utils.web.PortletHttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CachedPortletResourceData(org.apereo.portal.portlet.container.cache.CachedPortletResourceData) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 9 with CachedPortletResourceData

use of org.apereo.portal.portlet.container.cache.CachedPortletResourceData in project uPortal by Jasig.

the class PortletRendererImplTest method doServeResourceMarkupCaptureNegativeExpirationTime.

/**
     * No cached data exists, but mock a {@link CacheControl} with a negative value for
     * expirationtime. Will trigger the portletContainer#doServeResource, capture the output, and
     * give to the portlet cachecontrol service.
     *
     * <p>negative value for cacheControl expiration time means "cache forever."
     *
     * @throws PortletException
     * @throws IOException
     * @throws PortletContainerException
     */
@Test
public void doServeResourceMarkupCaptureNegativeExpirationTime() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("application/octet-stream");
    TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
    CacheControl cacheControl = cacheState.getCacheControl();
    cacheControl.setUseCachedContent(false);
    cacheControl.setExpirationTime(-1);
    setupPortletExecutionMocks(request);
    when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
    when(portletCacheControlService.shouldOutputBeCached(cacheControl)).thenReturn(true);
    ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
    portletRenderer.doServeResource(portletWindowId, request, response, handler);
    verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
    verify(portletCacheControlService, times(1)).getCacheSizeThreshold();
    verify(portletContainer, times(1)).doServeResource(eq(plutoPortletWindow), isA(PortletHttpServletRequestWrapper.class), isA(PortletResourceHttpServletResponseWrapper.class));
    verify(portletCacheControlService, times(1)).shouldOutputBeCached(isA(CacheControl.class));
    verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
Also used : PortletHttpServletRequestWrapper(org.apereo.portal.utils.web.PortletHttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CachedPortletResourceData(org.apereo.portal.portlet.container.cache.CachedPortletResourceData) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 10 with CachedPortletResourceData

use of org.apereo.portal.portlet.container.cache.CachedPortletResourceData in project uPortal by Jasig.

the class PortletRendererImplTest method doServeResourceUseBrowserContentTest.

/**
     * Same as {@link #doServeResourceCachedContentValidationMethodTest()}, but simulate browser
     * sending If-None-Match header that matches the etag. Verify no content returned and a 304
     * status code.
     *
     * @throws PortletException
     * @throws IOException
     * @throws PortletContainerException
     */
@Test
public void doServeResourceUseBrowserContentTest() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("If-None-Match", "123456");
    MockHttpServletResponse response = new MockHttpServletResponse();
    TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
    cacheState.setUseBrowserData(true);
    CacheControl cacheControl = cacheState.getCacheControl();
    cacheControl.setUseCachedContent(true);
    cacheControl.setExpirationTime(300);
    cacheControl.setETag("123456");
    final String output = "{ \"hello\": \"world\" }";
    final CachedPortletData<Long> cachedPortletData = new CachedPortletData<Long>(1000l, output, null, "application/json", false, cacheControl.getETag(), cacheControl.getExpirationTime());
    final CachedPortletResourceData<Long> cachedPortletResourceData = new CachedPortletResourceData<Long>(cachedPortletData, Collections.EMPTY_MAP, null, null, null, null);
    cacheState.setCachedPortletData(cachedPortletResourceData);
    setupPortletExecutionMocks(request);
    when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
    ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
    portletRenderer.doServeResource(portletWindowId, request, response, handler);
    //byte [] fromResponse = response.getContentAsByteArray();
    Assert.assertEquals(0, response.getContentLength());
    Assert.assertEquals(304, response.getStatus());
    verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
    verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
Also used : CachedPortletData(org.apereo.portal.portlet.container.cache.CachedPortletData) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CachedPortletResourceData(org.apereo.portal.portlet.container.cache.CachedPortletResourceData) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

CacheControl (javax.portlet.CacheControl)11 CachedPortletResourceData (org.apereo.portal.portlet.container.cache.CachedPortletResourceData)11 Test (org.junit.Test)10 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)10 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)10 CachedPortletData (org.apereo.portal.portlet.container.cache.CachedPortletData)7 PortletHttpServletRequestWrapper (org.apereo.portal.utils.web.PortletHttpServletRequestWrapper)7 IOException (java.io.IOException)1 List (java.util.List)1 PortletException (javax.portlet.PortletException)1 PortletContainerException (org.apache.pluto.container.PortletContainerException)1 PortletDispatchException (org.apereo.portal.portlet.PortletDispatchException)1 CachingPortletResourceOutputHandler (org.apereo.portal.portlet.container.cache.CachingPortletResourceOutputHandler)1 HeaderSettingCacheControl (org.apereo.portal.portlet.container.cache.HeaderSettingCacheControl)1 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)1