1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.communities.action;
21  
22  import com.liferay.portal.NoSuchLayoutSetException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.ContentTypes;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.LayoutSet;
30  import com.liferay.portal.service.GroupLocalServiceUtil;
31  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.SitemapUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.util.servlet.ServletResponseUtil;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.struts.action.Action;
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="SitemapAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Jorge Ferrer
50   *
51   */
52  public class SitemapAction extends Action {
53  
54      public ActionForward execute(
55              ActionMapping mapping, ActionForm form, HttpServletRequest request,
56              HttpServletResponse response)
57          throws Exception {
58  
59          try {
60              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
61                  WebKeys.THEME_DISPLAY);
62  
63              long groupId = ParamUtil.getLong(request, "groupId");
64              boolean privateLayout = ParamUtil.getBoolean(
65                  request, "privateLayout");
66  
67              LayoutSet layoutSet = null;
68  
69              if (groupId > 0) {
70                  Group group = GroupLocalServiceUtil.getGroup(groupId);
71  
72                  if (group.isStagingGroup()) {
73                      groupId = group.getLiveGroupId();
74                  }
75  
76                  layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
77                      groupId, privateLayout);
78              }
79              else {
80                  String host = PortalUtil.getHost(request);
81  
82                  layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(host);
83              }
84  
85              String sitemap = SitemapUtil.getSitemap(
86                  layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
87                  themeDisplay);
88  
89              ServletResponseUtil.sendFile(
90                  response, null, sitemap.getBytes(StringPool.UTF8),
91                  ContentTypes.TEXT_XML_UTF8);
92          }
93          catch (NoSuchLayoutSetException nslse) {
94              PortalUtil.sendError(
95                  HttpServletResponse.SC_NOT_FOUND, nslse, request, response);
96          }
97          catch (Exception e) {
98              if (_log.isWarnEnabled()) {
99                  _log.warn(e, e);
100             }
101 
102             PortalUtil.sendError(
103                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
104                 response);
105         }
106 
107         return null;
108     }
109 
110     private static Log _log = LogFactoryUtil.getLog(SitemapAction.class);
111 
112 }