001 /* 002 * Copyright 1999,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.feedparser.locate.blogservice; 018 019 import org.apache.commons.feedparser.FeedParserException; 020 import org.apache.commons.feedparser.locate.*; 021 022 /** 023 * Models the Flickr image blog service, encapsulating whether a given weblog 024 * is this type of service and where it usually keeps its feeds. 025 * 026 * @author Brad Neuberg, bkn3@columbia.edu 027 */ 028 public class Flickr extends BlogService { 029 030 /** Returns whether we can trust the results of this blog service's 031 * autodiscovery links. For example, TextAmerica returns invalid 032 * autodiscovery results. 033 */ 034 public boolean hasValidAutoDiscovery() { 035 return true; 036 } 037 038 /** Returns whether we should follow HTTP redirects for this blog service. 039 * Some services don't implement HTTP redirects correctly, while others, 040 * like Xanga, require it. 041 */ 042 public boolean followRedirects() { 043 return false; 044 } 045 046 /** Determines if the weblog at the given resource and with the given 047 * content is this blog service. 048 * @param resource A full URI to this resource, such as 049 * "http://www.codinginparadise.org". 050 * @param content The full HTML content at the resource's URL. 051 * @throws FeedParserException Thrown if an error occurs while 052 * determining the type of this weblog. 053 */ 054 public boolean isThisService(String resource, String content) 055 throws FeedParserException { 056 return resource.indexOf( "flickr.com" ) != -1; 057 } 058 059 /** 060 * Returns an array of FeedReferences that contains information on the 061 * usual locations this blog service contains its feed. The feeds should 062 * be ordered by quality, so that higher quality feeds come before lower 063 * quality ones (i.e. you would want to have an Atom FeedReference 064 * object come before an RSS 0.91 FeedReference object in this list). 065 * @param resource A URL to the given weblog that might be used to build 066 * up where feeds are usually located. 067 * @param content The full content of the resource URL, which might 068 * be useful to determine where feeds are usually located. This can be 069 * null. 070 * @throws FeedParserException Thrown if an error occurs while trying 071 * to determine the usual locations of feeds for this service. 072 */ 073 public FeedReference[] getFeedLocations(String resource, 074 String content) 075 throws FeedParserException { 076 resource = getBaseFeedPath(resource); 077 // * Input: http://flickr.com/photos/tags/cats/ 078 // * 079 // * Output: http://flickr.com/services/feeds/photos_public.gne?tags=cats&format=atom_03 080 081 if ( resource == null ) 082 return new FeedReference[0]; 083 084 int begin = resource.indexOf( "/tags/" ); 085 086 //we can't continue here. 087 if ( begin == -1 ) 088 return new FeedReference[0]; 089 090 begin += 6; 091 092 int end = resource.lastIndexOf( "/" ); 093 if ( end == -1 || end < begin ) 094 end = resource.length(); 095 096 String tag = resource.substring( begin, end ); 097 098 String location = "http://flickr.com/services/feeds/photos_public.gne?tags=" + 099 tag + 100 "&format=atom_03"; 101 102 FeedReference flickrLocations[] = 103 { new FeedReference(location, 104 FeedReference.ATOM_MEDIA_TYPE) }; 105 106 return flickrLocations; 107 } 108 }