Search in sources :

Example 56 with SlingHttpServletRequest

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

the class PlumberServletTest method testGetOnWriteExecute.

/**
     * in this test we execute a pipe that modifies content, with a flag mocking the GET request:
     * the execution should fail.
     */
@Test
public void testGetOnWriteExecute() throws ServletException {
    SlingHttpServletRequest request = mockPlumberServletRequest(context.resourceResolver(), pipedWritePath, null, null, null, null, null);
    boolean hasFailed = true;
    try {
        servlet.execute(request, response, false);
        hasFailed = false;
    } catch (Exception e) {
    }
    assertTrue("Execution should have failed", hasFailed);
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AbstractPipeTest(org.apache.sling.pipes.AbstractPipeTest) ContainerPipeTest(org.apache.sling.pipes.ContainerPipeTest) Test(org.junit.Test)

Example 57 with SlingHttpServletRequest

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

the class PlumberServletTest method testDummyTreeInfiniteSize.

@Test
public void testDummyTreeInfiniteSize() throws Exception {
    SlingHttpServletRequest request = mockPlumberServletRequest(context.resourceResolver(), dummyTreePath, null, null, null, null, "-1");
    servlet.execute(request, response, false);
    assertDummyTree(DUMMYTREE_TEST_SIZE);
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) AbstractPipeTest(org.apache.sling.pipes.AbstractPipeTest) ContainerPipeTest(org.apache.sling.pipes.ContainerPipeTest) Test(org.junit.Test)

Example 58 with SlingHttpServletRequest

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

the class PlumberServletTest method testWriteExecute.

@Test
public void testWriteExecute() throws ServletException {
    SlingHttpServletRequest request = mockPlumberServletRequest(context.resourceResolver(), pipedWritePath, null, null, null, null, null);
    servlet.execute(request, response, true);
    String finalResponse = stringResponse.toString();
    assertFalse("There should be a response", StringUtils.isBlank(finalResponse));
    assertFalse("There should be no more pending changes", context.resourceResolver().hasChanges());
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) AbstractPipeTest(org.apache.sling.pipes.AbstractPipeTest) ContainerPipeTest(org.apache.sling.pipes.ContainerPipeTest) Test(org.junit.Test)

Example 59 with SlingHttpServletRequest

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

the class JavaUseProvider method provide.

@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
    if (!JAVA_PATTERN.matcher(identifier).matches()) {
        LOG.debug("Identifier {} does not match a Java class name pattern.", identifier);
        return ProviderOutcome.failure();
    }
    Bindings globalBindings = renderContext.getBindings();
    SlingScriptHelper sling = BindingsUtils.getHelper(globalBindings);
    SlingHttpServletRequest request = BindingsUtils.getRequest(globalBindings);
    Map<String, Object> overrides = setRequestAttributes(request, arguments);
    Object result;
    try {
        result = sightlyJavaCompilerService.getResourceBackedUseObject(renderContext, identifier);
        if (result != null) {
            if (result instanceof Use) {
                ((Use) result).init(BindingsUtils.merge(globalBindings, arguments));
            }
            return ProviderOutcome.success(result);
        } else {
            LOG.debug("Attempting to load class {} from the classloader cache.", identifier);
            Class<?> cls = classLoaderWriter.getClassLoader().loadClass(identifier);
            // attempt OSGi service load
            result = sling.getService(cls);
            if (result != null) {
                return ProviderOutcome.success(result);
            }
            result = request.adaptTo(cls);
            if (result == null) {
                Resource resource = BindingsUtils.getResource(globalBindings);
                result = resource.adaptTo(cls);
            }
            if (result != null) {
                return ProviderOutcome.success(result);
            } else {
                /*
                     * the object was cached by the class loader but it's not adaptable from {@link Resource} or {@link
                     * SlingHttpServletRequest}; attempt to load it like a regular POJO that optionally could implement {@link Use}
                     */
                result = cls.newInstance();
                if (result instanceof Use) {
                    ((Use) result).init(BindingsUtils.merge(globalBindings, arguments));
                }
                return ProviderOutcome.notNullOrFailure(result);
            }
        }
    } catch (Exception e) {
        // any other exception is an error
        return ProviderOutcome.failure(e);
    } finally {
        resetRequestAttribute(request, overrides);
    }
}
Also used : SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Use(org.apache.sling.scripting.sightly.pojo.Use) Resource(org.apache.sling.api.resource.Resource) Bindings(javax.script.Bindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 60 with SlingHttpServletRequest

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

the class ResourceUseProvider method provide.

@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
    Bindings globalBindings = renderContext.getBindings();
    SlingHttpServletRequest request = BindingsUtils.getRequest(globalBindings);
    String path = normalizePath(request, identifier);
    try {
        Resource resource = request.getResourceResolver().getResource(path);
        if (resource != null && !ResourceUtil.isNonExistingResource(resource)) {
            return ProviderOutcome.success(resource);
        }
    } catch (Exception e) {
        return ProviderOutcome.failure(e);
    }
    return ProviderOutcome.failure();
}
Also used : Resource(org.apache.sling.api.resource.Resource) Bindings(javax.script.Bindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Aggregations

SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)98 Resource (org.apache.sling.api.resource.Resource)52 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)49 Test (org.junit.Test)48 Expectations (org.jmock.Expectations)32 RewriterResponse (org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse)31 ValueMap (org.apache.sling.api.resource.ValueMap)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 IOException (java.io.IOException)10 Bindings (javax.script.Bindings)10 SlingBindings (org.apache.sling.api.scripting.SlingBindings)9 ServletException (javax.servlet.ServletException)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 AbstractPipeTest (org.apache.sling.pipes.AbstractPipeTest)8 ContainerPipeTest (org.apache.sling.pipes.ContainerPipeTest)8 PrintWriter (java.io.PrintWriter)7 Map (java.util.Map)7 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)7 MockSlingHttpServlet3Request (org.apache.sling.servlets.post.impl.helper.MockSlingHttpServlet3Request)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5