I am exploring usage of SlingInternalRequest on AEM as a Cloud Service and have referenced the below articles for implementation -
https://sling.apache.org/documentation/bundles/servlet-helpers.html
https://kiransg.com/tag/slinginternalrequest/
However the core java bundle is not resolving after including code that imports the java package from sling-servlet-helpers.
Has anyone tried to use SlingInternalRequest in AEM as a Cloud Service and are able to share any additional steps they had to take to make it work ?
Thanks
Narendra
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Here is sample code
try {
// Use SlingInternalRequest to perform internal GET request to the path
return new SlingInternalRequest(resourceResolver, requestProcessor, path)
.withRequestMethod("GET")
.execute()
// We can skip content-type and status check or customize if needed
.getResponseAsString();
} catch (IOException e) {
LOG.error(
"An error occurred while getting HTML content for path {} - {}", path, e.getMessage());
}
In pom add the following
<configuration>
<bnd><![CDATA[
Import-Package: javax.annotation;version=0.0.0,!javax.annotation.meta,*
Sling-Model-Packages: com.abc.test.models
-includeresource: org.apache.sling.servlet-helpers-1.4.6.jar;lib:=true
]]></bnd>
</configuration>
@narendragandhi please mark correct once the build issue is resolved
Hi @narendragandhi,
I recently created a new AEM project using AEM Maven Archetype 54, and as part of exploring SlingInternalRequest, I tried integrating it into the core bundle following the examples you referenced above.
However, after including the dependency and importing the internal package, I ran into the following build-time error:
[ERROR] [api-regions-exportsimports] com.mysite:mysite.core:1.0.0-SNAPSHOT:
Bundle mysite.core:1.0.0-SNAPSHOT is importing package(s)
org.apache.sling.servlethelpers.internalrequests in start level 20
but no bundle is exporting these for that start level.
(com.mysite:mysite.all:1.0.0-SNAPSHOT)
After digging deeper, I found that this is due to AEM as a Cloud Service enforcing API Regions - which restricts the use of internal/private packages, including:
org.apache.sling.servlethelpers.internalrequests
Even though the org.apache.sling.servlet-helpers bundle is available and includes this class, AEMaaCS does not export internal packages, hence causing the error meaning that articels above highlight its use, those are often based on local SDK or older AEM versions.
So, in short
SlingInternalRequest is in the package:
org.apache.sling.servlethelpers.internalrequests
…which is marked as internal and not exported by default in AEM as a Cloud Service.
AEMaaCS strictly enforces API Regions and will throw an error.
What you could try instead (I haven't tried this):
Since the goal was to reuse servlet logic internally, you can switch to the supported approach using SlingRequestProcessor, like this:
ByteArrayOutputStream out = new ByteArrayOutputStream();
slingRequestProcessor.processRequest(slingRequest, new CustomHttpServletResponse(out), resourceResolver);
String response = out.toString(StandardCharsets.UTF_8.name());
Or better yet, then extract reusable servlet logic into a dedicated OSGi service, which can be invoked both from servlets and other components like models, schedulers, etc.
Here is sample code
try {
// Use SlingInternalRequest to perform internal GET request to the path
return new SlingInternalRequest(resourceResolver, requestProcessor, path)
.withRequestMethod("GET")
.execute()
// We can skip content-type and status check or customize if needed
.getResponseAsString();
} catch (IOException e) {
LOG.error(
"An error occurred while getting HTML content for path {} - {}", path, e.getMessage());
}
In pom add the following
<configuration>
<bnd><![CDATA[
Import-Package: javax.annotation;version=0.0.0,!javax.annotation.meta,*
Sling-Model-Packages: com.abc.test.models
-includeresource: org.apache.sling.servlet-helpers-1.4.6.jar;lib:=true
]]></bnd>
</configuration>
@narendragandhi please mark correct once the build issue is resolved
@narendragandhi just checking in! Were you able to get this resolved? If one of the replies above helped—whether it completely solved the issue or simply pointed you in the right direction—marking it as accepted can make it much easier for others with the same question to find a solution. And if you found a different way to fix it, sharing your approach would be a great contribution to the community. Your follow-up not only helps close the loop but also ensures others benefit from your experience. Thanks so much for being part of the conversation!
Views
Replies
Total Likes
@kautuk_sahni Yes, I am planning to try out the suggestion that @Hemant_arora provided. I will report back once I am able to test it.
Thanks
Narendra
Views
Replies
Total Likes