get resource from session

shaheenas113483

08-05-2019

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]

Accepted Solutions (1)

Accepted Solutions (1)

aemmarc

Employee

08-05-2019

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 { ... }

}

Answers (7)

Answers (7)

Arun_Patidar

MVP

08-05-2019

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

k_ra

08-05-2019

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();

Arun_Patidar

MVP

09-05-2019

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);