Search in sources :

Example 11 with PageManager

use of com.day.cq.wcm.api.PageManager in project acs-aem-commons by Adobe-Consulting-Services.

the class DesignReferenceProvider method findReferences.

@Override
public List<Reference> findReferences(Resource resource) {
    ResourceResolver resourceResolver = resource.getResourceResolver();
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    Designer designer = resourceResolver.adaptTo(Designer.class);
    Page page = pageManager.getContainingPage(resource);
    if (page == null) {
        return Collections.emptyList();
    }
    Design design = designer.getDesign(page);
    if (design == null) {
        return Collections.emptyList();
    }
    return Collections.singletonList(new Reference(TYPE_DESIGN_PAGE, String.format("%s (Design)", design.getId()), design.getContentResource(), getLastModified(design)));
}
Also used : Design(com.day.cq.wcm.api.designer.Design) Designer(com.day.cq.wcm.api.designer.Designer) PageManager(com.day.cq.wcm.api.PageManager) Reference(com.day.cq.wcm.api.reference.Reference) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Page(com.day.cq.wcm.api.Page)

Example 12 with PageManager

use of com.day.cq.wcm.api.PageManager in project acs-aem-commons by Adobe-Consulting-Services.

the class WCMViewsServlet method doGet.

@Override
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json");
    if (WCMMode.DISABLED.equals(WCMMode.fromRequest(request))) {
        response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND);
        response.getWriter().write("");
        return;
    }
    /* Valid WCMMode */
    final PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
    final Page page = pageManager.getContainingPage(request.getResource());
    final WCMViewsResourceVisitor visitor = new WCMViewsResourceVisitor();
    visitor.accept(page.getContentResource());
    final Set<String> viewSet = new HashSet<String>(visitor.getWCMViews());
    // Get the Views provided by the Servlet
    for (final Map.Entry<String, String[]> entry : this.defaultViews.entrySet()) {
        if (StringUtils.startsWith(page.getPath(), entry.getKey())) {
            viewSet.addAll(Arrays.asList(entry.getValue()));
        }
    }
    final List<String> views = new ArrayList<String>(viewSet);
    Collections.sort(views);
    log.debug("Collected WCM Views {} for Page [ {} ]", views, page.getPath());
    final JSONArray jsonArray = new JSONArray();
    for (final String view : views) {
        final JSONObject json = new JSONObject();
        try {
            json.put("title", StringUtils.capitalize(view) + " View");
            json.put("value", view);
            jsonArray.put(json);
        } catch (JSONException e) {
            log.error("Unable to build WCM Views JSON output.", e);
        }
    }
    response.getWriter().write(jsonArray.toString());
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.apache.sling.commons.json.JSONArray) JSONException(org.apache.sling.commons.json.JSONException) Page(com.day.cq.wcm.api.Page) PageManager(com.day.cq.wcm.api.PageManager) JSONObject(org.apache.sling.commons.json.JSONObject) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 13 with PageManager

use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ClientLibrariesImplTest method testGetCategoriesWithInjectedFilter.

@Test
void testGetCategoriesWithInjectedFilter() {
    PageManager pageManager = context.resourceResolver().adaptTo(PageManager.class);
    Page page = pageManager.getPage(ROOT_PAGE);
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(ClientLibraries.OPTION_RESOURCE_TYPES, Utils.getPageResourceTypes(page, context.request(), mock(ModelFactory.class)));
    attributes.put("filter", ".*teaser.*");
    ClientLibraries clientlibs = getClientLibrariesUnderTest(ROOT_PAGE, attributes);
    Set<String> categories = new HashSet<>();
    categories.add(TEASER_CATEGORY);
    assertEquals(categories, ((ClientLibrariesImpl) clientlibs).getCategoriesFromComponents());
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) HashMap(java.util.HashMap) Page(com.day.cq.wcm.api.Page) ClientLibraries(com.adobe.cq.wcm.core.components.models.ClientLibraries) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 14 with PageManager

use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ClientLibrariesImplTest method testGetCategoriesWithLoginException.

/**
 * Same as {@link #testGetCategories()} however a login exception will occur when fetching the
 * resource resolver.
 */
