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)));
}
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());
}
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());
}
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());
}
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;
}
Aggregations