Search in sources :

Example 26 with SlingHttpServletRequest

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

the class PlumberServletTest method testAdditionalBindingsAndWriter.

@Test
public void testAdditionalBindingsAndWriter() throws Exception {
    String testBinding = "testBinding";
    String testBindingLength = testBinding + "Length";
    String bindingValue = "testBindingValue";
    String pathLengthParam = "pathLength";
    String bindings = "{\"" + testBinding + "\":\"" + bindingValue + "\"}";
    String respObject = "{\"" + pathLengthParam + "\":\"${path.get(\\\"dummyGrandChild\\\").length}\",\"" + testBindingLength + "\":\"${" + testBinding + ".length}\"}";
    SlingHttpServletRequest request = mockPlumberServletRequest(context.resourceResolver(), dummyTreePath, null, bindings.toString(), respObject.toString(), null, null);
    servlet.execute(request, response, false);
    assertDummyTree();
    JsonObject response = Json.createReader(new StringReader(stringResponse.toString())).readObject();
    JsonArray array = response.getJsonArray(OutputWriter.KEY_ITEMS);
    for (int i = 0; i < array.size(); i++) {
        JsonObject object = array.getJsonObject(i);
        assertNotNull("there should be an object returned at each time", object);
        String path = object.getString(CustomWriter.PATH_KEY);
        assertNotNull("the string path should be returned for each item, containing the path of the resource");
        String pathLength = object.getString(pathLengthParam);
        assertNotNull("there should be a pathLength param, as specified in the writer", pathLength);
        assertEquals("Pathlength should be the string representation of the path length", path.length() + "", pathLength);
        String testBindingLengthValue = object.getString(testBindingLength);
        assertNotNull("testBindingLength should be there", testBindingLengthValue);
        assertEquals("testBindingLength should be the string representation of the additional binding length", bindingValue.length() + "", testBindingLengthValue);
    }
}
Also used : JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) AbstractPipeTest(org.apache.sling.pipes.AbstractPipeTest) ContainerPipeTest(org.apache.sling.pipes.ContainerPipeTest) Test(org.junit.Test)

Example 27 with SlingHttpServletRequest

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

the class PlumberServletTest method testDummyTreeThroughPlumber.

@Test
public void testDummyTreeThroughPlumber() throws Exception {
    SlingHttpServletRequest request = mockPlumberServletRequest(context.resourceResolver(), PATH_PIPE, dummyTreePath, null, null, null, null);
    servlet.execute(request, response, false);
    assertDummyTree();
}
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 28 with SlingHttpServletRequest

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

the class DistributionAgentCreationFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    SlingHttpServletRequest servletRequest = (SlingHttpServletRequest) request;
    // only intercept POST requests
    if (METHOD_POST.equalsIgnoreCase(servletRequest.getMethod())) {
        String name = request.getParameter(NAME);
        String type = request.getParameter(TYPE);
        if (type != null && name != null) {
            String filter = format(FACTORY_FILTER_PATTERN, name, type);
            try {
                ServiceReference[] services = context.getAllServiceReferences(DistributionAgent.class.getName(), filter);
                if (services != null && services.length > 0) {
                    String errorMessage = format("An agent named '%s' of different type than '%s' was already previously registered, please change the Agent name.", name, type);
                    ((HttpServletResponse) response).sendError(SC_CONFLICT, errorMessage);
                    return;
                }
            } catch (InvalidSyntaxException e) {
                // should not happen...
                log.error("Impossible to access to {} references", DistributionAgent.class.getName(), e);
            }
        }
    }
    chain.doFilter(request, response);
}
Also used : DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) HttpServletResponse(javax.servlet.http.HttpServletResponse) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ServiceReference(org.osgi.framework.ServiceReference)

Example 29 with SlingHttpServletRequest

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

Example 30 with SlingHttpServletRequest

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

the class RequestAnalysisLogger method doFilter.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (request instanceof SlingHttpServletRequest) {
        final long start = System.currentTimeMillis();
        final AnylserSlingHttpServletResponse slingRes = new AnylserSlingHttpServletResponse((SlingHttpServletResponse) response);
        try {
            chain.doFilter(request, response);
        } finally {
            final long end = System.currentTimeMillis();
            final SlingHttpServletRequest slingReq = (SlingHttpServletRequest) request;
            StringBuilder pw = new StringBuilder(1024);
            pw.append(String.format(":%d:%d:%s:%s:%s:%d%n", start, (end - start), slingReq.getMethod(), slingReq.getRequestURI(), slingRes.getContentType(), slingRes.getStatus()));
            final Iterator<String> entries = slingReq.getRequestProgressTracker().getMessages();
            while (entries.hasNext()) {
                pw.append('!').append(entries.next());
            }
            BufferedWriter out = this.logFile;
            if (out != null) {
                out.write(pw.toString());
                out.flush();
            }
        }
    } else {
        chain.doFilter(request, response);
    }
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) BufferedWriter(java.io.BufferedWriter)

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