Search in sources :

Example 1 with HeaderSettingCacheControl

use of org.apereo.portal.portlet.container.cache.HeaderSettingCacheControl 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)

Aggregations

IOException (java.io.IOException)1 CacheControl (javax.portlet.CacheControl)1 PortletException (javax.portlet.PortletException)1 PortletContainerException (org.apache.pluto.container.PortletContainerException)1 PortletDispatchException (org.apereo.portal.portlet.PortletDispatchException)1 CachedPortletResourceData (org.apereo.portal.portlet.container.cache.CachedPortletResourceData)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