X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloader.java;h=b0805494b8aae26818ee8db0e8180f6dc67946ed;hb=ad0bc2de2393c76b47d849f6ae74e44faafefbb0;hp=72ff0080832c0be12f30e870faeb7d1b6198e34f;hpb=50ce65f69e49ed10abeedaeb6615ffb37a0c0772;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 72ff008..b080549 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -1,5 +1,5 @@ /* - * Sone - SoneDownloader.java - Copyright © 2010–2012 David Roden + * Sone - SoneDownloader.java - Copyright © 2010–2013 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,6 +26,7 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.sone.core.FreenetInterface.Fetched; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Image; @@ -34,7 +35,9 @@ import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; -import net.pterodactylus.util.collection.Pair; +import net.pterodactylus.sone.data.SoneImpl; +import net.pterodactylus.sone.database.PostBuilder; +import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.number.Numbers; @@ -42,6 +45,7 @@ import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.xml.SimpleXML; import net.pterodactylus.util.xml.XML; +import com.google.common.annotations.VisibleForTesting; import org.w3c.dom.Document; import freenet.client.FetchResult; @@ -102,18 +106,6 @@ public class SoneDownloader extends AbstractService { } /** - * Removes the given Sone from the downloader. - * - * @param sone - * The Sone to stop watching - */ - public void removeSone(Sone sone) { - if (sones.remove(sone)) { - freenetInterface.unregisterUsk(sone); - } - } - - /** * Fetches the updated Sone. This method is a callback method for * {@link FreenetInterface#registerUsk(Sone, SoneDownloader)}. * @@ -155,15 +147,16 @@ public class SoneDownloader extends AbstractService { FreenetURI requestUri = soneUri.setMetaString(new String[] { "sone.xml" }); sone.setStatus(SoneStatus.downloading); try { - Pair fetchResults = freenetInterface.fetchUri(requestUri); + Fetched fetchResults = freenetInterface.fetchUri(requestUri); if (fetchResults == null) { /* TODO - mark Sone as bad. */ return null; } - logger.log(Level.FINEST, String.format("Got %d bytes back.", fetchResults.getRight().size())); - Sone parsedSone = parseSone(sone, fetchResults.getRight(), fetchResults.getLeft()); + logger.log(Level.FINEST, String.format("Got %d bytes back.", fetchResults.getFetchResult().size())); + Sone parsedSone = parseSone(sone, fetchResults.getFetchResult(), fetchResults.getFreenetUri()); if (parsedSone != null) { if (!fetchOnly) { + parsedSone.setStatus((parsedSone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); core.updateSone(parsedSone); addSone(parsedSone); } @@ -185,7 +178,7 @@ 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) { + private 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; @@ -222,7 +215,8 @@ public class SoneDownloader extends AbstractService { * @throws SoneException * if a parse error occurs, or the protocol is invalid */ - public Sone parseSone(Sone originalSone, InputStream soneInputStream) throws SoneException { + @VisibleForTesting + protected Sone parseSone(Sone originalSone, InputStream soneInputStream) throws SoneException { /* TODO - impose a size limit? */ Document document; @@ -236,7 +230,7 @@ public class SoneDownloader extends AbstractService { return null; } - Sone sone = new Sone(originalSone.getId()).setIdentity(originalSone.getIdentity()); + Sone sone = new SoneImpl(originalSone.getId(), originalSone.isLocal()).setIdentity(originalSone.getIdentity()); SimpleXML soneXml; try { @@ -304,16 +298,8 @@ public class SoneDownloader extends AbstractService { } } - String soneInsertUri = soneXml.getValue("insert-uri", null); - if ((soneInsertUri != null) && (sone.getInsertUri() == null)) { - try { - sone.setInsertUri(new FreenetURI(soneInsertUri)); - sone.setLatestEdition(Math.max(sone.getRequestUri().getEdition(), sone.getInsertUri().getEdition())); - } catch (MalformedURLException mue1) { - /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded Sone %s has invalid insert URI: %s", sone, soneInsertUri), mue1); - return null; - } + if (originalSone.getInsertUri() != null) { + sone.setInsertUri(originalSone.getInsertUri()); } SimpleXML profileXml = soneXml.getNode("profile"); @@ -346,7 +332,7 @@ public class SoneDownloader extends AbstractService { return null; } try { - profile.addField(fieldName).setValue(fieldValue); + profile.addField(fieldName.trim()).setValue(fieldValue); } catch (IllegalArgumentException iae1) { logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1); return null; @@ -372,11 +358,13 @@ public class SoneDownloader extends AbstractService { return null; } try { - Post post = core.getPost(postId).setSone(sone).setTime(Long.parseLong(postTime)).setText(postText); + PostBuilder postBuilder = core.postBuilder(); + /* TODO - parse time correctly. */ + postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { - post.setRecipient(core.getSone(postRecipientId)); + postBuilder.to(postRecipientId); } - posts.add(post); + posts.add(postBuilder.build()); } catch (NumberFormatException nfe1) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime)); @@ -403,7 +391,10 @@ public class SoneDownloader extends AbstractService { return null; } try { - replies.add(core.getReply(replyId).setSone(sone).setPost(core.getPost(replyPostId)).setTime(Long.parseLong(replyTime)).setText(replyText)); + PostReplyBuilder postReplyBuilder = core.postReplyBuilder(); + /* TODO - parse time correctly. */ + postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText); + replies.add(postReplyBuilder.build()); } catch (NumberFormatException nfe1) { /* TODO - mark Sone as bad. */ logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime)); @@ -448,7 +439,7 @@ public class SoneDownloader extends AbstractService { String title = albumXml.getValue("title", null); String description = albumXml.getValue("description", ""); String albumImageId = albumXml.getValue("album-image", null); - if ((id == null) || (title == null) || (description == null)) { + if ((id == null) || (title == null)) { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone)); return null; } @@ -460,7 +451,7 @@ public class SoneDownloader extends AbstractService { return null; } } - Album album = core.getAlbum(id).setSone(sone).setTitle(title).setDescription(description); + Album album = core.getAlbum(id).setSone(sone).modify().setTitle(title).setDescription(description).update(); if (parent != null) { parent.addAlbum(album); } else { @@ -487,13 +478,13 @@ public class SoneDownloader extends AbstractService { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString)); return null; } - Image image = core.getImage(imageId).setSone(sone).setKey(imageKey).setCreationTime(creationTime); - image.setTitle(imageTitle).setDescription(imageDescription); - image.setWidth(imageWidth).setHeight(imageHeight); + Image image = core.getImage(imageId).modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update(); + image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update(); + image = image.modify().setWidth(imageWidth).setHeight(imageHeight).update(); album.addImage(image); } } - album.setAlbumImage(albumImageId); + album.modify().setAlbumImage(albumImageId).update(); } } @@ -510,7 +501,9 @@ public class SoneDownloader extends AbstractService { sone.setReplies(replies); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); - sone.setAlbums(topLevelAlbums); + for (Album album : topLevelAlbums) { + sone.getRootAlbum().addAlbum(album); + } } return sone; @@ -530,4 +523,34 @@ public class SoneDownloader extends AbstractService { } } + public class FetchSoneWithUri implements Runnable { + + private final Sone sone; + + public FetchSoneWithUri(Sone sone) { + this.sone = sone; + } + + @Override + public void run() { + fetchSone(sone, sone.getRequestUri()); + } + + } + + public class FetchSone implements Runnable { + + private final Sone sone; + + public FetchSone(Sone sone) { + this.sone = sone; + } + + @Override + public void run() { + fetchSone(sone); + } + + } + }