1 19 20 package com.sslexplorer.rss; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 import java.net.URLConnection ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import com.sslexplorer.boot.Util; 31 import com.sun.syndication.feed.synd.SyndFeed; 32 import com.sun.syndication.io.FeedException; 33 import com.sun.syndication.io.SyndFeedInput; 34 import com.sun.syndication.io.XmlReader; 35 36 50 public class Feed { 51 52 final static Log log = LogFactory.getLog(Feed.class); 53 54 57 public static final int CONNECT_TIMEOUT = 5000; 58 59 62 public static final int READ_TIMEOUT = 5000; 63 64 67 public final static int STATUS_LOADING = 0; 68 69 72 public final static int STATUS_LOADED = 1; 73 74 77 public final static int STATUS_EMPTY = 2; 78 79 82 public final static int STATUS_FAILED_TO_LOAD = 3; 83 84 86 private String feedName; 87 private SyndFeed feed; 88 private int status; 89 private SyndFeedInput input; 90 private URL url; 91 92 101 public Feed(String feedName, SyndFeedInput input, URL url) throws IOException , FeedException { 102 super(); 103 this.url = url; 104 this.feedName = feedName; 105 this.input = input; 106 this.status = STATUS_LOADING; 107 } 108 109 114 public String getFeedName() { 115 return feedName; 116 } 117 118 124 public SyndFeed getFeed() { 125 return feed; 126 } 127 128 133 public int getStatus() { 134 return status; 135 } 136 137 void load() throws IOException , FeedException { 138 139 if (log.isInfoEnabled()) { 140 log.info("Retrieving RSS feeds from " + url); 141 } 142 143 URLConnection conx = url.openConnection(); 144 conx.setConnectTimeout(Feed.CONNECT_TIMEOUT); 145 conx.setReadTimeout(Feed.READ_TIMEOUT); 146 InputStream inputStream = null; 147 try { 148 inputStream = conx.getInputStream(); 149 status = STATUS_LOADING; 150 feed = input.build(new XmlReader(inputStream)); 151 if (log.isInfoEnabled()) 152 log.info("Retrieved feed " + url); 153 status = STATUS_LOADED; 154 155 } catch (IOException e) { 156 status = STATUS_FAILED_TO_LOAD; 157 throw e; 158 } catch (FeedException e) { 159 status = STATUS_FAILED_TO_LOAD; 160 throw e; 161 } finally { 162 Util.closeStream(inputStream); 163 } 164 } 165 } 166 | Popular Tags |