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.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.LayoutSet;
027    import com.liferay.portal.model.VirtualHost;
028    import com.liferay.portal.service.GroupLocalServiceUtil;
029    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
030    import com.liferay.portal.service.VirtualHostLocalServiceUtil;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portal.util.RobotsUtil;
034    
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    import org.apache.struts.action.Action;
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author David Truong
045     */
046    public class RobotsAction extends Action {
047    
048            @Override
049            public ActionForward execute(
050                            ActionMapping actionMapping, ActionForm actionForm,
051                            HttpServletRequest request, HttpServletResponse response)
052                    throws Exception {
053    
054                    try {
055                            String host = GetterUtil.getString(PortalUtil.getHost(request));
056    
057                            LayoutSet layoutSet = null;
058    
059                            VirtualHost virtualHost =
060                                    VirtualHostLocalServiceUtil.fetchVirtualHost(host);
061    
062                            if ((virtualHost != null) && (virtualHost.getLayoutSetId() > 0)) {
063                                    layoutSet = LayoutSetLocalServiceUtil.fetchLayoutSet(host);
064                            }
065                            else {
066                                    Company company = PortalUtil.getCompany(request);
067    
068                                    if (host.equals(company.getVirtualHostname()) &&
069                                            Validator.isNotNull(
070                                                    PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME)) {
071    
072                                            Group defaultGroup = GroupLocalServiceUtil.getGroup(
073                                                    company.getCompanyId(),
074                                                    PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);
075    
076                                            layoutSet = defaultGroup.getPublicLayoutSet();
077                                    }
078                            }
079    
080                            String robots = RobotsUtil.getRobots(layoutSet);
081    
082                            ServletResponseUtil.sendFile(
083                                    request, response, null, robots.getBytes(StringPool.UTF8),
084                                    ContentTypes.TEXT_PLAIN_UTF8);
085                    }
086                    catch (Exception e) {
087                            if (_log.isWarnEnabled()) {
088                                    _log.warn(e, e);
089                            }
090    
091                            PortalUtil.sendError(
092                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
093                                    response);
094                    }
095    
096                    return null;
097            }
098    
099            private static Log _log = LogFactoryUtil.getLog(RobotsAction.class);
100    
101    }