Java Code Examples for com.day.cq.wcm.api.Page#getContentResource()

The following examples show how to use com.day.cq.wcm.api.Page#getContentResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CatalogSearchSupport.java    From commerce-cif-connector with Apache License 2.0 7 votes vote down vote up
/**
 * Searches for the {@link #PN_CATALOG_PATH} property at the specified path or its parent pages.
 *
 * @param path a path in the content repository
 * @return the value of the {@link #PN_CATALOG_PATH} property or {@code null} if not found
 */
public String findCatalogPath(String path) {
    if (StringUtils.isBlank(path)) {
        return null;
    }
    Page parentPage = resolver.adaptTo(PageManager.class).getContainingPage(path);
    if (parentPage == null) {
        return null;
    }

    ConfigurationBuilder configBuilder = parentPage.getContentResource().adaptTo(ConfigurationBuilder.class);
    if (configBuilder != null) {
        ValueMap properties = configBuilder.name(CLOUDCONFIGS_BUCKET_NAME + "/" + COMMERCE_CONFIG_NAME).asValueMap();
        if (properties.containsKey(PN_CATALOG_PATH)) {
            return properties.get(PN_CATALOG_PATH, String.class);
        }
    }

    InheritanceValueMap inheritedProperties = new HierarchyNodeInheritanceValueMap(parentPage.getContentResource());
    return inheritedProperties.getInherited(PN_CATALOG_PATH, String.class);
}
 
Example 2
Source File: SiteNavigation.java    From aem-core-cif-components with Apache License 2.0 7 votes vote down vote up
/**
 * Retrieves a generic page based on a page or page ancestors using the page path configured
 * via the property of the root page.
 *
 * @param pageTypeProperty The name of the JCR property on the root page that points to the generic page
 * @param page the page for looking up the property
 * @return the generic page
 */
@Nullable
private static Page getGenericPage(String pageTypeProperty, Page page) {
    final InheritanceValueMap properties = new HierarchyNodeInheritanceValueMap(page.getContentResource());
    String utilityPagePath = properties.getInherited(pageTypeProperty, String.class);
    if (StringUtils.isBlank(utilityPagePath)) {
        LOGGER.warn("Page property {} not found at {}", pageTypeProperty, page.getPath());
        return null;
    }

    PageManager pageManager = page.getPageManager();
    Page categoryPage = pageManager.getPage(utilityPagePath);
    if (categoryPage == null) {
        LOGGER.warn("No page found at {}", utilityPagePath);
        return null;
    }
    return categoryPage;
}
 
Example 3
Source File: AEMDataLayerConfig.java    From AEM-DataLayer with Apache License 2.0 7 votes vote down vote up
/**
 * Retrieves the DataLayer config for the page or none if it is not
 * configured for that page or any inherited page.
 * 
 * @param page
 *            the page for which to get the data layer configuration
 * @return the DataLayer configuration
 */
public static AEMDataLayerConfig getDataLayerConfig(Page page) {
	if (page != null) {
		log.trace("Finding Digital Data config for {}", page.getPath());
		InheritanceValueMap properties = new HierarchyNodeInheritanceValueMap(page.getContentResource());
		String[] cloudServices = properties.getInherited(DataLayerConstants.PN_CLOUD_SERVICE_CONFIGS, new String[0]);
		for (String cloudService : cloudServices) {
			if (cloudService.startsWith(DataLayerConstants.AEM_DATALAYER_CONFIG_PATH)) {
				Page cloudServicePage = page.getPageManager().getContainingPage(cloudService);
				if (cloudServicePage != null) {
					return cloudServicePage.getContentResource().adaptTo(AEMDataLayerConfig.class);
				} else {
					log.warn("DataLayer cloud service not found at {}", cloudService);
				}
			}
		}
		log.debug("No Digital Data config found for {}", page.getPath());
	}
	return null;

}
 
