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

Servlet response is printing CQ related information

Avatar

Level 2

Hi,

I have written a servlet which calls OSGI service. And OSGI service invokes API and returns JSON response to servlet and servlet prints response on the page. But when printing servlet response on the page, CQ related information is getting printed. I'm not sure why. Can anyone please help me with this?

Below is my servlet code.

 

 

@component(service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Servlet to call API Url",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.resourceTypes="+ "aem-code/components/structure/page",
"sling.servlet.extensions=" + "txt"
})
public class CallApiServlet extends SlingSafeMethodsServlet {

private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(CallApiServlet.class);

@reference
private CallApiService apiCall;

@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json");
String result = apiCall.getApiData(req.getParameterMap());
log.info("called"+result); //Here the result is printing without CQ information
resp.getWriter().write(result);
}
}

 

my servlet response:

{"results":[{"name":{"title":"Ms","first":"Nalan","last":"Oraloğlu"},"login":{"uuid":"baffd062-6cc7-4dd5-b1fd-69d4cde0f845","username":"ticklishgorilla641","password":"zhan","salt":"VUD21uld","md5":"70b78925165996a5926af72b2cbe373d","sha1":"b50f720abecaa5cc9eed47724ddbf457cd501388","sha256":"d3cbfbb1990802e19467640376257db0f631755aaaf85afe9ce0b2925fa91970"},"nat":"TR"}],"info":{"seed":"05d85febdd32196c","results":1,"page":1,"version":"1.3"}}
<!--cq{"decorated":false,"type":"aem-code-challenge/components/structure/page","path":"/content/aem-code-challenge/en/jcr:content","selectors":"txt","servlet":"CallApiServlet","totalTime":114,"selfTime":114}-->

 

The above CQ information is getting printed which is not required.

Thank you!

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

First of all, you won't get this on publish. This information is some timing information, which is written only in Author, when the WCMMode is set accordingly. And also only for HTML renderings.

 

As your output is JSON, you should register the servlet correctly and use the "json" extension. Then all should work fine.

 

View solution in original post

10 Replies

Avatar

Level 4

 I am seeing you are using .txt extension. how you are hitting a servlet?

Avatar

Level 2

Iam hitting my servlet with the below url like:

http://localhost:4502/content/aem/en.txt.html

 

Iam getting the same result even if I give any other extensions.

Avatar

Community Advisor

@maheswari25  can you check what is the logic behind CallApiService apiCall; ?

 

And also if you can debug if apiCall.getApiData(req.getParameterMap()); is giving the response or else the CQ information is coming from the servlet class.

 

I am expecting that CQ information is coming from the api response.

Avatar

Level 2

The servlet response is not containing the cq information. I checked in the logs and its not coming from servlet response..CQ data is coming only when printing response on the page.

Avatar

Employee Advisor

I have written this sample servlet 

package com.aem.demo.core.servlets;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Servlet to call API Url",
		"sling.servlet.methods=" + HttpConstants.METHOD_GET,
		"sling.servlet.resourceTypes=" + "weretail/components/structure/page", "sling.servlet.extensions=" + "txt" })
public class DemoResponseServlet extends SlingAllMethodsServlet {

	private final Logger logger = LoggerFactory.getLogger(DemoResponseServlet.class);

	protected void doGet(SlingHttpServletRequest slingHttpServletRequest,
			SlingHttpServletResponse slingHttpServletResponse) throws IOException {

		slingHttpServletResponse.setContentType("application/json");
		Map<String, String[]> parameterMap = slingHttpServletRequest.getParameterMap();

		for (String key : parameterMap.keySet()) {
			String[] strArr = (String[]) parameterMap.get(key);
			for (String val : strArr) {
				//slingHttpServletResponse.getWriter().write(val);
			}
		}

	}

}

it's giving me CQ related information in servlet response using localhost:7070/content/we-retail/language-masters/en/men.txt.html as shown below -

DEBAL_DAS_0-1652545886698.png

txt is an extension here, why are you accessing as a selector?

 

 

 

 

 

 

 

 

 

 

 

 

Avatar

Level 2

Even if I give any other extension such as 'abc' or any text, CQ information is getting printed. But when I give xml as extension, CQ information is not getting printed. Is there a reason behind this?

If I access Url as http://localhost:4502/content/aem-code/en.html.xml  , Iam not getting CQ information

Avatar

Employee Advisor

 I am getting Internal error while trying to access http://localhost:7070/content/we-retail/language-masters/en/men.txt.abc

 

DEBAL_DAS_0-1652550068485.png

 

Again same question in your code txt is an extension here, why are you accessing as a selector?

Could you please try to access http://localhost:4502/content/aem/en/jcr:content.txt and share the response.

Avatar

Community Advisor

@maheswari25 

Decide on how you would like to call the servlet and then register it accordingly. 

 

Avatar

Community Advisor

Hi,

Are you using ACS Common and checking this in Author?

Can you try in publish?



Arun Patidar

Avatar

Correct answer by
Employee Advisor

First of all, you won't get this on publish. This information is some timing information, which is written only in Author, when the WCMMode is set accordingly. And also only for HTML renderings.

 

As your output is JSON, you should register the servlet correctly and use the "json" extension. Then all should work fine.