@Test
void testGetCategoriesWithLoginException() throws Exception {
    PageManager pageManager = context.pageManager();
    Page page = pageManager.getPage(ROOT_PAGE);
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(ClientLibraries.OPTION_RESOURCE_TYPES, Utils.getPageResourceTypes(page, context.request(), mock(ModelFactory.class)));
    ClientLibrariesImpl clientlibs = Objects.requireNonNull((ClientLibrariesImpl) getClientLibrariesUnderTest(ROOT_PAGE, attributes));
    ResourceResolverFactory factory = mock(ResourceResolverFactory.class);
    doThrow(new LoginException()).when(factory).getServiceResourceResolver(anyMap());
    clientlibs.resolverFactory = factory;
    assertEquals(new HashSet<>(), clientlibs.getCategoriesFromComponents());
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) HashMap(java.util.HashMap) LoginException(org.apache.sling.api.resource.LoginException) Page(com.day.cq.wcm.api.Page) Test(org.junit.jupiter.api.Test)

Example 15 with PageManager

use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class SearchResultServlet method getResults.

/**
 * Gets the search results.
 *
 * @param request The search request.
 * @param searchComponent The search component.
 * @param pageManager A PageManager.
 * @return List of search results.
 */
@NotNull
private List<ListItem> getResults(@NotNull final SlingHttpServletRequest request, @NotNull final Search searchComponent, @NotNull final PageManager pageManager) {
    List<ListItem> results = new ArrayList<>();
    String fulltext = request.getParameter(PARAM_FULLTEXT);
    if (fulltext == null || fulltext.length() < searchComponent.getSearchTermMinimumLength()) {
        return results;
    }
    long resultsOffset = Optional.ofNullable(request.getParameter(PARAM_RESULTS_OFFSET)).map(Long::parseLong).orElse(0L);
    Map<String, String> predicatesMap = new HashMap<>();
    predicatesMap.put(FulltextPredicateEvaluator.FULLTEXT, fulltext);
    predicatesMap.put(PathPredicateEvaluator.PATH, searchComponent.getSearchRootPagePath());
    predicatesMap.put(TypePredicateEvaluator.TYPE, NameConstants.NT_PAGE);
    PredicateGroup predicates = PredicateConverter.createPredicates(predicatesMap);
    ResourceResolver resourceResolver = request.getResource().getResourceResolver();
    Query query = queryBuilder.createQuery(predicates, resourceResolver.adaptTo(Session.class));
    if (searchComponent.getResultsSize() != 0) {
        query.setHitsPerPage(searchComponent.getResultsSize());
    }
    if (resultsOffset != 0) {
        query.setStart(resultsOffset);
    }
    SearchResult searchResult = query.getResult();
    LinkHandler linkHandler = request.adaptTo(LinkHandler.class);
    // Query builder has a leaking resource resolver, so the following work around is required.
    ResourceResolver leakingResourceResolver = null;
    try {
        Iterator<Resource> resourceIterator = searchResult.getResources();
        while (resourceIterator.hasNext()) {
            Resource resource = resourceIterator.next();
            // Get a reference to QB's leaking resource resolver
            if (leakingResourceResolver == null) {
                leakingResourceResolver = resource.getResourceResolver();
            }
            Optional.of(resource).map(res -> resourceResolver.getResource(res.getPath())).map(pageManager::getContainingPage).map(page -> new PageListItemImpl(linkHandler, page, searchComponent.getId(), null)).ifPresent(results::add);
        }
    } finally {
        if (leakingResourceResolver != null) {
            leakingResourceResolver.close();
        }
    }
    return results;
}
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) Query(com.day.cq.search.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) SearchResult(com.day.cq.search.result.SearchResult) PredicateGroup(com.day.cq.search.PredicateGroup) PageListItemImpl(com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) Session(javax.jcr.Session) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PageManager (com.day.cq.wcm.api.PageManager)36 Page (com.day.cq.wcm.api.Page)31 Resource (org.apache.sling.api.resource.Resource)23 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)14 ArrayList (java.util.ArrayList)10 ValueMap (org.apache.sling.api.resource.ValueMap)8 HashMap (java.util.HashMap)7 Map (java.util.Map)5 Asset (com.day.cq.dam.api.Asset)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Pattern (java.util.regex.Pattern)3 PostConstruct (javax.annotation.PostConstruct)3 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)2 Image (com.adobe.cq.wcm.core.components.models.Image)2 Rendition (com.day.cq.dam.api.Rendition)2 ReplicationException (com.day.cq.replication.ReplicationException)2 NameConstants (com.day.cq.wcm.api.NameConstants)2 Template (com.day.cq.wcm.api.Template)2 Reference (com.day.cq.wcm.api.reference.Reference)2