1
22
23 package com.liferay.portal.servlet;
24
25 import com.liferay.portal.NoSuchLayoutSetException;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.model.LayoutSet;
28 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
29 import com.liferay.portal.util.PortalUtil;
30 import com.liferay.portal.util.SitemapUtil;
31
32 import java.io.IOException;
33 import java.io.OutputStreamWriter;
34
35 import javax.servlet.ServletException;
36 import javax.servlet.http.HttpServlet;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43
49 public class SitemapServlet extends HttpServlet {
50
51 public void service(HttpServletRequest req, HttpServletResponse res)
52 throws IOException, ServletException {
53
54 res.setContentType("text/xml; charset=UTF-8");
55
56 OutputStreamWriter out = new OutputStreamWriter(res.getOutputStream());
57
58 try {
59 String host = PortalUtil.getHost(req);
60
61 long groupId = ParamUtil.getLong(req, "groupId");
62 boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
63
64 LayoutSet layoutSet = null;
65
66 if (groupId > 0) {
67 layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
68 groupId, privateLayout);
69 }
70 else {
71 layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(host);
72 }
73
74 String portalURL = PortalUtil.getPortalURL(
75 host, req.getServerPort(), req.isSecure());
76
77 String mainPath = PortalUtil.PATH_MAIN;
78
79 String sitemap = SitemapUtil.getSitemap(
80 layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
81 portalURL + mainPath);
82
83 if (!res.isCommitted()) {
84 out.write(sitemap);
85 }
86 }
87 catch (NoSuchLayoutSetException e) {
88 res.sendError(HttpServletResponse.SC_NOT_FOUND);
89 }
90 catch (Exception e) {
91 if (_log.isWarnEnabled()) {
92 _log.warn(e, e);
93 }
94
95 res.sendError(
96 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
97 }
98 finally {
99 out.flush();
100 out.close();
101 }
102 }
103
104 private static Log _log = LogFactory.getLog(SitemapServlet.class);
105
106 }