Search in sources :

Example 6 with SlingHttpServletRequest

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

the class SlingRequestDispatcher method dispatch.

private void dispatch(ServletRequest request, ServletResponse sResponse, boolean include) throws ServletException, IOException {
    SlingHttpServletRequest cRequest = RequestData.unwrap(request);
    RequestData rd = RequestData.getRequestData(cRequest);
    String absPath = getAbsolutePath(cRequest, path);
    RequestProgressTracker requestProgressTracker = cRequest.getRequestProgressTracker();
    // doing anything
    if (!(sResponse instanceof HttpServletResponse)) {
        log.error("include: Failed to include {}, response has wrong type", absPath);
        return;
    }
    if (resource == null) {
        String timerName = "resolveIncludedResource(" + absPath + ")";
        requestProgressTracker.startTimer(timerName);
        // resolve the absolute path in the resource resolver, using
        // only those parts of the path as if it would be request path
        resource = cRequest.getResourceResolver().resolve(absPath);
        // if the resource could not be resolved, fail gracefully
        if (resource == null) {
            log.error("include: Could not resolve {} to a resource, not including", absPath);
            return;
        }
        requestProgressTracker.logTimer(timerName, "path={0} resolves to Resource={1}", absPath, resource);
    }
    // ensure request path info and optional merges
    SlingRequestPathInfo info = getMergedRequestPathInfo(cRequest);
    requestProgressTracker.log("Including resource {0} ({1})", resource, info);
    rd.getSlingRequestProcessor().dispatchRequest(request, sResponse, resource, info, include);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 7 with SlingHttpServletRequest

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

the class ConfigurationBindingsValueProvider method addBindings.

@Override
public void addBindings(Bindings bindings) {
    if (!enabled || !bindings.containsKey(SlingBindings.REQUEST)) {
        return;
    }
    SlingHttpServletRequest request = (SlingHttpServletRequest) bindings.get(SlingBindings.REQUEST);
    Resource resource = request.getResource();
    if (resource == null) {
        return;
    }
    Map<String, Object> configMap = new ConfigMap(resource, configMetadataProvider);
    bindings.put(BINDING_VARIABLE, configMap);
}
Also used : Resource(org.apache.sling.api.resource.Resource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 8 with SlingHttpServletRequest

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

the class AbstractResourceTypeViaProvider method getAdaptable.

@Override
public Object getAdaptable(Object original, String value) {
    if (!handle(value)) {
        return ORIGINAL;
    }
    if (original instanceof Resource) {
        final Resource resource = (Resource) original;
        final String resourceType = getResourceType(resource, value);
        if (resourceType == null) {
            log.warn("Could not determine forced resource type for {} using via value {}.", resource, value);
            return null;
        }
        return new ResourceTypeForcingResourceWrapper(resource, resourceType);
    } else if (original instanceof SlingHttpServletRequest) {
        final SlingHttpServletRequest request = (SlingHttpServletRequest) original;
        final Resource resource = request.getResource();
        if (resource == null) {
            return null;
        }
        final String resourceType = getResourceType(resource, value);
        if (resourceType == null) {
            log.warn("Could not determine forced resource type for {} using via value {}.", resource, value);
            return null;
        }
        return new ResourceTypeForcingRequestWrapper(request, resource, resourceType);
    } else {
        log.warn("Received unexpected adaptable of type {}.", original.getClass().getName());
        return null;
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 9 with SlingHttpServletRequest

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

the class ResourceResolverInjectorTest method testFromRequest.

@Test
public void testFromRequest() {
    SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);
    ResourceResolver resourceResolver = mock(ResourceResolver.class);
    when(request.getResourceResolver()).thenReturn(resourceResolver);
    Object result = injector.getValue(request, "resourceResolver", ResourceResolver.class, element, registry);
    assertEquals(resourceResolver, result);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Test(org.junit.Test)

Example 10 with SlingHttpServletRequest

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

the class DefaultSlingScript method service.

/**
     * @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
public void service(ServletRequest req, ServletResponse res) {
    final SlingHttpServletRequest request = (SlingHttpServletRequest) req;
    try {
        // prepare the properties for the script
        final SlingBindings props = new SlingBindings();
        props.setRequest((SlingHttpServletRequest) req);
        props.setResponse((SlingHttpServletResponse) res);
        // try to set content type (unless included)
        if (request.getAttribute(SlingConstants.ATTR_INCLUDE_SERVLET_PATH) == null) {
            final String contentType = request.getResponseContentType();
            if (contentType != null) {
                res.setContentType(contentType);
                // see SLING-679
                if (contentType.startsWith("text/")) {
                    res.setCharacterEncoding("UTF-8");
                }
            } else {
                LOGGER.debug("service: No response content type defined for request {}.", request.getRequestURI());
            }
        } else {
            LOGGER.debug("service: Included request, not setting content type and encoding");
        }
        // evaluate the script now using the ScriptEngine
        eval(props);
    } catch (ScriptEvaluationException see) {
        // log in the request progress tracker
        logScriptError(request, see);
        throw see;
    } catch (SlingException e) {
        // log in the request progress tracker
        logScriptError(request, e);
        throw e;
    } catch (Exception e) {
        // log in the request progress tracker
        logScriptError(request, e);
        throw new SlingException("Cannot get DefaultSlingScript: " + e.getMessage(), e);
    }
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) SlingException(org.apache.sling.api.SlingException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ScriptException(javax.script.ScriptException) SlingException(org.apache.sling.api.SlingException) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) IOException(java.io.IOException)

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