mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
display favicon
This commit is contained in:
42
src/main/java/com/commafeed/backend/HttpGetter.java
Normal file
42
src/main/java/com/commafeed/backend/HttpGetter.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.commafeed.backend;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.params.HttpConnectionParams;
|
||||
import org.apache.http.params.HttpProtocolParams;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
@Stateless
|
||||
public class HttpGetter {
|
||||
|
||||
public String get(String url) throws Exception {
|
||||
return new String(getBinary(url), "UTF-8");
|
||||
}
|
||||
|
||||
public byte[] getBinary(String url) throws Exception {
|
||||
byte[] content = null;
|
||||
|
||||
HttpClient httpclient = new DefaultHttpClient();
|
||||
HttpProtocolParams.setContentCharset(httpclient.getParams(), "UTF-8");
|
||||
HttpConnectionParams
|
||||
.setConnectionTimeout(httpclient.getParams(), 15000);
|
||||
HttpConnectionParams.setSoTimeout(httpclient.getParams(), 15000);
|
||||
|
||||
try {
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
HttpResponse response = httpclient.execute(httpget);
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
content = EntityUtils.toByteArray(entity);
|
||||
}
|
||||
} finally {
|
||||
httpclient.getConnectionManager().shutdown();
|
||||
}
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -3,20 +3,13 @@ package com.commafeed.backend.feeds;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.params.HttpConnectionParams;
|
||||
import org.apache.http.params.HttpProtocolParams;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.select.Elements;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.commafeed.backend.HttpGetter;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.sun.syndication.io.FeedException;
|
||||
|
||||
@@ -28,34 +21,23 @@ public class FeedFetcher {
|
||||
@Inject
|
||||
FeedParser parser;
|
||||
|
||||
@Inject
|
||||
HttpGetter getter;
|
||||
|
||||
public Feed fetch(String feedUrl) throws FeedException {
|
||||
log.debug("Fetching feed {}", feedUrl);
|
||||
Feed feed = null;
|
||||
|
||||
HttpClient httpclient = new DefaultHttpClient();
|
||||
HttpProtocolParams.setContentCharset(httpclient.getParams(), "UTF-8");
|
||||
HttpConnectionParams
|
||||
.setConnectionTimeout(httpclient.getParams(), 15000);
|
||||
HttpConnectionParams.setSoTimeout(httpclient.getParams(), 15000);
|
||||
|
||||
try {
|
||||
HttpGet httpget = new HttpGet(feedUrl);
|
||||
HttpResponse response = httpclient.execute(httpget);
|
||||
HttpEntity entity = response.getEntity();
|
||||
String content = EntityUtils.toString(entity, "UTF-8");
|
||||
String content = getter.get(feedUrl);
|
||||
String extractedUrl = extractFeedUrl(content);
|
||||
if (extractedUrl != null) {
|
||||
httpget = new HttpGet(extractedUrl);
|
||||
response = httpclient.execute(httpget);
|
||||
entity = response.getEntity();
|
||||
content = EntityUtils.toString(entity, "UTF-8");
|
||||
content = getter.get(extractedUrl);
|
||||
feedUrl = extractedUrl;
|
||||
}
|
||||
feed = parser.parse(feedUrl, content);
|
||||
} catch (Exception e) {
|
||||
throw new FeedException(e.getMessage(), e);
|
||||
} finally {
|
||||
httpclient.getConnectionManager().shutdown();
|
||||
}
|
||||
return feed;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ public class FeedParser {
|
||||
SyndFeed rss = new SyndFeedInput().build(new StringReader(xml));
|
||||
feed.setUrl(feedUrl);
|
||||
feed.setTitle(rss.getTitle());
|
||||
feed.setLink(rss.getLink());
|
||||
List<SyndEntry> items = rss.getEntries();
|
||||
for (SyndEntry item : items) {
|
||||
FeedEntry entry = new FeedEntry();
|
||||
|
||||
@@ -38,6 +38,10 @@ public class FeedUpdater {
|
||||
|
||||
try {
|
||||
Feed fetchedFeed = fetcher.fetch(feed.getUrl());
|
||||
if (feed.getLink() == null) {
|
||||
feed.setLink(fetchedFeed.getLink());
|
||||
feedService.update(feed);
|
||||
}
|
||||
feedEntryService.updateEntries(feed.getUrl(),
|
||||
fetchedFeed.getEntries());
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -24,6 +24,9 @@ public class Feed extends AbstractModel {
|
||||
@Transient
|
||||
private String title;
|
||||
|
||||
@Column(length = 2048)
|
||||
private String link;
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date lastUpdated;
|
||||
|
||||
@@ -92,4 +95,12 @@ public class Feed extends AbstractModel {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
public void setLink(String link) {
|
||||
this.link = link;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user