Search in sources :

Example 1 with SlingScriptHelper

use of org.apache.sling.api.scripting.SlingScriptHelper 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 2 with SlingScriptHelper

use of org.apache.sling.api.scripting.SlingScriptHelper 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 3 with SlingScriptHelper

use of org.apache.sling.api.scripting.SlingScriptHelper 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 4 with SlingScriptHelper

use of org.apache.sling.api.scripting.SlingScriptHelper 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 5 with SlingScriptHelper

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

SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)26 SlingBindings (org.apache.sling.api.scripting.SlingBindings)15 Resource (org.apache.sling.api.resource.Resource)12 Bindings (javax.script.Bindings)10 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)9 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)8 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)5 ScriptException (javax.script.ScriptException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 Servlet (javax.servlet.Servlet)3 ServletResolver (org.apache.sling.api.servlets.ServletResolver)3 SightlyException (org.apache.sling.scripting.sightly.SightlyException)3 Test (org.junit.Test)3 BufferedReader (java.io.BufferedReader)2 Reader (java.io.Reader)2 Locale (java.util.Locale)2 JspException (javax.servlet.jsp.JspException)2 BodyContent (javax.servlet.jsp.tagext.BodyContent)2