Dynamic Sitemap usinf factory Configuration | Community
Skip to main content
March 12, 2025
Solved

Dynamic Sitemap usinf factory Configuration

  • March 12, 2025
  • 2 replies
  • 968 views

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();
}
.................
..........
...........................
}

2 replies

giuseppebaglio
Level 10
March 12, 2025

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

 

March 12, 2025

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

giuseppebaglio
Level 10
March 12, 2025

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?

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
March 13, 2025