001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2012-2014 ForgeRock AS
015 */
016
017package org.forgerock.doc.maven.site;
018
019import org.apache.commons.io.FileUtils;
020import org.apache.maven.plugin.MojoExecutionException;
021import org.forgerock.doc.maven.AbstractDocbkxMojo;
022import org.forgerock.doc.maven.utils.HtmlUtils;
023
024import java.io.File;
025import java.io.IOException;
026
027/**
028 * Add {@code .htaccess} file to the site layout.
029 */
030public class Htaccess {
031
032    /**
033     * The Mojo that holds configuration and related methods.
034     */
035    private AbstractDocbkxMojo m;
036
037    /**
038     * Constructor setting the Mojo that holds the configuration.
039     *
040     * @param mojo The Mojo that holds the configuration.
041     */
042    public Htaccess(final AbstractDocbkxMojo mojo) {
043        m = mojo;
044    }
045
046    /**
047     * Add {@code .htaccess} file to the site layout.
048     *
049     * @throws MojoExecutionException Failed to copy file.
050     */
051    public void execute() throws MojoExecutionException {
052        final String layoutDir = new File(m.getSiteDirectory(), "doc").getPath();
053        final File htaccess = new File(m.getBuildDirectory(), ".htaccess");
054
055        FileUtils.deleteQuietly(htaccess);
056        try {
057            FileUtils.copyURLToFile(getClass().getResource("/.htaccess"), htaccess);
058            HtmlUtils.addHtaccess(layoutDir, htaccess);
059        } catch (IOException e) {
060            throw new MojoExecutionException("Failed to copy .htaccess: " + e.getMessage(), e);
061        }
062    }
063}