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 #2
0
  public static void logout(Context context) {
    NBSyncService.softInterrupt();
    NBSyncService.clearState();

    // wipe the prefs store
    context.getSharedPreferences(PrefConstants.PREFERENCES, 0).edit().clear().commit();

    // wipe the local DB
    FeedUtils.dropAndRecreateTables();

    // prompt for a new login
    Intent i = new Intent(context, Login.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(i);
  }
  public FetchedFeed fetch(
      String feedUrl,
      boolean extractFeedUrlFromHtml,
      String lastModified,
      String eTag,
      Date lastPublishedDate,
      String lastContentHash)
      throws FeedException, ClientProtocolException, IOException, NotModifiedException {
    log.debug("Fetching feed {}", feedUrl);
    FetchedFeed fetchedFeed = null;

    HttpResult result = getter.getBinary(feedUrl, lastModified, eTag);
    if (extractFeedUrlFromHtml) {
      String extractedUrl = extractFeedUrl(StringUtils.newStringUtf8(result.getContent()), feedUrl);
      if (org.apache.commons.lang.StringUtils.isNotBlank(extractedUrl)) {
        result = getter.getBinary(extractedUrl, lastModified, eTag);
        feedUrl = extractedUrl;
      }
    }
    byte[] content = result.getContent();

    if (content == null) {
      throw new IOException("Feed content is empty.");
    }

    String hash = DigestUtils.sha1Hex(content);
    if (lastContentHash != null && hash != null && lastContentHash.equals(hash)) {
      log.debug("content hash not modified: {}", feedUrl);
      throw new NotModifiedException();
    }

    fetchedFeed = parser.parse(feedUrl, content);

    if (lastPublishedDate != null
        && fetchedFeed.getFeed().getLastPublishedDate() != null
        && lastPublishedDate.getTime() == fetchedFeed.getFeed().getLastPublishedDate().getTime()) {
      log.debug("publishedDate not modified: {}", feedUrl);
      throw new NotModifiedException();
    }

    Feed feed = fetchedFeed.getFeed();
    feed.setLastModifiedHeader(result.getLastModifiedSince());
    feed.setEtagHeader(FeedUtils.truncate(result.geteTag(), 255));
    feed.setLastContentHash(hash);
    fetchedFeed.setFetchDuration(result.getDuration());
    return fetchedFeed;
  }
Example #4
0
  public static void clearPrefsAndDbForLoginAs(Context context) {
    NBSyncService.softInterrupt();
    NBSyncService.clearState();

    // wipe the prefs store except for the cookie and login keys since we need to
    // authenticate further API calls
    SharedPreferences prefs = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
    Set<String> keys = new HashSet<String>(prefs.getAll().keySet());
    keys.remove(PrefConstants.PREF_COOKIE);
    keys.remove(PrefConstants.PREF_UNIQUE_LOGIN);
    SharedPreferences.Editor editor = prefs.edit();
    for (String key : keys) {
      editor.remove(key);
    }
    editor.commit();

    // wipe the local DB
    FeedUtils.dropAndRecreateTables();
  }