1 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.feed.synd.SyndFeed; 30 import com.sun.syndication.fetcher.FeedFetcher; 31 import com.sun.syndication.fetcher.FetcherEvent; 32 import com.sun.syndication.fetcher.FetcherException; 33 import com.sun.syndication.fetcher.FetcherListener; 34 35 36 public abstract class AbstractFeedFetcher implements FeedFetcher { 37 private Set fetcherEventListeners; 38 private String userAgent; 39 private boolean usingDeltaEncoding; 40 41 public AbstractFeedFetcher() { 42 fetcherEventListeners = Collections.synchronizedSet(new HashSet ()); 43 44 Properties props = new Properties (System.getProperties()); 45 String resourceName = "fetcher.properties"; 46 47 try { 48 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resourceName); 49 if (inputStream == null) { 50 inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName); 51 } 52 if (inputStream != null) { 53 props.load(inputStream); 54 System.getProperties().putAll(props); 55 inputStream.close(); 56 } else { 57 System.err.println("Could not find " + resourceName + " on classpath"); 58 } 59 } catch (IOException e) { 60 System.err.println("Error reading " + resourceName + " from classpath: " + e.getMessage()); 62 } 63 64 65 userAgent = DEFAULT_USER_AGENT + " Ver: " + System.getProperty("rome.fetcher.version", "UNKNOWN"); 66 } 67 68 71 public String getUserAgent() { 72 return userAgent; 73 } 74 75 78 public void setUserAgent(String string) { 79 userAgent = string; 80 } 81 82 86 protected void fireEvent(String eventType, URLConnection connection) { 87 fireEvent(eventType, connection.getURL().toExternalForm(), null); 88 } 89 90 91 96 protected void fireEvent(String eventType, URLConnection connection, SyndFeed feed) { 97 fireEvent(eventType, connection.getURL().toExternalForm(), feed); 98 } 99 100 104 protected void fireEvent(String eventType, String urlStr) { 105 fireEvent(eventType, urlStr, null); 106 } 107 108 113 protected void fireEvent(String eventType, String urlStr, SyndFeed feed) { 114 FetcherEvent fetcherEvent = new FetcherEvent(this, urlStr, eventType, feed); 115 synchronized(fetcherEventListeners) { 116 Iterator iter = fetcherEventListeners.iterator(); 117 while ( iter.hasNext()) { 118 FetcherListener fetcherEventListener = (FetcherListener) iter.next(); 119 fetcherEventListener.fetcherEvent(fetcherEvent); 120 } 121 } 122 } 123 124 127 public void addFetcherEventListener(FetcherListener listener) { 128 if (listener != null) { 129 fetcherEventListeners.add(listener); 130 } 131 132 } 133 134 137 public void removeFetcherEventListener(FetcherListener listener) { 138 if (listener != null) { 139 fetcherEventListeners.remove(listener); 140 } 141 } 142 143 146 public boolean isUsingDeltaEncoding() { 147 return usingDeltaEncoding; 148 } 149 152 public void setUsingDeltaEncoding(boolean useDeltaEncoding) { 153 this.usingDeltaEncoding = useDeltaEncoding; 154 } 155 156 162 protected void handleErrorCodes(int responseCode) throws FetcherException { 163 if (responseCode == 403) { 166 throwAuthenticationError(responseCode); 168 } else if (responseCode >= 400 && responseCode < 500) { 169 throw4XXError(responseCode); 170 } else if (responseCode >= 500 && responseCode < 600) { 171 throw new FetcherException(responseCode, "The server encounted an error. HTTP Response code was:" + responseCode); 172 } 173 } 174 175 protected void throw4XXError(int responseCode) throws FetcherException { 176 throw new FetcherException(responseCode, "The requested resource could not be found. HTTP Response code was:" + responseCode); 177 } 178 179 protected void throwAuthenticationError(int responseCode) throws FetcherException { 180 throw new FetcherException(responseCode, "Authentication required for that resource. HTTP Response code was:" + responseCode); 181 } 182 183 193 public static SyndFeed combineFeeds(SyndFeed originalFeed, SyndFeed newFeed) { 194 SyndFeed result; 195 try { 196 result = (SyndFeed) newFeed.clone(); 197 198 result.getEntries().addAll(result.getEntries().size(), originalFeed.getEntries()); 199 200 return result; 201 } catch (CloneNotSupportedException e) { 202 IllegalArgumentException iae = new IllegalArgumentException ("Cannot clone feed"); 203 iae.initCause(e); 204 throw iae; 205 } 206 } 207 208 } 209 | Popular Tags |