use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class IncludeRuntimeExtension method includeScript.
private void includeScript(final Bindings bindings, String script, PrintWriter out) {
if (StringUtils.isEmpty(script)) {
throw new SightlyException("Path for data-sly-include is empty");
} else {
LOG.debug("Attempting to include script {}.", script);
SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings);
ServletResolver servletResolver = slingScriptHelper.getService(ServletResolver.class);
if (servletResolver != null) {
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
Servlet servlet = servletResolver.resolveServlet(request.getResource(), script);
if (servlet != null) {
try {
SlingHttpServletResponse response = BindingsUtils.getResponse(bindings);
PrintWriterResponseWrapper resWrapper = new PrintWriterResponseWrapper(out, response);
servlet.service(request, resWrapper);
} catch (Exception e) {
throw new SightlyException("Failed to include script " + script, e);
}
} else {
throw new SightlyException("Failed to locate script " + script);
}
} else {
throw new SightlyException("Sling ServletResolver service is unavailable, failed to include " + script);
}
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class ResourceRuntimeExtension method provideResource.
private String provideResource(final RenderContext renderContext, Object pathObj, Map<String, Object> options) {
Map<String, Object> opts = new HashMap<>(options);
final Bindings bindings = renderContext.getBindings();
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
Map originalAttributes = ExtensionUtils.setRequestAttributes(request, (Map) options.remove(OPTION_REQUEST_ATTRIBUTES));
RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
String resourceType = runtimeObjectModel.toString(getAndRemoveOption(opts, OPTION_RESOURCE_TYPE));
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
if (pathObj instanceof Resource) {
Resource includedResource = (Resource) pathObj;
Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
includeResource(bindings, printWriter, includedResource, dispatcherOptions, resourceType);
} else {
String includePath = runtimeObjectModel.toString(pathObj);
// build path completely
includePath = buildPath(includePath, options, request.getResource());
if (includePath != null) {
// check if path identifies an existing resource
Resource includedResource = request.getResourceResolver().getResource(includePath);
PathInfo pathInfo;
if (includedResource != null) {
Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
includeResource(bindings, printWriter, includedResource, dispatcherOptions, resourceType);
} else {
// analyse path and decompose potential selectors from the path
pathInfo = new PathInfo(includePath);
Map<String, String> dispatcherOptionsMap = handleSelectors(request, pathInfo.selectors, opts, runtimeObjectModel);
String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
includeResource(bindings, printWriter, pathInfo.path, dispatcherOptions, resourceType);
}
} else {
// use the current resource
Map<String, String> dispatcherOptionsMap = handleSelectors(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
String dispatcherOptions = createDispatcherOptions(dispatcherOptionsMap);
includeResource(bindings, printWriter, request.getResource(), dispatcherOptions, resourceType);
}
}
ExtensionUtils.setRequestAttributes(request, originalAttributes);
return writer.toString();
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class AbstractPostOperationTest method testNoRemainingPostfixIsSuccessful.
@Test
public void testNoRemainingPostfixIsSuccessful() {
TestingResourceResolver resourceResolver = new TestingResourceResolver();
MockSlingHttpServlet3Request request = new MockSlingHttpServlet3Request("/test", null, null, null, null);
request.setResourceResolver(resourceResolver);
final PostOperation operation = new AbstractPostOperation() {
@Override
protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws RepositoryException {
changes.add(Modification.onChange(ModificationType.CREATE, "/content/test"));
}
};
HtmlResponse response = new HtmlResponse();
operation.run(request, response, new SlingPostProcessor[0]);
assertTrue(response.isSuccessful());
assertTrue(resourceResolver.commitCalled);
assertFalse(resourceResolver.revertCalled);
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class AbstractPostOperationTest method testRemainingPostfixWithoutUnPostfixedIsSuccessful.
@Test
public void testRemainingPostfixWithoutUnPostfixedIsSuccessful() {
TestingResourceResolver resourceResolver = new TestingResourceResolver();
MockSlingHttpServlet3Request request = new MockSlingHttpServlet3Request("/test", null, null, null, null);
request.setResourceResolver(resourceResolver);
final PostOperation operation = new AbstractPostOperation() {
@Override
protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws RepositoryException {
changes.add(Modification.onChange(ModificationType.CREATE, "/content/test@Postfix"));
}
};
HtmlResponse response = new HtmlResponse();
operation.run(request, response, new SlingPostProcessor[0]);
assertTrue(response.isSuccessful());
assertTrue(resourceResolver.commitCalled);
assertFalse(resourceResolver.revertCalled);
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class UrlFilter method doFilter.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request instanceof SlingHttpServletRequest && response instanceof SlingHttpServletResponse) {
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
RequestPathInfo pathInfo = slingRequest.getRequestPathInfo();
Resource definitionResource = findUrlFilterDefinitionResource(slingRequest.getResource(), slingRequest.getResourceResolver());
if (definitionResource != null) {
logger.debug("found url filter definition resource at {}", definitionResource.getPath());
ValueMap properties = definitionResource.adaptTo(ValueMap.class);
if (properties != null) {
if (checkSelector(pathInfo, properties) && checkSuffix(pathInfo, properties) && checkExtension(pathInfo, properties)) {
logger.debug("url filter definition resource at {} passed for request {}.", definitionResource.getPath(), slingRequest.getRequestPathInfo());
} else {
logger.info("url filter definition resource at {} FAILED for request {}.", definitionResource.getPath(), slingRequest.getRequestPathInfo());
slingResponse.sendError(403);
return;
}
}
}
}
chain.doFilter(request, response);
}
Aggregations