Example 4
Source File: IsProductDetailPageServlet.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
private boolean isProductDetailPage(String path, ResourceResolver resourceResolver) {
    if (StringUtils.isBlank(path)) {
        return false;
    }

    Resource resource = resourceResolver.resolve(path);
    if (resource instanceof NonExistingResource) {
        return false;
    }

    Page page = resourceResolver.adaptTo(PageManager.class).getPage(resource.getPath());
    if (page == null) {
        return false;
    }

    Resource pageContent = page.getContentResource();
    if (pageContent == null) {
        return false;
    }

    boolean val = containsComponent(PRODUCT_RT, pageContent);

    return val;
}
 
Example 5
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 6 votes vote down vote up
private void correctlyNullCheckedContentResource(Resource resource1, Resource resource2) {
  Page page1 = resource1.adaptTo(Page.class);
  Page page2 = resource2.adaptTo(Page.class);
  Resource contentResource1 = page1.getContentResource();
  Resource contentResource2 = page2.getContentResource();
  Iterable<Resource> children = null;
  if (contentResource1 != null) {
    children = contentResource1.getChildren();
    if (contentResource2 != null) {
      children = contentResource1.getChildren();
    }
    children = contentResource1.getChildren();
  } else if (contentResource2 != null) {
    children = contentResource2.getChildren();
  }
}
 
Example 6
Source File: GraphQLCategoryProviderTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetChildCategories() throws IOException {
    Page page = spy(context.currentPage("/content/pageA"));
    Resource pageContent = spy(page.getContentResource());
    when(page.getContentResource()).thenReturn(pageContent);

    when(pageContent.adaptTo(GraphqlClient.class)).thenReturn(graphqlClient);
    when(pageContent.adaptTo(ComponentsConfiguration.class)).thenReturn(MOCK_CONFIGURATION_OBJECT);

    GraphQLCategoryProvider categoryProvider = new GraphQLCategoryProvider(page
        .getContentResource(), null);

    // Test null categoryId
    Assert.assertTrue(categoryProvider.getChildCategories(null, 5).isEmpty());

    // Test category children found
    List<CategoryTree> categories = categoryProvider.getChildCategories(2, 5);
    Assert.assertEquals(6, categories.size());
}
 
Example 7
Source File: GraphQLCategoryProviderTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
@Test
public void testCategoryNotOrNoChildren() throws IOException {
    Page page = mock(Page.class);
    Resource pageContent = mock(Resource.class);
    when(page.getContentResource()).thenReturn(pageContent);
    GraphQLCategoryProvider categoryProvider = new GraphQLCategoryProvider(page
        .getContentResource(), null);
    MagentoGraphqlClient graphqlClient = mock(MagentoGraphqlClient.class);
    Whitebox.setInternalState(categoryProvider, "magentoGraphqlClient", graphqlClient);

    // test category not found
    GraphqlResponse<Query, Error> response = mock(GraphqlResponse.class);
    when(graphqlClient.execute(anyString())).thenReturn(response);
    Query rootQuery = mock(Query.class);
    when(response.getData()).thenReturn(rootQuery);
    Assert.assertNull(rootQuery.getCategory());
    Assert.assertTrue(categoryProvider.getChildCategories(-10, 10).isEmpty());

    // test category children not found
    CategoryTree category = mock(CategoryTree.class);
    when(rootQuery.getCategory()).thenReturn(category);
    when(category.getChildren()).thenReturn(null);
    Assert.assertNull(category.getChildren());
    Assert.assertTrue(categoryProvider.getChildCategories(13, 10).isEmpty());
}
 
Example 8
Source File: IsProductListPageServlet.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
private boolean isProductListPage(String path, ResourceResolver resourceResolver) {
    if (StringUtils.isBlank(path)) {
        return false;
    }

    Resource resource = resourceResolver.resolve(path);
    if (resource instanceof NonExistingResource) {
        return false;
    }

    Page page = resourceResolver.adaptTo(PageManager.class).getPage(resource.getPath());
    if (page == null) {
        return false;
    }

    Resource pageContent = page.getContentResource();
    if (pageContent == null) {
        return false;
    }

    boolean val = containsComponent(PRODUCT_LIST_RT, pageContent);

    return val;
}
 
