Skip to main content
Level 4
Apr 30, 2024 7:16 AM UTC
Solved

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

 

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???
sravs
sravsAccepted solution
Community Advisor
May 6, 2024 7:51 AM UTC

@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.


Try using Sling Internal Request to achieve your use case

 

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

21 replies

Level 4
Apr 30, 2024 7:45 AM UTC

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.

Translate
sravs
Community Advisor
Community Advisor
May 2, 2024 6:36 AM UTC

@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();
 }
Translate
gkalyan
Adobe Champion, Community Advisor, UG Leader
Adobe Champion, Community Advisor, UG Leader
Apr 30, 2024 8:48 PM UTC

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/contentsync/handler/util/RequestResponseFactory.html

Translate
Level 4
May 2, 2024 5:56 AM UTC

Hello @gkalyan ,

This is RequestResponseFactory requestResponseFactory already deprecated.

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

Translate
HrishikeshKagne
Community Advisor
Community Advisor
May 4, 2024 5:46 AM UTC

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.

Translate
Hrishikesh Kagane
New Member
Jul 17, 2024 12:15 PM UTC

@hrishikeshkagne 

 

There is no method:

wrappedRequest.createRequest(HttpConstants.METHOD_GET, path);

Translate