Highlighted

How alter SlingHttpServletRequest with different Resource?

Avatar

Avatar

puradawid

Avatar

puradawid

puradawid

19-01-2018

I am struggling with some generic piece of code:

Resource resource = dynamicallyFindNeededResource(res);

Class<?> someModel = pickUpSomeSlingModelClass();

return resource.adaptTo(someModel);

The problem is I want to expand this type of adapting to request as well:


Adaptable adaptable = checkIfModelAdaptableByRequest(someModel) ? changeRequestResourceToCurrentOne(request, resource) : resource;

return adaptable.adaptTo(someModel);

What comes to my mind is the changeRequestResourceToCurrentOne method has to use SlingRequestWrapper which would be kind of lame solution. Any other ides how to alter that request with resource without wrapping it with new resoruce? Thanks in advance.

Replies

Highlighted

Avatar

Avatar

edubey

Total Posts

(val/1000)?string[".0"]}K

Likes

264

Correct Answer

392

Avatar

edubey

Total Posts

(val/1000)?string[".0"]}K

Likes

264

Correct Answer

392
edubey

19-01-2018

I am just trying to understand why would you need to adapt to another resource?
You can always read any resource with resource resolver.

Highlighted

Avatar

Avatar

PuzanovsP

MVP

Avatar

PuzanovsP

MVP

PuzanovsP
MVP

19-01-2018

Hi,

Via your request SlingHttpServletRequest (Apache Sling (Builder) 6 API) get request dispatcher SlingHttpServletRequest (Apache Sling (Builder) 6 API) then use standard API to forward your request to a new place.

Agree with the previous answer, why not direct the request to the correct location in the first place or adapt existing request to desired location?

Regards,

Peter

Highlighted

Avatar

Avatar

puradawid

Avatar

puradawid

puradawid

21-01-2018

So the point is there is a mechanism that is looking for another resource in a content tree (nearest sibling with resource type) to generate this one, i.e. "configuration".

There is no way to forward this request nor include neither.

Highlighted

Avatar

Avatar

edubey

Total Posts

(val/1000)?string[".0"]}K

Likes

264

Correct Answer

392

Avatar

edubey

Total Posts

(val/1000)?string[".0"]}K

Likes

264

Correct Answer

392
edubey

22-01-2018

So assuming you have a resource path, you are looking for sibling resource with a particulate resource type..

You can use resource iterator for it.

For current resource get the parent and from parent use listchildren, you will access to all sibling.

Avatar

Avatar

puradawid

Avatar

puradawid

puradawid

22-01-2018

The point is I have got this resource already, but I want to adapt it to specific sling model.

Highlighted

Avatar

Avatar

edubey

Total Posts

(val/1000)?string[".0"]}K

Likes

264

Correct Answer

392

Avatar

edubey

Total Posts

(val/1000)?string[".0"]}K

Likes

264

Correct Answer

392
edubey

22-01-2018

Why do you need to adapt to sling model?

Why not simply use resource API [0] to read properties or anything?

[0]  https://sling.apache.org/apidocs/sling7/org/apache/sling/api/resource/Resource.html

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

(val/1000)?string[".0"]}K

Likes

(val/1000)?string[".0"]}K

Correct Answer

(val/1000)?string[".0"]}K

Avatar

smacdonald2008

Total Posts

(val/1000)?string[".0"]}K

Likes

(val/1000)?string[".0"]}K

Correct Answer

(val/1000)?string[".0"]}K
smacdonald2008

22-01-2018

Also - once you have a resource in the JCR - you can create a Sling Model - as discussed here:

Adobe Experience Manager Help | Creating Adobe Experience Manager 6.3 Sling Model Components

Look at how we bind the UserInfo class to nodes and props in the JCR.