001    /**
002     * Copyright (c) 2000-present 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.portal.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.LayoutSet;
023    import com.liferay.util.ContentUtil;
024    
025    /**
026     * @author David Truong
027     */
028    public class RobotsUtil {
029    
030            public static String getDefaultRobots() {
031                    return getDefaultRobots(null);
032            }
033    
034            public static String getDefaultRobots(String virtualHost) {
035                    if (Validator.isNotNull(virtualHost)) {
036                            String content = ContentUtil.get(
037                                    PropsValues.ROBOTS_TXT_WITH_SITEMAP);
038    
039                            content = StringUtil.replace(content, "[$HOST$]", virtualHost);
040    
041                            return content;
042                    }
043    
044                    return ContentUtil.get(PropsValues.ROBOTS_TXT_WITHOUT_SITEMAP);
045            }
046    
047            public static String getRobots(LayoutSet layoutSet) throws PortalException {
048                    if (layoutSet == null) {
049                            return getDefaultRobots(null);
050                    }
051    
052                    Group group = layoutSet.getGroup();
053    
054                    return GetterUtil.get(
055                            group.getTypeSettingsProperty(
056                                    layoutSet.isPrivateLayout() + "-robots.txt"),
057                            getDefaultRobots(PortalUtil.getVirtualHostname(layoutSet)));
058            }
059    
060    }