Search in sources :

Example 96 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 97 with SlingHttpServletRequest

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

the class SlingIncludeAttributeTagProcessor method doProcess.

@Override
protected void doProcess(final ITemplateContext templateContext, final IProcessableElementTag processableElementTag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler elementTagStructureHandler) {
    try {
        final SlingHttpServletRequest slingHttpServletRequest = (SlingHttpServletRequest) templateContext.getVariable(SlingBindings.REQUEST);
        final SlingHttpServletResponse slingHttpServletResponse = (SlingHttpServletResponse) templateContext.getVariable(SlingBindings.RESPONSE);
        final IEngineConfiguration configuration = templateContext.getConfiguration();
        final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
        final IStandardExpression expression = expressionParser.parseExpression(templateContext, attributeValue);
        final Object include = expression.execute(templateContext);
        String path = null;
        if (include instanceof String) {
            path = (String) include;
        }
        Resource resource = null;
        if (include instanceof Resource) {
            resource = (Resource) include;
        }
        // request dispatcher options
        final RequestDispatcherOptions requestDispatcherOptions = prepareRequestDispatcherOptions(expressionParser, templateContext, processableElementTag, elementTagStructureHandler);
        // dispatch
        final String content = dispatch(resource, path, slingHttpServletRequest, slingHttpServletResponse, requestDispatcherOptions);
        // add output
        final Boolean unwrap = (Boolean) parseAttribute(expressionParser, templateContext, processableElementTag, elementTagStructureHandler, UNWRAP_ATTRIBUTE_NAME);
        if (unwrap != null && unwrap) {
            elementTagStructureHandler.replaceWith(content, false);
        } else {
            elementTagStructureHandler.setBody(content, false);
        }
    } catch (Exception e) {
        throw new RuntimeException("unable to process include attribute", e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 98 with SlingHttpServletRequest

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

the class JavaScriptEngineFactory method callServlet.

/**
     * Call the servlet.
     * @param bindings The bindings for the script invocation
     * @param scriptHelper The script helper.
     * @param context The script context.
     * @throws SlingServletException
     * @throws SlingIOException
     */
private void callServlet(final Bindings bindings, final SlingScriptHelper scriptHelper, final ScriptContext context) {
    // create a SlingBindings object
    final SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    ResourceResolver resolver = (ResourceResolver) context.getAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, SlingScriptConstants.SLING_SCOPE);
    if (resolver == null) {
        resolver = scriptHelper.getScript().getScriptResource().getResourceResolver();
    }
    ioProvider.setRequestResourceResolver(resolver);
    final SlingHttpServletRequest request = slingBindings.getRequest();
    final Object oldValue = request.getAttribute(SlingBindings.class.getName());
    try {
        final ServletWrapper servlet = getWrapperAdapter(scriptHelper);
        request.setAttribute(SlingBindings.class.getName(), slingBindings);
        servlet.service(request, slingBindings.getResponse());
    } catch (SlingException se) {
        // rethrow as is
        throw se;
    } catch (IOException ioe) {
        throw new SlingIOException(ioe);
    } catch (ServletException se) {
        throw new SlingServletException(se);
    } catch (Exception ex) {
        throw new SlingException(null, ex);
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldValue);
        ioProvider.resetRequestResourceResolver();
    }
}
Also used : ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException) SlingIOException(org.apache.sling.api.SlingIOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ServletException(javax.servlet.ServletException) ScriptException(javax.script.ScriptException) SlingServletException(org.apache.sling.api.SlingServletException) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException)

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