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.LayoutSetVirtualHostException;
018    import com.liferay.portal.NoSuchLayoutSetException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Company;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.LayoutSet;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
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                            try {
060                                    layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(host);
061                            }
062                            catch (LayoutSetVirtualHostException lsvhe) {
063                                    Company company = PortalUtil.getCompany(request);
064    
065                                    if (host.equals(company.getVirtualHostname()) &&
066                                            Validator.isNotNull(
067                                                    PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME)) {
068    
069                                            Group defaultGroup = GroupLocalServiceUtil.getGroup(
070                                                    company.getCompanyId(),
071                                                    PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);
072    
073                                            layoutSet = defaultGroup.getPublicLayoutSet();
074                                    }
075                            }
076                            catch (NoSuchLayoutSetException nslse) {
077                            }
078    
079                            String robots = RobotsUtil.getRobots(layoutSet);
080    
081                            ServletResponseUtil.sendFile(
082                                    request, response, null, robots.getBytes(StringPool.UTF8),
083                                    ContentTypes.TEXT_PLAIN_UTF8);
084                    }
085                    catch (Exception e) {
086                            if (_log.isWarnEnabled()) {
087                                    _log.warn(e, e);
088                            }
089    
090                            PortalUtil.sendError(
091                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
092                                    response);
093                    }
094    
095                    return null;
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(RobotsAction.class);
099    
100    }