Don’t process a download if the downloader was aborted.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloader.java
index bea7702..a406f1b 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.core;
 
+import static java.util.logging.Level.FINER;
 import static net.pterodactylus.sone.data.Sone.TO_FREENET_URI;
 
 import java.io.InputStream;
@@ -36,8 +37,6 @@ import freenet.client.FetchResult;
 import freenet.keys.FreenetURI;
 import freenet.support.api.Bucket;
 
-import com.google.common.base.Optional;
-
 /**
  * The Sone downloader is responsible for download Sones as they are updated.
  *
@@ -140,7 +139,7 @@ public class SoneDownloader extends AbstractService {
         * @return The downloaded Sone, or {@code null} if the Sone could not be
         *         downloaded
         */
-       public Optional<Sone> fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly) {
+       public Sone fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly) {
                logger.log(Level.FINE, String.format("Starting fetch for Sone “%s” from %s…", sone, soneUri));
                FreenetURI requestUri = soneUri.setMetaString(new String[] { "sone.xml" });
                sone.setStatus(SoneStatus.downloading);
@@ -150,13 +149,17 @@ public class SoneDownloader extends AbstractService {
                                /* TODO - mark Sone as bad. */
                                return null;
                        }
+                       if (shouldStop()) {
+                               logger.log(FINER, "Sone was stopped, won’t process download.");
+                               return null;
+                       }
                        logger.log(Level.FINEST, String.format("Got %d bytes back.", fetchResults.getFetchResult().size()));
-                       Optional<Sone> parsedSone = parseSone(sone, fetchResults.getFetchResult(), fetchResults.getFreenetUri());
-                       if (parsedSone.isPresent()) {
+                       Sone parsedSone = parseSone(sone, fetchResults.getFetchResult(), fetchResults.getFreenetUri());
+                       if (parsedSone != null) {
                                if (!fetchOnly) {
-                                       parsedSone.get().setStatus((parsedSone.get().getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
-                                       core.updateSone(parsedSone.get());
-                                       addSone(parsedSone.get());
+                                       parsedSone.setStatus((parsedSone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
+                                       core.updateSone(parsedSone);
+                                       addSone(parsedSone);
                                }
                        }
                        return parsedSone;
@@ -176,15 +179,15 @@ public class SoneDownloader extends AbstractService {
         *            The requested URI
         * @return The parsed Sone, or {@code null} if the Sone could not be parsed
         */
-       public Optional<Sone> parseSone(Sone originalSone, FetchResult fetchResult, FreenetURI requestUri) {
+       public Sone parseSone(Sone originalSone, FetchResult fetchResult, FreenetURI requestUri) {
                logger.log(Level.FINEST, String.format("Parsing FetchResult (%d bytes, %s) for %s…", fetchResult.size(), fetchResult.getMimeType(), originalSone));
                Bucket soneBucket = fetchResult.asBucket();
                InputStream soneInputStream = null;
                try {
                        soneInputStream = soneBucket.getInputStream();
-                       Optional<Sone> parsedSone = parseSone(originalSone, soneInputStream);
-                       if (parsedSone.isPresent()) {
-                               parsedSone.get().modify().setLatestEdition(requestUri.getEdition()).update();
+                       Sone parsedSone = parseSone(originalSone, soneInputStream);
+                       if (parsedSone != null) {
+                               parsedSone.modify().setLatestEdition(requestUri.getEdition()).update();
                        }
                        return parsedSone;
                } catch (Exception e1) {
@@ -206,7 +209,7 @@ public class SoneDownloader extends AbstractService {
         *            The input stream to parse the Sone from
         * @return The parsed Sone
         */
-       public Optional<Sone> parseSone(Sone originalSone, InputStream soneInputStream) {
+       public Sone parseSone(Sone originalSone, InputStream soneInputStream) {
                return new SoneParser().parseSone(core.getDatabase(), originalSone, soneInputStream);
        }