Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Do we have any substitute for com.day.cq.contentsync.handler.util.RequestResponseFactory as its deprecated???

Avatar

Level 3

 

how to achieve the below result using any alternative in AEMaaCS?? 

HttpServletRequest request = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path);

can any one help me on this???
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Try using Sling Internal Request to achieve your use case

 

https://kiransg.com/tag/slinghttpservletrequest/

View solution in original post

21 Replies

Avatar

Level 3

Hello @sravs , thanks for input, i have tried the Builders not able to achive desired results, 

HttpServletRequest request = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path);

 could you please share any example implementation if you have??

i'm trying to get HttpServletRequest object here.

Avatar

Community Advisor

@KarthickVe , I don't have any examples using the sling builder. can you please explain bit more details so that I can provide an alternative method.

Are you making a request to any third party API? If yes try to use

org.apache.http.impl.client.HttpClients

  

 CloseableHttpClient httpclient = HttpClients.createDefault();
 try {
      HttpGet httpGet = new HttpGet("http://targethost/homepage");
      CloseableHttpResponse response = httpclient.execute(httpGet);
      try {
          System.out.println(response.getStatusLine());
          HttpEntity entity = response.getEntity();
          // do something useful with the response body
          // and ensure it is fully consumed
          EntityUtils.consume(entity);
      } finally {
          response.close();
      }
 } finally {
      httpclient.close();
 }

Avatar

Level 3

Hello @sravs ,

 

i'm currently looking for a solution:

My useCase:

Fetch HTML content from a path from /content/dam/dom/abc/test.html within a AEMaaCS environment by simulating a GET request. It captures the response from the simulated request and returns the extracted HTML content as a String.

Avatar

Community Advisor

@KarthickVe , can you try this

 

SlingHttpServletRequestBuilder builder = Builders.newRequestBuilder(resolver.getResource("/content/dam/dom/abc/test"));
SlingHttpServletRequest httpServletRequest = builder.build();

 

Avatar

Level 3

@sravs , Thanks for your input, this is work well for simulating a GET request but not working for capturing the response from the simulated request and returns also not able extracted HTML content as a String.

Avatar

Correct answer by
Community Advisor

Try using Sling Internal Request to achieve your use case

 

https://kiransg.com/tag/slinghttpservletrequest/

Avatar

Level 3

Hello @sravs , After implementing the above solution its build failed in AEMaaCS

 

its reported as critical issue:

The product interface org.apache.sling.api.request.RequestPathInfo annotated with @ProviderType should not be implemented by custom code. Detected in org.apache.sling.servlethelpers.MockRequestPathInfo contained in /apps/aem-code-packages/application/install/core-bocker.core-1.2.4-SNAPSHOT.jar

 

documentation:

https://www.adobe.com/go/aem_cmcq_cqbp-84_en

Avatar

Level 1

@sravs 

I am not able to deploy the servlet into AEM SDK local instance. I have the maven pom.xml updated. Code compile and deployed. But bundle did not get Installed. Underlying issue is:

org.osgi.framework.BundleException: Unable to resolve aem-permissions-provider.core [747](R 747.0): missing requirement [aem-permissions-provider.core [747](R 747.0)] osgi.wiring.package; (&(osgi.wiring.package=org.apache.sling.servlethelpers.internalrequests)(version>=1.0.0)(!(version>=2.0.0))) Unresolved requirements: [[aem-permissions-provider.core [747](R 747.0)] osgi.wiring.package; (&(osgi.wiring.package=org.apache.sling.servlethelpers.internalrequests)(version>=1.0.0)(!(version>=2.0.0)))]
	at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4398)
	at org.apache.felix.framework.Felix.startBundle(Felix.java:2308)
	at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:1006)
	at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:992)
	at org.apache.sling.installer.core.impl.tasks.BundleStartTask.execute(BundleStartTask.java:97) [org.apache.sling.installer.core:3.14.0]
	at org.apache.sling.installer.core.impl.OsgiInstallerImpl.doExecuteTasks(OsgiInstallerImpl.java:918) [org.apache.sling.installer.core:3.14.0]
	at org.apache.sling.installer.core.impl.OsgiInstallerImpl.executeTasks(OsgiInstallerImpl.java:755) [org.apache.sling.installer.core:3.14.0]
	at org.apache.sling.installer.core.impl.OsgiInstallerImpl.run(OsgiInstallerImpl.java:304) [org.apache.sling.installer.core:3.14.0]
	at java.base/java.lang.Thread.run(Thread.java:834)

Avatar

Level 2

@sravs I am trying to use MD5 method in the

com.day.cq.contentsync.handler.util.RequestResponseFactory

to get the MD5 hash of the response body, modify the content and store it into the node. Is there any equivalent method from another API class which can be used for the same purpose?

 

This is a blocker for us unfortunately.

Avatar

