Search in sources :

Example 86 with SlingHttpServletRequest

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

the class RewriterFilter method doFilter.

/**
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (!(request instanceof SlingHttpServletRequest)) {
        throw new ServletException("Request is not a Apache Sling HTTP request.");
    }
    final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
    final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
    final RewriterResponse rewriterResponse = new RewriterResponse(slingRequest, slingResponse, this.pipelineManager);
    boolean errorOccured = true;
    try {
        chain.doFilter(request, rewriterResponse);
        errorOccured = false;
    } finally {
        rewriterResponse.finished(errorOccured);
    }
}
Also used : ServletException(javax.servlet.ServletException) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 87 with SlingHttpServletRequest

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

the class ScriptUtils method resolveScript.

public static Resource resolveScript(ResourceResolver resolver, RenderContext renderContext, String scriptIdentifier) {
    SlingHttpServletRequest request = BindingsUtils.getRequest(renderContext.getBindings());
    Resource caller = ResourceResolution.getResourceForRequest(resolver, request);
    Resource result = ResourceResolution.getResourceFromSearchPath(caller, scriptIdentifier);
    if (result == null) {
        SlingScriptHelper sling = BindingsUtils.getHelper(renderContext.getBindings());
        caller = resolver.getResource(sling.getScript().getScriptResource().getPath());
        result = ResourceResolution.getResourceFromSearchPath(caller, scriptIdentifier);
    }
    return result;
}
Also used : SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 88 with SlingHttpServletRequest

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

the class RequestAdapterFactory method getAdapter.

@Override
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
    if (type == RequestAdapterUseObject.class && adaptable instanceof SlingHttpServletRequest) {
        SlingHttpServletRequest request = (SlingHttpServletRequest) adaptable;
        Resource resource = request.getResource();
        ValueMap properties = resource.adaptTo(ValueMap.class);
        String title = (String) properties.get("jcr:title");
        return (AdapterType) new RequestAdapterUseObjectImpl(title);
    }
    return null;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) RequestAdapterUseObject(org.apache.sling.scripting.sightly.testing.adaptable.RequestAdapterUseObject) Resource(org.apache.sling.api.resource.Resource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 89 with SlingHttpServletRequest

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

the class SlingModelsUseProvider method provide.

@Override
public ProviderOutcome provide(final String identifier, final RenderContext renderContext, final Bindings arguments) {
    if (!JAVA_PATTERN.matcher(identifier).matches()) {
        LOGGER.debug("Identifier {} does not match a Java class name pattern.", identifier);
        return ProviderOutcome.failure();
    }
    final Class<?> cls;
    try {
        cls = dynamicClassLoaderManager.getDynamicClassLoader().loadClass(identifier);
    } catch (ClassNotFoundException e) {
        LOGGER.debug("Could not find class with the given name {}.", identifier);
        // next use provider will be queried
        return ProviderOutcome.failure();
    }
    if (!modelFactory.isModelClass(cls)) {
        LOGGER.debug("{} is not a Sling Model.");
        // next use provider will be queried
        return ProviderOutcome.failure();
    }
    Bindings globalBindings = renderContext.getBindings();
    SlingHttpServletRequest request = (SlingHttpServletRequest) globalBindings.get(SlingBindings.REQUEST);
    if (request == null) {
        return ProviderOutcome.failure(new IllegalStateException("Could not get request from bindings."));
    }
    // pass parameters as request attributes
    Map<String, Object> overrides = setRequestAttributes(request, arguments);
    try {
        // try to instantiate class via Sling Models (first via request, then via resource)
        if (modelFactory.canCreateFromAdaptable(request, cls)) {
            LOGGER.debug("Trying to instantiate class {} as Sling Model from request.", cls);
            return ProviderOutcome.notNullOrFailure(modelFactory.createModel(request, cls));
        }
        Resource resource = (Resource) globalBindings.get(SlingBindings.RESOURCE);
        if (resource == null) {
            LOGGER.debug("Could not get resource from bindings.");
            return ProviderOutcome.failure(new IllegalStateException("Could not get resource from bindings."));
        }
        if (modelFactory.canCreateFromAdaptable(resource, cls)) {
            LOGGER.debug("Trying to instantiate class {} as Sling Model from resource.", cls);
            return ProviderOutcome.notNullOrFailure(modelFactory.createModel(resource, cls));
        }
        return ProviderOutcome.failure(new IllegalStateException("Could not adapt the given Sling Model from neither request nor resource: " + cls));
    } catch (Throwable e) {
        return ProviderOutcome.failure(e);
    } finally {
        resetRequestAttribute(request, overrides);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 90 with SlingHttpServletRequest

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

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