Search in sources :

Example 11 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.

the class AdaptToTag method getClassLoader.

/**
	 * Method for retrieving the classloader from the OSGi console.
	 * 
	 * @return the classloader
	 */
protected ClassLoader getClassLoader() {
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final DynamicClassLoaderManager dynamicClassLoaderManager = scriptHelper.getService(DynamicClassLoaderManager.class);
    final ClassLoader classLoader = dynamicClassLoaderManager.getDynamicClassLoader();
    return classLoader;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) DynamicClassLoaderManager(org.apache.sling.commons.classloader.DynamicClassLoaderManager)

Example 12 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings 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)

Example 13 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.

the class EvalTagHandler 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("EvalTagHandler doEndTag");
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
    final Servlet servlet;
    if (!this.ignoreResourceTypeHierarchy) {
        // detecte resource
        final Resource resource;
        if (this.resource != null) {
            resource = this.resource;
        } else if (this.resourceType != null) {
            resource = new SyntheticResource(bindings.getRequest().getResourceResolver(), bindings.getResource().getPath(), this.resourceType);
        } else {
            resource = bindings.getResource();
        }
        servlet = servletResolver.resolveServlet(resource, this.script);
    } else {
        final ResourceResolver rr = bindings.getRequest().getResourceResolver();
        final String scriptPath;
        if (!script.startsWith("/")) {
            // resolve relative script
            String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
            // check if parent resides on search path
            for (String sp : rr.getSearchPath()) {
                if (parentPath.startsWith(sp)) {
                    parentPath = parentPath.substring(sp.length());
                    break;
                }
            }
            scriptPath = parentPath + '/' + script;
        } else {
            scriptPath = this.script;
        }
        servlet = servletResolver.resolveServlet(rr, scriptPath);
    }
    if (servlet == null) {
        throw new JspException("Could not find script '" + script + "' referenced in jsp " + scriptHelper.getScript().getScriptResource().getPath());
    }
    try {
        if (flush && !(pageContext.getOut() instanceof BodyContent)) {
            // might throw an IOException of course
            pageContext.getOut().flush();
        }
        // wrap the response to get the correct output order
        SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
        servlet.service(pageContext.getRequest(), response);
        return EVAL_PAGE;
    } catch (Exception e) {
        log.error("Error while executing script " + script, e);
        throw new JspException("Error while executing script " + script, e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) JspException(javax.servlet.jsp.JspException) BodyContent(javax.servlet.jsp.tagext.BodyContent) JspException(javax.servlet.jsp.JspException) ServletResolver(org.apache.sling.api.servlets.ServletResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Servlet(javax.servlet.Servlet)

Example 14 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.

the class FindResourcesTag method getResourceResolver.

/**
	 * Method for retrieving the ResourceResolver from the page context.
	 * 
	 * @return the resource resolver
	 */
protected ResourceResolver getResourceResolver() {
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ResourceResolver resolver = scriptHelper.getRequest().getResourceResolver();
    return resolver;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Example 15 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project sling by apache.

the class GetResourceTag method getResourceResolver.

/**
	 * Method for retrieving the ResourceResolver from the page context.
	 * 
	 * @return the resource resolver
	 */
protected ResourceResolver getResourceResolver() {
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ResourceResolver resolver = scriptHelper.getRequest().getResourceResolver();
    return resolver;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Aggregations

SlingBindings (org.apache.sling.api.scripting.SlingBindings)41 Resource (org.apache.sling.api.resource.Resource)14 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)10 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 Page (com.day.cq.wcm.api.Page)8 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)8 Before (org.junit.Before)8 Bindings (javax.script.Bindings)7 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)7 SimpleBindings (javax.script.SimpleBindings)6 IOException (java.io.IOException)4 PrintWriter (java.io.PrintWriter)4 ScriptException (javax.script.ScriptException)4 ServletException (javax.servlet.ServletException)3 SlingException (org.apache.sling.api.SlingException)3 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)3 SlingScript (org.apache.sling.api.scripting.SlingScript)3 Test (org.junit.Test)3 SightlyWCMMode (com.adobe.cq.sightly.SightlyWCMMode)2 Style (com.day.cq.wcm.api.designer.Style)2