use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class PlumberServletTest method testGetOnWriteExecute.
/**
* in this test we execute a pipe that modifies content, with a flag mocking the GET request:
* the execution should fail.
*/
@Test
public void testGetOnWriteExecute() throws ServletException {
SlingHttpServletRequest request = mockPlumberServletRequest(context.resourceResolver(), pipedWritePath, null, null, null, null, null);
boolean hasFailed = true;
try {
servlet.execute(request, response, false);
hasFailed = false;
} catch (Exception e) {
}
assertTrue("Execution should have failed", hasFailed);
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class PlumberServletTest method testDummyTreeInfiniteSize.
@Test
public void testDummyTreeInfiniteSize() throws Exception {
SlingHttpServletRequest request = mockPlumberServletRequest(context.resourceResolver(), dummyTreePath, null, null, null, null, "-1");
servlet.execute(request, response, false);
assertDummyTree(DUMMYTREE_TEST_SIZE);
}
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());
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class JavaUseProvider method provide.
@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
if (!JAVA_PATTERN.matcher(identifier).matches()) {
LOG.debug("Identifier {} does not match a Java class name pattern.", identifier);
return ProviderOutcome.failure();
}
Bindings globalBindings = renderContext.getBindings();
SlingScriptHelper sling = BindingsUtils.getHelper(globalBindings);
SlingHttpServletRequest request = BindingsUtils.getRequest(globalBindings);
Map<String, Object> overrides = setRequestAttributes(request, arguments);
Object result;
try {
result = sightlyJavaCompilerService.getResourceBackedUseObject(renderContext, identifier);
if (result != null) {
if (result instanceof Use) {
((Use) result).init(BindingsUtils.merge(globalBindings, arguments));
}
return ProviderOutcome.success(result);
} else {
LOG.debug("Attempting to load class {} from the classloader cache.", identifier);
Class<?> cls = classLoaderWriter.getClassLoader().loadClass(identifier);
// attempt OSGi service load
result = sling.getService(cls);
if (result != null) {
return ProviderOutcome.success(result);
}
result = request.adaptTo(cls);
if (result == null) {
Resource resource = BindingsUtils.getResource(globalBindings);
result = resource.adaptTo(cls);
}
if (result != null) {
return ProviderOutcome.success(result);
} else {
/*
* the object was cached by the class loader but it's not adaptable from {@link Resource} or {@link
* SlingHttpServletRequest}; attempt to load it like a regular POJO that optionally could implement {@link Use}
*/
result = cls.newInstance();
if (result instanceof Use) {
((Use) result).init(BindingsUtils.merge(globalBindings, arguments));
}
return ProviderOutcome.notNullOrFailure(result);
}
}
} catch (Exception e) {
// any other exception is an error
return ProviderOutcome.failure(e);
} finally {
resetRequestAttribute(request, overrides);
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class ResourceUseProvider method provide.
@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
Bindings globalBindings = renderContext.getBindings();
SlingHttpServletRequest request = BindingsUtils.getRequest(globalBindings);
String path = normalizePath(request, identifier);
try {
Resource resource = request.getResourceResolver().getResource(path);
if (resource != null && !ResourceUtil.isNonExistingResource(resource)) {
return ProviderOutcome.success(resource);
}
} catch (Exception e) {
return ProviderOutcome.failure(e);
}
return ProviderOutcome.failure();
}
Aggregations