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