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 java.net.URI;
027import java.util.Collections;
028import java.util.Set;
029import java.util.TreeSet;
030
031/**
032 * This class handles recordings of URIs not found during URI lookup.
033 */
034
035public class MissingURIRecorder extends URIObserver {
036    /**
037     * The recorded list of URIs. We use TreeSet which removes duplicates and sorts the entries.
038     */
039    private Set<URI> uriSet = Collections.synchronizedSortedSet(new TreeSet<URI>());
040
041    /**
042     * Indicates whether we are actively recording reported URIs at the moment.
043     */
044    private boolean recordingURIs;
045
046    /**
047     * Start recording missing URIs.
048     */
049    public void startRecordingURIs() {
050        recordingURIs = true;
051    }
052
053    /**
054     * Stop recording missing URIs.
055     */
056    public void stopRecordingURIs() {
057        recordingURIs = false;
058    }
059
060    /**
061     * Clear list of missing URIs.
062     */
063    public void clearRecordedURIs() {
064        uriSet = Collections.synchronizedSortedSet(new TreeSet<URI>());
065    }
066
067    /**
068     * Getter for the recorded missing URIs.
069     *
070     * @return the recorded URIs, as a sorted set. Note that this is the primary copy, so don't modify it!
071     */
072    public Set<URI> getRecordedURIs() {
073        return uriSet;
074    }
075
076    /**
077     * If we are recording URIs, and the response code is NOT_FOUND, then add URI to the list of missing URIs.
078     *
079     * @param uri The URI observed.
080     * @param responseCode The responsecode of the uri.
081     */
082    public void notify(URI uri, int responseCode) {
083        if (recordingURIs && responseCode == URIResolver.NOT_FOUND) {
084            uriSet.add(uri);
085        }
086    }
087
088    /**
089     * Returns whether we are currently collecting URIs.
090     *
091     * @return True if we are currently collecting URIs.
092     */
093    public boolean isRecordingURIs() {
094        return recordingURIs;
095    }
096}