001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.communities.action;
016    
017    import com.liferay.portal.NoSuchLayoutSetException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.LayoutSet;
025    import com.liferay.portal.service.GroupLocalServiceUtil;
026    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.SitemapUtil;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.util.servlet.ServletResponseUtil;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    import org.apache.struts.action.Action;
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Jorge Ferrer
043     */
044    public class SitemapAction extends Action {
045    
046            public ActionForward execute(
047                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
048                            HttpServletResponse response)
049                    throws Exception {
050    
051                    try {
052                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
053                                    WebKeys.THEME_DISPLAY);
054    
055                            long groupId = ParamUtil.getLong(request, "groupId");
056                            boolean privateLayout = ParamUtil.getBoolean(
057                                    request, "privateLayout");
058    
059                            LayoutSet layoutSet = null;
060    
061                            if (groupId > 0) {
062                                    Group group = GroupLocalServiceUtil.getGroup(groupId);
063    
064                                    if (group.isStagingGroup()) {
065                                            groupId = group.getLiveGroupId();
066                                    }
067    
068                                    layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
069                                            groupId, privateLayout);
070                            }
071                            else {
072                                    String host = PortalUtil.getHost(request);
073    
074                                    layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(host);
075                            }
076    
077                            String sitemap = SitemapUtil.getSitemap(
078                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
079                                    themeDisplay);
080    
081                            ServletResponseUtil.sendFile(
082                                    request, response, null, sitemap.getBytes(StringPool.UTF8),
083                                    ContentTypes.TEXT_XML_UTF8);
084                    }
085                    catch (NoSuchLayoutSetException nslse) {
086                            PortalUtil.sendError(
087                                    HttpServletResponse.SC_NOT_FOUND, nslse, request, response);
088                    }
089                    catch (Exception e) {
090                            if (_log.isWarnEnabled()) {
091                                    _log.warn(e, e);
092                            }
093    
094                            PortalUtil.sendError(
095                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
096                                    response);
097                    }
098    
099                    return null;
100            }
101    
102            private static Log _log = LogFactoryUtil.getLog(SitemapAction.class);
103    
104    }