Search in sources :

Example 16 with SlingHttpServletRequest

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

the class IncludeRuntimeExtension method includeScript.

private void includeScript(final Bindings bindings, String script, PrintWriter out) {
    if (StringUtils.isEmpty(script)) {
        throw new SightlyException("Path for data-sly-include is empty");
    } else {
        LOG.debug("Attempting to include script {}.", script);
        SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings);
        ServletResolver servletResolver = slingScriptHelper.getService(ServletResolver.class);
        if (servletResolver != null) {
            SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
            Servlet servlet = servletResolver.resolveServlet(request.getResource(), script);
            if (servlet != null) {
                try {
                    SlingHttpServletResponse response = BindingsUtils.getResponse(bindings);
                    PrintWriterResponseWrapper resWrapper = new PrintWriterResponseWrapper(out, response);
                    servlet.service(request, resWrapper);
                } catch (Exception e) {
                    throw new SightlyException("Failed to include script " + script, e);
                }
            } else {
                throw new SightlyException("Failed to locate script " + script);
            }
        } else {
            throw new SightlyException("Sling ServletResolver service is unavailable, failed to include " + script);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletResolver(org.apache.sling.api.servlets.ServletResolver) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Servlet(javax.servlet.Servlet) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Example 17 with SlingHttpServletRequest

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

the class ResourceRuntimeExtension method provideResource.

private String provideResource(final RenderContext renderContext, Object pathObj, Map<String, Object> options) {
    Map<String, Object> opts = new HashMap<>(options);
    final Bindings bindings = renderContext.getBindings();
    SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
    Map originalAttributes = ExtensionUtils.setRequestAttributes(request, (Map) options.remove(OPTION_REQUEST_ATTRIBUTES));
    RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
    String resourceType = runtimeObjectModel.toString(getAndRemoveOption(opts, OPTION_RESOURCE_TYPE));
    StringWriter writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    if (pathObj instanceof Resource) {
        Resource includedResource = (Resource) pathObj;
        Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
        String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
        includeResource(bindings, printWriter, includedResource, dispatcherOptions, resourceType);
    } else {
        String includePath = runtimeObjectModel.toString(pathObj);
        // build path completely
        includePath = buildPath(includePath, options, request.getResource());
        if (includePath != null) {
            // check if path identifies an existing resource
            Resource includedResource = request.getResourceResolver().getResource(includePath);
            PathInfo pathInfo;
            if (includedResource != null) {
                Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
                String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
                includeResource(bindings, printWriter, includedResource, dispatcherOptions, resourceType);
            } else {
                // analyse path and decompose potential selectors from the path
                pathInfo = new PathInfo(includePath);
                Map<String, String> dispatcherOptionsMap = handleSelectors(request, pathInfo.selectors, opts, runtimeObjectModel);
                String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
                includeResource(bindings, printWriter, pathInfo.path, dispatcherOptions, resourceType);
            }
        } else {
            // use the current resource
            Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
            String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
            includeResource(bindings, printWriter, request.getResource(), dispatcherOptions, resourceType);
        }
    }
    ExtensionUtils.setRequestAttributes(request, originalAttributes);
    return writer.toString();
}
Also used : HashMap(java.util.HashMap) RuntimeObjectModel(org.apache.sling.scripting.sightly.render.RuntimeObjectModel) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Bindings(javax.script.Bindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 18 with SlingHttpServletRequest

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

the class AbstractPostOperationTest method testNoRemainingPostfixIsSuccessful.

@Test
public void testNoRemainingPostfixIsSuccessful() {
    TestingResourceResolver resourceResolver = new TestingResourceResolver();
    MockSlingHttpServlet3Request request = new MockSlingHttpServlet3Request("/test", null, null, null, null);
    request.setResourceResolver(resourceResolver);
    final PostOperation operation = new AbstractPostOperation() {

        @Override
        protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws RepositoryException {
            changes.add(Modification.onChange(ModificationType.CREATE, "/content/test"));
        }
    };
    HtmlResponse response = new HtmlResponse();
    operation.run(request, response, new SlingPostProcessor[0]);
    assertTrue(response.isSuccessful());
    assertTrue(resourceResolver.commitCalled);
    assertFalse(resourceResolver.revertCalled);
}
Also used : MockSlingHttpServlet3Request(org.apache.sling.servlets.post.impl.helper.MockSlingHttpServlet3Request) List(java.util.List) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Example 19 with SlingHttpServletRequest

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

the class AbstractPostOperationTest method testRemainingPostfixWithoutUnPostfixedIsSuccessful.

@Test
public void testRemainingPostfixWithoutUnPostfixedIsSuccessful() {
    TestingResourceResolver resourceResolver = new TestingResourceResolver();
    MockSlingHttpServlet3Request request = new MockSlingHttpServlet3Request("/test", null, null, null, null);
    request.setResourceResolver(resourceResolver);
    final PostOperation operation = new AbstractPostOperation() {

        @Override
        protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws RepositoryException {
            changes.add(Modification.onChange(ModificationType.CREATE, "/content/test@Postfix"));
        }
    };
    HtmlResponse response = new HtmlResponse();
    operation.run(request, response, new SlingPostProcessor[0]);
    assertTrue(response.isSuccessful());
    assertTrue(resourceResolver.commitCalled);
    assertFalse(resourceResolver.revertCalled);
}
Also used : MockSlingHttpServlet3Request(org.apache.sling.servlets.post.impl.helper.MockSlingHttpServlet3Request) List(java.util.List) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Example 20 with SlingHttpServletRequest

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

the class UrlFilter method doFilter.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (request instanceof SlingHttpServletRequest && response instanceof SlingHttpServletResponse) {
        SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
        SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
        RequestPathInfo pathInfo = slingRequest.getRequestPathInfo();
        Resource definitionResource = findUrlFilterDefinitionResource(slingRequest.getResource(), slingRequest.getResourceResolver());
        if (definitionResource != null) {
            logger.debug("found url filter definition resource at {}", definitionResource.getPath());
            ValueMap properties = definitionResource.adaptTo(ValueMap.class);
            if (properties != null) {
                if (checkSelector(pathInfo, properties) && checkSuffix(pathInfo, properties) && checkExtension(pathInfo, properties)) {
                    logger.debug("url filter definition resource at {} passed for request {}.", definitionResource.getPath(), slingRequest.getRequestPathInfo());
                } else {
                    logger.info("url filter definition resource at {} FAILED for request {}.", definitionResource.getPath(), slingRequest.getRequestPathInfo());
                    slingResponse.sendError(403);
                    return;
                }
            }
        }
    }
    chain.doFilter(request, response);
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) RequestPathInfo(org.apache.sling.api.request.RequestPathInfo) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) 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