Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Unable to Prevent LinkChecking on Servlet Request

Avatar

Avatar
Establish
Level 2
luke_grover
Level 2

Likes

18 likes

Total Posts

14 posts

Correct Reply

3 solutions
Top badges earned
Establish
Contributor
Shape 1
Give Back
Validate 1
View profile

Avatar
Establish
Level 2
luke_grover
Level 2

Likes

18 likes

Total Posts

14 posts

Correct Reply

3 solutions
Top badges earned
Establish
Contributor
Shape 1
Give Back
Validate 1
View profile
luke_grover
Level 2

14-05-2018

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

View Entire Topic

Avatar

Avatar
Validate 1
Level 4
cqsapientu69896
Level 4

Likes

22 likes

Total Posts

76 posts

Correct Reply

4 solutions
Top badges earned
Validate 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile

Avatar
Validate 1
Level 4
cqsapientu69896
Level 4

Likes

22 likes

Total Posts

76 posts

Correct Reply

4 solutions
Top badges earned
Validate 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile
cqsapientu69896
Level 4

29-01-2019

Thanks Luke, this helped . Just putting it here if anyone else tries this - the rewrite configuration sometimes works after restarting AEM instance.