Expand my Community achievements bar.

Who Me Too'd this topic

Avatar

Level 3

I have a simple servlet that should return some commerce data. I have simplified the example here... but the issue is that if there is a link in any of the properties then I get invalid JSON and the link checker filter appears to altering the response even though I have disabled it via configuration.

package com.example.issue;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.ValueMap;

import org.apache.sling.api.servlets.ServletResolverConstants;

import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import org.json.JSONException;

import org.json.JSONObject;

import org.osgi.service.component.annotations.Component;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import java.io.IOException;

@Component(

    service= Servlet.class,

    property = {

            SimpleCommerceServlet.RESOURCE_TYPE_DEFAULT,

            SimpleCommerceServlet.SELECTOR,

            SimpleCommerceServlet.METHOD_GET,

            SimpleCommerceServlet.EXTENSION_JSON

    }

)

public class SimpleCommerceServlet extends SlingAllMethodsServlet {

    private static final long serialVersionUID = 1647028361800528653L;

    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleCommerceServlet.class);

    public static final String RESOURCE_TYPE_DEFAULT = ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=" + ServletResolverConstants.DEFAULT_RESOURCE_TYPE;

    public static final String EXTENSION_JSON = ServletResolverConstants.SLING_SERVLET_EXTENSIONS+"=json";

    public static final String METHOD_GET = ServletResolverConstants.SLING_SERVLET_METHODS + "=GET";

    public static final String SELECTOR = ServletResolverConstants.SLING_SERVLET_SELECTORS +"=getCommerceDetails";

    private ResourceResolver resourceResolver;

    @Override

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

        try {

            resourceResolver = request.getResourceResolver();

            Resource resource = resourceResolver.getResource("/etc/commerce/products/we-retail/custom/sample_product");

            JSONObject jsonObject = new JSONObject();

            ValueMap vm = resource.adaptTo(ValueMap.class);

            jsonObject.put("Title", vm.get("jcr:title"));

            jsonObject.put("Summary", vm.get("summary"));

            response.setHeader("Content-Type", "application/json");

            response.setCharacterEncoding("UTF-8");

            response.getWriter().write(jsonObject.toString());

        } catch (JSONException  e) {

            LOGGER.error("Failed to get and process JSON");

        } finally {

            resourceResolver.close();

        }

    }

}

I have created a new product at /etc/commerce/products/we-retail/custom/sample_product with properties

jcr:title = Sample Product

summary =

<p>This is a summary but it also has a <a title="Google" href="https://www" target="_blank">link</a>.</p>

Calling the Servlet like:

http://localhost:8080/bin/commerce.getCommerceDetails.details.json

Results in invalid json

{"Title":"Sample Product","Summary":"<p>This is a summary but it also has a <img src="/libs/cq/linkchecker/resources/linkcheck_o.gif" alt="invalid link: Google\\" title="invalid link: Google\\" border="0">link<\/a>.<\/p>\r\n"}

Note the insertion of the linkcheck gif!

I have tried using LinkCheckerSetting to ignore Internal and External.

I have disabled link checking and rewriting via com.day.cq.rewriter.linkchecker.impl.LinkCheckerTransformerFactory

I have changed the Link Check Override to ^. in com.day.cq.rewriter.linkchecker.impl.LinkCheckerImpl

Any other ideas on how to prevent the Link Checker from changing the response of this Servlet?

I'm running AEM 6.3 SP1

Who Me Too'd this topic