Community Advisor

Please try to follow the approaches mentioned in earlier comments, if that doesn't satisfy your condition, can you please share the code snippet of your use case if possible?

Avatar

Level 2

@sravs Above solutions are the recommendations to create a HttpServletRequest object which is still valid via com.day.cq.contentsync.handler.util.RequestResponseFactory object. Our problem is regarding getMD5 method since it is deprecated we are looking for an alternative for the same to hash the response body and then process the response for image paths mainly. Please find the code snippet below for reference. 

 

 

 

String md5 = requestResponseFactory.getMD5(response);
String md5Path = cachePath + "/" + JcrConstants.JCR_CONTENT + "/" + MD5_HASH_PROPERTY;

 

 
Main Requirement - This is mainly for creating a zip package of a page using ContentSync API and use it for our specific requirement. 

 

FYI @rahulh56481972 

Avatar

Community Advisor

@shankarram2407, please try above approach org.apache.commons.codec.digest.DigestUtils class for MD5 hashing instead of relying on RequestResponseFactory.

Like,

String md5Hex = DigestUtils.md5Hex(responseAsString)

 

Hope this helps!!

Avatar

Level 2

@sravs  - That really helped. Thanks for the swift response. We will completely remove requestResponseFactory usage as well once the complete logic is sorted out since that is the main priority as of now. 

 

FYI @rahulh56481972 

Avatar

Level 1

According to documentation, https://sling.apache.org/documentation/bundles/servlet-helpers.html
Not all servlets and scripts are suitable to be called by the ServletInternalRequest, depending on their "environmental" requirements like Request attributes for example.

 

I was trying to use builders to load experience fragment master variation URL. My code looks like:

 

 

        try {
            
            /* Setup request */
            SlingHttpServletRequestBuilder requestBuilder = Builders.newRequestBuilder(slingRequest.getResourceResolver().getResource("/content/experience-fragments/projects/experience-fragment-1/master"))
            													.withExtension("html")
            													.withRequestMethod("GET");
            SlingHttpServletRequest req = requestBuilder.useRequestDispatcherFrom(slingRequest).build();
            /* Setup response */
            SlingHttpServletResponseBuilder responseBuilder = Builders.newResponseBuilder();
            SlingHttpServletResponseResult resp = responseBuilder.build();

            Servlet servlet = servletResolver.resolveServlet(req);
            if (servlet != null) {
                servlet.service(req, resp);
            }
            
            if(resp.getStatus() == HttpServletResponse.SC_OK) {
            	String html = resp.getOutputAsString();
            	logger.debug("response content : " + html);
    	        if (StringUtils.isNotBlank(html)) {
    	        	Document document = Jsoup.parse(html);
    	        	html = document.select("div.root.container.responsivegrid").first().html();
    				response_object.addProperty("content", html);
    	        }
            } 
		} catch (ServletException | IOException e) {
			response_object.addProperty("error_message", e.getMessage());
		}

 

 

 

it doesnt seem to work at all. Looks like there is no way to export experience fragments without using the deprecated RequestResponseFactory which works fine for the same requirement.

Avatar

Community Advisor

hI @KarthickVe 

I second what @sravs said about using Apache Sling Builders.

 

To obtain an instance of the RequestResponseFactory interface, you can use the org.apache.sling.api.SlingHttpServletRequest object.
 
Here’s an example of how you can achieve the same result:
RequestResponseFactory requestResponseFactory = slingRequest.getRequestResponseFactory();
HttpServletRequest request = requestResponseFactory.createRequest(HttpConstants.METHOD_GET, path);

 

Reference documentation:

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/index.html?com/day/cq...

Avatar

Level 3

Hello @gkalyan ,

This is RequestResponseFactory requestResponseFactory already deprecated.

I'm looking for Apache Sling Builders example implementation for the same.

Avatar

Community Advisor

HI @KarthickVe ,

To achieve the functionality provided by com.day.cq.contentsync.handler.util.RequestResponseFactory, which is deprecated, you can use the SlingHttpServletRequest object directly to create a request.

Here's how you can achieve the same result using SlingHttpServletRequest:

 

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;

// Inject SlingHttpServletRequest into your service or component
private SlingHttpServletRequest request;

// Create a wrapper around the existing SlingHttpServletRequest
SlingHttpServletRequestWrapper wrappedRequest = new SlingHttpServletRequestWrapper(request) {};

// Now, you can use the wrappedRequest to create your request
SlingHttpServletRequest getRequest = wrappedRequest.createRequest(HttpConstants.METHOD_GET, path);

 

In this code:

  • SlingHttpServletRequestWrapper is used to create a wrapper around the existing SlingHttpServletRequest.
  • The createRequest() method is then called on the wrapper object to create the request.

This approach should help you achieve similar functionality without relying on the deprecated RequestResponseFactory.