001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.robots;
017
018import java.io.IOException;
019
020import org.apache.avalon.framework.parameters.ParameterException;
021import org.apache.cocoon.ProcessingException;
022import org.apache.cocoon.xml.XMLUtils;
023import org.xml.sax.SAXException;
024
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.metadata.CompositeMetadata;
027import org.ametys.web.repository.page.Page;
028import org.ametys.web.repository.page.jcr.DefaultPage;
029import org.ametys.web.repository.site.Site;
030import org.ametys.web.repository.sitemap.Sitemap;
031
032/**
033 * Generates robots.txt file
034 */
035public class RobotsGenerator extends AbstractRobotsGenerator
036{
037
038    private static final String DISALLOW_LABEL = "Disallow:";
039
040    private static final String NEW_LINE = System.getProperty("line.separator");
041
042    private static final String SITEMAP_LABEL = "Sitemap:";
043
044    private static final String SPACE_CHAR = " ";
045
046    private static final String TEXT_TAG = "text";
047
048    private static final String USER_AGENT_LABEL = "User-agent:";
049
050    private void _serializePageInformation(Page page, StringBuilder builder)
051    {
052        CompositeMetadata metadataHolder = page.getMetadataHolder();
053        boolean disallow = metadataHolder.getBoolean(DefaultPage.METADATA_ROBOTS_DISALLOW, false);
054
055        if (disallow)
056        {
057            builder.append(DISALLOW_LABEL).append(SPACE_CHAR).append("/" + page.getSitemapName() + "/" + page.getPathInSitemap() + ".html" + NEW_LINE);
058            builder.append(DISALLOW_LABEL).append(SPACE_CHAR).append("/" + page.getSitemapName() + "/" + page.getPathInSitemap() + "/" + NEW_LINE);
059        }
060        else
061        {
062            AmetysObjectIterable< ? extends Page> childrenPages = page.getChildrenPages();
063            for (Page child : childrenPages)
064            {
065                _serializePageInformation(child, builder);
066            }
067        }
068
069    }
070
071    private void _serializeSitemapProperty(Site site, StringBuilder builder)
072    {
073        builder.append(SITEMAP_LABEL).append(" ").append(site.getUrl()).append("/sitemap.xml");
074    }
075
076    @Override
077    public void generate() throws IOException, SAXException, ProcessingException
078    {
079        String siteName;
080        try
081        {
082            siteName = parameters.getParameter("siteName");
083
084            Site site = getSite(siteName);
085
086            StringBuilder builder = new StringBuilder();
087            // User agent information
088            builder.append(USER_AGENT_LABEL).append(" *").append(NEW_LINE);
089            // Pages informations
090            for (Sitemap sitemap : site.getSitemaps())
091            {
092                for (Page page : sitemap.getChildrenPages())
093                {
094                    _serializePageInformation(page, builder);
095                }
096            }
097            // Sitemap URL
098            _serializeSitemapProperty(site, builder);
099            contentHandler.startDocument();
100            XMLUtils.createElement(contentHandler, TEXT_TAG, builder.toString());
101            contentHandler.endDocument();
102        }
103        catch (ParameterException e)
104        {
105            getLogger().error("Error when getting site name", e);
106        }
107    }
108}