Modify the OOTB LinkChecker Config

Modify the OOTB LinkChecker Config

I've come into the issue recently of needing to modify the prefix and suffix for invalid links detected by the linkchecker. This seemingly simple task is unfortunately not as easy as overlaying the

/libs/cq/linkchecker/config.author/invalid
to
/apps/cq/linkchecker/config.author/invalid
. For reasons unknown (to me at least) the standard overlay does not work for this type of update. Instead, after digging into the code, I found this information:

REQUEST_ATTRIBUTE_NAME = LinkCheckerSettings.class.getName();
LinkCheckerSettings settings = (LinkCheckerSettings)request.getAttribute(REQUEST_ATTRIBUTE_NAME);

The above lines indicate that if we set a request attribute to the LinkCheckerSettings object we wish to use, we will use our custom settings instead of the OOTB ones defined in that node. Now that we have this goal, we can start creating a LinkCheckerSettings object to use. I would suggest to include these settings as a getter method in a custom OSGi Service, such that you can reference and obtain them easily from any project on the server. Although these instructions will work fine if done directly in the JSP, they will be focused on the service implementation.

First, create a getter method in your OSGi Service. For example:

import  com.day.cq.rewriter.linkchecker.LinkCheckerSettings;
...

  public static LinkCheckerSettings getLinkCheckerSettings(){
    LinkCheckerSettings lcs = new LinkCheckerSettings();
    return lcs;
  }

The above just is creating the default LinkCheckerSettings object, which is a good start, but we want to specifically update the invalid link config items. To do this, we add the LinkCheckerSettings.setInvalidLinkConfig(<LinkRewriteConfig> rewriteConfig) method:

  public static LinkCheckerSettings getLinkCheckerSettings(){
    LinkCheckerSettings lcs = new LinkCheckerSettings();
    String invalidPrefix = "<div class=\"invalidLink\"><img src=\"/libs/cq/linkchecker/resources/linkcheck_o.gif\">";
    String invalidSuffix = "<img src=\"/libs/cq/linkchecker/resources/linkcheck_c.gif\" border=\"0\"></div>";
    lcs.setInvalidConfig(
            new LinkRewriteConfig(true,invalidPrefix,invalidSuffix)
            );
  return lcs;
}

In the above, we're now changing the invalid configuration item to have our customized changes. In the example above, I am adding a <div> wrapper around the invalid link, with the invalidLink class applied. The same can be applied to predated and expired link configurations as well.

Finally, in order for the above to successfully compile, you'll need to add the following maven dependency:

<dependency>
      <groupId>com.day.cq</groupId>
      <artifactId>cq-rewriter</artifactId>
      <version>5.6.4</version>
      <scope>provided</scope>
</dependency>

Now that we have successfully created our updated link class, we just need to set the request attribute in order to use it as opposed to the OOTB items. To do this, add the following to the page components you wish to use this updated settings item (or the base component if you want it universal). NOTE: This needs to be in the initializers for the page. For example, if your page component is named base, include this as the first script in base.jsp. Here's the code:

  LinkCheckerSettings lcs = CustomOSGiService.getLinkCheckerSettings();
  request.setAttribute(LinkCheckerSettings.class.getName(), lcs);

That's it! Your customized prefix and suffixes should now be applied!

Share this post

0 Comments

comments powered by Disqus