내 커뮤니티 업적 표시줄을 확대합니다.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

what is the best alternative RequestResponseFactory as its deprecated

Avatar

Level 3

In the below code there is use of RequestResponseFactory to generate the Html output, I would like to refactor the code as its deprecated now.

 

also HttpServletRequest httpRequest passed to this method its also using requestResponseFacotry in the servlet who call the below method:

HttpServletRequest httpRequest = requestResponseFacotry.createRequest(HttpConstants.METHOD_GET, url); 

 

스포일러
public static String fetchHtmlContent(HttpServletRequest httpRequest, ResourceResolver resolver,
RequestResponseFactory requestResponseFactory, SlingRequestProcessor requestProcessor) {
String htmlContentAsString = null;
WCMMode.DISABLED.toRequest(httpRequest);
try (ByteArrayOutputStream htmlByteArrayOutputStream = new ByteArrayOutputStream(); ) {
HttpServletResponse response = requestResponseFactory.createResponse(htmlByteArrayOutputStream);
requestProcessor.processRequest(httpRequest, response, resolver);
response.getWriter().flush();
htmlContent = htmlByteArrayOutputStream.toString(UTF_8);
}
catch (ServletException | IOException e) {
LOG.error("not able not generate HTML content '{}' ", e.getMessage());
}
return htmlContentAsString;
}

currently i'm facing challenge to replace RequestResponseFactory.
Could anyone had face this challenge or any solution without changing solution?

주제

토픽은 커뮤니티 콘텐츠를 분류하여 관련성 있는 콘텐츠를 찾는 데 도움이 됩니다.

5 답변 개

Avatar

Community Advisor

Avatar

Level 3

Thanks @h_kataria , SlingInternalRequest is an internal API. Using it is not generally recommended for production.

Avatar

Community Advisor and Adobe Champion

Avatar

Level 3

Hello @MayurSatav , Yes, but not helpful. As there is no org.apache.sling.api.request.RequestResponseFactory interface exist.

Avatar

Employee

https://sling.apache.org/documentation/bundles/servlet-helpers.html#internalrequest-helpers is not production safe instead use SlingRequestProcessor.

 

....
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.builder.Builders;
import org.apache.sling.api.request.builder.SlingHttpServletResponseResult;
import org.apache.sling.engine.SlingRequestProcessor;
....

/** The sling processor. */
	@Reference
	private SlingRequestProcessor slingProcessor;
.....
// Build synthetic Sling request
			SlingHttpServletRequest slingRequest = Builders.newRequestBuilder(targetResource)
					.withExtension("html")
					.withSelectors("test")
					.withParameter("wcmmode", "disabled")
					.withRequestMethod("GET").build();

			SlingHttpServletResponseResult responseResult = Builders.newResponseBuilder().build();

			// Process the request
			slingProcessor.processRequest(slingRequest, responseResult, resolver);
			String res = responseResult.getOutputAsString();