Search in sources :

Example 41 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.

the class ContentDispositionFilterTest method test_doFilter6.

@Test
public void test_doFilter6() throws Throwable {
    final SlingHttpServletRequest request = context.mock(SlingHttpServletRequest.class);
    final SlingHttpServletResponse response = context.mock(SlingHttpServletResponse.class);
    final Resource resource = context.mock(Resource.class, "resource");
    final ValueMap properties = context.mock(ValueMap.class);
    callActivateWithConfiguration(new String[] { "/content/usergenerated/*" }, new String[] { "" });
    final AtomicInteger counter = new AtomicInteger();
    context.checking(new Expectations() {

        {
            allowing(request).getMethod();
            will(returnValue("GET"));
            allowing(response).containsHeader("Content-Disposition");
            will(returnValue(false));
            allowing(request).getAttribute(RewriterResponse.ATTRIBUTE_NAME);
            will(returnValue(null));
            allowing(request).setAttribute(RewriterResponse.ATTRIBUTE_NAME, "text/html");
            allowing(request).getResource();
            will(returnValue(resource));
            allowing(resource).getPath();
            will(returnValue("/content/usergenerated/"));
            allowing(resource).adaptTo(ValueMap.class);
            will(returnValue(properties));
            allowing(properties).containsKey(PROP_JCR_DATA);
            will(returnValue(true));
            allowing(response).setContentType("text/html");
            //CONTENT DISPOSITION IS SET
            exactly(1).of(response).addHeader("Content-Disposition", "attachment");
        }
    });
    final ContentDispositionFilter.RewriterResponse rewriterResponse = contentDispositionFilter.new RewriterResponse(request, response) {

        @Override
        public void addHeader(String name, String value) {
            counter.incrementAndGet();
        }
    };
    rewriterResponse.setContentType("text/html");
    Assert.assertEquals(1, counter.intValue());
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Expectations(org.jmock.Expectations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Example 42 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.

the class ContentDispositionFilterTest method test_doFilter20.

@Test
public void test_doFilter20() throws Throwable {
    final SlingHttpServletRequest request = context.mock(SlingHttpServletRequest.class);
    final SlingHttpServletResponse response = context.mock(SlingHttpServletResponse.class);
    final Resource resource = context.mock(Resource.class, "resource");
    final ValueMap properties = context.mock(ValueMap.class);
    callActivateWithConfiguration(new String[] { "/content/usergenerated" }, new String[] { "/content/other" }, true);
    final AtomicInteger counter = new AtomicInteger();
    context.checking(new Expectations() {

        {
            allowing(request).getMethod();
            will(returnValue("GET"));
            exactly(1).of(response).containsHeader("Content-Disposition");
            will(returnValue(false));
            exactly(1).of(response).containsHeader("Content-Disposition");
            will(returnValue(true));
            exactly(1).of(request).getAttribute(RewriterResponse.ATTRIBUTE_NAME);
            will(returnValue(null));
            exactly(1).of(request).getAttribute(RewriterResponse.ATTRIBUTE_NAME);
            will(returnValue("text/html"));
            allowing(request).setAttribute(RewriterResponse.ATTRIBUTE_NAME, "text/xml");
            allowing(request).setAttribute(RewriterResponse.ATTRIBUTE_NAME, "text/html");
            allowing(request).getResource();
            will(returnValue(resource));
            allowing(resource).getPath();
            will(returnValue("/content/other"));
            allowing(resource).adaptTo(ValueMap.class);
            will(returnValue(properties));
            allowing(properties).containsKey(PROP_JCR_DATA);
            will(returnValue(true));
            allowing(response).setContentType("text/html");
            allowing(response).setContentType("text/xml");
            //CONTENT DISPOSITION IS NOT SET
            never(response).addHeader("Content-Disposition", "attachment");
        }
    });
    final ContentDispositionFilter.RewriterResponse rewriterResponse = contentDispositionFilter.new RewriterResponse(request, response) {

        @Override
        public void addHeader(String name, String value) {
            counter.incrementAndGet();
        }
    };
    rewriterResponse.setContentType("text/html");
    Assert.assertEquals(0, counter.intValue());
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Expectations(org.jmock.Expectations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Example 43 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.

the class ContentDispositionFilterTest method test_doFilter21b.

/**
     * Test multiple calls of setContentType which each result in a content-disposition header being needed.
     * Only one header should be added.
     * @throws Throwable
     */
@Test
public void test_doFilter21b() throws Throwable {
    final SlingHttpServletRequest request = context.mock(SlingHttpServletRequest.class);
    final SlingHttpServletResponse response = context.mock(SlingHttpServletResponse.class);
    final Resource resource = context.mock(Resource.class, "resource");
    final ValueMap properties = context.mock(ValueMap.class);
    callActivateWithConfiguration(new String[] { "/content/usergenerated" }, new String[] { "/content" });
    final AtomicInteger counter = new AtomicInteger();
    context.checking(new Expectations() {

        {
            allowing(request).getMethod();
            will(returnValue("GET"));
            allowing(response).containsHeader("Content-Disposition");
            will(returnValue(false));
            exactly(1).of(response).reset();
            exactly(1).of(request).removeAttribute(RewriterResponse.ATTRIBUTE_NAME);
            allowing(request).getAttribute(RewriterResponse.ATTRIBUTE_NAME);
            will(returnValue(null));
            allowing(request).setAttribute(RewriterResponse.ATTRIBUTE_NAME, "text/html");
            allowing(request).setAttribute(RewriterResponse.ATTRIBUTE_NAME, "text/html");
            allowing(request).getResource();
            will(returnValue(resource));
            allowing(resource).getPath();
            will(returnValue("/content/usergenerated"));
            allowing(resource).adaptTo(ValueMap.class);
            will(returnValue(properties));
            allowing(properties).containsKey(PROP_JCR_DATA);
            will(returnValue(true));
            // Exactly 2 setContentType should reach the mock response.
            exactly(1).of(response).setContentType("text/html");
            exactly(1).of(response).setContentType("text/html");
            //CONTENT DISPOSITION IS SET
            exactly(1).of(response).addHeader("Content-Disposition", "attachment");
        }
    });
    final ContentDispositionFilter.RewriterResponse rewriterResponse = contentDispositionFilter.new RewriterResponse(request, response) {

        @Override
        public void addHeader(String name, String value) {
            counter.incrementAndGet();
        }
    };
    rewriterResponse.setContentType("text/html");
    Assert.assertEquals(1, counter.intValue());
    rewriterResponse.reset();
    /* Reset on the response clears all the headers, so if we setContentType again the content type header *and* the
         * content disposition should both reappear. The counter counts each time the content disposition header is added.
         * The setContentType calls on the mock response are enforced in the Expectations checked above.
         */
    rewriterResponse.setContentType("text/html");
    Assert.assertEquals(2, counter.intValue());
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Expectations(org.jmock.Expectations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Example 44 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.

the class ContentDispositionFilterTest method test_doFilter3.

@Test
public void test_doFilter3() throws Throwable {
    final SlingHttpServletRequest request = context.mock(SlingHttpServletRequest.class);
    final SlingHttpServletResponse response = context.mock(SlingHttpServletResponse.class);
    final Resource resource = context.mock(Resource.class, "resource");
    final ValueMap properties = context.mock(ValueMap.class);
    callActivateWithConfiguration(new String[] { "/content/usergenerated" }, new String[] { "" });
    final AtomicInteger counter = new AtomicInteger();
    context.checking(new Expectations() {

        {
            allowing(request).getMethod();
            will(returnValue("GET"));
            allowing(response).containsHeader("Content-Disposition");
            will(returnValue(false));
            allowing(request).getAttribute(RewriterResponse.ATTRIBUTE_NAME);
            will(returnValue(null));
            allowing(request).setAttribute(RewriterResponse.ATTRIBUTE_NAME, "text/html");
            allowing(request).getResource();
            will(returnValue(resource));
            allowing(resource).getPath();
            will(returnValue("/content/usergenerated"));
            allowing(resource).adaptTo(ValueMap.class);
            will(returnValue(properties));
            allowing(properties).containsKey(PROP_JCR_DATA);
            will(returnValue(true));
            allowing(response).setContentType("text/html");
            //CONTENT DISPOSITION IS SET
            exactly(1).of(response).addHeader("Content-Disposition", "attachment");
        }
    });
    final ContentDispositionFilter.RewriterResponse rewriterResponse = contentDispositionFilter.new RewriterResponse(request, response) {

        @Override
        public void addHeader(String name, String value) {
            counter.incrementAndGet();
        }
    };
    rewriterResponse.setContentType("text/html");
    Assert.assertEquals(1, counter.intValue());
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Expectations(org.jmock.Expectations) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Example 45 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.

the class ContentDispositionFilterTest method test_doFilter12.

@Test
public void test_doFilter12() throws Throwable {
    final SlingHttpServletRequest request = context.mock(SlingHttpServletRequest.class);
    final SlingHttpServletResponse response = context.mock(SlingHttpServletResponse.class);
    final Resource resource = context.mock(Resource.class, "resource");
    callActivateWithConfiguration(new String[] { "/content/usergenerated/*:text/html,text/plain" }, new String[] { "" });
    context.checking(new Expectations() {

        {
            allowing(request).getMethod();
            will(returnValue("GET"));
            allowing(request).getAttribute(RewriterResponse.ATTRIBUTE_NAME);
            will(returnValue(null));
            allowing(request).setAttribute(RewriterResponse.ATTRIBUTE_NAME, "text/html");
            allowing(request).getResource();
            will(returnValue(resource));
            allowing(resource).getPath();
            will(returnValue("/content/usergenerated/author"));
            allowing(response).setContentType("text/html");
            //CONTENT DISPOSITION MUST NOT SET
            never(response).addHeader("Content-Disposition", "attachment");
        }
    });
    ContentDispositionFilter.RewriterResponse rewriterResponse = contentDispositionFilter.new RewriterResponse(request, response);
    rewriterResponse.setContentType("text/html");
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Expectations(org.jmock.Expectations) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) Resource(org.apache.sling.api.resource.Resource) RewriterResponse(org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Aggregations

SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)55 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)49 Resource (org.apache.sling.api.resource.Resource)39 Test (org.junit.Test)35 RewriterResponse (org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse)31 Expectations (org.jmock.Expectations)31 ValueMap (org.apache.sling.api.resource.ValueMap)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 ServletException (javax.servlet.ServletException)5 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)5 FilterChain (javax.servlet.FilterChain)4 Servlet (javax.servlet.Servlet)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)4 SlingBindings (org.apache.sling.api.scripting.SlingBindings)4 ServletResolver (org.apache.sling.api.servlets.ServletResolver)4 IOException (java.io.IOException)3 RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)3 AbstractSlingFilterChain (org.apache.sling.engine.impl.filter.AbstractSlingFilterChain)3 FilterHandle (org.apache.sling.engine.impl.filter.FilterHandle)3