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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Extending the Apache Sling Sitemap Generator for AEM 6.5.11 and AEMaaCs | AEM Community Blog Seeding

kautuk_sahni
Employee
Employee

BlogImage.jpg

Extending the Apache Sling Sitemap Generator for AEM 6.5.11 and AEMaaCs by Nikhil Kumar

Abstract

As mentioned in the earlier article of Apache Sling Sitemap Generator we will be exploring on how we extend it to have out extended Apache Sling Sitemap with properties like lastModifiedData, priority and frequency.

As Apache Sling Sitemap bundle is open-source. So we will see how we can extend it to handle our custom properties. We can refer it’s README.md file for the whole process to extend the Apache Sling Sitemap Generator.

We will first start with creating or Java class MySitemapGenerator and with a higher Ranking (let’s say 20 here). As the PageTreeSitemapGeneratorImpl in the components console. already have a ranking of 10. Now we will go ahead and create MySitemapGenerator that extends the abstract class ResourceTreeSitemapGenerator.


package com.mynewsite.core.service.impl;

import java.util.Calendar;
import java.util.Optional;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.sitemap.SitemapException;
import org.apache.sling.sitemap.builder.Sitemap;
import org.apache.sling.sitemap.builder.Url;
import org.apache.sling.sitemap.spi.common.SitemapLinkExternalizer;
import org.apache.sling.sitemap.spi.generator.ResourceTreeSitemapGenerator;
import org.apache.sling.sitemap.spi.generator.SitemapGenerator;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.propertytypes.ServiceRanking;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.Page;

@Component(service = SitemapGenerator.class)
@ServiceRanking(20)
public class MySitemapGenerator extends ResourceTreeSitemapGenerator {

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

@Reference
private SitemapLinkExternalizer externalizer;

@Override
protected void addResource(final String name, final Sitemap sitemap, final Resource resource)
throws
SitemapException {

final Page page = resource.adaptTo(Page.class);
final String location = this.externalizer.externalize(resource);
final Url url = sitemap.addUrl(location);
final Calendar lastmod = Optional.ofNullable(page.getLastModified())
.orElse(page.getContentResource()
.getValueMap()
.get(JcrConstants.JCR_CREATED, Calendar.class));
if (lastmod != null) {
url.setLastModified(lastmod.toInstant());
url.setPriority(2);

}
log.debug("Added the {} to Extended Sitemap", url);
}
}

Read Full Blog

Extending the Apache Sling Sitemap Generator for AEM 6.5.11 and AEMaaCs

Q&A

Please use this thread to ask the related questions.

0 Replies