Search in sources :

Example 6 with SlingHttpServletResponse

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

the class EvalTagHandler method doEndTag.

/**
     * Called after the body has been processed.
     *
     * @return whether additional evaluations of the body are desired
     */
public int doEndTag() throws JspException {
    log.debug("EvalTagHandler doEndTag");
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
    final Servlet servlet;
    if (!this.ignoreResourceTypeHierarchy) {
        // detecte resource
        final Resource resource;
        if (this.resource != null) {
            resource = this.resource;
        } else if (this.resourceType != null) {
            resource = new SyntheticResource(bindings.getRequest().getResourceResolver(), bindings.getResource().getPath(), this.resourceType);
        } else {
            resource = bindings.getResource();
        }
        servlet = servletResolver.resolveServlet(resource, this.script);
    } else {
        final ResourceResolver rr = bindings.getRequest().getResourceResolver();
        final String scriptPath;
        if (!script.startsWith("/")) {
            // resolve relative script
            String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
            // check if parent resides on search path
            for (String sp : rr.getSearchPath()) {
                if (parentPath.startsWith(sp)) {
                    parentPath = parentPath.substring(sp.length());
                    break;
                }
            }
            scriptPath = parentPath + '/' + script;
        } else {
            scriptPath = this.script;
        }
        servlet = servletResolver.resolveServlet(rr, scriptPath);
    }
    if (servlet == null) {
        throw new JspException("Could not find script '" + script + "' referenced in jsp " + scriptHelper.getScript().getScriptResource().getPath());
    }
    try {
        if (flush && !(pageContext.getOut() instanceof BodyContent)) {
            // might throw an IOException of course
            pageContext.getOut().flush();
        }
        // wrap the response to get the correct output order
        SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
        servlet.service(pageContext.getRequest(), response);
        return EVAL_PAGE;
    } catch (Exception e) {
        log.error("Error while executing script " + script, e);
        throw new JspException("Error while executing script " + script, e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) JspException(javax.servlet.jsp.JspException) BodyContent(javax.servlet.jsp.tagext.BodyContent) JspException(javax.servlet.jsp.JspException) ServletResolver(org.apache.sling.api.servlets.ServletResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Servlet(javax.servlet.Servlet)

Example 7 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse 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 8 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse 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)

Example 9 with SlingHttpServletResponse

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

the class StreamRendererServletTest method test_setHeaders.

@Test
public void test_setHeaders() throws Throwable {
    final Resource resource = Mockito.mock(Resource.class);
    final SlingHttpServletResponse response = Mockito.mock(SlingHttpServletResponse.class);
    final ResourceMetadata meta = Mockito.mock(ResourceMetadata.class);
    final ServletContext sc = Mockito.mock(ServletContext.class);
    StreamRendererServlet streamRendererServlet = new StreamRendererServlet(true, new String[] { "/" }) {

        @Override
        public ServletContext getServletContext() {
            return sc;
        }
    };
    Mockito.when(resource.getResourceMetadata()).thenReturn(meta);
    PrivateAccessor.invoke(streamRendererServlet, "setHeaders", new Class[] { Resource.class, SlingHttpServletResponse.class }, new Object[] { resource, response });
    Mockito.verify(response, Mockito.times(1)).setContentType("application/octet-stream");
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Resource(org.apache.sling.api.resource.Resource) ServletContext(javax.servlet.ServletContext) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) Test(org.junit.Test)

Example 10 with SlingHttpServletResponse

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

the class ThymeleafScriptEngine method eval.

@Override
public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
    final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
    if (helper == null) {
        throw new ScriptException("SlingScriptHelper missing from bindings");
    }
    final SlingHttpServletRequest request = helper.getRequest();
    final SlingHttpServletResponse response = helper.getResponse();
    // only used by Thymeleaf's ServletContextResourceResolver (TODO check if still true for 3.0)
    final ServletContext servletContext = null;
    final Locale locale = helper.getResponse().getLocale();
    final String scriptName = helper.getScript().getScriptResource().getPath();
    final Writer writer = scriptContext.getWriter();
    try {
        final ResourceResolver resourceResolver = thymeleafScriptEngineFactory.getRequestScopedResourceResolver();
        final IContext context = new SlingWebContext(request, response, servletContext, resourceResolver, locale, bindings);
        thymeleafScriptEngineFactory.getTemplateEngine().process(scriptName, context, writer);
    } catch (Exception e) {
        logger.error("Failure rendering Thymeleaf template '{}': {}", scriptName, e.getMessage());
        throw new ScriptException(e);
    }
    return null;
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Locale(java.util.Locale) IContext(org.thymeleaf.context.IContext) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ServletContext(javax.servlet.ServletContext) Writer(java.io.Writer)

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