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;
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.
*
* @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);
/* 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;
* 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) {
* 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);
}