1   /**
2    * Copyright (c) 2000-2007 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.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  /**
44   * <a href="SitemapServlet.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Jorge Ferrer
47   *
48   */
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 }