001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.viewerproxy;
025
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import dk.netarkivet.common.exceptions.ArgumentNotValid;
030import dk.netarkivet.common.exceptions.IOFailure;
031import dk.netarkivet.harvester.datamodel.H3HeritrixTemplate;
032
033/**
034 * Wrapper for an URIResolver, which gives failures on specific specific URLs, and forwards all others to the wrapped
035 * handler. This allows you to reserve a specific host for commands and get a well-defined error if giving an undefined
036 * command.
037 */
038public class UnknownCommandResolver extends CommandResolver {
039    /** Logger for this class. */
040         private static final Logger log = LoggerFactory.getLogger(H3HeritrixTemplate.class);
041
042    /**
043     * Make a new UnknownCommandResolver, which gives an error for any command- like URL and forwards other URLs to the
044     * given URIResolver.
045     *
046     * @param ur The URIResolver to handle all other uris.
047     * @throws ArgumentNotValid if either argument is null.
048     */
049    public UnknownCommandResolver(URIResolver ur) {
050        super(ur);
051    }
052
053    /**
054     * Helper method that checks if this is a command URL and throw an error if it is.
055     *
056     * @param request The request to check
057     * @param response The response to give command results to if it is a command
058     * @return Whether this was a command URL
059     */
060    protected boolean executeCommand(Request request, Response response) {
061        // If the url is for this host (potential command)
062        if (isCommandHostRequest(request)) {
063            log.debug("Blocking unknown command " + request.getURI());
064            throw new IOFailure("Bad request: '" + request.getURI() + "':\n" + "Unknown command");
065        }
066        return false;
067    }
068}