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.impl;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.URLConnection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.Properties;
27  import java.util.Set;
28  
29  import com.sun.syndication.fetcher.FeedFetcher;
30  import com.sun.syndication.fetcher.FetcherEvent;
31  import com.sun.syndication.fetcher.FetcherException;
32  import com.sun.syndication.fetcher.FetcherListener;
33  
34  
35  public abstract class AbstractFeedFetcher implements FeedFetcher {
36  	private Set fetcherEventListeners;
37  	private String userAgent;
38  	
39  	public AbstractFeedFetcher() {
40  		fetcherEventListeners = Collections.synchronizedSet(new HashSet());
41  		String fetcherVersion = "UNKNOWN";
42  		
43  		Properties props = new Properties(System.getProperties());
44  		String resourceName = "fetcher.properties";
45  		
46  		try {
47  			InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resourceName);
48  			if (inputStream == null) {
49  			    inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
50  			}			
51  			if (inputStream != null) {
52  				props.load(inputStream);
53  				System.getProperties().putAll(props);
54  				inputStream.close();
55  			} else {
56  				System.err.println("Could not find " + resourceName + " on classpath");
57  			}
58  		} catch (IOException e) {
59  			// do nothing - we don't want to fail just because we could not find the version
60  			System.err.println("Error reading " + resourceName + " from classpath: " + e.getMessage());
61  		}		
62  		
63  		
64  		userAgent =  DEFAULT_USER_AGENT + " Ver: " + System.getProperty("rome.fetcher.version", "UNKNOWN");
65  	}
66  
67  	/***
68  	 * @return the User-Agent currently being sent to servers
69  	 */
70  	public String getUserAgent() {
71  		return userAgent;
72  	}
73  
74  	/***
75  	 * @param string The User-Agent to sent to servers
76  	 */
77  	public void setUserAgent(String string) {
78  		userAgent = string;
79  	}
80  
81  	/***
82  	 * @param eventType The event type to fire
83  	 * @param connection the current connection
84  	 */
85  	protected void fireEvent(String eventType, URLConnection connection) {
86  		fireEvent(eventType, connection.toString());
87  	}
88  
89  	/***
90  	 * @param eventType The event type to fire
91  	 * @param urlStr the current url as a string
92  	 */
93  	protected void fireEvent(String eventType, String urlStr) {
94  		FetcherEvent fetcherEvent = new FetcherEvent(this, urlStr, eventType);
95  		synchronized(fetcherEventListeners) {
96  			Iterator iter = fetcherEventListeners.iterator();
97  			while ( iter.hasNext()) {
98  				FetcherListener fetcherEventListener = (FetcherListener) iter.next();
99  				fetcherEventListener.fetcherEvent(fetcherEvent);							
100 			}					
101 		}
102 	}	
103 	
104 	/***
105 	 * @see com.sun.syndication.fetcher.FeedFetcher#addFetcherEventListener(com.sun.syndication.fetcher.FetcherListener)
106 	 */
107 	public void addFetcherEventListener(FetcherListener listener) {
108 		if (listener != null) {
109 			fetcherEventListeners.add(listener);		
110 		}	
111 		
112 	}
113 
114 	/***
115 	 * @see com.sun.syndication.fetcher.FeedFetcher#removeFetcherPolledListener(com.sun.syndication.fetcher.FetcherListener)
116 	 */
117 	public void removeFetcherPolledListener(FetcherListener listener) {
118 		if (listener != null) {
119 			fetcherEventListeners.remove(listener);		
120 		}		
121 	}
122 
123 	/***
124 	 * <p>Handles HTTP error codes.</p>
125 	 *
126 	 * @param responseCode the HTTP response code
127 	 * @throws FetcherException if response code is in the range 400 to 599 inclusive
128 	 */
129 	protected void handleErrorCodes(int responseCode) throws FetcherException {
130 		// Handle 2xx codes as OK, so ignore them here
131 		// 3xx codes are handled by the HttpURLConnection class
132 		if (responseCode >= 400 && responseCode < 500) {
133 			throw4XXError(responseCode);
134 		} else if (responseCode >= 500 && responseCode < 600) {
135 			throw new FetcherException(responseCode, "The server encounted an error");
136 		}
137 	}
138 
139 	protected void throw4XXError(int responseCode) throws FetcherException {
140 		throw new FetcherException(responseCode, "The requested resource could not be found");
141 	}
142 	
143 	
144 }