Search in sources :

Example 1 with CachedPortletResourceData

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

the class PortletRendererImplTest method doServeResourceCachedContentValidationMethodIfNoneMatchInvalidTest.

/**
     * Same as {@link #doServeResourceCachedContentValidationMethodTest()}, but simulate browser
     * sending If-None-Match header with mismatched etag. Response is 200 with content and new etag
     *
     * @throws PortletException
     * @throws IOException
     * @throws PortletContainerException
     */
@Test
public void doServeResourceCachedContentValidationMethodIfNoneMatchInvalidTest() 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.setUseCachedData(true);
    CacheControl cacheControl = cacheState.getCacheControl();
    cacheControl.setUseCachedContent(true);
    cacheControl.setExpirationTime(300);
    cacheControl.setETag("123457");
    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();
    assertArrayEquals(output.getBytes(), fromResponse);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("123457", response.getHeader("ETag"));
    verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
    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) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with CachedPortletResourceData

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

the class PortletRendererImplTest method testDoServeResourceNoCache.

/**
     * {@link CacheControl} says don't cache, make sure no caching for doServeResource.
     *
     * @throws PortletException
     * @throws IOException
     * @throws PortletContainerException
     */
@SuppressWarnings("unchecked")
@Test
public void testDoServeResourceNoCache() 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(false);
    cacheControl.setExpirationTime(0);
    setupPortletExecutionMocks(request);
    when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
    when(portletCacheControlService.shouldOutputBeCached(cacheControl)).thenReturn(false);
    ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
    portletRenderer.doServeResource(portletWindowId, request, response, handler);
    // call 2 times
    portletRenderer.doServeResource(portletWindowId, request, response, handler);
    verify(portletCacheControlService, times(2)).getCacheSizeThreshold();
    verify(portletCacheControlService, times(2)).getPortletResourceState(request, portletWindowId);
    verify(portletContainer, times(2)).doServeResource(eq(plutoPortletWindow), isA(PortletHttpServletRequestWrapper.class), isA(PortletResourceHttpServletResponseWrapper.class));
    verify(portletCacheControlService, times(2)).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 3 with CachedPortletResourceData

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

the class PortletRendererImplTest method doServeResourceCachedContentValidationMethodTest.

/**
     * Mimic workflow when cached portlet data using "validation" method is available.
     *
     * @throws PortletContainerException
     * @throws IOException
     * @throws PortletException
     */
@Test
public void doServeResourceCachedContentValidationMethodTest() 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);
    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();
    assertArrayEquals(output.getBytes(), fromResponse);
    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 4 with CachedPortletResourceData

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

the class PortletRendererImplTest method doServeResourceCachedContentReplayHeadersTest.

/**
     * Verify headers stored in cache are replayed on the response for cached doServeResource
     * content.
     *
     * @throws PortletContainerException
     * @throws IOException
     * @throws PortletException
     */
@Test
public void doServeResourceCachedContentReplayHeadersTest() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
    cacheState.setUseCachedData(true);
    CacheControl cacheControl = cacheState.getCacheControl();
    cacheControl.setUseCachedContent(true);
    cacheControl.setExpirationTime(300);
    final String output = "{ \"hello\": \"world\" }";
    final Map<String, List<Serializable>> headers = ImmutableMap.<String, List<Serializable>>of("header1", Arrays.<Serializable>asList("value1"), "header2", Arrays.<Serializable>asList("value2", "value3"));
    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, headers, 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();
    assertArrayEquals(output.getBytes(), fromResponse);
    Assert.assertEquals("value1", response.getHeader("header1"));
    Assert.assertEquals(Arrays.asList(new String[] { "value2", "value3" }), response.getHeaders("header2"));
    verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
    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) List(java.util.List) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 5 with CachedPortletResourceData

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

the class PortletRendererImplTest method doServeResourceCachedContentValidationMethodNotModifiedTest.

@Test
public void doServeResourceCachedContentValidationMethodNotModifiedTest() 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.setBrowserSetEtag(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);
    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)

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