1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.communities.action;
16  
17  import com.liferay.portal.NoSuchLayoutSetException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.ContentTypes;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.model.Group;
24  import com.liferay.portal.model.LayoutSet;
25  import com.liferay.portal.service.GroupLocalServiceUtil;
26  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.SitemapUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.util.servlet.ServletResponseUtil;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.apache.struts.action.Action;
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="SitemapAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Jorge Ferrer
45   */
46  public class SitemapAction extends Action {
47  
48      public ActionForward execute(
49              ActionMapping mapping, ActionForm form, HttpServletRequest request,
50              HttpServletResponse response)
51          throws Exception {
52  
53          try {
54              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
55                  WebKeys.THEME_DISPLAY);
56  
57              long groupId = ParamUtil.getLong(request, "groupId");
58              boolean privateLayout = ParamUtil.getBoolean(
59                  request, "privateLayout");
60  
61              LayoutSet layoutSet = null;
62  
63              if (groupId > 0) {
64                  Group group = GroupLocalServiceUtil.getGroup(groupId);
65  
66                  if (group.isStagingGroup()) {
67                      groupId = group.getLiveGroupId();
68                  }
69  
70                  layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
71                      groupId, privateLayout);
72              }
73              else {
74                  String host = PortalUtil.getHost(request);
75  
76                  layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(host);
77              }
78  
79              String sitemap = SitemapUtil.getSitemap(
80                  layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
81                  themeDisplay);
82  
83              ServletResponseUtil.sendFile(
84                  request, response, null, sitemap.getBytes(StringPool.UTF8),
85                  ContentTypes.TEXT_XML_UTF8);
86          }
87          catch (NoSuchLayoutSetException nslse) {
88              PortalUtil.sendError(
89                  HttpServletResponse.SC_NOT_FOUND, nslse, request, response);
90          }
91          catch (Exception e) {
92              if (_log.isWarnEnabled()) {
93                  _log.warn(e, e);
94              }
95  
96              PortalUtil.sendError(
97                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
98                  response);
99          }
100 
101         return null;
102     }
103 
104     private static Log _log = LogFactoryUtil.getLog(SitemapAction.class);
105 
106 }