2013-04-11 20:49:08 +02:00
|
|
|
package com.commafeed.backend.services;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-04-08 20:41:16 +02:00
|
|
|
import java.util.ArrayList;
|
2013-03-20 20:33:42 +01:00
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Collection;
|
2013-03-22 09:29:30 +01:00
|
|
|
import java.util.List;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
import javax.ejb.Stateless;
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-04-07 12:01:50 +02:00
|
|
|
import org.apache.commons.lang.ObjectUtils;
|
2013-03-25 23:33:06 +01:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2013-04-14 18:51:12 +02:00
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
|
import org.jsoup.nodes.Document.OutputSettings;
|
|
|
|
|
import org.jsoup.nodes.Entities.EscapeMode;
|
|
|
|
|
import org.jsoup.safety.Whitelist;
|
2013-03-25 23:33:06 +01:00
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
import com.commafeed.backend.dao.FeedDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
2013-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.backend.model.Feed;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntry;
|
2013-04-14 18:51:12 +02:00
|
|
|
import com.commafeed.backend.model.FeedEntryContent;
|
2013-03-26 16:02:26 +01:00
|
|
|
import com.commafeed.backend.model.FeedEntryStatus;
|
2013-04-08 07:54:46 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-03-25 23:33:06 +01:00
|
|
|
import com.google.common.collect.Lists;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
@Stateless
|
2013-04-11 20:49:08 +02:00
|
|
|
public class FeedUpdateService {
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
@Inject
|
2013-04-11 20:49:08 +02:00
|
|
|
FeedDAO feedDAO;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-04-08 07:54:46 +02:00
|
|
|
@Inject
|
2013-04-11 20:49:08 +02:00
|
|
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedEntryDAO feedEntryDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedEntryStatusDAO feedEntryStatusDAO;
|
2013-04-08 07:54:46 +02:00
|
|
|
|
2013-04-14 18:12:35 +02:00
|
|
|
public void updateEntries(Feed feed, Collection<FeedEntry> entries) {
|
2013-03-25 23:33:06 +01:00
|
|
|
List<String> guids = Lists.newArrayList();
|
2013-03-20 20:33:42 +01:00
|
|
|
for (FeedEntry entry : entries) {
|
2013-03-25 23:33:06 +01:00
|
|
|
guids.add(entry.getGuid());
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-08 20:41:16 +02:00
|
|
|
List<FeedEntry> existingEntries = guids.isEmpty() ? new ArrayList<FeedEntry>()
|
2013-04-11 20:49:08 +02:00
|
|
|
: feedEntryDAO.findByGuids(guids);
|
2013-03-25 23:33:06 +01:00
|
|
|
for (FeedEntry entry : entries) {
|
2013-04-07 12:01:50 +02:00
|
|
|
FeedEntry foundEntry = null;
|
2013-03-25 23:33:06 +01:00
|
|
|
for (FeedEntry existingEntry : existingEntries) {
|
|
|
|
|
if (StringUtils
|
|
|
|
|
.equals(entry.getGuid(), existingEntry.getGuid())) {
|
2013-04-07 12:01:50 +02:00
|
|
|
foundEntry = existingEntry;
|
2013-03-25 23:33:06 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-07 12:01:50 +02:00
|
|
|
if (foundEntry == null) {
|
2013-04-14 18:51:12 +02:00
|
|
|
FeedEntryContent content = entry.getContent();
|
|
|
|
|
content.setContent(handleContent(content.getContent()));
|
2013-04-16 12:57:40 +02:00
|
|
|
|
|
|
|
|
String title = handleContent(content.getTitle());
|
|
|
|
|
content.setTitle(title.substring(0,
|
|
|
|
|
Math.min(2048, title.length())));
|
2013-04-14 18:51:12 +02:00
|
|
|
|
2013-04-09 11:10:26 +02:00
|
|
|
entry.setInserted(Calendar.getInstance().getTime());
|
2013-04-08 07:54:46 +02:00
|
|
|
addFeedToEntry(entry, feed);
|
2013-04-07 12:01:50 +02:00
|
|
|
} else {
|
|
|
|
|
boolean foundFeed = false;
|
|
|
|
|
for (Feed existingFeed : foundEntry.getFeeds()) {
|
|
|
|
|
if (ObjectUtils.equals(existingFeed.getId(), feed.getId())) {
|
|
|
|
|
foundFeed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!foundFeed) {
|
2013-04-08 07:54:46 +02:00
|
|
|
addFeedToEntry(foundEntry, feed);
|
2013-04-07 12:01:50 +02:00
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
2013-03-25 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
|
2013-04-08 07:54:46 +02:00
|
|
|
private void addFeedToEntry(FeedEntry entry, Feed feed) {
|
|
|
|
|
entry.getFeeds().add(feed);
|
2013-04-11 20:49:08 +02:00
|
|
|
feedEntryDAO.saveOrUpdate(entry);
|
|
|
|
|
List<FeedSubscription> subscriptions = feedSubscriptionDAO
|
2013-04-08 07:54:46 +02:00
|
|
|
.findByFeed(feed);
|
|
|
|
|
for (FeedSubscription sub : subscriptions) {
|
|
|
|
|
FeedEntryStatus status = new FeedEntryStatus();
|
|
|
|
|
status.setEntry(entry);
|
2013-04-08 13:06:53 +02:00
|
|
|
status.setSubscription(sub);
|
2013-04-11 20:49:08 +02:00
|
|
|
feedEntryStatusDAO.save(status);
|
2013-04-08 07:54:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2013-04-14 18:51:12 +02:00
|
|
|
|
|
|
|
|
private String handleContent(String content) {
|
|
|
|
|
if (StringUtils.isNotBlank(content)) {
|
|
|
|
|
Whitelist whitelist = Whitelist.relaxed();
|
|
|
|
|
whitelist.addEnforcedAttribute("a", "target", "_blank");
|
|
|
|
|
|
|
|
|
|
whitelist.addTags("iframe");
|
|
|
|
|
whitelist.addAttributes("iframe", "src", "height", "width",
|
|
|
|
|
"allowfullscreen", "frameborder");
|
|
|
|
|
|
|
|
|
|
content = Jsoup.clean(content, "", whitelist,
|
|
|
|
|
new OutputSettings().escapeMode(EscapeMode.extended));
|
|
|
|
|
}
|
|
|
|
|
return content;
|
|
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|