use of org.apache.sling.api.resource.SyntheticResource in project sling by apache.
the class ResourceRuntimeExtension method includeResource.
private void includeResource(final Bindings bindings, PrintWriter out, String path, String dispatcherOptions, String resourceType) {
if (StringUtils.isEmpty(path)) {
throw new SightlyException("Resource path cannot be empty");
} else {
SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
Resource includeRes = request.getResourceResolver().resolve(path);
if (ResourceUtil.isNonExistingResource(includeRes)) {
includeRes = new SyntheticResource(request.getResourceResolver(), path, resourceType);
}
includeResource(bindings, out, includeRes, dispatcherOptions, resourceType);
}
}
use of org.apache.sling.api.resource.SyntheticResource in project sling by apache.
the class NoResourceResolverTypeTest method testRoot.
@Test
public void testRoot() {
// resgister dummy resource provider because otherwise ResourceResolverFactory get's not activated
// with lates sling resource resolver implementation
context.registerService(ResourceProvider.class, resourceProvider, ResourceProvider.PROPERTY_ROOT, "/");
Resource root = context.resourceResolver().getResource("/");
assertTrue(root instanceof SyntheticResource);
}
use of org.apache.sling.api.resource.SyntheticResource in project sling by apache.
the class SlingIncludeAttributeTagProcessor method dispatch.
/**
* @param resource the resource to include
* @param path the path to include
* @param slingHttpServletRequest the current request
* @param slingHttpServletResponse the current response
* @param requestDispatcherOptions the options for the request dispatcher
* @return the character response from the include call to request dispatcher
* @see "org.apache.sling.scripting.jsp.taglib.IncludeTagHandler"
*/
protected String dispatch(Resource resource, String path, final SlingHttpServletRequest slingHttpServletRequest, final SlingHttpServletResponse slingHttpServletResponse, final RequestDispatcherOptions requestDispatcherOptions) {
// ensure the path (if set) is absolute and normalized
if (path != null) {
if (!path.startsWith("/")) {
path = slingHttpServletRequest.getResource().getPath() + "/" + path;
}
path = ResourceUtil.normalize(path);
}
// check the resource
if (resource == null) {
if (path == null) {
// neither resource nor path is defined, use current resource
resource = slingHttpServletRequest.getResource();
} else {
// check whether the path (would) resolve, else SyntheticRes.
final String resourceType = requestDispatcherOptions.getForceResourceType();
final Resource tmp = slingHttpServletRequest.getResourceResolver().resolve(path);
if (tmp == null && resourceType != null) {
// TODO DispatcherSyntheticResource?
resource = new SyntheticResource(slingHttpServletRequest.getResourceResolver(), path, resourceType);
// remove resource type overwrite as synthetic resource is correctly typed as requested
requestDispatcherOptions.remove(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
}
}
}
try {
// create a dispatcher for the resource or path
final RequestDispatcher dispatcher;
if (resource != null) {
dispatcher = slingHttpServletRequest.getRequestDispatcher(resource, requestDispatcherOptions);
} else {
dispatcher = slingHttpServletRequest.getRequestDispatcher(path, requestDispatcherOptions);
}
if (dispatcher != null) {
try {
final CaptureResponseWrapper wrapper = new CaptureResponseWrapper(slingHttpServletResponse);
dispatcher.include(slingHttpServletRequest, wrapper);
if (!wrapper.isBinaryResponse()) {
return wrapper.getCapturedCharacterResponse();
}
} catch (ServletException e) {
logger.error(e.getMessage(), e);
}
} else {
logger.error("no request dispatcher: unable to include {}/'{}'", resource, path);
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
}
Aggregations