View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package com.sun.syndication.fetcher.samples;
19  import java.net.URL;
20  
21  import com.sun.syndication.feed.synd.SyndFeedI;
22  import com.sun.syndication.fetcher.FeedFetcherI;
23  import com.sun.syndication.fetcher.FetcherEvent;
24  import com.sun.syndication.fetcher.FetcherListener;
25  import com.sun.syndication.fetcher.impl.FeedFetcherCacheI;
26  import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
27  import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;
28  /***
29   * Reads and prints any RSS/Atom feed type. Converted from the
30   * original Rome sample FeedReader
31   * <p>
32   * @author Alejandro Abdelnur
33   * @author Nick Lothian
34   *
35   */
36  public class FeedReader {
37  	public static void main(String[] args) {
38  		boolean ok = false;
39  		if (args.length==1) {
40  			try {
41  				URL feedUrl = new URL(args[0]);
42  				FeedFetcherCacheI feedInfoCache = HashMapFeedInfoCache.getInstance();
43  				FeedFetcherI fetcher = new HttpURLFeedFetcher(feedInfoCache);				
44  				
45  				FetcherEventListenerImpl listener = new FetcherEventListenerImpl();
46  				
47  				fetcher.addFetcherEventListener(listener);
48  
49  				System.err.println("Retrieving feed " + feedUrl);
50  				// Retrieve the feed.
51  				// We will get a Feed Polled Event and then a 
52  				// Feed Retrieved event (assuming the feed is valid)				
53  				SyndFeedI feed = fetcher.retrieveFeed(feedUrl);				
54  				
55  				System.err.println(feedUrl + " retrieved");
56  				System.err.println(feedUrl + " has a title: " + feed.getTitle() + " and contains " + feed.getEntries().size() + " entries.");				
57  				// We will now retrieve the feed again. If the feed is unmodified
58  				// and the server supports conditional gets, we will get a "Feed 
59  				// Unchanged" event after the Feed Polled event 
60  				System.err.println("Polling " + feedUrl + " again to test conditional get support.");
61  				SyndFeedI feed2 = fetcher.retrieveFeed(feedUrl);
62  				System.err.println("If a \"Feed Unchanged\" event fired then the server supports conditional gets.");
63  				
64  				ok = true;
65  			}
66  			catch (Exception ex) {							
67  				System.out.println("ERROR: "+ex.getMessage());
68  				ex.printStackTrace();
69  			}
70  		}
71  
72  		if (!ok) {
73  			System.out.println();
74  			System.out.println("FeedReader reads and prints any RSS/Atom feed type.");
75  			System.out.println("The first parameter must be the URL of the feed to read.");
76  			System.out.println();
77  		}
78  		
79  	}
80  	
81  	static class FetcherEventListenerImpl implements FetcherListener {	
82  		/***
83  		 * @see com.sun.syndication.fetcher.FetcherListener#fetcherEvent(com.sun.syndication.fetcher.FetcherEvent)
84  		 */
85  		public void fetcherEvent(FetcherEvent event) {
86  			String eventType = event.getEventType();
87  			if (FetcherEvent.EVENT_TYPE_FEED_POLLED.equals(eventType)) {
88  				System.err.println("\tEVENT: Feed Polled. URL = " + event.getUrl().toString());						
89  			} else if (FetcherEvent.EVENT_TYPE_FEED_RETRIEVED.equals(eventType)) {
90  				System.err.println("\tEVENT: Feed Retrieved. URL = " + event.getUrl().toString());				
91  			} else if (FetcherEvent.EVENT_TYPE_FEED_UNCHANGED.equals(eventType)) {
92  				System.err.println("\tEVENT: Feed Unchanged. URL = " + event.getUrl().toString());
93  			}			
94  		}
95  	}	
96  }