Alex Theys
Alex Theys

Reputation: 31

AEM Servlet response writter removing links

In AEM, I'm trying to write a JSON object that contains a string object via a get servlet, like this:

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonObject.toString());

Response being of type SlingHttpServletResponse

When the servlet is accessed in a browser the is stripped with a warning coming out of the aem log:

03.08.2015 16:55:27.359 *WARN* [127.0.0.1 [1438617327343] GET /bin/integration.json HTTP/1.1] com.day.cq.rewriter.linkchecker.impl.LinkCheckerImpl Ignoring malformed URI: java.net.URISyntaxException: Illegal character in path at index 0: \

Link checker is bypassed for a lot of patterns including the link above.

For example the string object inside the json:

pageIntro:'this <a href="http://www.domain.com/my-section/page.html">link</a>  doesn't work' 

becomes:

pageIntro:'this link</a>  doesn't work' 

Any help would be much appreciated.

Cheers, Alex

Upvotes: 3

Views: 1232

Answers (1)

Sabya
Sabya

Reputation: 1429

By doing a quick fiddle around AEM 6.0 , I am not able to reproduce this issue . Following is what I did in the servlet. Attaching the snippet below. Is there anything else you are doing to achieve this ?

    import java.io.IOException;
    import javax.servlet.ServletException;
    import org.apache.felix.scr.annotations.sling.SlingServlet;
    import org.apache.sling.api.SlingHttpServletRequest;
    import org.apache.sling.api.SlingHttpServletResponse;
    import org.apache.sling.api.servlets.SlingAllMethodsServlet;
    import org.apache.sling.commons.json.JSONException;
    import org.apache.sling.commons.json.JSONObject;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
     @SlingServlet(  label = "Stack Overflow - Sabya Test Servlet", 
                    description = "Used for quick fiddle",
                    paths="/bin/sabya-servlet.json",
                    metatype = true
                )
    public class SabyaTestServlet extends SlingAllMethodsServlet {

        private static final long serialVersionUID = 1335893917596088016L;

        private static final Logger log = LoggerFactory
                .getLogger(SabyaTestServlet.class);


        @Override
        protected void doGet(SlingHttpServletRequest request,
                SlingHttpServletResponse response) throws ServletException,
                IOException {
            log.trace("Sabya Test Servlet : doGet() starts .. ");

            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("pageIntro", "this <a href='http://www.domain.com/my-section/page.html'>link</a>  doesn't work");
                response.setContentType("application/json");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write(jsonObject.toString());
            } catch (JSONException e) {
                log.error("Something ridiculous has happened !! {}" , e);
            }

            log.trace("Sabya Test Servlet : doGet() ends .. ");
        }
    }

Request URL : http://localhost:4502/bin/sabya-servlet.json

Response :

{
pageIntro: "this <a href='http://www.domain.com/my-section/page.html'>link</a> doesn't work"
}

Note : I believe you are using org.apache.sling.commons.json.JSONObject .

Upvotes: 2

Related Questions