Search in sources :

Example 16 with SyntheticResource

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);
    }
}
Also used : SightlyException(org.apache.sling.scripting.sightly.SightlyException) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 17 with SyntheticResource

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);
}
Also used : Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 18 with 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;
}
Also used : ServletException(javax.servlet.ServletException) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) IOException(java.io.IOException) CaptureResponseWrapper(org.apache.sling.scripting.core.servlet.CaptureResponseWrapper) RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

SyntheticResource (org.apache.sling.api.resource.SyntheticResource)18 Resource (org.apache.sling.api.resource.Resource)16 Test (org.junit.Test)10 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 AuthenticatedResourceProvider (org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)2 Expectations (org.jmock.Expectations)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 JspException (javax.servlet.jsp.JspException)1 BodyContent (javax.servlet.jsp.tagext.BodyContent)1 IteratorChain (org.apache.commons.collections4.iterators.IteratorChain)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1