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