001    /**
002     * Copyright (c) 2000-2012 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.layoutsadmin.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.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.ContentTypes;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.LayoutSet;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.layoutsadmin.util.SitemapUtil;
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            @Override
047            public ActionForward execute(
048                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
049                            HttpServletResponse response)
050                    throws Exception {
051    
052                    try {
053                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
054                                    WebKeys.THEME_DISPLAY);
055    
056                            long groupId = ParamUtil.getLong(request, "groupId");
057                            boolean privateLayout = ParamUtil.getBoolean(
058                                    request, "privateLayout");
059    
060                            LayoutSet layoutSet = null;
061    
062                            if (groupId > 0) {
063                                    Group group = GroupLocalServiceUtil.getGroup(groupId);
064    
065                                    if (group.isStagingGroup()) {
066                                            groupId = group.getLiveGroupId();
067                                    }
068    
069                                    layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
070                                            groupId, privateLayout);
071                            }
072                            else {
073                                    String host = PortalUtil.getHost(request);
074    
075                                    layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(host);
076                            }
077    
078                            String sitemap = SitemapUtil.getSitemap(
079                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
080                                    themeDisplay);
081    
082                            ServletResponseUtil.sendFile(
083                                    request, response, null, sitemap.getBytes(StringPool.UTF8),
084                                    ContentTypes.TEXT_XML_UTF8);
085                    }
086                    catch (NoSuchLayoutSetException nslse) {
087                            PortalUtil.sendError(
088                                    HttpServletResponse.SC_NOT_FOUND, nslse, request, response);
089                    }
090                    catch (Exception e) {
091                            if (_log.isWarnEnabled()) {
092                                    _log.warn(e, e);
093                            }
094    
095                            PortalUtil.sendError(
096                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
097                                    response);
098                    }
099    
100                    return null;
101            }
102    
103            private static Log _log = LogFactoryUtil.getLog(SitemapAction.class);
104    
105    }