Search in sources :

Example 91 with Resource

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

the class ResourceResolverImplTest method test_getResourceSuperType.

@Test
public void test_getResourceSuperType() {
    final PathBasedResourceResolverImpl resolver = getPathBasedResourceResolver();
    // the resources to test
    final Resource r = resolver.add(new SyntheticResource(resolver, "/a", "a:b"));
    final Resource r2 = resolver.add(new SyntheticResource(resolver, "/a2", "a:c"));
    resolver.add(new SyntheticResourceWithSupertype(resolver, "/a/b", "x:y", "t:c"));
    assertEquals("t:c", resolver.getParentResourceType(r.getResourceType()));
    assertNull(resolver.getParentResourceType(r2.getResourceType()));
}
Also used : NonExistingResource(org.apache.sling.api.resource.NonExistingResource) 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 92 with Resource

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

the class SlingBindingsVariablesListJsonServlet method getBindingsByEngine.

/**
     * Gets the {@link Bindings} object for the given {@link ScriptEngineFactory}.
     * It only considers the default context "request".
     *
     * @see <a href="https://issues.apache.org/jira/browse/SLING-3038">binding contexts(SLING-3083)</a>
     *
     * @param scriptEngineFactory the factory of the script engine, for which to retrieve the bindings
     * @param request the current request (necessary to create the bindings)
     * @param response the current response (necessary to create the bindings)
     * @return the bindings (list of key/value pairs) as defined by {@link Bindings} for the given script engine.
     * @throws IOException
     */
private Bindings getBindingsByEngine(ScriptEngineFactory scriptEngineFactory, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    // use default context only
    String context = SlingScriptAdapterFactory.BINDINGS_CONTEXT;
    final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProviderTracker.getBindingsValuesProviders(scriptEngineFactory, context);
    Resource invalidScriptResource = new NonExistingResource(request.getResourceResolver(), "some/invalid/scriptpath");
    DefaultSlingScript defaultSlingScript = new DefaultSlingScript(bundleContext, invalidScriptResource, scriptEngineFactory.getScriptEngine(), bindingsValuesProviders, null, null);
    // prepare the bindings (similar as in DefaultSlingScript#service)
    final SlingBindings initalBindings = new SlingBindings();
    initalBindings.setRequest(request);
    initalBindings.setResponse(response);
    final Bindings bindings = defaultSlingScript.verifySlingBindings(initalBindings);
    // only thing being added in {DefaultSlingScript#call(...)} is resource resolver
    bindings.put(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, request.getResourceResolver());
    return bindings;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings)

Example 93 with Resource

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

the class SlingScriptAdapterFactory method getAdapter.

// ---------- AdapterFactory -----------------------------------------------
@Override
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
    Resource resource = (Resource) adaptable;
    String path = resource.getPath();
    String ext = path.substring(path.lastIndexOf('.') + 1);
    ScriptEngine engine = scriptEngineManager.getEngineByExtension(ext);
    if (engine != null) {
        final Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProviderTracker.getBindingsValuesProviders(engine.getFactory(), BINDINGS_CONTEXT);
        // unchecked cast
        return (AdapterType) new DefaultSlingScript(this.bundleContext, resource, engine, bindingsValuesProviders, this.serviceCache, scriptCache);
    }
    return null;
}
Also used : BindingsValuesProvider(org.apache.sling.scripting.api.BindingsValuesProvider) Resource(org.apache.sling.api.resource.Resource) ScriptEngine(javax.script.ScriptEngine)

Example 94 with Resource

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

the class AbstractDispatcherTagHandler method doEndTag.

/**
     * Called after the body has been processed.
     *
     * @return whether additional evaluations of the body are desired
     */
public int doEndTag() throws JspException {
    log.debug("AbstractDispatcherTagHandler.doEndTag");
    final SlingHttpServletRequest request = TagUtil.getRequest(pageContext);
    // set request dispatcher options according to tag attributes. This
    // depends on the implementation, that using a "null" argument
    // has no effect
    final RequestDispatcherOptions opts = new RequestDispatcherOptions();
    opts.setForceResourceType(resourceType);
    opts.setReplaceSelectors(replaceSelectors);
    opts.setAddSelectors(addSelectors);
    opts.setReplaceSuffix(replaceSuffix);
    // ensure the path (if set) is absolute and normalized
    if (path != null) {
        if (!path.startsWith("/")) {
            path = request.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 = request.getResource();
        } else {
            // check whether the path (would) resolve, else SyntheticRes.
            Resource tmp = request.getResourceResolver().resolve(path);
            if (tmp == null && resourceType != null) {
                resource = new DispatcherSyntheticResource(request.getResourceResolver(), path, resourceType);
                // remove resource type overwrite as synthetic resource
                // is correctly typed as requested
                opts.remove(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
            }
        }
    }
    try {
        // create a dispatcher for the resource or path
        RequestDispatcher dispatcher;
        if (resource != null) {
            dispatcher = request.getRequestDispatcher(resource, opts);
        } else {
            dispatcher = request.getRequestDispatcher(path, opts);
        }
        if (dispatcher != null) {
            SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
            dispatch(dispatcher, request, response);
        } else {
            TagUtil.log(log, pageContext, "No content to include...", null);
        }
    } catch (final JspTagException jte) {
        throw jte;
    } catch (final IOException ioe) {
        throw new JspTagException(ioe);
    } catch (final ServletException ce) {
        throw new JspTagException(TagUtil.getRootCause(ce));
    }
    return EVAL_PAGE;
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletException(javax.servlet.ServletException) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) IOException(java.io.IOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) JspTagException(javax.servlet.jsp.JspTagException) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 95 with Resource

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

the class DefineObjectsTag method doEndTag.

/**
     * Creates Scripting variables for:
     * <ul>
     * <li><code>SlingHttpServletRequest</code>
     * <li><code>SlingHttpServletResponse</code>
     * <li>current <code>Resource</code>
     * <li>current <code>Node</code> (if resource is adaptable to a node)
     * <li>current <code>Logger</code>
     * <li>current <code>SlingScriptHelper</code>
     * </ul>
     *
     * @return always {@link #EVAL_PAGE}.
     */
public int doEndTag() {
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    pageContext.setAttribute(requestName, scriptHelper.getRequest());
    pageContext.setAttribute(responseName, scriptHelper.getResponse());
    final Resource resource = scriptHelper.getRequest().getResource();
    pageContext.setAttribute(resourceName, resource);
    pageContext.setAttribute(resourceResolverName, scriptHelper.getRequest().getResourceResolver());
    pageContext.setAttribute(slingName, scriptHelper);
    pageContext.setAttribute(logName, bindings.getLog());
    pageContext.setAttribute(bindingsName, bindings);
    if (JCR_NODE_CLASS != null) {
        final Object node = resource.adaptTo(JCR_NODE_CLASS);
        if (node != null) {
            pageContext.setAttribute(nodeName, node);
        }
    }
    return EVAL_PAGE;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1151 Test (org.junit.Test)633 ValueMap (org.apache.sling.api.resource.ValueMap)263 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)164 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)159 Node (javax.jcr.Node)116 HashMap (java.util.HashMap)104 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)99 PersistenceException (org.apache.sling.api.resource.PersistenceException)94 ArrayList (java.util.ArrayList)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)60 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)59 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)55 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)43 Map (java.util.Map)41 InputStream (java.io.InputStream)38 LoginException (org.apache.sling.api.resource.LoginException)37 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)36 Iterator (java.util.Iterator)33