1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
45   * <a href="SitemapServlet.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   *
49   */
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 }