Search code examples
aemjcrsling

CQ - Check whether the resource object is valid


I need to check whether the resource object is valid or not for the below 'resource' object. For example If I pass any url like getResource("some path which is not available in cq") in this case I need to restrict it

Resource resource= resourceResolver.getResource(/content/rc/test/jcr:content");
Node node = resource.adaptTo(Node.class);
String parentPagePath= node.getProperty("someproperty").getValue().getString();

Is there any way to do?


Solution

  • If you are using getResource a null check is sufficient. If you use resolve, then you have to use the !ResourceUtil.isNonExistingResource(resource). On Node level you can check the existence of a property with hasProperty.