Example #1
0
  @PostConstruct
  private void init() {
    startupTime = Calendar.getInstance().getTimeInMillis();
    if (userDAO.getCount() == 0) {
      initialData();
    }

    initSupportedLanguages();

    ApplicationSettings settings = applicationSettingsService.get();
    int threads = settings.getBackgroundThreads();
    log.info("Starting {} background threads", threads);

    executor = Executors.newFixedThreadPool(Math.max(threads, 1));
    for (int i = 0; i < threads; i++) {
      final int threadId = i;
      executor.execute(
          new Runnable() {
            @Override
            public void run() {
              FeedRefreshWorker worker = workers.get();
              worker.start(running, "Thread " + threadId);
            }
          });
    }
  }
Example #2
0
 @PostConstruct
 public void init() {
   ApplicationSettings settings = applicationSettingsService.get();
   int threads = Math.max(settings.getDatabaseUpdateThreads(), 1);
   pool = new FeedRefreshExecutor("feed-refresh-updater", threads, Math.min(50 * threads, 1000));
   locks = Striped.lazyWeakLock(threads * 100000);
 }
Example #3
0
 @Path("/settings")
 @POST
 @ApiOperation(value = "Save application settings", notes = "Save application settings")
 public Response saveSettings(@ApiParam(required = true) ApplicationSettings settings) {
   Preconditions.checkNotNull(settings);
   applicationSettingsService.save(settings);
   return Response.ok().build();
 }
Example #4
0
 @Path("/settings")
 @GET
 @ApiOperation(
     value = "Retrieve application settings",
     notes = "Retrieve application settings",
     responseClass = "com.commafeed.backend.model.ApplicationSettings")
 public Response getSettings() {
   return Response.ok(applicationSettingsService.get()).build();
 }
Example #5
0
  private void initialData() {
    log.info("Populating database with default values");

    ApplicationSettings settings = new ApplicationSettings();
    settings.setAnnouncement("Set the Public URL in the admin section !");
    applicationSettingsService.save(settings);

    userService.register(USERNAME_ADMIN, "admin", Arrays.asList(Role.ADMIN, Role.USER));
    userService.register(USERNAME_DEMO, "demo", Arrays.asList(Role.USER));
  }
  private void update(Feed feed)
      throws NotSupportedException, SystemException, SecurityException, IllegalStateException,
          RollbackException, HeuristicMixedException, HeuristicRollbackException, JMSException {

    FetchedFeed fetchedFeed = null;
    Collection<FeedEntry> entries = null;

    String message = null;
    int errorCount = 0;
    Date disabledUntil = null;

    try {
      fetchedFeed =
          fetcher.fetch(feed.getUrl(), false, feed.getLastModifiedHeader(), feed.getEtagHeader());
      // stops here if NotModifiedException or any other exception is
      // thrown
      entries = fetchedFeed.getEntries();
      if (applicationSettingsService.get().isHeavyLoad()) {
        disabledUntil = FeedUtils.buildDisabledUntil(fetchedFeed);
      }

      feed.setLastUpdateSuccess(Calendar.getInstance().getTime());
      feed.setLink(fetchedFeed.getFeed().getLink());
      feed.setLastModifiedHeader(fetchedFeed.getFeed().getLastModifiedHeader());
      feed.setEtagHeader(fetchedFeed.getFeed().getEtagHeader());

      handlePubSub(feed, fetchedFeed);

    } catch (NotModifiedException e) {
      log.debug("Feed not modified (304) : " + feed.getUrl());
      if (feed.getErrorCount() == 0) {
        // not modified and had no error before, do nothing
        return;
      }
    } catch (Exception e) {
      message = "Unable to refresh feed " + feed.getUrl() + " : " + e.getMessage();
      if (e instanceof FeedException) {
        log.debug(e.getClass().getName() + " " + message);
      } else {
        log.debug(e.getClass().getName() + " " + message);
      }

      errorCount = feed.getErrorCount() + 1;
      disabledUntil = FeedUtils.buildDisabledUntil(errorCount);
    }

    feed.setErrorCount(errorCount);
    feed.setMessage(message);
    feed.setDisabledUntil(disabledUntil);

    feedRefreshUpdater.updateEntries(feed, entries);
  }
Example #7
0
  private Category buildCategory(
      Long id,
      List<FeedCategory> categories,
      List<FeedSubscription> subscriptions,
      Map<Long, UnreadCount> unreadCount) {
    Category category = new Category();
    category.setId(String.valueOf(id));
    category.setExpanded(true);

    for (FeedCategory c : categories) {
      if ((id == null && c.getParent() == null)
          || (c.getParent() != null && ObjectUtils.equals(c.getParent().getId(), id))) {
        Category child = buildCategory(c.getId(), categories, subscriptions, unreadCount);
        child.setId(String.valueOf(c.getId()));
        child.setName(c.getName());
        child.setPosition(c.getPosition());
        if (c.getParent() != null && c.getParent().getId() != null) {
          child.setParentId(String.valueOf(c.getParent().getId()));
        }
        child.setExpanded(!c.isCollapsed());
        category.getChildren().add(child);
      }
    }
    Collections.sort(
        category.getChildren(),
        new Comparator<Category>() {
          @Override
          public int compare(Category o1, Category o2) {
            return ObjectUtils.compare(o1.getPosition(), o2.getPosition());
          }
        });

    for (FeedSubscription subscription : subscriptions) {
      if ((id == null && subscription.getCategory() == null)
          || (subscription.getCategory() != null
              && ObjectUtils.equals(subscription.getCategory().getId(), id))) {
        UnreadCount uc = unreadCount.get(subscription.getId());
        Subscription sub =
            Subscription.build(subscription, applicationSettingsService.get().getPublicUrl(), uc);
        category.getFeeds().add(sub);
      }
    }
    Collections.sort(
        category.getFeeds(),
        new Comparator<Subscription>() {
          @Override
          public int compare(Subscription o1, Subscription o2) {
            return ObjectUtils.compare(o1.getPosition(), o2.getPosition());
          }
        });
    return category;
  }
Example #8
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();
  }
Example #9
0
  public BasePage() {

    String lang = "en";
    User user = CommaFeedSession.get().getUser();
    if (user != null) {
      UserSettings settings = userSettingsDAO.findByUser(user);
      if (settings != null) {
        lang = settings.getLanguage() == null ? "en" : settings.getLanguage();
      }
    }

    add(new TransparentWebMarkupContainer("html").add(new AttributeModifier("lang", lang)));

    settings = applicationSettingsService.get();
    add(new HeaderResponseContainer("footer-container", "footer-container"));
    add(
        new WebMarkupContainer("uservoice") {
          @Override
          protected void onConfigure() {
            super.onConfigure();
            setVisibilityAllowed(settings.isFeedbackButton());
          }
        });
  }
Example #10
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();
  }