1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class SitemapAction extends Action {
55  
56      public ActionForward execute(
57              ActionMapping mapping, ActionForm form, HttpServletRequest request,
58              HttpServletResponse response)
59          throws Exception {
60  
61          try {
62              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
63                  WebKeys.THEME_DISPLAY);
64  
65              long groupId = ParamUtil.getLong(request, "groupId");
66              boolean privateLayout = ParamUtil.getBoolean(
67                  request, "privateLayout");
68  
69              LayoutSet layoutSet = null;
70  
71              if (groupId > 0) {
72                  Group group = GroupLocalServiceUtil.getGroup(groupId);
73  
74                  if (group.isStagingGroup()) {
75                      groupId = group.getLiveGroupId();
76                  }
77  
78                  layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
79                      groupId, privateLayout);
80              }
81              else {
82                  String host = PortalUtil.getHost(request);
83  
84                  layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(host);
85              }
86  
87              String sitemap = SitemapUtil.getSitemap(
88                  layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
89                  themeDisplay);
90  
91              ServletResponseUtil.sendFile(
92                  response, null, sitemap.getBytes(StringPool.UTF8),
93                  ContentTypes.TEXT_XML_UTF8);
94          }
95          catch (NoSuchLayoutSetException nslse) {
96              PortalUtil.sendError(
97                  HttpServletResponse.SC_NOT_FOUND, nslse, request, response);
98          }
99          catch (Exception e) {
100             if (_log.isWarnEnabled()) {
101                 _log.warn(e, e);
102             }
103 
104             PortalUtil.sendError(
105                 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
106                 response);
107         }
108 
109         return null;
110     }
111 
112     private static Log _log = LogFactoryUtil.getLog(SitemapAction.class);
113 
114 }