1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
131
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 }