Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

get resource from session

Avatar

Level 4

i made a connection to repository using JCRUtils and got session. Next, I need the resource but, when I am trying to fetch resource using resourceResolver, I am getting null. Whereas, if I use session.getNode, i am getting the node (not null). I also tried to use AdaptManager to convert the node to resource, but I am getting null in this case also.

How do I get the resource from the node? or How do I fetch resource directly from session?

[I need to use valueMap, thus I need resource]

1 Accepted Solution

Avatar

Correct answer by
Employee

final ResourceResolver resource_resolver = request.getResourceResolver();

Iterator<Resource> some_it = resource_resolver.findResources("select * from [dam:Asset] as asset", "JCR-SQL2");

some_it.forEachRemaining(object_from_iterator -> {

        Node jcr_node = object_from_iterator.adaptTo(Node.class);

        try {

              String someProperty = jcr_node.getProperty("some:Property").getString();

              jcr_node.getSession().getWorkspace().getObservationManager().setUserData("changedByWorkflowProcess");

              jcr_node.setProperty("some:Property", "some new value");

              logger.info("blah blah made some changes");

              jcr_node.getSession().save();

         }

        catch { ... }

        finally { ... }

}

View solution in original post

9 Replies

Avatar

Level 2

Hi, Are you trying to get ValueMap to fetch properties?  if so you can use session.getNode("path to the node you wanna access").getProperties();

Avatar

Level 2

When you try to get resource this way (adapterManager.getAdapter(nodeObj, Resource.class)) are getting null ?

Avatar

Correct answer by
Employee

final ResourceResolver resource_resolver = request.getResourceResolver();

Iterator<Resource> some_it = resource_resolver.findResources("select * from [dam:Asset] as asset", "JCR-SQL2");

some_it.forEachRemaining(object_from_iterator -> {

        Node jcr_node = object_from_iterator.adaptTo(Node.class);

        try {

              String someProperty = jcr_node.getProperty("some:Property").getString();

              jcr_node.getSession().getWorkspace().getObservationManager().setUserData("changedByWorkflowProcess");

              jcr_node.setProperty("some:Property", "some new value");

              logger.info("blah blah made some changes");

              jcr_node.getSession().save();

         }

        catch { ... }

        finally { ... }

}

Avatar

Level 10

WHen using the JCR API - you can get props - as Marc as shown.

Avatar

Community Advisor

You can get Resource from from jcr session like below:

Map<String, Object> paramMap = new HashMap<String, Object>();

paramMap.put("user.jcr.session", session);

resourceResolver = resourceFactory.getResourceResolver(paramMap);

Resource resource=resourceResolver.getResource("/apps/AEM64App");

where session is JCR Session object

Avatar

Community Advisor

You can adapt resource to ValueMap

Example :

Map<String, Object> paramMap = new HashMap<String, Object>();

paramMap.put("user.jcr.session", session);

resourceResolver = resourceFactory.getResourceResolver(paramMap);

Resource resource=resourceResolver.getResource("/content/AEM64App/en/jcr:content/par/mycomponent");

ValueMap prop = resource.adaptTo(ValueMap.class);

String lastModified = prop.get("jcr:lastModified", String.class);