001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.stream.functions; 031 032import java.net.URL; 033import java.util.ArrayList; 034import java.util.Arrays; 035import java.util.List; 036 037import org.apache.log4j.Logger; 038import org.openimaj.util.function.MultiFunction; 039import org.openimaj.web.scraping.SiteSpecificConsumer; 040 041/** 042 * This class implements a function that will given an input URL outputs a list 043 * of URLs based on applying a list of {@link SiteSpecificConsumer}s to the 044 * input. 045 * 046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 047 */ 048public class SiteSpecificURLExtractor implements MultiFunction<URL, URL> { 049 private static final Logger logger = Logger.getLogger(SiteSpecificURLExtractor.class); 050 051 /** 052 * the site specific consumers 053 */ 054 protected List<SiteSpecificConsumer> siteSpecific = new ArrayList<SiteSpecificConsumer>(); 055 056 /** 057 * Construct with the given list of consumers. 058 * 059 * @param consumers 060 * the consumers 061 */ 062 public SiteSpecificURLExtractor(List<SiteSpecificConsumer> consumers) { 063 this.siteSpecific = consumers; 064 } 065 066 /** 067 * Construct with the given consumers. 068 * 069 * @param consumers 070 * the consumers 071 */ 072 public SiteSpecificURLExtractor(SiteSpecificConsumer... consumers) { 073 this.siteSpecific = Arrays.asList(consumers); 074 } 075 076 /** 077 * Construct with empty list of consumers. 078 */ 079 protected SiteSpecificURLExtractor() { 080 this.siteSpecific = new ArrayList<SiteSpecificConsumer>(); 081 } 082 083 @Override 084 public List<URL> apply(URL in) { 085 final List<URL> imageUrls = processURLs(in); 086 087 if (imageUrls == null) 088 return new ArrayList<URL>(); 089 090 return imageUrls; 091 } 092 093 /** 094 * First, try all the {@link SiteSpecificConsumer} instances loaded into 095 * {@link #siteSpecific}. If any consumer takes control of a link the 096 * consumer's output is used 097 * 098 * if this fails use 099 * {@link HttpUtils#readURLAsByteArrayInputStream(URL, org.apache.http.client.RedirectStrategy)} 100 * with a {@link StatusConsumerRedirectStrategy} which specifically 101 * disallows redirects to be dealt with automatically and forces this 102 * function to be called for each redirect. 103 * 104 * @param url 105 * @return a list of images or null 106 */ 107 protected List<URL> processURLs(URL url) { 108 logger.debug("Resolving URL: " + url); 109 logger.debug("Attempting site specific consumers"); 110 111 for (final SiteSpecificConsumer consumer : siteSpecific) { 112 if (consumer.canConsume(url)) { 113 logger.debug("Site specific consumer: " + consumer.getClass().getName() + " working on link"); 114 final List<URL> urlList = consumer.consume(url); 115 116 if (urlList != null && !urlList.isEmpty()) { 117 logger.debug("Site specific consumer returned non-null, returning the URLs"); 118 119 return urlList; 120 } 121 } 122 } 123 return null; 124 } 125}