示例#1
0
  @Path("/entriesAsFeed")
  @GET
  @ApiOperation(value = "Get category entries as feed", notes = "Get a feed of category entries")
  @Produces(MediaType.APPLICATION_XML)
  @SecurityCheck(value = Role.USER, apiKeyAllowed = true)
  public Response getCategoryEntriesAsFeed(
      @ApiParam(value = "id of the category, 'all' or 'starred'", required = true) @QueryParam("id")
          String id) {

    Preconditions.checkNotNull(id);

    ReadingMode readType = ReadingMode.all;
    ReadingOrder order = ReadingOrder.desc;
    int offset = 0;
    int limit = 20;

    Entries entries =
        (Entries)
            getCategoryEntries(id, readType, null, offset, limit, order, null, false).getEntity();

    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 Response.ok(writer.toString()).build();
  }
示例#2
0
  @Path("/entries")
  @GET
  @ApiOperation(
      value = "Get category entries",
      notes = "Get a list of category entries",
      responseClass = "com.commafeed.frontend.model.Entries")
  public Response getCategoryEntries(
      @ApiParam(value = "id of the category, 'all' or 'starred'", required = true) @QueryParam("id")
          String id,
      @ApiParam(
              value = "all entries or only unread ones",
              allowableValues = "all,unread",
              required = true)
          @DefaultValue("unread")
          @QueryParam("readType")
          ReadingMode readType,
      @ApiParam(value = "only entries newer than this") @QueryParam("newerThan") Long newerThan,
      @ApiParam(value = "offset for paging") @DefaultValue("0") @QueryParam("offset") int offset,
      @ApiParam(value = "limit for paging, default 20, maximum 1000")
          @DefaultValue("20")
          @QueryParam("limit")
          int limit,
      @ApiParam(value = "date ordering", allowableValues = "asc,desc")
          @QueryParam("order")
          @DefaultValue("desc")
          ReadingOrder order,
      @ApiParam(
              value =
                  "search for keywords in either the title or the content of the entries, separated by spaces, 3 characters minimum")
          @QueryParam("keywords")
          String keywords,
      @ApiParam(value = "return only entry ids") @DefaultValue("false") @QueryParam("onlyIds")
          boolean onlyIds) {

    Preconditions.checkNotNull(readType);

    keywords = StringUtils.trimToNull(keywords);
    Preconditions.checkArgument(keywords == null || StringUtils.length(keywords) >= 3);

    limit = Math.min(limit, 1000);
    limit = Math.max(0, limit);

    Entries entries = new Entries();
    entries.setOffset(offset);
    entries.setLimit(limit);
    boolean unreadOnly = readType == ReadingMode.unread;
    if (StringUtils.isBlank(id)) {
      id = ALL;
    }

    Date newerThanDate = newerThan == null ? null : new Date(Long.valueOf(newerThan));

    if (ALL.equals(id)) {
      entries.setName("All");
      List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(getUser());
      List<FeedEntryStatus> list =
          feedEntryStatusDAO.findBySubscriptions(
              subscriptions,
              unreadOnly,
              keywords,
              newerThanDate,
              offset,
              limit + 1,
              order,
              true,
              onlyIds);

      for (FeedEntryStatus status : list) {
        entries
            .getEntries()
            .add(
                Entry.build(
                    status,
                    applicationSettingsService.get().getPublicUrl(),
                    applicationSettingsService.get().isImageProxyEnabled()));
      }

    } else if (STARRED.equals(id)) {
      entries.setName("Starred");
      List<FeedEntryStatus> starred =
          feedEntryStatusDAO.findStarred(
              getUser(), newerThanDate, offset, limit + 1, order, !onlyIds);
      for (FeedEntryStatus status : starred) {
        entries
            .getEntries()
            .add(
                Entry.build(
                    status,
                    applicationSettingsService.get().getPublicUrl(),
                    applicationSettingsService.get().isImageProxyEnabled()));
      }
    } else {
      FeedCategory parent = feedCategoryDAO.findById(getUser(), Long.valueOf(id));
      if (parent != null) {
        List<FeedCategory> categories =
            feedCategoryDAO.findAllChildrenCategories(getUser(), parent);
        List<FeedSubscription> subs = feedSubscriptionDAO.findByCategories(getUser(), categories);

        List<FeedEntryStatus> list =
            feedEntryStatusDAO.findBySubscriptions(
                subs, unreadOnly, keywords, newerThanDate, offset, limit + 1, order, true, onlyIds);

        for (FeedEntryStatus status : list) {
          entries
              .getEntries()
              .add(
                  Entry.build(
                      status,
                      applicationSettingsService.get().getPublicUrl(),
                      applicationSettingsService.get().isImageProxyEnabled()));
        }
        entries.setName(parent.getName());
      }
    }

    boolean hasMore = entries.getEntries().size() > limit;
    if (hasMore) {
      entries.setHasMore(true);
      entries.getEntries().remove(entries.getEntries().size() - 1);
    }

    entries.setTimestamp(System.currentTimeMillis());
    FeedUtils.removeUnwantedFromSearch(entries.getEntries(), keywords);
    return Response.ok(entries).build();
  }