Merge branch 'next' into dev/image
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloader.java
index 8d0d1f2..4da1226 100644 (file)
@@ -26,7 +26,6 @@ import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import net.pterodactylus.sone.core.Core.Preferences;
 import net.pterodactylus.sone.core.Core.SoneStatus;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Client;
@@ -122,13 +121,12 @@ public class SoneDownloader extends AbstractService {
         *            The Sone to fetch
         */
        public void fetchSone(Sone sone) {
-               fetchSone(sone, sone.getRequestUri());
+               fetchSone(sone, sone.getRequestUri().sskForUSK());
        }
 
        /**
         * Fetches the updated Sone. This method can be used to fetch a Sone from a
-        * specific URI (which happens when {@link Preferences#isSoneRescueMode()
-        * „Sone rescue mode“} is active).
+        * specific URI.
         *
         * @param sone
         *            The Sone to fetch
@@ -136,6 +134,23 @@ public class SoneDownloader extends AbstractService {
         *            The URI to fetch the Sone from
         */
        public void fetchSone(Sone sone, FreenetURI soneUri) {
+               fetchSone(sone, soneUri, false);
+       }
+
+       /**
+        * Fetches the Sone from the given URI.
+        *
+        * @param sone
+        *            The Sone to fetch
+        * @param soneUri
+        *            The URI of the Sone to fetch
+        * @param fetchOnly
+        *            {@code true} to only fetch and parse the Sone, {@code false}
+        *            to {@link Core#updateSone(Sone) update} it in the core
+        * @return The downloaded Sone, or {@code null} if the Sone could not be
+        *         downloaded
+        */
+       public Sone fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly) {
                logger.log(Level.FINE, "Starting fetch for Sone “%s” from %s…", new Object[] { sone, soneUri });
                FreenetURI requestUri = soneUri.setMetaString(new String[] { "sone.xml" });
                core.setSoneStatus(sone, SoneStatus.downloading);
@@ -143,14 +158,17 @@ public class SoneDownloader extends AbstractService {
                        Pair<FreenetURI, FetchResult> fetchResults = freenetInterface.fetchUri(requestUri);
                        if (fetchResults == null) {
                                /* TODO - mark Sone as bad. */
-                               return;
+                               return null;
                        }
                        logger.log(Level.FINEST, "Got %d bytes back.", fetchResults.getRight().size());
                        Sone parsedSone = parseSone(sone, fetchResults.getRight(), fetchResults.getLeft());
                        if (parsedSone != null) {
-                               addSone(parsedSone);
-                               core.updateSone(parsedSone);
+                               if (!fetchOnly) {
+                                       core.updateSone(parsedSone);
+                                       addSone(parsedSone);
+                               }
                        }
+                       return parsedSone;
                } finally {
                        core.setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
                }