mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
rest endpoint to retrieve a feed or a category as a feed
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user