1   /**
2    * Copyright (c) 2000-2009 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.portlet.communities.action;
24  
25  import com.liferay.portal.NoSuchLayoutSetException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ContentTypes;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.model.LayoutSet;
33  import com.liferay.portal.service.GroupLocalServiceUtil;
34  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.SitemapUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.util.servlet.ServletResponseUtil;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.struts.action.Action;
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="SitemapAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Jorge Ferrer
53   *
54   */
55  public class SitemapAction extends Action {
56  
57      public ActionForward execute(
58              ActionMapping mapping, ActionForm form, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          try {
63              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
64                  WebKeys.THEME_DISPLAY);
65  
66              long groupId = ParamUtil.getLong(request, "groupId");
67              boolean privateLayout = ParamUtil.getBoolean(
68                  request, "privateLayout");
69  
70              LayoutSet layoutSet = null;
71  
72              if (groupId > 0) {
73                  Group group = GroupLocalServiceUtil.getGroup(groupId);
74  
75                  if (group.isStagingGroup()) {
76                      groupId = group.getLiveGroupId();
77                  }
78  
79                  layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
80                      groupId, privateLayout);
81              }
82              else {
83                  String host = PortalUtil.getHost(request);
84  
85                  layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(host);
86              }
87  
88              String sitemap = SitemapUtil.getSitemap(
89                  layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
90                  themeDisplay);
91  
92              ServletResponseUtil.sendFile(
93                  response, null, sitemap.getBytes(StringPool.UTF8),
94                  ContentTypes.TEXT_XML_UTF8);
95          }
96          catch (NoSuchLayoutSetException nslse) {
97              PortalUtil.sendError(
98                  HttpServletResponse.SC_NOT_FOUND, nslse, request, response);
99          }
100         catch (Exception e) {
101             if (_log.isWarnEnabled()) {
102                 _log.warn(e, e);
103             }
104 
105             PortalUtil.sendError(
106                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
107                 response);
108         }
109 
110         return null;
111     }
112 
113     private static Log _log = LogFactoryUtil.getLog(SitemapAction.class);
114 
115 }