Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to place variables into a resource path within a sling servlet?

Tags:

aem

sling

We are trying to provide a clean URI structure for external endpoints to pull json information from CQ5.

For example, if you want to fetch information about a particular users history (assuming you have permissions etc), ideally we would like the endpoint to be able to do the following:

/bin/api/user/abc123/phone/555-klondike-5/history.json

In the URI, we would specifying /bin/api/user/{username}/phone/{phoneNumber}/history.json so that it is very easy to leverage the dispatcher to invalidate caching changes etc without invalidating a broad swath of cached information.

We would like to use a sling servlet to handle the request, however, I am not aware as to how to put variables into the path.

It would be great if there were something like @PathParam from JaxRS to add to the sling path variable, but I suspect it's not available.

The other approach we had in mind was to use a selector to recognise when we are accessing the api, and thus could return whatever we wanted to from the path, but it would necessitate a singular sling servlet to handle all of the requests, and so I am not happy about the approach as it glues a lot of unrelated code together.

Any help with this would be appreciated.


UPDATE:

If we were to use a OptingServlet, then put some logic inside the accepts function, we could stack a series of sling servlets on and make the acceptance decisions from the path with a regex.

Then during execution, the path itself can be parsed for the variables.

like image 629
Bayani Portier Avatar asked Feb 14 '23 04:02

Bayani Portier


2 Answers

If the data that you provide comes from the JCR repository, the best is to structure it exactly as you want the URLs to be, that's the recommended way of doing things with Sling.

If the data is external you can create a custom Sling ResourceProvider that you mount on the /bin/api/user path and acquires or generates the corresponding data based on the rest of the path.

The Sling test suite's PlanetsResourceProvider is a simple example of that, see http://svn.apache.org/repos/asf/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/

The Sling resources docs at https://sling.apache.org/documentation/the-sling-engine/resources.html document the general resource resolution mechanism.

like image 159
Bertrand Delacretaz Avatar answered Feb 23 '23 00:02

Bertrand Delacretaz


It is now possible to integrate jersy(JAX-RS) with CQ. We are able to create primitive prototype to say "Hello" to the world.

https://github.com/hstaudacher/osgi-jax-rs-connector

With this we can use the @PathParam to map the requests

Thanks and Regards, San

like image 24
kallada Avatar answered Feb 22 '23 23:02

kallada