use of com.day.cq.wcm.api.Page in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class CoreComponentsMockTest method testLinkHandling.
/**
* Tests a component with link handling that depends on the presence of a path processor implementation.
*/
@Test
void testLinkHandling() {
Page page = context.create().page("/content/test");
Resource resource = context.create().resource(page, "title", "sling:resourceType", "core/wcm/components/title/v3/title", "linkURL", "https://example.com");
context.currentResource(resource);
Title title = context.request().adaptTo(Title.class);
assertNotNull(title, "Unable instantiate title component");
assertNotNull(title.getLink(), "Missing link");
assertEquals("https://example.com", title.getLink().getURL());
}
use of com.day.cq.wcm.api.Page 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;
}
use of com.day.cq.wcm.api.Page in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class LanguageNavigationImpl method getLocalizedPage.
private Page getLocalizedPage(Page page, Page languageRoot) {
Page localizedPage;
String path = languageRoot.getPath();
String relativePath = page.getPath();
if (relativePath.startsWith(path)) {
localizedPage = page;
} else {
String separator = "/";
int i = relativePath.indexOf(separator);
int occurrence = StringUtils.countMatches(path, separator) + 1;
while (--occurrence > 0 && i != -1) {
i = relativePath.indexOf(separator, i + 1);
}
relativePath = (i > 0) ? relativePath.substring(i) : "";
path = path.concat(relativePath);
PageManager pageManager = page.getPageManager();
localizedPage = pageManager.getPage(path);
}
return localizedPage;
}
use of com.day.cq.wcm.api.Page in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ListImpl method getSearchListItems.
/**
* Get the list items if using the search source type.
*
* @return The search list items.
*/
private Stream<Page> getSearchListItems() {
Optional<Page> searchRoot = getRootPage(PN_SEARCH_IN);
String query = properties.get(PN_SEARCH_QUERY, String.class);
if (!StringUtils.isBlank(query) && searchRoot.isPresent()) {
SimpleSearch search = resource.adaptTo(SimpleSearch.class);
if (search != null) {
search.setQuery(query);
search.setSearchIn(searchRoot.get().getPath());
search.addPredicate(new Predicate("type", "type").set("type", NameConstants.NT_PAGE));
int limit = properties.get(PN_SEARCH_LIMIT, SEARCH_LIMIT_DEFAULT);
search.setHitsPerPage(limit);
return safeGetSearchResult(search).map(SearchResult::getResources).map(it -> (Iterable<Resource>) () -> it).map(it -> StreamSupport.stream(it.spliterator(), false)).orElseGet(Stream::empty).filter(Objects::nonNull).map(currentPage.getPageManager()::getContainingPage).filter(Objects::nonNull);
}
}
return Stream.empty();
}
use of com.day.cq.wcm.api.Page in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class RedirectItemImpl method getRedirectPage.
private Page getRedirectPage(@NotNull SlingHttpServletRequest request) {
Page page = null;
ResourceResolver resourceResolver = request.getResourceResolver();
Resource targetResource = resourceResolver.getResource(redirectTarget);
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
if (pageManager != null && targetResource != null) {
page = pageManager.getContainingPage(targetResource);
}
return page;
}
Aggregations