Question

I'm trying to figure out to retrieve a jcr resource via it's identifier. Basically I'm doing the following.

component1

String compID = resource.adaptTo(Node.class).getIdentifier();

then when it gets passed to component2 I would like to retrieve component1. If it helps i'm passing the identifier via Query String Parameters to help support some other client requirements.

component2

//code to get id from query string
//want to do something like:

Node resNode = resourceResolver.adaptTo(Node.class);
Session jcrSession = resNode.getSession();
Resource myRes = jcrSession.getNodeByIdentifier(eventId).adaptTo(Resource.class);

//use myRes as I please.

The 2nd example of course doesn't work, and I realize that Node does not have adaptTo support. So I am more or less curious if anyone can shed some light on how I could accomplish what my component2 example is failing to do.

thank you, Brodie

Was it helpful?

Solution

You should adapt ResourceResolver directly to a Session. Then get a node by the identifier. There is no way to create Resource from the Node, so use Node path and get the Resource from the ResourceResolver:

Session jcrSession = resourceResolver.adaptTo(Session.class);
Node node = jcrSession.getNodeByIdentifier(eventId);
Resource resource = resourceResolver.getResource(node.getPath());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top