Example 9
Source File: GraphQLCategoryProviderTest.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingMagentoGraphqlClient() throws IOException {
    Page page = Mockito.spy(context.currentPage("/content/pageA"));
    GraphQLCategoryProvider categoryProvider = new GraphQLCategoryProvider(page
        .getContentResource(), null);
    Assert.assertNull(Whitebox.getInternalState(categoryProvider, "magentoGraphqlClient"));
    Assert.assertTrue(categoryProvider.getChildCategories(10, 10).isEmpty());
}
 
Example 10
Source File: NavigationImpl.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
private Integer readPageConfiguration(Page page, String propertyName) {
    InheritanceValueMap properties = new HierarchyNodeInheritanceValueMap(page.getContentResource());
    return properties.getInherited(propertyName, Integer.class);
}
 
Example 11
Source File: NavigationImpl.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
private boolean isCatalogPage(Page page) {
    if (page == null) {
        return false;
    }

    Resource contentResource = page.getContentResource();
    if (contentResource == null) {
        return false;
    }

    return contentResource.isResourceType(RT_CATALOG_PAGE);
}
 
Example 12
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void notSafeUseOfResourceAfterIfWithNullCheck(Resource resource) {
  Page page = resource.adaptTo(Page.class);
  Resource contentResource = page.getContentResource("test");
  if (contentResource != null) {
    // do something
  }
  Iterable<Resource> children = contentResource.getChildren(); // Noncompliant
}
 
Example 13
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void nullCheckUsedOnDifferentResourceThanCheckedInIfStatement(Resource resource1,
    Resource resource2) {
  Page page1 = resource1.adaptTo(Page.class);
  Page page2 = resource2.adaptTo(Page.class);
  Resource contentResource1 = page1.getContentResource();
  Resource contentResource2 = page2.getContentResource("test");
  if (contentResource1 != null) {
    Iterable<Resource> children = contentResource2.getChildren(); // Noncompliant
  }
}
 
Example 14
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private boolean contentResourceNullCheckWithImmediateReturn(Resource resource) {
  Page page = resource.adaptTo(Page.class);
  Resource contentResource = page.getContentResource();
  if (contentResource == null) {
    return false;
  }
  contentResource.getValueMap();
  return true;
}
 
Example 15
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private ValueMap checkForNullWithObjectsNonNull(Resource resource) {
  ValueMap result = new ValueMapDecorator(new HashMap<>());
  Page page = resource.adaptTo(Page.class);
  Resource pageResource = page.getContentResource("test");
  if (Objects.nonNull(pageResource)) {
    result = pageResource.getValueMap();
  }
  return result;
}
 
Example 16
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private ValueMap checkForNullWithObjectsIsNull(Resource resource) {
  ValueMap result = new ValueMapDecorator(new HashMap<>());
  Page page = resource.adaptTo(Page.class);
  Resource pageResource = page.getContentResource("test");
  if (Objects.isNull(pageResource)) {
    return result;
  } else {
    return pageResource.getValueMap();
  }
}
 
Example 17
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private ValueMap checkForNullWithCommonsObjectUtilsAllNotNull(Resource resource) {
  ValueMap result = new ValueMapDecorator(new HashMap<>());
  Page page = resource.adaptTo(Page.class);
  Resource pageResource = page.getContentResource("test");
  if (ObjectUtils.allNotNull(pageResource)) {
    result = pageResource.getValueMap();
  }
  return result;
}
 
Example 18
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private ValueMap checkForNonNullWithCommonsObjectUtilsAllNotNullMultipleResources(Resource resource) {
  ValueMap result = new ValueMapDecorator(new HashMap<>());
  Page page = resource.adaptTo(Page.class);
  Resource pageResource = page.getContentResource("test");
  Resource pageResource2 = page.getContentResource("test");
  Resource pageResource3 = page.getContentResource("test");
  if (ObjectUtils.allNotNull(pageResource, pageResource2)) {
    result = pageResource.getValueMap();
    result = pageResource2.getValueMap();
    result = pageResource3.getValueMap(); // Noncompliant
  }
  return result;
}
 
Example 19
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private void contentResourceNotNullCheckedBeforeUsage(Resource resource) {
  Page page = resource.adaptTo(Page.class);
  Resource contentResourceA = page.getContentResource();
  Iterable<Resource> children = contentResourceA.getChildren(); // Noncompliant
}