private String provideResource()

in src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/ResourceRuntimeExtension.java [76:114]


    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();
        StringWriter writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        if (pathObj instanceof Resource) {
            Resource includedResource = (Resource) pathObj;
            RequestDispatcherOptions requestDispatcherOptions = handleDispatcherOptions(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
            includeResource(request, bindings, printWriter, includedResource, requestDispatcherOptions);
        } 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) {
                    RequestDispatcherOptions requestDispatcherOptions =
                            handleDispatcherOptions(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
                    includeResource(request, bindings, printWriter, includedResource, requestDispatcherOptions);
                } else {
                    // analyse path and decompose potential selectors from the path
                    pathInfo = new PathInfo(includePath);
                    RequestDispatcherOptions requestDispatcherOptions = handleDispatcherOptions(request, pathInfo.selectors, opts, runtimeObjectModel);
                    includeResource(request, bindings, printWriter, pathInfo.path, requestDispatcherOptions);
                }
            } else {
                // use the current resource
                RequestDispatcherOptions requestDispatcherOptions = handleDispatcherOptions(request, new LinkedHashSet<String>(), opts, runtimeObjectModel);
                includeResource(request, bindings, printWriter, request.getResource(), requestDispatcherOptions);
            }
        }
        ExtensionUtils.setRequestAttributes(request, originalAttributes);
        return writer.toString();
    }