| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
package net.sourceforge.pebble.aggregator; |
| 33 |
|
|
| 34 |
|
import com.sun.syndication.feed.WireFeed; |
| 35 |
|
import com.sun.syndication.feed.atom.Content; |
| 36 |
|
import com.sun.syndication.feed.atom.Entry; |
| 37 |
|
import com.sun.syndication.feed.atom.Link; |
| 38 |
|
import com.sun.syndication.feed.rss.Channel; |
| 39 |
|
import com.sun.syndication.feed.rss.Item; |
| 40 |
|
import com.sun.syndication.io.FeedException; |
| 41 |
|
import com.sun.syndication.io.WireFeedInput; |
| 42 |
|
import com.sun.syndication.io.XmlReader; |
| 43 |
|
import net.sourceforge.pebble.domain.Blog; |
| 44 |
|
import org.apache.commons.logging.Log; |
| 45 |
|
import org.apache.commons.logging.LogFactory; |
| 46 |
|
|
| 47 |
|
import java.io.IOException; |
| 48 |
|
import java.net.URL; |
| 49 |
|
import java.util.*; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
@author |
| 55 |
|
|
|
|
|
| 0% |
Uncovered Elements: 108 (108) |
Complexity: 28 |
Complexity Density: 0,38 |
|
| 56 |
|
public class NewsFeedCache { |
| 57 |
|
|
| 58 |
|
private static final int FEED_ENTRY_LIMIT = 20; |
| 59 |
|
|
| 60 |
|
private static final Log log = LogFactory.getLog(NewsFeedCache.class); |
| 61 |
|
private static final NewsFeedCache instance = new NewsFeedCache(); |
| 62 |
|
|
| 63 |
|
private final Map<String,Set<String>> subscriptions = new HashMap<String,Set<String>>(); |
| 64 |
|
private final Map<String, NewsFeed> feeds = new HashMap<String, NewsFeed>(); |
| 65 |
|
private final Map<String,List<NewsFeedEntry>> entries = new HashMap<String,List<NewsFeedEntry>>(); |
| 66 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 67 |
0
|
private NewsFeedCache() {... |
| 68 |
|
} |
| 69 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 70 |
0
|
public static NewsFeedCache getInstance() {... |
| 71 |
0
|
return instance; |
| 72 |
|
} |
| 73 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
| 74 |
0
|
public void addSubscription(Blog blog, String url) {... |
| 75 |
0
|
synchronized (feeds) { |
| 76 |
0
|
Set<String> urls = getUrls(blog.getId()); |
| 77 |
0
|
urls.add(url); |
| 78 |
|
|
| 79 |
0
|
NewsFeed feed = feeds.get(url); |
| 80 |
0
|
if (feed == null) { |
| 81 |
0
|
feed = updateFeed(url); |
| 82 |
0
|
feeds.put(url, feed); |
| 83 |
|
} |
| 84 |
|
} |
| 85 |
|
} |
| 86 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 87 |
0
|
public void removeAllSubscriptions(Blog blog) {... |
| 88 |
0
|
synchronized (feeds) { |
| 89 |
0
|
Set<String> urls = getUrls(blog.getId()); |
| 90 |
0
|
urls.clear(); |
| 91 |
|
} |
| 92 |
|
} |
| 93 |
|
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 3 |
Complexity Density: 0,21 |
|
| 94 |
0
|
public void refreshFeeds() {... |
| 95 |
0
|
for (String url : feeds.keySet()) { |
| 96 |
0
|
try { |
| 97 |
0
|
NewsFeed updatedFeed = updateFeed(url); |
| 98 |
0
|
synchronized (feeds) { |
| 99 |
0
|
feeds.put(url, updatedFeed); |
| 100 |
|
} |
| 101 |
|
} catch (Exception e) { |
| 102 |
0
|
log.warn("Couldn't update feed from " + url, e); |
| 103 |
|
} |
| 104 |
|
} |
| 105 |
|
|
| 106 |
0
|
for (String blogId : subscriptions.keySet()) { |
| 107 |
0
|
List<NewsFeedEntry> entriesForBlog = new LinkedList<NewsFeedEntry>(); |
| 108 |
0
|
for (String url : getUrls(blogId)) { |
| 109 |
0
|
entriesForBlog.addAll(feeds.get(url).getEntries()); |
| 110 |
|
} |
| 111 |
|
|
| 112 |
0
|
Collections.sort(entriesForBlog, new NewsFeedEntryComparator()); |
| 113 |
|
|
| 114 |
0
|
if (entriesForBlog.size() > FEED_ENTRY_LIMIT) { |
| 115 |
0
|
entriesForBlog = entriesForBlog.subList(0, FEED_ENTRY_LIMIT); |
| 116 |
|
} |
| 117 |
|
|
| 118 |
0
|
entries.put(blogId, entriesForBlog); |
| 119 |
|
} |
| 120 |
|
} |
| 121 |
|
|
|
|
|
| 0% |
Uncovered Elements: 56 (56) |
Complexity: 15 |
Complexity Density: 0,39 |
|
| 122 |
0
|
private NewsFeed updateFeed(String url) {... |
| 123 |
0
|
NewsFeed feed = new NewsFeed(url); |
| 124 |
|
|
| 125 |
0
|
try { |
| 126 |
0
|
log.debug("Refreshing feed from " + url); |
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
0
|
WireFeedInput input = new WireFeedInput(true); |
| 148 |
0
|
WireFeed wf = input.build(new XmlReader(new URL(url))); |
| 149 |
|
|
| 150 |
0
|
if (wf.getFeedType() != null && wf.getFeedType().startsWith("rss")) { |
| 151 |
0
|
Channel rssFeed = (Channel)wf; |
| 152 |
0
|
feed.setTitle(rssFeed.getTitle()); |
| 153 |
0
|
feed.setLink(rssFeed.getLink()); |
| 154 |
|
|
| 155 |
0
|
for (Item item : (List<Item>)rssFeed.getItems()) { |
| 156 |
0
|
NewsFeedEntry fe = new NewsFeedEntry( |
| 157 |
|
item.getLink(), |
| 158 |
|
item.getTitle(), |
| 159 |
0
|
item.getDescription() != null ? item.getDescription().getValue() : "", |
| 160 |
|
item.getAuthor(), |
| 161 |
|
item.getPubDate() |
| 162 |
|
); |
| 163 |
0
|
feed.add(fe); |
| 164 |
|
} |
| 165 |
0
|
} else if (wf.getFeedType() != null && wf.getFeedType().startsWith("atom")) { |
| 166 |
0
|
com.sun.syndication.feed.atom.Feed atomFeed = (com.sun.syndication.feed.atom.Feed)wf; |
| 167 |
0
|
feed.setTitle(atomFeed.getTitle()); |
| 168 |
0
|
for (Link link : (List<Link>)atomFeed.getAlternateLinks()) { |
| 169 |
0
|
if ("text/html".equals(link.getType())) |
| 170 |
0
|
feed.setLink(link.getHref()); |
| 171 |
|
} |
| 172 |
|
|
| 173 |
0
|
for (Entry entry : (List<Entry>)atomFeed.getEntries()) { |
| 174 |
0
|
String href = ""; |
| 175 |
0
|
for (Link link : (List<Link>)entry.getAlternateLinks()) { |
| 176 |
0
|
if ("text/html".equals(link.getType())) |
| 177 |
0
|
href = link.getHref(); |
| 178 |
|
} |
| 179 |
0
|
String body = null; |
| 180 |
0
|
for (Content content : (List<Content>)entry.getContents()) { |
| 181 |
0
|
if ("html".equals(content.getType())) |
| 182 |
0
|
body = content.getValue(); |
| 183 |
|
} |
| 184 |
0
|
if (body == null) { |
| 185 |
0
|
for (Content content : (List<Content>)entry.getSummary()) { |
| 186 |
0
|
if ("html".equals(content.getType())) |
| 187 |
0
|
body = content.getValue(); |
| 188 |
|
} |
| 189 |
|
} |
| 190 |
0
|
String author = entry.getAuthors() != null && entry.getAuthors().size() > 0 ? entry.getAuthors().get(0).toString() : ""; |
| 191 |
0
|
NewsFeedEntry fe = new NewsFeedEntry( |
| 192 |
|
href, |
| 193 |
|
entry.getTitle(), |
| 194 |
|
body, |
| 195 |
|
author, |
| 196 |
|
entry.getPublished() |
| 197 |
|
); |
| 198 |
0
|
feed.add(fe); |
| 199 |
|
} |
| 200 |
|
} |
| 201 |
|
|
| 202 |
0
|
log.debug("Refreshed feed from " + url); |
| 203 |
|
} catch (FeedException e) { |
| 204 |
0
|
log.warn("Error while updating feed from " + url, e); |
| 205 |
|
} catch (IOException e) { |
| 206 |
0
|
log.warn("Error while updating feed from " + url, e); |
| 207 |
|
} |
| 208 |
|
|
| 209 |
0
|
return feed; |
| 210 |
|
} |
| 211 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 212 |
0
|
public NewsFeed getFeed(String url) {... |
| 213 |
0
|
return feeds.get(url); |
| 214 |
|
} |
| 215 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
| 216 |
0
|
public List<NewsFeedEntry> getNewsFeedEntries(Blog blog) {... |
| 217 |
0
|
List<NewsFeedEntry> list = entries.get(blog.getId()); |
| 218 |
0
|
if (list == null) { |
| 219 |
0
|
list = new LinkedList<NewsFeedEntry>(); |
| 220 |
|
} |
| 221 |
|
|
| 222 |
0
|
return list; |
| 223 |
|
} |
| 224 |
|
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
| 225 |
0
|
private Set<String> getUrls(String blogId) {... |
| 226 |
0
|
Set<String> urls = subscriptions.get(blogId); |
| 227 |
0
|
if (urls == null) { |
| 228 |
0
|
urls = new HashSet<String>(); |
| 229 |
0
|
subscriptions.put(blogId, urls); |
| 230 |
|
} |
| 231 |
|
|
| 232 |
0
|
return urls; |
| 233 |
|
} |
| 234 |
|
|
| 235 |
|
} |