001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Company;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.LayoutSet;
026    import com.liferay.portal.service.CompanyLocalServiceUtil;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.util.ContentUtil;
029    
030    /**
031     * @author David Truong
032     */
033    public class RobotsUtil {
034    
035            public static String getDefaultRobots() {
036                    return getDefaultRobots(null);
037            }
038    
039            public static String getDefaultRobots(String virtualHost) {
040                    if (Validator.isNotNull(virtualHost)) {
041                            String content = ContentUtil.get(
042                                    PropsValues.ROBOTS_TXT_WITH_SITEMAP);
043    
044                            content = StringUtil.replace(content, "[$HOST$]", virtualHost);
045    
046                            return content;
047                    }
048    
049                    return ContentUtil.get(PropsValues.ROBOTS_TXT_WITHOUT_SITEMAP);
050            }
051    
052            public static String getRobots(LayoutSet layoutSet)
053                    throws PortalException, SystemException {
054    
055                    if (layoutSet == null) {
056                            return getDefaultRobots(null);
057                    }
058    
059                    String virtualHostname = StringPool.BLANK;
060    
061                    try {
062                            virtualHostname = layoutSet.getVirtualHostname();
063                    }
064                    catch (Exception e) {
065                    }
066    
067                    if (Validator.isNull(virtualHostname) &&
068                            Validator.isNotNull(PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME) ) {
069    
070                            Group group = GroupLocalServiceUtil.getGroup(
071                                    layoutSet.getCompanyId(),
072                                    PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);
073    
074                            if (layoutSet.getGroupId() == group.getGroupId()) {
075                                    Company company = CompanyLocalServiceUtil.getCompany(
076                                            layoutSet.getCompanyId());
077    
078                                    virtualHostname = company.getVirtualHostname();
079                            }
080                    }
081    
082                    Group group = layoutSet.getGroup();
083    
084                    return GetterUtil.get(
085                            group.getTypeSettingsProperty(
086                                    layoutSet.isPrivateLayout() + "-robots.txt"),
087                                    getDefaultRobots(virtualHostname));
088            }
089    
090    }