Search in sources :

Example 46 with SlingBindings

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

the class CallTag method doEndTag.

@Override
public int doEndTag() throws JspException {
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
    final RequestProgressTracker tracker = TagUtil.getRequest(pageContext).getRequestProgressTracker();
    String servletName = null;
    final Servlet servlet;
    if (!ignoreComponentHierarchy) {
        final Resource resource = bindings.getResource();
        servlet = servletResolver.resolveServlet(resource, this.script);
        if (servlet != null) {
            servletName = RequestUtil.getServletName(servlet);
            tracker.log("Including script {0} for path={1}, type={2}: {3}", script, resource.getPath(), resource.getResourceType(), servletName);
        }
    } else {
        final ResourceResolver resolver = 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 : resolver.getSearchPath()) {
                if (parentPath.startsWith(sp)) {
                    parentPath = parentPath.substring(sp.length());
                    break;
                }
            }
            scriptPath = parentPath + "/" + script;
        } else {
            scriptPath = this.script;
        }
        servlet = servletResolver.resolveServlet(resolver, scriptPath);
        if (servlet != null) {
            servletName = RequestUtil.getServletName(servlet);
            tracker.log("Including script {0} (ignoring component hierarchy): {1}", script, servletName);
        }
    }
    if (servlet == null) {
        throw new JspException("Could not find script " + script);
    }
    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);
        tracker.startTimer(servletName);
        servlet.service(pageContext.getRequest(), response);
        tracker.logTimer(servletName);
        return EVAL_PAGE;
    } catch (Exception 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) 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) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker)

Example 47 with SlingBindings

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

the class GetPropertyTag 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 48 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project acs-aem-commons by Adobe-Consulting-Services.

the class IncludeDesignLibrariesTag method doEndTag.

/**
 * {@inheritDoc}
 */
@Override
public int doEndTag() throws JspException {
    final SlingHttpServletRequest request = TagUtil.getRequest(pageContext);
    final SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName());
    final DesignHtmlLibraryManager manager = bindings.getSling().getService(DesignHtmlLibraryManager.class);
    if (manager != null) {
        try {
            if (js && css) {
                manager.writeIncludes(request, getDesign(), region, pageContext.getOut());
            } else if (js) {
                manager.writeJsInclude(request, getDesign(), region, pageContext.getOut());
            } else if (css) {
                manager.writeCssInclude(request, getDesign(), region, pageContext.getOut());
            }
        } catch (IOException e) {
            throw new JspException("Unable to write client library includes", e);
        }
    }
    reset();
    return EVAL_PAGE;
}
Also used : JspException(javax.servlet.jsp.JspException) SlingBindings(org.apache.sling.api.scripting.SlingBindings) IOException(java.io.IOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 49 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project acs-aem-commons by Adobe-Consulting-Services.

the class DefineObjects method doEndTag.

@Override
public int doEndTag() {
    SlingBindings bindings = (SlingBindings) this.pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    pageContext.setAttribute("globalProperties", bindings.get("globalProperties"));
    pageContext.setAttribute("sharedProperties", bindings.get("sharedProperties"));
    pageContext.setAttribute("mergedProperties", bindings.get("mergedProperties"));
    return EVAL_PAGE;
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings)

Example 50 with SlingBindings

use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class CarouselImplTest method getCarouselUnderTest.

private Carousel getCarouselUnderTest(@NotNull final String resourcePath) {
    Utils.enableDataLayer(context, true);
    context.currentResource(Objects.requireNonNull(context.resourceResolver().getResource(resourcePath)));
    Component component = mock(Component.class);
    when(component.getResourceType()).thenReturn(CarouselImpl.RESOURCE_TYPE);
    MockSlingHttpServletRequest request = context.request();
    SlingBindings slingBindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName());
    slingBindings.put(WCMBindings.COMPONENT, component);
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
    return context.request().adaptTo(Carousel.class);
}
Also used : SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Component(com.day.cq.wcm.api.components.Component)

Aggregations

SlingBindings (org.apache.sling.api.scripting.SlingBindings)57 Resource (org.apache.sling.api.resource.Resource)26 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)19 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)12 Page (com.day.cq.wcm.api.Page)11 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)9 Before (org.junit.Before)8 Bindings (javax.script.Bindings)7 IOException (java.io.IOException)6 SimpleBindings (javax.script.SimpleBindings)6 Map (java.util.Map)5 MockRequestPathInfo (org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo)5 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)5 PrintWriter (java.io.PrintWriter)4 HashMap (java.util.HashMap)4 ScriptException (javax.script.ScriptException)4 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)4 Servlet (javax.servlet.Servlet)3 ServletException (javax.servlet.ServletException)3