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);
}
}
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();
}
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);
}
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;
}
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);
}
}
Aggregations