Search in sources :

Example 96 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class TestGetResourceTag method init.

/**
	 * Initializes the fields for this test.
	 */
@SuppressWarnings("serial")
@Before
public void init() {
    log.info("init");
    final MockResourceResolver resolver = new MockResourceResolver();
    resource = new MockResource(resolver, TEST_ABSOLUTE_PATH, "test");
    resolver.addResource(resource);
    MockResource child = new MockResource(resolver, TEST_ABSOLUTE_PATH + "/" + TEST_RELATIVE_PATH, "test");
    resolver.addResource(child);
    getResourceTag = new GetResourceTag() {

        protected ResourceResolver getResourceResolver() {
            return resolver;
        }
    };
    pageContext = new MockPageContext();
    getResourceTag.setPageContext(pageContext);
    log.info("init Complete");
}
Also used : MockResource(org.apache.sling.commons.testing.sling.MockResource) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) Before(org.junit.Before)

Example 97 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ScriptingResourceResolverTest method testClone.

@Test
public void testClone() throws Exception {
    final Map<String, Object> authenticationInfo = mock(Map.class);
    final ResourceResolver result = scriptingResourceResolver.clone(authenticationInfo);
    assertTrue(result instanceof ScriptingResourceResolver);
    final ScriptingResourceResolver resultS = (ScriptingResourceResolver) result;
    assertArrayEquals(searchPaths, resultS.getSearchPath());
    verify(delegateClone).getSearchPath();
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 98 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class JspScriptEngineFactory method callErrorPageJsp.

/**
     * Call the error page
     * @param bindings The bindings
     * @param scriptHelper Script helper service
     * @param context The script context
     * @param scriptName The name of the script
     */
private void callErrorPageJsp(final Bindings bindings, final SlingScriptHelper scriptHelper, final ScriptContext context, final String scriptName) {
    final SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    ResourceResolver resolver = (ResourceResolver) context.getAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, SlingScriptConstants.SLING_SCOPE);
    if (resolver == null) {
        resolver = scriptHelper.getScript().getScriptResource().getResourceResolver();
    }
    final SlingIOProvider io = this.ioProvider;
    final JspFactoryHandler jspfh = this.jspFactoryHandler;
    // abort if JSP Support is shut down concurrently (SLING-2704)
    if (io == null || jspfh == null) {
        logger.warn("callJsp: JSP Script Engine seems to be shut down concurrently; not calling {}", scriptHelper.getScript().getScriptResource().getPath());
        return;
    }
    final ResourceResolver oldResolver = io.setRequestResourceResolver(resolver);
    jspfh.incUsage();
    try {
        final JspServletWrapper errorJsp = getJspWrapper(scriptName, slingBindings);
        errorJsp.service(slingBindings);
        // The error page could be inside an include.
        final SlingHttpServletRequest request = slingBindings.getRequest();
        final Throwable t = (Throwable) request.getAttribute("javax.servlet.jsp.jspException");
        final Object newException = request.getAttribute("javax.servlet.error.exception");
        // t==null means the attribute was not set.
        if ((newException != null) && (newException == t)) {
            request.removeAttribute("javax.servlet.error.exception");
        }
        // now clear the error code - to prevent double handling.
        request.removeAttribute("javax.servlet.error.status_code");
        request.removeAttribute("javax.servlet.error.request_uri");
        request.removeAttribute("javax.servlet.error.status_code");
        request.removeAttribute("javax.servlet.jsp.jspException");
    } finally {
        jspfh.decUsage();
        io.resetRequestResourceResolver(oldResolver);
    }
}
Also used : JspServletWrapper(org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) JspFactoryHandler(org.apache.sling.scripting.jsp.jasper.compiler.JspRuntimeContext.JspFactoryHandler) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 99 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class JspScriptEngineFactory method callJsp.

/**
     * Call a JSP script
     * @param bindings The bindings
     * @param scriptHelper Script helper service
     * @param context The script context
     * @throws SlingServletException
     * @throws SlingIOException
     */
private void callJsp(final Bindings bindings, final SlingScriptHelper scriptHelper, final ScriptContext context) {
    ResourceResolver resolver = (ResourceResolver) context.getAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, SlingScriptConstants.SLING_SCOPE);
    if (resolver == null) {
        resolver = scriptHelper.getScript().getScriptResource().getResourceResolver();
    }
    final SlingIOProvider io = this.ioProvider;
    final JspFactoryHandler jspfh = this.jspFactoryHandler;
    // abort if JSP Support is shut down concurrently (SLING-2704)
    if (io == null || jspfh == null) {
        logger.warn("callJsp: JSP Script Engine seems to be shut down concurrently; not calling {}", scriptHelper.getScript().getScriptResource().getPath());
        return;
    }
    final ResourceResolver oldResolver = io.setRequestResourceResolver(resolver);
    jspfh.incUsage();
    try {
        final SlingBindings slingBindings = new SlingBindings();
        slingBindings.putAll(bindings);
        final JspServletWrapper jsp = getJspWrapper(scriptHelper, slingBindings);
        // create a SlingBindings object
        jsp.service(slingBindings);
    } finally {
        jspfh.decUsage();
        io.resetRequestResourceResolver(oldResolver);
    }
}
Also used : JspServletWrapper(org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) JspFactoryHandler(org.apache.sling.scripting.jsp.jasper.compiler.JspRuntimeContext.JspFactoryHandler) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Example 100 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class SlingIOProvider method getResourcePaths.

Set<String> getResourcePaths(final String path) {
    final Set<String> paths = new HashSet<String>();
    ResourceResolver resolver = requestResourceResolver.get();
    if (resolver != null) {
        try {
            final String cleanedPath = cleanPath(path, false);
            final boolean startsWithWebInfTags = cleanedPath.startsWith(WEB_INF_TAGS);
            Resource resource = resolver.getResource(startsWithWebInfTags ? cleanedPath.substring(WEB_INF_TAGS.length()) : cleanedPath);
            if (resource != null) {
                Iterator<Resource> entries = resolver.listChildren(resource);
                while (entries.hasNext()) {
                    final String entryPath = entries.next().getPath();
                    if (startsWithWebInfTags) {
                        paths.add(WEB_INF_TAGS + entryPath);
                    } else {
                        paths.add(entryPath);
                    }
                }
            }
        } catch (final SlingException se) {
            log.warn("getResourcePaths: Cannot list children of " + path, se);
        }
    }
    return paths.isEmpty() ? null : paths;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) SlingException(org.apache.sling.api.SlingException) HashSet(java.util.HashSet)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)339 Resource (org.apache.sling.api.resource.Resource)168 Test (org.junit.Test)131 HashMap (java.util.HashMap)65 LoginException (org.apache.sling.api.resource.LoginException)53 PersistenceException (org.apache.sling.api.resource.PersistenceException)52 Session (javax.jcr.Session)31 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)29 ValueMap (org.apache.sling.api.resource.ValueMap)27 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 ArrayList (java.util.ArrayList)23 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 Map (java.util.Map)19 Before (org.junit.Before)19 IOException (java.io.IOException)17 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)17 ChildResource (org.apache.sling.validation.model.ChildResource)17 HashSet (java.util.HashSet)16 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)15