rest endpoint to retrieve a feed or a category as a feed

This commit is contained in:
Athou
2013-04-30 07:12:45 +02:00
parent aef82fe690
commit 2cbf51c287
3 changed files with 132 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package com.commafeed.frontend.model;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
@@ -9,6 +10,9 @@ import javax.xml.bind.annotation.XmlRootElement;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntryStatus;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.wordnik.swagger.annotations.ApiClass;
import com.wordnik.swagger.annotations.ApiProperty;
@@ -23,6 +27,7 @@ public class Entry implements Serializable {
FeedEntry feedEntry = status.getEntry();
entry.setId(String.valueOf(status.getId()));
entry.setGuid(feedEntry.getGuid());
entry.setTitle(feedEntry.getContent().getTitle());
entry.setContent(feedEntry.getContent().getContent());
entry.setEnclosureUrl(status.getEntry().getContent().getEnclosureUrl());
@@ -40,9 +45,26 @@ public class Entry implements Serializable {
return entry;
}
public SyndEntry asRss() {
SyndEntry entry = new SyndEntryImpl();
entry.setUri(getGuid());
entry.setTitle(getTitle());
SyndContentImpl content = new SyndContentImpl();
content.setValue(getContent());
entry.setContents(Arrays.asList(content));
entry.setLink(getUrl());
entry.setPublishedDate(getDate());
return entry;
}
@ApiProperty("entry id")
private String id;
@ApiProperty("entry guid")
private String guid;
@ApiProperty("entry title")
private String title;
@@ -172,4 +194,12 @@ public class Entry implements Serializable {
this.enclosureType = enclosureType;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
}

View File

@@ -1,5 +1,6 @@
package com.commafeed.frontend.rest.resources;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
@@ -11,12 +12,16 @@ import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedEntryStatus;
@@ -33,6 +38,11 @@ import com.commafeed.frontend.model.request.MarkRequest;
import com.commafeed.frontend.model.request.RenameRequest;
import com.commafeed.frontend.rest.Enums.ReadType;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
@@ -41,6 +51,8 @@ import com.wordnik.swagger.annotations.ApiParam;
@Api(value = "/category", description = "Operations about user categories")
public class CategoryREST extends AbstractResourceREST {
private static Logger log = LoggerFactory.getLogger(CategoryREST.class);
public static final String ALL = "all";
public static final String STARRED = "starred";
@@ -95,6 +107,46 @@ public class CategoryREST extends AbstractResourceREST {
return entries;
}
@Path("/entriesAsFeed")
@GET
@ApiOperation(value = "Get category entries as feed", notes = "Get a feed of category entries")
@Produces(MediaType.APPLICATION_XML)
public String getCategoryEntriesAsFeed(
@ApiParam(value = "id of the category, 'all' or 'starred'", required = true) @QueryParam("id") String id) {
Preconditions.checkNotNull(id);
ReadType readType = ReadType.all;
ReadingOrder order = ReadingOrder.desc;
int offset = 0;
int limit = 20;
Entries entries = getCategoryEntries(id, readType, offset, limit, order);
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("CommaFeed - " + entries.getName());
feed.setDescription("CommaFeed - " + entries.getName());
String publicUrl = applicationSettingsService.get().getPublicUrl();
feed.setLink(publicUrl);
List<SyndEntry> children = Lists.newArrayList();
for (Entry entry : entries.getEntries()) {
children.add(entry.asRss());
}
feed.setEntries(children);
SyndFeedOutput output = new SyndFeedOutput();
StringWriter writer = new StringWriter();
try {
output.output(feed, writer);
} catch (Exception e) {
writer.write("Could not get feed information");
log.error(e.getMessage(), e);
}
return writer.toString();
}
@Path("/mark")
@POST
@ApiOperation(value = "Mark category entries", notes = "Mark feed entries of this category as read")

View File

@@ -1,5 +1,6 @@
package com.commafeed.frontend.rest.resources;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@@ -22,6 +23,8 @@ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.commafeed.backend.feeds.FetchedFeed;
import com.commafeed.backend.model.FeedCategory;
@@ -37,7 +40,12 @@ import com.commafeed.frontend.model.request.RenameRequest;
import com.commafeed.frontend.model.request.SubscribeRequest;
import com.commafeed.frontend.rest.Enums.ReadType;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.sun.syndication.feed.opml.Opml;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;
import com.sun.syndication.io.WireFeedOutput;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
@@ -47,6 +55,8 @@ import com.wordnik.swagger.annotations.ApiParam;
@Api(value = "/feed", description = "Operations about feeds")
public class FeedREST extends AbstractResourceREST {
private static Logger log = LoggerFactory.getLogger(FeedREST.class);
@Path("/entries")
@GET
@ApiOperation(value = "Get feed entries", notes = "Get a list of feed entries", responseClass = "com.commafeed.frontend.model.Entries")
@@ -82,6 +92,46 @@ public class FeedREST extends AbstractResourceREST {
return entries;
}
@Path("/entriesAsFeed")
@GET
@ApiOperation(value = "Get feed entries as a feed", notes = "Get a feed of feed entries")
@Produces(MediaType.APPLICATION_XML)
public String getFeedEntriesAsFeed(
@ApiParam(value = "id of the feed", required = true) @QueryParam("id") String id) {
Preconditions.checkNotNull(id);
ReadType readType = ReadType.all;
ReadingOrder order = ReadingOrder.desc;
int offset = 0;
int limit = 20;
Entries entries = getFeedEntries(id, readType, offset, limit, order);
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("CommaFeed - " + entries.getName());
feed.setDescription("CommaFeed - " + entries.getName());
String publicUrl = applicationSettingsService.get().getPublicUrl();
feed.setLink(publicUrl);
List<SyndEntry> children = Lists.newArrayList();
for (Entry entry : entries.getEntries()) {
children.add(entry.asRss());
}
feed.setEntries(children);
SyndFeedOutput output = new SyndFeedOutput();
StringWriter writer = new StringWriter();
try {
output.output(feed, writer);
} catch (Exception e) {
writer.write("Could not get feed information");
log.error(e.getMessage(), e);
}
return writer.toString();
}
@GET
@Path("/fetch")
@ApiOperation(value = "Fetch a feed", notes = "Fetch a feed by its url", responseClass = "com.commafeed.backend.model.Feed")