X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloader.java;h=bea7702a930177b8ef0901312bbf06a2c145c277;hb=d35ab97a494fd187d3b8fe6b3253b65e3427a077;hp=6eaa1b0209a8bbd45199ff10c41427a4ce8079e0;hpb=1380d276bf5886e320f89cea232a6db7e4cdb7b8;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 6eaa1b0..bea7702 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -36,6 +36,8 @@ 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. * @@ -138,7 +140,7 @@ public class SoneDownloader extends AbstractService { * @return The downloaded Sone, or {@code null} if the Sone could not be * downloaded */ - public Sone fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly) { + public Optional 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); @@ -149,12 +151,12 @@ public class SoneDownloader extends AbstractService { return null; } logger.log(Level.FINEST, String.format("Got %d bytes back.", fetchResults.getFetchResult().size())); - Sone parsedSone = parseSone(sone, fetchResults.getFetchResult(), fetchResults.getFreenetUri()); - if (parsedSone != null) { + Optional parsedSone = parseSone(sone, fetchResults.getFetchResult(), fetchResults.getFreenetUri()); + if (parsedSone.isPresent()) { if (!fetchOnly) { - parsedSone.setStatus((parsedSone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); - core.updateSone(parsedSone); - addSone(parsedSone); + parsedSone.get().setStatus((parsedSone.get().getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); + core.updateSone(parsedSone.get()); + addSone(parsedSone.get()); } } return parsedSone; @@ -174,15 +176,15 @@ public class SoneDownloader extends AbstractService { * The requested URI * @return The parsed Sone, or {@code null} if the Sone could not be parsed */ - public Sone parseSone(Sone originalSone, FetchResult fetchResult, FreenetURI requestUri) { + public Optional 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(); - Sone parsedSone = parseSone(originalSone, soneInputStream); - if (parsedSone != null) { - parsedSone.setLatestEdition(requestUri.getEdition()); + Optional parsedSone = parseSone(originalSone, soneInputStream); + if (parsedSone.isPresent()) { + parsedSone.get().modify().setLatestEdition(requestUri.getEdition()).update(); } return parsedSone; } catch (Exception e1) { @@ -203,11 +205,9 @@ public class SoneDownloader extends AbstractService { * @param soneInputStream * The input stream to parse the Sone from * @return The parsed Sone - * @throws SoneException - * if a parse error occurs, or the protocol is invalid */ - public Sone parseSone(Sone originalSone, InputStream soneInputStream) throws SoneException { - return new SoneParser(core).parseSone(originalSone, soneInputStream); + public Optional parseSone(Sone originalSone, InputStream soneInputStream) { + return new SoneParser().parseSone(core.getDatabase(), originalSone, soneInputStream); } //