From d525d4ad65878cfd895945512650d978f6a5c034 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 18 Nov 2014 22:12:25 +0100 Subject: [PATCH] Use the new client layer from fred. --- pom.xml | 2 +- .../pterodactylus/sone/core/FreenetInterface.java | 39 +++--- .../net/pterodactylus/sone/core/SoneInserter.java | 10 +- .../freenet/PluginStoreConfigurationBackend.java | 13 +- .../pterodactylus/sone/freenet/StringBucket.java | 146 --------------------- .../net/pterodactylus/sone/main/SonePlugin.java | 4 +- 6 files changed, 36 insertions(+), 178 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/freenet/StringBucket.java diff --git a/pom.xml b/pom.xml index e96a971..550acc4 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ org.freenetproject fred - 0.7.5.1405 + 0.7.5.1467-SNAPSHOT provided diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index e92cf8e..7cb8a9a 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -34,12 +34,12 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.util.logging.Logging; -import com.db4o.ObjectContainer; import com.google.common.eventbus.EventBus; import com.google.inject.Inject; import freenet.client.ClientMetadata; import freenet.client.FetchException; +import freenet.client.FetchException.FetchExceptionMode; import freenet.client.FetchResult; import freenet.client.HighLevelSimpleClient; import freenet.client.HighLevelSimpleClientImpl; @@ -55,9 +55,12 @@ import freenet.keys.FreenetURI; import freenet.keys.InsertableClientSSK; import freenet.keys.USK; import freenet.node.Node; +import freenet.node.RequestClient; import freenet.node.RequestStarter; import freenet.support.api.Bucket; +import freenet.support.api.RandomAccessBucket; import freenet.support.io.ArrayBucket; +import freenet.support.io.ResumeFailedException; /** * Contains all necessary functionality for interacting with the Freenet node. @@ -118,7 +121,7 @@ public class FreenetInterface { fetchResult = client.fetch(currentUri); return new Fetched(currentUri, fetchResult); } catch (FetchException fe1) { - if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) { + if (fe1.getMode() == FetchExceptionMode.PERMANENT_REDIRECT) { currentUri = fe1.newURI; continue; } @@ -157,11 +160,11 @@ public class FreenetInterface { InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, ""); FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint); InsertContext insertContext = client.getInsertContext(true); - Bucket bucket = new ArrayBucket(temporaryImage.getImageData()); + RandomAccessBucket bucket = new ArrayBucket(temporaryImage.getImageData()); ClientMetadata metadata = new ClientMetadata(temporaryImage.getMimeType()); InsertBlock insertBlock = new InsertBlock(bucket, metadata, targetUri); try { - ClientPutter clientPutter = client.insert(insertBlock, false, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS); + ClientPutter clientPutter = client.insert(insertBlock, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS); insertToken.setClientPutter(clientPutter); } catch (InsertException ie1) { throw new SoneInsertException("Could not start image insert.", ie1); @@ -205,7 +208,7 @@ public class FreenetInterface { @Override @SuppressWarnings("synthetic-access") - public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) { + public void onFoundEdition(long edition, USK key, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) { logger.log(Level.FINE, String.format("Found USK update for Sone “%s” at %s, new known good: %s, new slot too: %s.", sone, key, newKnownGood, newSlotToo)); if (edition > sone.getLatestEdition()) { sone.setLatestEdition(edition); @@ -269,7 +272,7 @@ public class FreenetInterface { USKCallback uskCallback = new USKCallback() { @Override - public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) { + public void onFoundEdition(long edition, USK key, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) { callback.editionFound(key.getURI(), edition, newKnownGood, newSlotToo); } @@ -441,7 +444,7 @@ public class FreenetInterface { */ @SuppressWarnings("synthetic-access") public void cancel() { - clientPutter.cancel(null, node.clientCore.clientContext); + clientPutter.cancel(node.clientCore.clientContext); eventBus.post(new ImageInsertAbortedEvent(image)); } @@ -449,12 +452,14 @@ public class FreenetInterface { // INTERFACE ClientPutCallback // - /** - * {@inheritDoc} - */ @Override - public void onMajorProgress(ObjectContainer objectContainer) { - /* ignore, we don’t care. */ + public RequestClient getRequestClient() { + return clientPutter.getClient(); + } + + @Override + public void onResume(ClientContext context) throws ResumeFailedException { + /* ignore. */ } /** @@ -462,7 +467,7 @@ public class FreenetInterface { */ @Override @SuppressWarnings("synthetic-access") - public void onFailure(InsertException insertException, BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onFailure(InsertException insertException, BaseClientPutter clientPutter) { if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) { eventBus.post(new ImageInsertAbortedEvent(image)); } else { @@ -474,7 +479,7 @@ public class FreenetInterface { * {@inheritDoc} */ @Override - public void onFetchable(BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onFetchable(BaseClientPutter clientPutter) { /* ignore, we don’t care. */ } @@ -482,7 +487,7 @@ public class FreenetInterface { * {@inheritDoc} */ @Override - public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter) { /* ignore, we don’t care. */ } @@ -490,7 +495,7 @@ public class FreenetInterface { * {@inheritDoc} */ @Override - public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter) { resultingUri = generatedUri; } @@ -499,7 +504,7 @@ public class FreenetInterface { */ @Override @SuppressWarnings("synthetic-access") - public void onSuccess(BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onSuccess(BaseClientPutter clientPutter) { eventBus.post(new ImageInsertFinishedEvent(image, resultingUri)); } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index a1bb903..7e90b2b 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -37,7 +37,6 @@ import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; -import net.pterodactylus.sone.freenet.StringBucket; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; @@ -51,12 +50,15 @@ import net.pterodactylus.util.template.TemplateException; import net.pterodactylus.util.template.TemplateParser; import net.pterodactylus.util.template.XmlFilter; +import com.google.common.base.Charsets; import com.google.common.collect.FluentIterable; import com.google.common.collect.Ordering; import com.google.common.eventbus.EventBus; -import freenet.client.async.ManifestElement; import freenet.keys.FreenetURI; +import freenet.support.api.ManifestElement; +import freenet.support.api.RandomAccessBucket; +import freenet.support.io.ArrayBucket; /** * A Sone inserter is responsible for inserting a Sone if it has changed. @@ -391,10 +393,10 @@ public class SoneInserter extends AbstractService { templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition()); templateContext.set("version", SonePlugin.VERSION); StringWriter writer = new StringWriter(); - StringBucket bucket = null; + RandomAccessBucket bucket = null; try { template.render(templateContext, writer); - bucket = new StringBucket(writer.toString(), utf8Charset); + bucket = new ArrayBucket(writer.toString().getBytes(Charsets.UTF_8)); return new ManifestElement(name, bucket, contentType, bucket.size()); } catch (TemplateException te1) { logger.log(Level.SEVERE, String.format("Could not render template “%s”!", templateName), te1); diff --git a/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java b/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java index 1e95389..5bde0b0 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java +++ b/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java @@ -24,7 +24,7 @@ import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.config.ExtendedConfigurationBackend; import net.pterodactylus.util.logging.Logging; -import freenet.client.async.DatabaseDisabledException; +import freenet.client.async.PersistenceDisabledException; import freenet.pluginmanager.PluginRespirator; import freenet.pluginmanager.PluginStore; @@ -50,15 +50,12 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac * * @param pluginRespirator * The plugin respirator - * @throws DatabaseDisabledException + * @throws PersistenceDisabledException * if the plugin store is not available */ - public PluginStoreConfigurationBackend(PluginRespirator pluginRespirator) throws DatabaseDisabledException { + public PluginStoreConfigurationBackend(PluginRespirator pluginRespirator) throws PersistenceDisabledException { this.pluginRespirator = pluginRespirator; this.pluginStore = pluginRespirator.getStore(); - if (this.pluginStore == null) { - throw new DatabaseDisabledException(); - } } /** @@ -176,8 +173,8 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac public void save() throws ConfigurationException { try { pluginRespirator.putStore(pluginStore); - } catch (DatabaseDisabledException dde1) { - throw new ConfigurationException("Could not store plugin store, database is disabled.", dde1); + } catch (PersistenceDisabledException pde1) { + throw new ConfigurationException("Could not store plugin store, persistence is disabled.", pde1); } } diff --git a/src/main/java/net/pterodactylus/sone/freenet/StringBucket.java b/src/main/java/net/pterodactylus/sone/freenet/StringBucket.java deleted file mode 100644 index c96a3cd..0000000 --- a/src/main/java/net/pterodactylus/sone/freenet/StringBucket.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Sone - StringBucket.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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.freenet; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.Charset; - -import com.db4o.ObjectContainer; - -import freenet.support.api.Bucket; - -/** - * {@link Bucket} implementation wrapped around a {@link String}. - * - * @author David ‘Bombe’ Roden - */ -public class StringBucket implements Bucket { - - /** The string to deliver. */ - private final String string; - - /** The encoding for the data. */ - private final Charset encoding; - - /** - * Creates a new string bucket using the default encoding. - * - * @param string - * The string to wrap - */ - public StringBucket(String string) { - this(string, Charset.defaultCharset()); - } - - /** - * Creates a new string bucket, using the given encoding to create a byte - * array from the string. - * - * @param string - * The string to wrap - * @param encoding - * The encoding of the data - */ - public StringBucket(String string, Charset encoding) { - this.string = string; - this.encoding = encoding; - } - - /** - * {@inheritDoc} - */ - @Override - public Bucket createShadow() { - return new StringBucket(string); - } - - /** - * {@inheritDoc} - */ - @Override - public void free() { - /* ignore. */ - } - - /** - * {@inheritDoc} - */ - @Override - public InputStream getInputStream() { - return new ByteArrayInputStream(string.getBytes(encoding)); - } - - /** - * {@inheritDoc} - */ - @Override - public String getName() { - return getClass().getName() + "@" + hashCode(); - } - - /** - * {@inheritDoc} - */ - @Override - public OutputStream getOutputStream() { - return null; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isReadOnly() { - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public void removeFrom(ObjectContainer objectContainer) { - /* ignore. */ - } - - /** - * {@inheritDoc} - */ - @Override - public void setReadOnly() { - /* ignore, it is already read-only. */ - } - - /** - * {@inheritDoc} - */ - @Override - public long size() { - return string.getBytes(encoding).length; - } - - /** - * {@inheritDoc} - */ - @Override - public void storeTo(ObjectContainer objectContainer) { - /* ignore. */ - } - -} diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index 42eeb45..1bb7b5b 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -56,7 +56,7 @@ import com.google.inject.spi.InjectionListener; import com.google.inject.spi.TypeEncounter; import com.google.inject.spi.TypeListener; -import freenet.client.async.DatabaseDisabledException; +import freenet.client.async.PersistenceDisabledException; import freenet.l10n.BaseL10n.LANGUAGE; import freenet.l10n.PluginL10n; import freenet.node.Node; @@ -189,7 +189,7 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr try { oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator)); logger.log(Level.INFO, "Plugin store loaded."); - } catch (DatabaseDisabledException dde1) { + } catch (PersistenceDisabledException pde1) { logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!"); oldConfiguration = new Configuration(new MapConfigurationBackend()); } -- 2.7.4