Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to retrieve SlingHttpServletRequest from ResourceResolver

Avatar

Level 2

I would like to retrieve the request used to resolve the resources I retrieve from the ResourceResolver.

I am performing integration testing using Teleporter Rule And can resolve the resource but can't find its corresponding request.

@Rule public final TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "Launchpad");
 
@Test public void testPage() throws LoginException {
     ResourceResolverFactory resourceResolverFactory = teleporter.getService(ResourceResolverFactory.class);
     ResourceResolver resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
     Resource resource = resourceResolver.getResource("/content/test/en");
     PageModel page = resource.adaptTo(PageModel.class);
}

I would like to be able to retrieve the request like I can with my unit tests using SlingContext.

SlingContextImpl slingContext = new SlingContextImpl(); 
slingContext.currentResource("/content/test/en");
slingContext.request()

I would like to retrieve the request like I can with my unit tests and properly test my sling models that inject the request.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

I think that SlingModels, which only adapt from a Resource, are much more versatile than SlingModels which can only be adapted from a request. Because the adaption works in nearly every case, and not just in the context of a request. It also makes testing much easier.

You can "simulate" a request from javacode, see [1] for an example. It should also work in the context of your IntegrationTests.

[1] Get the rendered HTML for an AEM resource, component or page - Adobe Experience Manager | AEM/CQ | A...

View solution in original post

4 Replies

Avatar

Employee Advisor

A resourceResolver is not necessarily tied to a request, but every Request does have a ResourceResolver.

Avatar

Level 2

Hi Jorg,

Ok then, if that is the case. What would be the best way to stimulate a request for a server side integration test and receive a SlingHttpServletRequest from which I can retrieve the resource.

Avatar

Correct answer by
Employee Advisor

I think that SlingModels, which only adapt from a Resource, are much more versatile than SlingModels which can only be adapted from a request. Because the adaption works in nearly every case, and not just in the context of a request. It also makes testing much easier.

You can "simulate" a request from javacode, see [1] for an example. It should also work in the context of your IntegrationTests.

[1] Get the rendered HTML for an AEM resource, component or page - Adobe Experience Manager | AEM/CQ | A...

Avatar

Level 2

Using the requestResponseFactory in conjunction with the request processor is definitely what I would like to do to. The issue is from that approach is that I seem unable to adapt the request to my model afterwards.