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.io.HttpUtils;
039import org.openimaj.util.function.MultiFunction;
040import org.openimaj.web.scraping.SiteSpecificConsumer;
041
042/**
043 * This class implements a function that will given an input URL outputs a list
044 * of URLs based on applying a list of {@link SiteSpecificConsumer}s to the
045 * input.
046 * 
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 */
049public class SiteSpecificURLExtractor implements MultiFunction<URL, URL> {
050        private static final Logger logger = Logger.getLogger(SiteSpecificURLExtractor.class);
051
052        /**
053         * the site specific consumers
054         */
055        protected List<SiteSpecificConsumer> siteSpecific = new ArrayList<SiteSpecificConsumer>();
056
057        /**
058         * Construct with the given list of consumers.
059         * 
060         * @param consumers
061         *            the consumers
062         */
063        public SiteSpecificURLExtractor(List<SiteSpecificConsumer> consumers) {
064                this.siteSpecific = consumers;
065        }
066
067        /**
068         * Construct with the given consumers.
069         * 
070         * @param consumers
071         *            the consumers
072         */
073        public SiteSpecificURLExtractor(SiteSpecificConsumer... consumers) {
074                this.siteSpecific = Arrays.asList(consumers);
075        }
076
077        /**
078         * Construct with empty list of consumers.
079         */
080        protected SiteSpecificURLExtractor() {
081                this.siteSpecific = new ArrayList<SiteSpecificConsumer>();
082        }
083
084        @Override
085        public List<URL> apply(URL in) {
086                final List<URL> imageUrls = processURLs(in);
087
088                if (imageUrls == null)
089                        return new ArrayList<URL>();
090
091                return imageUrls;
092        }
093
094        /**
095         * First, try all the {@link SiteSpecificConsumer} instances loaded into
096         * {@link #siteSpecific}. If any consumer takes control of a link the
097         * consumer's output is used
098         * 
099         * if this fails use
100         * {@link HttpUtils#readURLAsByteArrayInputStream(URL, org.apache.http.client.RedirectStrategy)}
101         * with a {@link StatusConsumerRedirectStrategy} which specifically
102         * disallows redirects to be dealt with automatically and forces this
103         * function to be called for each redirect.
104         * 
105         * @param url
106         * @return a list of images or null
107         */
108        protected List<URL> processURLs(URL url) {
109                logger.debug("Resolving URL: " + url);
110                logger.debug("Attempting site specific consumers");
111
112                for (final SiteSpecificConsumer consumer : siteSpecific) {
113                        if (consumer.canConsume(url)) {
114                                logger.debug("Site specific consumer: " + consumer.getClass().getName() + " working on link");
115                                final List<URL> urlList = consumer.consume(url);
116
117                                if (urlList != null && !urlList.isEmpty()) {
118                                        logger.debug("Site specific consumer returned non-null, returning the URLs");
119
120                                        return urlList;
121                                }
122                        }
123                }
124                return null;
125        }
126}