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.Observable; 028 029import dk.netarkivet.common.exceptions.ArgumentNotValid; 030 031/** 032 * A wrapper class for URI resolver, which also notifies an URIObserver about all URIs visited and their response codes. 033 */ 034public class NotifyingURIResolver extends Observable implements URIResolver, URIResolverHandler { 035 /** The URIResolver used by this NotifyingURIResolver. */ 036 private URIResolver ur; 037 038 /** 039 * Initialise the wrapper. Accepts the class to wrap and the observer to notify. 040 * 041 * @param ur The Wrapped URI resolver 042 * @param uo The URI Observer to notify on each url. 043 * @throws ArgumentNotValid if either argument is null. 044 */ 045 public NotifyingURIResolver(URIResolver ur, URIObserver uo) { 046 ArgumentNotValid.checkNotNull(uo, "URIObserver uo"); 047 addObserver(uo); 048 setURIResolver(ur); 049 } 050 051 /** 052 * Sets the current URIResolver wrapped. 053 * 054 * @param anUR URI resolver to wrap. 055 * @throws ArgumentNotValid if argument is null. 056 */ 057 public void setURIResolver(URIResolver anUR) { 058 ArgumentNotValid.checkNotNull(anUR, "URIResolver anUR"); 059 this.ur = anUR; 060 } 061 062 /** 063 * Passes the uri to the current wrapped resolver and notifies the observer of the result. 064 * 065 * @param request A given request 066 * @param response A given response 067 * @return the response code from the wrapped class 068 * @see URIResolver#lookup(Request, Response) 069 */ 070 public int lookup(Request request, Response response) { 071 int responseCode = ur.lookup(request, response); 072 setChanged(); 073 URI uri = null; 074 if (request != null) { 075 uri = request.getURI(); 076 } 077 notifyObservers(new URIObserver.URIResponseCodePair(uri, responseCode)); 078 return responseCode; 079 } 080}