initial commit

This commit is contained in:
Athou
2013-03-20 20:33:42 +01:00
commit 7b3c53fcb9
82 changed files with 3346 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package com.commafeed.backend.feeds;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import javax.ejb.AccessTimeout;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.inject.Inject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.commafeed.model.Feed;
@Singleton
public class FeedFetcher {
private static Logger log = LoggerFactory.getLogger(FeedFetcher.class);
@Inject
FeedParser parser;
@Asynchronous
@Lock(LockType.READ)
@AccessTimeout(value = 15, unit = TimeUnit.SECONDS)
public Future<Feed> fetch(String feedUrl) {
log.debug("Fetching feed {}", feedUrl);
Feed feed = null;
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(feedUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
feed = parser.parse(feedUrl, responseBody);
} catch (Exception e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}
return new AsyncResult<Feed>(feed);
}
}

View File

@@ -0,0 +1,49 @@
package com.commafeed.backend.feeds;
import java.io.StringReader;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import com.commafeed.model.Feed;
import com.commafeed.model.FeedEntry;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
@Stateless
public class FeedParser {
@SuppressWarnings("unchecked")
public Feed parse(String feedUrl, String xml) throws FeedException {
Feed feed = new Feed();
feed.setUrl(feedUrl);
feed.setLastUpdated(Calendar.getInstance().getTime());
try {
SyndFeed rss = new SyndFeedInput().build(new StringReader(xml));
List<SyndEntry> items = rss.getEntries();
for (SyndEntry item : items) {
FeedEntry entry = new FeedEntry();
entry.setGuid(item.getUri());
entry.setTitle(item.getTitle());
entry.setContent(item.getDescription() == null ? null : item
.getDescription().getValue());
entry.setUrl(item.getLink());
entry.setUpdated(item.getUpdatedDate() != null ? item
.getUpdatedDate() : item.getPublishedDate());
feed.getEntries().add(entry);
}
} catch (Exception e) {
throw new FeedException("Could not parse feed : " + e.getMessage(),
e);
}
return feed;
}
}

View File

@@ -0,0 +1,48 @@
package com.commafeed.backend.feeds;
import java.util.List;
import java.util.concurrent.Future;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.inject.Inject;
import com.commafeed.backend.dao.FeedEntryService;
import com.commafeed.backend.dao.FeedService;
import com.commafeed.model.Feed;
import com.google.common.collect.Lists;
@Singleton
public class FeedTimer {
@Inject
FeedService feedService;
@Inject
FeedEntryService feedEntryService;
@Inject
FeedFetcher fetcher;
@Schedule(hour = "*", minute = "*", persistent = false)
private void timeout() {
List<Feed> feeds = feedService.findAll();
List<Future<Feed>> futures = Lists.newArrayList();
for (Feed feed : feeds) {
Future<Feed> future = fetcher.fetch(feed.getUrl());
futures.add(future);
}
for (Future<Feed> future : futures) {
try {
Feed feed = future.get();
feedEntryService
.updateEntries(feed.getUrl(), feed.getEntries());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}