Java Code Examples for org.apache.sling.api.resource.Resource#getChildren()

The following examples show how to use org.apache.sling.api.resource.Resource#getChildren() . 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: RatingsServiceImpl.java    From sling-samples with Apache License 2.0 7 votes vote down vote up
/**
 * @see org.apache.sling.sample.slingshot.ratings.RatingsService#getRating(org.apache.sling.api.resource.Resource)
 */
@Override
public double getRating(final Resource resource) {
    final String fullPath = getRatingsResourcePath(resource);
    float rating = 0;
    if ( fullPath != null ) {
        final Resource ratingsResource = resource.getChild(fullPath);
        if ( ratingsResource != null ) {
            int count = 0;
            for(final Resource r : ratingsResource.getChildren()) {
                final ValueMap vm = r.getValueMap();
                final double current = vm.get(RatingsUtil.PROPERTY_RATING, 0.0);
                rating += current;
                count++;
            }
            if ( count > 0 ) {
                rating = rating / count;
            }
        }
    }
    return rating;
}
 
Example 2
Source File: IsProductDetailPageServlet.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
static boolean containsComponent(String resourceType, Resource resource) {
    if (resource == null)
        return false;

    if (resource.isResourceType(resourceType)) {
        return true;
    }

    for (Resource child : resource.getChildren()) {
        boolean val = containsComponent(resourceType, child);
        if (val) {
            return true;
        }
    }

    return false;
}
 
Example 3
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 4
Source File: CommerceTeaserImpl.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
void populateActions() {
    Resource actionsNode = resource.getChild(CommerceTeaser.NN_ACTIONS);
    if (actionsNode != null) {
        for (Resource action : actionsNode.getChildren()) {

            ValueMap properties = action.getValueMap();
            String title = properties.get(PN_ACTION_TEXT, String.class);
            String productSlug = properties.get(PN_ACTION_PRODUCT_SLUG, String.class);
            String categoryId = properties.get(PN_ACTION_CATEGORY_ID, String.class);
            String selector = null;
            Page page;
            boolean isProduct = false;

            if (categoryId != null) {
                page = categoryPage;
                selector = categoryId;
            } else if (productSlug != null) {
                page = productPage;
                selector = productSlug;
                isProduct = true;
            } else {
                page = currentPage;

            }
            actions.add(new CommerceTeaserActionItem(title, selector, page, request, urlProvider, isProduct));
        }
    }
}
 
Example 5
Source File: SubjectLettersEntropy.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
private void iterate(Resource r, int lvl) {
	for (Resource child : r.getChildren()) {
		if (lvl == 0) {
			countLetters(child);
		} else {
			iterate(child, lvl-1);
		}
	}
}
 
Example 6
Source File: MessageStoreImplRepositoryTestUtil.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
static void assertLayer(Resource root, List<String> types, int depth) {
    for (Resource child : root.getChildren()) {
        final ModifiableValueMap m = child.adaptTo(ModifiableValueMap.class);
        if (m.keySet().contains(MessageStoreImplRepositoryTest.TEST_RT_KEY)) {
            String type = m.get(MessageStoreImplRepositoryTest.TEST_RT_KEY, String.class);
            assertEquals(String.format("Expecting %s to have %s type", child.getPath(), types.get(depth)), types.get(depth), type);
        }
        if (child.getChildren().iterator().hasNext()) {
            assertLayer(child, types, depth+1);
        }
    }

}
 
Example 7
Source File: MessageStoreImplAttachmentsTest.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
private void assertSaveMessageWithAttachments(Message msg, int num) throws IOException {
    store.save(msg);

    List<BodyPart> attList = new LinkedList<BodyPart>();
    MessageStoreImpl.recursiveMultipartProcessing((Multipart) msg.getBody(), new StringBuilder(), new StringBuilder(), false, attList); 
    @SuppressWarnings("unchecked")
    Queue<BodyPart> attachmentsMsg = (Queue<BodyPart>) attList;
    assertTrue("No attachments found", attachmentsMsg.size() > 0);
    assertEquals("", num, attachmentsMsg.size());
    
    final Resource r = resolver.getResource(getResourcePath(msg, store));
    assertNotNull("Expecting non-null Resource", r);
    for (Resource aRes : r.getChildren()) {
        final ModifiableValueMap aMap = aRes.adaptTo(ModifiableValueMap.class);
        BodyPart aMsg = attachmentsMsg.poll();
        assertNotNull("JCR contains more attachments", aMsg);

        for (Field f : aMsg.getHeader().getFields()) {
            String name = f.getName();
            assertEquals("Field "+name+" is different", (aMap.get(name, String.class)), f.getBody());
        }
        
        if (aMsg.getBody() instanceof TextBody) {
            assertEquals("Content is not the same", MessageStoreImpl.getTextPart(aMsg), aMap.get(CONTENT, String.class));
        } else if (aMsg.getBody() instanceof BinaryBody) {
            assertEquals("Content is not the same", getBinPart(aMsg), aMap.get(CONTENT, String.class));
        } else {
            fail("Unknown type of attachment body");
        }
    }
    assertEquals("Message contains more attachments", attachmentsMsg.poll(), null);
}
 
Example 8
Source File: ScriptsDatasourceServlet.java    From APM with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
  String path = request.getRequestPathInfo().getSuffix();
  List<Resource> scripts = new ArrayList<>();
  Resource resource = request.getResourceResolver().getResource(path);
  for (Resource child : resource.getChildren()) {
    if (ScriptsRowModel.isFolder(child) || ScriptModel.isScript(child)) {
      scripts.add(new ResourceTypeWrapper(child));
    }
  }
  DataSource dataSource = new SimpleDataSource(scripts.iterator());
  request.setAttribute(DataSource.class.getName(), dataSource);
}
 
Example 9
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 10
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 11
Source File: FeaturedCategoryListImpl.java    From aem-core-cif-components with Apache License 2.0 4 votes vote down vote up
@PostConstruct
private void initModel() {
    categoryPage = SiteNavigation.getCategoryPage(currentPage);
    if (categoryPage == null) {
        categoryPage = currentPage;
    }

    List<String> categoryIds = new ArrayList<>();
    assetOverride = new HashMap<>();

    // Iterate entries of composite multifield
    Resource items = resource.getChild(ITEMS_PROP);
    if (items != null) {
        for (Resource item : items.getChildren()) {
            ValueMap props = item.getValueMap();
            String categoryId = props.get(CATEGORY_ID_PROP, String.class);
            if (StringUtils.isEmpty(categoryId)) {
                continue;
            }
            categoryIds.add(categoryId);

            // Check if an override asset was set. If yes, store it in a map for later use.
            String assetPath = props.get(ASSET_PROP, String.class);
            if (StringUtils.isEmpty((assetPath))) {
                continue;
            }

            Resource assetResource = resource.getResourceResolver().getResource(assetPath);
            if (assetResource == null) {
                continue;
            }

            Asset overrideAsset = assetResource.adaptTo(Asset.class);
            assetOverride.put(categoryId, overrideAsset);
        }

        if (categoryIds.size() > 0) {
            MagentoGraphqlClient magentoGraphqlClient = MagentoGraphqlClient.create(resource);
            if (magentoGraphqlClient != null) {
                categoriesRetriever = new CategoriesRetriever(magentoGraphqlClient);
                categoriesRetriever.setIdentifiers(categoryIds);
            }
        }
    }
}
 
Example 12
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
}
 
Example 13
Source File: ResourceResolverImpl.java    From jackalope with Apache License 2.0 4 votes vote down vote up
@Override
public Iterable<Resource> getChildren(Resource resource) {
    return resource.getChildren();
}