Search in sources :

Example 56 with SlingBindings

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

the class EditorTest method getItemsEditor.

private Editor getItemsEditor(String suffix) {
    // get the carousel component node resource
    Resource resource = context.resourceResolver().getResource(CAROUSEL_PATH);
    // prepare the request object
    final MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver(), context.bundleContext());
    // set the suffix
    MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
    requestPathInfo.setSuffix(suffix);
    // define the bindings
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.put(SlingBindings.RESOURCE, resource);
    if (resource != null) {
        slingBindings.put(WCMBindings.PROPERTIES, resource.adaptTo(ValueMap.class));
    }
    slingBindings.put(WCMBindings.PAGE_MANAGER, context.pageManager());
    request.setAttribute(SlingBindings.class.getName(), slingBindings);
    // adapt to the class to test
    return request.adaptTo(Editor.class);
}
Also used : MockRequestPathInfo(org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo) SlingBindings(org.apache.sling.api.scripting.SlingBindings) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource)

Example 57 with SlingBindings

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

the class SearchResultServlet method doGet.

@Override
protected void doGet(@NotNull final SlingHttpServletRequest request, @NotNull final SlingHttpServletResponse response) throws IOException {
    Page currentPage = Optional.ofNullable(request.getResourceResolver().adaptTo(PageManager.class)).map(pm -> pm.getContainingPage(request.getResource())).orElse(null);
    if (currentPage != null) {
        SlingBindings bindings = new SlingBindings();
        bindings.setSling(new ScriptHelper(bundleContext, null, request, response));
        request.setAttribute(SlingBindings.class.getName(), bindings);
        Search searchComponent = getSearchComponent(request, currentPage);
        try {
            List<ListItem> results = getResults(request, searchComponent, currentPage.getPageManager());
            response.setContentType("application/json");
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            new ObjectMapper().writeValue(response.getWriter(), results);
        } catch (NumberFormatException e) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        }
    } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) TypePredicateEvaluator(com.day.cq.search.eval.TypePredicateEvaluator) StringUtils(org.apache.commons.lang3.StringUtils) Page(com.day.cq.wcm.api.Page) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SearchResult(com.day.cq.search.result.SearchResult) PredicateConverter(com.day.cq.search.PredicateConverter) Map(java.util.Map) PageListItemImpl(com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl) Search(com.adobe.cq.wcm.core.components.models.Search) PN_FRAGMENT_VARIATION_PATH(com.adobe.cq.wcm.core.components.models.ExperienceFragment.PN_FRAGMENT_VARIATION_PATH) PathPredicateEvaluator(com.day.cq.search.eval.PathPredicateEvaluator) Session(javax.jcr.Session) Servlet(javax.servlet.Servlet) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) PageManager(com.day.cq.wcm.api.PageManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Query(com.day.cq.search.Query) Optional(java.util.Optional) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) NotNull(org.jetbrains.annotations.NotNull) FulltextPredicateEvaluator(com.day.cq.search.eval.FulltextPredicateEvaluator) ModelFactory(org.apache.sling.models.factory.ModelFactory) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) HashMap(java.util.HashMap) LanguageManager(com.day.cq.wcm.api.LanguageManager) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) StreamSupport(java.util.stream.StreamSupport) Activate(org.osgi.service.component.annotations.Activate) SearchImpl(com.adobe.cq.wcm.core.components.internal.models.v1.SearchImpl) QueryBuilder(com.day.cq.search.QueryBuilder) Iterator(java.util.Iterator) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) LocalizationUtils(com.adobe.cq.wcm.core.components.internal.LocalizationUtils) PredicateGroup(com.day.cq.search.PredicateGroup) Template(com.day.cq.wcm.api.Template) NameConstants(com.day.cq.wcm.api.NameConstants) Reference(org.osgi.service.component.annotations.Reference) PageManager(com.day.cq.wcm.api.PageManager) SlingBindings(org.apache.sling.api.scripting.SlingBindings) Search(com.adobe.cq.wcm.core.components.models.Search) Page(com.day.cq.wcm.api.Page) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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