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.ejb.Stateless;
|
||||||
import javax.inject.Inject;
|
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.Jsoup;
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
import org.jsoup.select.Elements;
|
import org.jsoup.select.Elements;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.commafeed.backend.HttpGetter;
|
||||||
import com.commafeed.backend.model.Feed;
|
import com.commafeed.backend.model.Feed;
|
||||||
import com.sun.syndication.io.FeedException;
|
import com.sun.syndication.io.FeedException;
|
||||||
|
|
||||||
@@ -28,34 +21,23 @@ public class FeedFetcher {
|
|||||||
@Inject
|
@Inject
|
||||||
FeedParser parser;
|
FeedParser parser;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
HttpGetter getter;
|
||||||
|
|
||||||
public Feed fetch(String feedUrl) throws FeedException {
|
public Feed fetch(String feedUrl) throws FeedException {
|
||||||
log.debug("Fetching feed {}", feedUrl);
|
log.debug("Fetching feed {}", feedUrl);
|
||||||
Feed feed = null;
|
Feed feed = null;
|
||||||
|
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
|
||||||
HttpProtocolParams.setContentCharset(httpclient.getParams(), "UTF-8");
|
|
||||||
HttpConnectionParams
|
|
||||||
.setConnectionTimeout(httpclient.getParams(), 15000);
|
|
||||||
HttpConnectionParams.setSoTimeout(httpclient.getParams(), 15000);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
HttpGet httpget = new HttpGet(feedUrl);
|
String content = getter.get(feedUrl);
|
||||||
HttpResponse response = httpclient.execute(httpget);
|
|
||||||
HttpEntity entity = response.getEntity();
|
|
||||||
String content = EntityUtils.toString(entity, "UTF-8");
|
|
||||||
String extractedUrl = extractFeedUrl(content);
|
String extractedUrl = extractFeedUrl(content);
|
||||||
if (extractedUrl != null) {
|
if (extractedUrl != null) {
|
||||||
httpget = new HttpGet(extractedUrl);
|
content = getter.get(extractedUrl);
|
||||||
response = httpclient.execute(httpget);
|
|
||||||
entity = response.getEntity();
|
|
||||||
content = EntityUtils.toString(entity, "UTF-8");
|
|
||||||
feedUrl = extractedUrl;
|
feedUrl = extractedUrl;
|
||||||
}
|
}
|
||||||
feed = parser.parse(feedUrl, content);
|
feed = parser.parse(feedUrl, content);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FeedException(e.getMessage(), e);
|
throw new FeedException(e.getMessage(), e);
|
||||||
} finally {
|
|
||||||
httpclient.getConnectionManager().shutdown();
|
|
||||||
}
|
}
|
||||||
return feed;
|
return feed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ public class FeedParser {
|
|||||||
SyndFeed rss = new SyndFeedInput().build(new StringReader(xml));
|
SyndFeed rss = new SyndFeedInput().build(new StringReader(xml));
|
||||||
feed.setUrl(feedUrl);
|
feed.setUrl(feedUrl);
|
||||||
feed.setTitle(rss.getTitle());
|
feed.setTitle(rss.getTitle());
|
||||||
|
feed.setLink(rss.getLink());
|
||||||
List<SyndEntry> items = rss.getEntries();
|
List<SyndEntry> items = rss.getEntries();
|
||||||
for (SyndEntry item : items) {
|
for (SyndEntry item : items) {
|
||||||
FeedEntry entry = new FeedEntry();
|
FeedEntry entry = new FeedEntry();
|
||||||
|
|||||||
@@ -38,6 +38,10 @@ public class FeedUpdater {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Feed fetchedFeed = fetcher.fetch(feed.getUrl());
|
Feed fetchedFeed = fetcher.fetch(feed.getUrl());
|
||||||
|
if (feed.getLink() == null) {
|
||||||
|
feed.setLink(fetchedFeed.getLink());
|
||||||
|
feedService.update(feed);
|
||||||
|
}
|
||||||
feedEntryService.updateEntries(feed.getUrl(),
|
feedEntryService.updateEntries(feed.getUrl(),
|
||||||
fetchedFeed.getEntries());
|
fetchedFeed.getEntries());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ public class Feed extends AbstractModel {
|
|||||||
@Transient
|
@Transient
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
|
@Column(length = 2048)
|
||||||
|
private String link;
|
||||||
|
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
private Date lastUpdated;
|
private Date lastUpdated;
|
||||||
|
|
||||||
@@ -92,4 +95,12 @@ public class Feed extends AbstractModel {
|
|||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getLink() {
|
||||||
|
return link;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLink(String link) {
|
||||||
|
this.link = link;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import org.reflections.vfs.ZipDir;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.commafeed.frontend.pages.FaviconPage;
|
||||||
import com.commafeed.frontend.pages.HomePage;
|
import com.commafeed.frontend.pages.HomePage;
|
||||||
import com.commafeed.frontend.pages.LoginPage;
|
import com.commafeed.frontend.pages.LoginPage;
|
||||||
import com.commafeed.frontend.pages.LogoutPage;
|
import com.commafeed.frontend.pages.LogoutPage;
|
||||||
@@ -57,6 +58,7 @@ public class CommaFeedApplication extends AuthenticatedWebApplication {
|
|||||||
mountPage("login", LoginPage.class);
|
mountPage("login", LoginPage.class);
|
||||||
mountPage("logout", LogoutPage.class);
|
mountPage("logout", LogoutPage.class);
|
||||||
mountPage("error", DisplayExceptionPage.class);
|
mountPage("error", DisplayExceptionPage.class);
|
||||||
|
mountPage("favicon", FaviconPage.class);
|
||||||
|
|
||||||
setupInjection();
|
setupInjection();
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ public class Subscription implements Serializable {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
private String message;
|
private String message;
|
||||||
|
private String feedUrl;
|
||||||
private int unread;
|
private int unread;
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
@@ -42,4 +43,12 @@ public class Subscription implements Serializable {
|
|||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getFeedUrl() {
|
||||||
|
return feedUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFeedUrl(String feedUrl) {
|
||||||
|
this.feedUrl = feedUrl;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
81
src/main/java/com/commafeed/frontend/pages/FaviconPage.java
Normal file
81
src/main/java/com/commafeed/frontend/pages/FaviconPage.java
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package com.commafeed.frontend.pages;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.apache.wicket.request.IRequestCycle;
|
||||||
|
import org.apache.wicket.request.IRequestHandler;
|
||||||
|
import org.apache.wicket.request.mapper.parameter.PageParameters;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.commafeed.backend.HttpGetter;
|
||||||
|
import com.commafeed.backend.model.UserRole.Role;
|
||||||
|
import com.commafeed.frontend.SecurityCheck;
|
||||||
|
|
||||||
|
@SuppressWarnings("serial")
|
||||||
|
@SecurityCheck(Role.USER)
|
||||||
|
public class FaviconPage extends BasePage {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory
|
||||||
|
.getLogger(FaviconPage.class);
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
HttpGetter getter;
|
||||||
|
|
||||||
|
public FaviconPage(PageParameters params) {
|
||||||
|
final String url = params.get("url").toString();
|
||||||
|
getRequestCycle().scheduleRequestHandlerAfterCurrent(
|
||||||
|
new IRequestHandler() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void respond(IRequestCycle requestCycle) {
|
||||||
|
requestCycle.getResponse().write(getImage(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void detach(IRequestCycle requestCycle) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] getImage(String url) {
|
||||||
|
byte[] img = null;
|
||||||
|
try {
|
||||||
|
if (StringUtils.isNotBlank(url)) {
|
||||||
|
int index = Math.max(url.length(), url.lastIndexOf("?"));
|
||||||
|
url = url.substring(0, index);
|
||||||
|
|
||||||
|
String iconUrl = "http://g.etfv.co/"
|
||||||
|
+ URLEncoder.encode(url, "UTF-8") + "?defaulticon=none";
|
||||||
|
img = getter.getBinary(iconUrl);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
if (img == null) {
|
||||||
|
img = getDefaultIcon();
|
||||||
|
}
|
||||||
|
return img;
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] getDefaultIcon() {
|
||||||
|
byte[] bytes = null;
|
||||||
|
InputStream is = null;
|
||||||
|
try {
|
||||||
|
is = getClass().getResourceAsStream("/favicon.gif");
|
||||||
|
bytes = IOUtils.toByteArray(is);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(is);
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -59,13 +59,12 @@ public class SubscriptionsREST extends AbstractREST {
|
|||||||
Feed fetchedFeed = fetchFeed(req.getUrl());
|
Feed fetchedFeed = fetchFeed(req.getUrl());
|
||||||
Feed feed = feedService.findByUrl(fetchedFeed.getUrl());
|
Feed feed = feedService.findByUrl(fetchedFeed.getUrl());
|
||||||
if (feed == null) {
|
if (feed == null) {
|
||||||
feed = new Feed();
|
feed = fetchedFeed;
|
||||||
feed.setUrl(fetchedFeed.getUrl());
|
|
||||||
feedService.save(feed);
|
feedService.save(feed);
|
||||||
}
|
}
|
||||||
|
|
||||||
FeedSubscription sub = new FeedSubscription();
|
FeedSubscription sub = new FeedSubscription();
|
||||||
sub.setCategory("all".equals(req.getCategoryId()) ? null
|
sub.setCategory(EntriesREST.ALL.equals(req.getCategoryId()) ? null
|
||||||
: feedCategoryService.findById(Long.valueOf(req.getCategoryId())));
|
: feedCategoryService.findById(Long.valueOf(req.getCategoryId())));
|
||||||
sub.setFeed(feed);
|
sub.setFeed(feed);
|
||||||
sub.setTitle(req.getTitle());
|
sub.setTitle(req.getTitle());
|
||||||
@@ -206,6 +205,7 @@ public class SubscriptionsREST extends AbstractREST {
|
|||||||
sub.setId(subscription.getId());
|
sub.setId(subscription.getId());
|
||||||
sub.setName(subscription.getTitle());
|
sub.setName(subscription.getTitle());
|
||||||
sub.setMessage(subscription.getFeed().getMessage());
|
sub.setMessage(subscription.getFeed().getMessage());
|
||||||
|
sub.setFeedUrl(subscription.getFeed().getLink());
|
||||||
int size = feedEntryService.getEntries(subscription.getFeed(),
|
int size = feedEntryService.getEntries(subscription.getFeed(),
|
||||||
getUser(), true).size();
|
getUser(), true).size();
|
||||||
sub.setUnread(size);
|
sub.setUnread(size);
|
||||||
@@ -220,5 +220,4 @@ public class SubscriptionsREST extends AbstractREST {
|
|||||||
});
|
});
|
||||||
return category;
|
return category;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
src/main/resources/favicon.gif
Normal file
BIN
src/main/resources/favicon.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 238 B |
@@ -16,7 +16,9 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<a ng-click="feedClick({id: feed.id})" class="feed-link"
|
<a ng-click="feedClick({id: feed.id})" class="feed-link"
|
||||||
ng-class="{selected: (feed.id == selectedId && selectedType == 'feed'), unread: feed.unread }">{{formatFeedName({feed:feed})}}
|
ng-class="{selected: (feed.id == selectedId && selectedType == 'feed'), unread: feed.unread }">
|
||||||
|
<img ng-src="favicon?url={{feed.feedUrl}}" width="16" height="16"></img>
|
||||||
|
{{formatFeedName({feed:feed})}}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
Reference in New Issue
Block a user