From: David ‘Bombe’ Roden Date: Sat, 19 Oct 2013 18:04:46 +0000 (+0200) Subject: Set client information in Sone builder. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f1a337e97c0188170e8ea8bb06b99324f3a07dee;hp=9f9834453e9555175e4771932d9521209bd7188c;p=Sone.git Set client information in Sone builder. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 1109537..0f22b81 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -559,9 +559,8 @@ public class Core extends AbstractService implements SoneProvider { logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity)); synchronized (sones) { final Sone sone; - sone = database.newSoneBuilder().by(ownIdentity.getId()).local().build(Optional.absent()); + sone = database.newSoneBuilder().by(ownIdentity.getId()).local().using(new Client("Sone", SonePlugin.VERSION.toString())).build(Optional.absent()); sone.modify().setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)).update(); - sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); sone.setKnown(true); /* TODO - load posts ’n stuff */ sones.put(ownIdentity.getId(), sone); diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 79fc09a..7900a10 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -90,17 +90,29 @@ public class SoneParser { return null; } - Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal()); - SimpleXML soneXml; try { soneXml = SimpleXML.fromDocument(document); } catch (NullPointerException npe1) { /* for some reason, invalid XML can cause NPEs. */ - logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", sone), npe1); + logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone), npe1); return null; } + SimpleXML clientXml = soneXml.getNode("client"); + Client soneClient = originalSone.getClient(); + if (clientXml != null) { + String clientName = clientXml.getValue("name", null); + String clientVersion = clientXml.getValue("version", null); + if ((clientName == null) || (clientVersion == null)) { + logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", originalSone)); + return null; + } + soneClient = new Client(clientName, clientVersion); + } + + Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal(), soneClient); + Integer protocolVersion = null; String soneProtocolVersion = soneXml.getValue("protocol-version", null); if (soneProtocolVersion != null) { @@ -136,17 +148,6 @@ public class SoneParser { return null; } - SimpleXML clientXml = soneXml.getNode("client"); - if (clientXml != null) { - String clientName = clientXml.getValue("name", null); - String clientVersion = clientXml.getValue("version", null); - if ((clientName == null) || (clientVersion == null)) { - logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone)); - return null; - } - sone.setClient(new Client(clientName, clientVersion)); - } - SimpleXML profileXml = soneXml.getNode("profile"); if (profileXml == null) { /* TODO - mark Sone as bad. */ diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 083b3a5..989c6a1 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -267,15 +267,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { Client getClient(); /** - * Sets the client used by this Sone. - * - * @param client - * The client used by this Sone, or {@code null} - * @return This Sone (for method chaining) - */ - Sone setClient(Client client); - - /** * Returns whether this Sone is known. * * @return {@code true} if this Sone is known, {@code false} otherwise diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java index 140fbd5..878ba8d 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java @@ -2,6 +2,7 @@ package net.pterodactylus.sone.data.impl; import static com.google.common.base.Preconditions.checkState; +import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.database.SoneBuilder; import net.pterodactylus.sone.freenet.wot.Identity; @@ -17,6 +18,7 @@ public abstract class AbstractSoneBuilder implements SoneBuilder { protected String id; protected boolean local; + protected Client client; @Override public SoneBuilder by(String id) { @@ -30,8 +32,15 @@ public abstract class AbstractSoneBuilder implements SoneBuilder { return this; } + @Override + public SoneBuilder using(Client client) { + this.client = client; + return this; + } + protected void validate() throws IllegalStateException { checkState(id != null, "id must not be null"); + checkState(client != null, "client must not be null"); } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java index 3abd1d4..92d7d5f 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java @@ -88,7 +88,7 @@ public class DefaultSone implements Sone { private volatile Profile profile = new Profile(this); /** The client used by the Sone. */ - private volatile Client client; + private final Client client; /** Whether this Sone is known. */ private volatile boolean known; @@ -122,10 +122,11 @@ public class DefaultSone implements Sone { * @param local * {@code true} if the Sone is a local Sone, {@code false} otherwise */ - public DefaultSone(Database database, String id, boolean local) { + public DefaultSone(Database database, String id, boolean local, Client client) { this.database = database; this.id = id; this.local = local; + this.client = client; rootAlbum = new DefaultAlbumBuilder(database, this, null).build(); } @@ -223,11 +224,6 @@ public class DefaultSone implements Sone { return client; } - public Sone setClient(Client client) { - this.client = client; - return this; - } - public boolean isKnown() { return known; } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSoneBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSoneBuilder.java index 37a83fd..02a2d87 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSoneBuilder.java @@ -22,7 +22,7 @@ public class DefaultSoneBuilder extends AbstractSoneBuilder { @Override public Sone build(Optional soneCreated) throws IllegalStateException { validate(); - Sone sone = new DefaultSone(database, id, local); + Sone sone = new DefaultSone(database, id, local, client); if (soneCreated.isPresent()) { soneCreated.get().soneCreated(sone); } diff --git a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java index d0b38af..ebac97e 100644 --- a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.database; +import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.wot.Identity; @@ -31,6 +32,7 @@ public interface SoneBuilder { SoneBuilder by(String id); SoneBuilder local(); + SoneBuilder using(Client client); Sone build(Optional soneCreated) throws IllegalStateException; diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index 0edd5f3..a9208dc 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -237,7 +237,7 @@ public class SoneTextParser implements Parser { * don’t use create=true above, we don’t want * the empty shell. */ - sone = Optional.of(new DefaultSone(database, soneId, false)); + sone = Optional.of(new DefaultSone(database, soneId, false, null)); } parts.add(new SonePart(sone.get())); line = line.substring(50);