001    /**
002     * Copyright (c) 2000-2013 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.kernel.util.Validator;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.GroupConstants;
027    import com.liferay.portal.model.LayoutSet;
028    import com.liferay.portal.model.VirtualHost;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
031    import com.liferay.portal.service.VirtualHostLocalServiceUtil;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.PropsValues;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.layoutsadmin.util.SitemapUtil;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    import org.apache.struts.action.Action;
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Jorge Ferrer
048     */
049    public class SitemapAction extends Action {
050    
051            @Override
052            public ActionForward execute(
053                            ActionMapping actionMapping, ActionForm actionForm,
054                            HttpServletRequest request, HttpServletResponse response)
055                    throws Exception {
056    
057                    try {
058                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
059                                    WebKeys.THEME_DISPLAY);
060    
061                            long groupId = ParamUtil.getLong(request, "groupId");
062                            boolean privateLayout = ParamUtil.getBoolean(
063                                    request, "privateLayout");
064    
065                            LayoutSet layoutSet = null;
066    
067                            if (groupId > 0) {
068                                    Group group = GroupLocalServiceUtil.getGroup(groupId);
069    
070                                    if (group.isStagingGroup()) {
071                                            groupId = group.getLiveGroupId();
072                                    }
073    
074                                    layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
075                                            groupId, privateLayout);
076                            }
077                            else {
078                                    String host = PortalUtil.getHost(request);
079    
080                                    host = host.toLowerCase();
081                                    host = host.trim();
082    
083                                    VirtualHost virtualHost =
084                                            VirtualHostLocalServiceUtil.getVirtualHost(host);
085    
086                                    if (virtualHost.getLayoutSetId() != 0) {
087                                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
088                                                    virtualHost.getLayoutSetId());
089                                    }
090                                    else {
091                                            String groupName =
092                                                    PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME;
093    
094                                            if (Validator.isNull(groupName)) {
095                                                    groupName = GroupConstants.GUEST;
096                                            }
097    
098                                            Group group = GroupLocalServiceUtil.getGroup(
099                                                    themeDisplay.getCompanyId(), groupName);
100    
101                                            layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
102                                                    group.getGroupId(), false);
103                                    }
104                            }
105    
106                            String sitemap = SitemapUtil.getSitemap(
107                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
108                                    themeDisplay);
109    
110                            ServletResponseUtil.sendFile(
111                                    request, response, null, sitemap.getBytes(StringPool.UTF8),
112                                    ContentTypes.TEXT_XML_UTF8);
113                    }
114                    catch (NoSuchLayoutSetException nslse) {
115                            PortalUtil.sendError(
116                                    HttpServletResponse.SC_NOT_FOUND, nslse, request, response);
117                    }
118                    catch (Exception e) {
119                            if (_log.isWarnEnabled()) {
120                                    _log.warn(e, e);
121                            }
122    
123                            PortalUtil.sendError(
124                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
125                                    response);
126                    }
127    
128                    return null;
129            }
130    
131            private static Log _log = LogFactoryUtil.getLog(SitemapAction.class);
132    
133    }