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