Customize Sitemap

Simone Romei, modified 14 Years ago. New Member Posts: 2 Join Date: 6/22/11 Recent Posts
In liferay 5.X (maybe even in 6) only not hidden page can be part of sitemap.xml.

For customize xml generation the only need is:

- put in struts-config-ext.xml

<action path="/layout_management/sitemap" type="package.SitemapActionHiddenPage" />

and create custom class like:

public class SitemapActionHiddenPage extends Action {

public ActionForward execute(
ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response)
throws Exception {

try {
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
WebKeys.THEME_DISPLAY);

long groupId = ParamUtil.getLong(request, "groupId");
boolean privateLayout = ParamUtil.getBoolean(
request, "privateLayout");

LayoutSet layoutSet = null;

if (groupId > 0) {
Group group = GroupLocalServiceUtil.getGroup(groupId);

if (group.isStagingGroup()) {
groupId = group.getLiveGroupId();
}

layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
groupId, privateLayout);
}
else {
String host = PortalUtil.getHost(request);

layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(host);
}

String sitemap = SitemapUtilHiddenPage.getSitemap(
layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
themeDisplay);

// ServletResponseUtil.sendFile(
// response, null, sitemap.getBytes(StringPool.UTF8),
// ContentTypes.TEXT_XML_UTF8);

response.setContentType(ContentTypes.TEXT_XML_UTF8);
response.getWriter().print(sitemap);

}
catch (NoSuchLayoutSetException nslse) {
PortalUtil.sendError(
HttpServletResponse.SC_NOT_FOUND, nslse, request, response);
}
catch (Exception e) {
if (_log.isWarnEnabled()) {
_log.warn(e, e);
}

PortalUtil.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
response);
}

return null;
}

With a Sitemap generator like

public class SitemapUtil {

public static String getSitemap(
long groupId, boolean privateLayout, ThemeDisplay themeDisplay)
throws PortalException, SystemException {

Document doc = SAXReaderUtil.createDocument();

doc.setXMLEncoding(StringPool.UTF8);

Element root = doc.addElement(
"urlset", "http://www.google.com/schemas/sitemap/0.84");

List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
groupId, privateLayout);

_visitLayouts(root, layouts, themeDisplay);

return doc.asXML();
}

public static String encodeXML(String input) {
return StringUtil.replace(
input,
new String[] {"&", "<", ">", "'", "\""},
new String[] {"&amp;", "&lt;", "&gt;", "&apos;", "&quot;"});
}

private static void _visitLayouts(
Element element, List<Layout> layouts, ThemeDisplay themeDisplay)
throws PortalException, SystemException {

for (Layout layout : layouts) {
UnicodeProperties props = layout.getTypeSettingsProperties();

if (PortalUtil.isLayoutSitemapable(layout) && !layout.isHidden() &&
GetterUtil.getBoolean(
props.getProperty("sitemap-include"), true)) {

Element url = element.addElement("url");

String layoutURL = PortalUtil.getLayoutURL(
layout, themeDisplay);

if (!HttpUtil.hasDomain(layoutURL)) {
layoutURL = themeDisplay.getPortalURL() + layoutURL;
}

url.addElement("loc").addText(encodeXML(layoutURL));

String changefreq = props.getProperty("sitemap-changefreq");

if (Validator.isNotNull(changefreq)) {
url.addElement("changefreq").addText(changefreq);
}

String priority = props.getProperty("sitemap-priority");

if (Validator.isNotNull(priority)) {
url.addElement("priority").addText(priority);
}

List<Layout> children = layout.getChildren();

_visitLayouts(element, children, themeDisplay);
}
}
}

}