Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.
SOLVED

Dynamic Sitemap usinf factory Configuration

Avatar

Level 1

I have created a servlet, but for multiple sites this not working only for one site this working


@Component(service = Servlet.class, property = {
"sling.servlet.methods=GET",
"sling.servlet.resourceTypes=sling/servlet/default",
"sling.servlet.extensions=sitemap.xml"
})
@Designate(ocd = DynamicSitemapConfig.class, factory = true)
public class DynamicSitemapServlet extends SlingSafeMethodsServlet {

private static final Logger LOG = LoggerFactory.getLogger(DynamicSitemapServlet.class);

private String rootPath;
private List<String> excludedPaths;
private List<DynamicSitemapServlet> configsList;
@Activate
@Modified
protected void activate(DynamicSitemapConfig config) {
this.rootPath = config.rootPath();
this.excludedPaths = Arrays.asList(config.excludedPaths());
}

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

response.setContentType("application/xml");
response.setCharacterEncoding("UTF-8");

response.getWriter().write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
response.flushBuffer();

ResourceResolver resolver = request.getResourceResolver();
PageManager pageManager = resolver.adaptTo(PageManager.class);

if (pageManager == null) {
LOG.error("PageManager is null.");
response.getWriter().write("<error>Unable to retrieve PageManager.</error>");
return;
}

Page rootPage = pageManager.getPage(rootPath);
if (rootPage == null) {
LOG.error("Root page not found: {}", rootPath);
response.getWriter().write("<error>Root page not found.</error>");
return;
}

LOG.info("Generating sitemap for root page: {}", rootPage.getPath());

String domain = getDomainFromRequest(request);
boolean includeLastModified = shouldIncludeLastModified(rootPage);

// Generate XML sitemap
StringBuilder xml = new StringBuilder();
xml.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");

generateSitemap(rootPage, xml, request, includeLastModified);

xml.append("</urlset>");

// Write final XML content
response.getWriter().write(xml.toString());
response.getWriter().flush();
response.getWriter().close();
}
.................
..........
...........................
}
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @_Anon_ymous_ 

Factory Servlet will not work like this.

You need to register Factory Servlet dynamically based on different config.

 

Please check below example

https://github.com/adobe/aem-core-wcm-components/blob/main/bundles/core/src/main/java/com/adobe/cq/w...

https://github.com/adobe/aem-core-wcm-components/blob/main/bundles/core/src/main/java/com/adobe/cq/w... 

Arun Patidar

AEM LinksLinkedIn

View solution in original post

6 Replies

Avatar

Level 10

Could you please provide more details about scenarios where it does not work well?

In the meantime, you can try using NameConstants.NT_PAGE, which is equal to cq:Page, a very general type that is valid for all AEM pages:

sling.servlet.resourceTypes=NameConstants.NT_PAGE

 

Avatar

Level 1

If I add two configurations, its taking only one configuration and giving that details

ex: root path for first config is: content/site1 and for 2nd config is content/site2 here I'm getting only first root path's details

Avatar

Level 10

Do you have any caching involved in your use case? Is the sitemap always generated for site1, or does it also work for site2? Could it be that the order of the calls affects the output of the servlet?

Avatar

Level 10

I've just noticed that your annotation is incorrect; please note how I utilized sling.servlet.selectors. 

"sling.servlet.methods=GET",
"sling.servlet.selectors=" + "sitemap",
"sling.servlet.resourceTypes=" + NameConstants.NT_PAGE,
"sling.servlet.extensions=" + "xml"

Let me know if this is helpful.

 

Avatar

Level 1

I have corrected this, but what the requirement is if I give root path as /content/site1 
sitemap should be generated for that particular path and the url of site map should be http://localhost:4502/content/site1.sitemap.xml and similarly for other sites too

Avatar

Correct answer by
Community Advisor

Hi @_Anon_ymous_ 

Factory Servlet will not work like this.

You need to register Factory Servlet dynamically based on different config.

 

Please check below example

https://github.com/adobe/aem-core-wcm-components/blob/main/bundles/core/src/main/java/com/adobe/cq/w...

https://github.com/adobe/aem-core-wcm-components/blob/main/bundles/core/src/main/java/com/adobe/cq/w... 

Arun Patidar

AEM LinksLinkedIn