Search in sources :

Example 21 with SlingHttpServletResponse

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

the class ContentDispositionFilterTest method test_doFilter8.

@Test
public void test_doFilter8() 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)

Example 22 with SlingHttpServletResponse

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

the class ContentDispositionFilterTest method test_isJcrData6.

@Test
public void test_isJcrData6() throws Throwable {
    contentDispositionFilter = new ContentDispositionFilter();
    final SlingHttpServletRequest request = context.mock(SlingHttpServletRequest.class);
    final SlingHttpServletResponse response = context.mock(SlingHttpServletResponse.class);
    final Resource resource = context.mock(Resource.class);
    context.checking(new Expectations() {

        {
            allowing(request).getResource();
            will(returnValue(resource));
            allowing(resource).adaptTo(ValueMap.class);
            will(returnValue(null));
            allowing(resource).getChild(JCR_CONTENT_LEAF);
            will(returnValue(null));
        }
    });
    final ContentDispositionFilter.RewriterResponse rewriterResponse = contentDispositionFilter.new RewriterResponse(request, response);
    Boolean result = (Boolean) PrivateAccessor.invoke(rewriterResponse, "isJcrData", new Class[] { Resource.class }, new Object[] { resource });
    Assert.assertFalse(result);
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Expectations(org.jmock.Expectations) 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 23 with SlingHttpServletResponse

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

the class ContentDispositionFilterTest method test_doFilter10.

@Test
public void test_doFilter10() 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);
    contentDispositionFilter = new ContentDispositionFilter();
    callActivateWithConfiguration(new String[] { "/content/usergenerated:text/html,text/plain" }, 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, "image/jpeg");
            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("image/jpeg");
            //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("image/jpeg");
    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 24 with SlingHttpServletResponse

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

the class ContentDispositionFilterTest method test_doFilter2.

@Test
public void test_doFilter2() 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" }, 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)

Example 25 with SlingHttpServletResponse

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

the class DefaultSlingScript method verifySlingBindings.

Bindings verifySlingBindings(final SlingBindings slingBindings) throws IOException {
    final Bindings bindings = new SimpleBindings();
    final SlingHttpServletRequest request = slingBindings.getRequest();
    // check sling object
    Object slingObject = slingBindings.get(SLING);
    if (slingObject == null) {
        if (request != null) {
            slingObject = new InternalScriptHelper(this.bundleContext, this, request, slingBindings.getResponse(), this.cache);
        } else {
            slingObject = new InternalScriptHelper(this.bundleContext, this, this.cache);
        }
    } else if (!(slingObject instanceof SlingScriptHelper)) {
        throw fail(SLING, "Wrong type");
    }
    final SlingScriptHelper sling = (SlingScriptHelper) slingObject;
    bindings.put(SLING, sling);
    if (request != null) {
        final SlingHttpServletResponse response = slingBindings.getResponse();
        if (response == null) {
            throw fail(RESPONSE, "Missing or wrong type");
        }
        Object resourceObject = slingBindings.get(RESOURCE);
        if (resourceObject != null && !(resourceObject instanceof Resource)) {
            throw fail(RESOURCE, "Wrong type");
        }
        Object resolverObject = slingBindings.get(RESOLVER);
        if (resolverObject != null && !(resolverObject instanceof ResourceResolver)) {
            throw fail(RESOLVER, "Wrong type");
        }
        Object writerObject = slingBindings.get(OUT);
        if (writerObject != null && !(writerObject instanceof PrintWriter)) {
            throw fail(OUT, "Wrong type");
        }
        // if there is a provided sling script helper, check arguments
        if (slingBindings.get(SLING) != null) {
            if (sling.getRequest() != request) {
                throw fail(REQUEST, "Not the same as request field of SlingScriptHelper");
            }
            if (sling.getResponse() != response) {
                throw fail(RESPONSE, "Not the same as response field of SlingScriptHelper");
            }
            if (resourceObject != null && sling.getRequest().getResource() != resourceObject) {
                throw fail(RESOURCE, "Not the same as resource of the SlingScriptHelper request");
            }
            if (resolverObject != null && sling.getRequest().getResourceResolver() != resolverObject) {
                throw fail(RESOLVER, "Not the same as the resource resolver of the SlingScriptHelper request's resolver");
            }
            if (writerObject != null && sling.getResponse().getWriter() != writerObject) {
                throw fail(OUT, "Not the same as writer of the SlingScriptHelper response");
            }
        }
        // set base variables when executing inside a request
        bindings.put(REQUEST, sling.getRequest());
        bindings.put(READER, sling.getRequest().getReader());
        bindings.put(RESPONSE, sling.getResponse());
        bindings.put(RESOURCE, sling.getRequest().getResource());
        bindings.put(RESOLVER, sling.getRequest().getResourceResolver());
        bindings.put(OUT, sling.getResponse().getWriter());
    }
    Object logObject = slingBindings.get(LOG);
    if (logObject == null) {
        logObject = LoggerFactory.getLogger(getLoggerName());
    } else if (!(logObject instanceof Logger)) {
        throw fail(LOG, "Wrong type");
    }
    bindings.put(LOG, logObject);
    // copy non-base variables
    for (Map.Entry<String, Object> entry : slingBindings.entrySet()) {
        if (!bindings.containsKey(entry.getKey())) {
            bindings.put(entry.getKey(), entry.getValue());
        }
    }
    if (!bindingsValuesProviders.isEmpty()) {
        Set<String> protectedKeys = new HashSet<String>();
        protectedKeys.addAll(PROTECTED_KEYS);
        ProtectedBindings protectedBindings = new ProtectedBindings(bindings, protectedKeys);
        for (BindingsValuesProvider provider : bindingsValuesProviders) {
            provider.addBindings(protectedBindings);
        }
    }
    return bindings;
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Logger(org.slf4j.Logger) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ProtectedBindings(org.apache.sling.scripting.core.impl.helper.ProtectedBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SimpleBindings(javax.script.SimpleBindings) ProtectedBindings(org.apache.sling.scripting.core.impl.helper.ProtectedBindings) BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Map(java.util.Map) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

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