🚧 Don’t cancel on archive manifests
[Sone.git] / src / main / java / net / pterodactylus / sone / core / FreenetInterface.java
1 /*
2  * Sone - FreenetInterface.java - Copyright Â© 2010–2020 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import static freenet.keys.USK.create;
21 import static java.lang.String.format;
22 import static java.util.logging.Level.WARNING;
23 import static java.util.logging.Logger.getLogger;
24
25 import java.io.IOException;
26 import java.net.MalformedURLException;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33
34 import javax.annotation.Nonnull;
35 import javax.inject.Inject;
36
37 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
38 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
39 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
40 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
41 import net.pterodactylus.sone.data.Image;
42 import net.pterodactylus.sone.data.Sone;
43 import net.pterodactylus.sone.data.TemporaryImage;
44
45 import com.google.common.base.Function;
46 import com.google.common.collect.ArrayListMultimap;
47 import com.google.common.collect.Multimap;
48 import com.google.common.eventbus.EventBus;
49 import com.google.inject.Singleton;
50
51 import freenet.client.ClientMetadata;
52 import freenet.client.FetchContext;
53 import freenet.client.FetchException;
54 import freenet.client.FetchException.FetchExceptionMode;
55 import freenet.client.FetchResult;
56 import freenet.client.HighLevelSimpleClient;
57 import freenet.client.InsertBlock;
58 import freenet.client.InsertContext;
59 import freenet.client.InsertException;
60 import freenet.client.Metadata;
61 import freenet.client.async.BaseClientPutter;
62 import freenet.client.async.ClientContext;
63 import freenet.client.async.ClientGetCallback;
64 import freenet.client.async.ClientGetter;
65 import freenet.client.async.ClientPutCallback;
66 import freenet.client.async.ClientPutter;
67 import freenet.client.async.SnoopMetadata;
68 import freenet.client.async.USKCallback;
69 import freenet.client.async.USKManager;
70 import freenet.keys.FreenetURI;
71 import freenet.keys.InsertableClientSSK;
72 import freenet.keys.USK;
73 import freenet.node.Node;
74 import freenet.node.RequestClient;
75 import freenet.node.RequestClientBuilder;
76 import freenet.node.RequestStarter;
77 import freenet.support.api.Bucket;
78 import freenet.support.api.RandomAccessBucket;
79 import freenet.support.io.ArrayBucket;
80 import freenet.support.io.ResumeFailedException;
81 import net.pterodactylus.sone.freenet.*;
82
83 /**
84  * Contains all necessary functionality for interacting with the Freenet node.
85  */
86 @Singleton
87 public class FreenetInterface {
88
89         /** The logger. */
90         private static final Logger logger = getLogger(FreenetInterface.class.getName());
91
92         /** The event bus. */
93         private final EventBus eventBus;
94
95         /** The node to interact with. */
96         private final Node node;
97         private final USKManager uskManager;
98         private final ClientContext clientContext;
99
100         private final SoneUriCreator soneUriCreator;
101
102         /** The high-level client to use for requests. */
103         private final HighLevelSimpleClient client;
104         private final RequestClient requestClient = new RequestClientBuilder().realTime().build();
105
106         /** The USK callbacks. */
107         private final Multimap<String, USKCallback> soneUskCallbacks = ArrayListMultimap.create();
108
109         /** The not-Sone-related USK callbacks. */
110         private final Map<FreenetURI, USKCallback> uriUskCallbacks = Collections.synchronizedMap(new HashMap<FreenetURI, USKCallback>());
111
112         private final RequestClient imageInserts = new RequestClientBuilder().realTime().build();
113         private final RequestClient imageLoader = new RequestClientBuilder().realTime().build();
114
115         @Inject
116         public FreenetInterface(EventBus eventBus, Node node, USKManager uskManager, ClientContext clientContext, SoneUriCreator soneUriCreator, HighLevelSimpleClientCreator highLevelSimpleClientCreator) {
117                 this.eventBus = eventBus;
118                 this.node = node;
119                 this.uskManager = uskManager;
120                 this.clientContext = clientContext;
121                 this.soneUriCreator = soneUriCreator;
122                 this.client = highLevelSimpleClientCreator.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
123         }
124
125         //
126         // ACTIONS
127         //
128
129         /**
130          * Fetches the given URI.
131          *
132          * @param uri
133          *            The URI to fetch
134          * @return The result of the fetch, or {@code null} if an error occured
135          */
136         public Fetched fetchUri(FreenetURI uri) {
137                 FreenetURI currentUri = new FreenetURI(uri);
138                 while (true) {
139                         try {
140                                 FetchResult fetchResult = client.fetch(currentUri);
141                                 return new Fetched(currentUri, fetchResult);
142                         } catch (FetchException fe1) {
143                                 if (fe1.getMode() == FetchExceptionMode.PERMANENT_REDIRECT) {
144                                         currentUri = fe1.newURI;
145                                         continue;
146                                 }
147                                 logger.log(Level.WARNING, String.format("Could not fetch â€ś%s”!", uri), fe1);
148                                 return null;
149                         }
150                 }
151         }
152
153         public void startFetch(final FreenetURI uri, final BackgroundFetchCallback backgroundFetchCallback) {
154                 ClientGetCallback callback = new ClientGetCallback() {
155                         @Override
156                         public void onSuccess(FetchResult result, ClientGetter state) {
157                                 try {
158                                         backgroundFetchCallback.loaded(uri, result.getMimeType(), result.asByteArray());
159                                 } catch (IOException e) {
160                                         backgroundFetchCallback.failed(uri);
161                                 }
162                         }
163
164                         @Override
165                         public void onFailure(FetchException e, ClientGetter state) {
166                                 backgroundFetchCallback.failed(uri);
167                         }
168
169                         @Override
170                         public void onResume(ClientContext context) throws ResumeFailedException {
171                                 /* do nothing. */
172                         }
173
174                         @Override
175                         public RequestClient getRequestClient() {
176                                 return imageLoader;
177                         }
178                 };
179                 SnoopMetadata snoop = new SnoopMetadata() {
180                         @Override
181                         public boolean snoopMetadata(Metadata meta, ClientContext context) {
182                                 if (meta.isArchiveManifest()) {
183                                         return false;
184                                 }
185                                 String mimeType = meta.getMIMEType();
186                                 boolean cancel = (mimeType == null) || backgroundFetchCallback.shouldCancel(uri, mimeType, meta.dataLength());
187                                 if (cancel) {
188                                         backgroundFetchCallback.failed(uri);
189                                 }
190                                 return cancel;
191                         }
192                 };
193                 FetchContext fetchContext = client.getFetchContext();
194                 fetchContext.dontEnterImplicitArchives = false;
195                 try {
196                         ClientGetter clientGetter = client.fetch(uri, 2097152, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
197                         clientGetter.setMetaSnoop(snoop);
198                         clientGetter.restart(uri, fetchContext.filterData, clientContext);
199                 } catch (FetchException fe) {
200                         /* stupid exception that can not actually be thrown! */
201                 }
202         }
203
204         public interface BackgroundFetchCallback {
205                 boolean shouldCancel(@Nonnull FreenetURI uri, @Nonnull String mimeType, long size);
206                 void loaded(@Nonnull FreenetURI uri, @Nonnull String mimeType, @Nonnull byte[] data);
207                 void failed(@Nonnull FreenetURI uri);
208         }
209
210         /**
211          * Inserts the image data of the given {@link TemporaryImage} and returns
212          * the given insert token that can be used to add listeners or cancel the
213          * insert.
214          *
215          * @param temporaryImage
216          *            The temporary image data
217          * @param image
218          *            The image
219          * @param insertToken
220          *            The insert token
221          * @throws SoneException
222          *             if the insert could not be started
223          */
224         public void insertImage(TemporaryImage temporaryImage, Image image, InsertToken insertToken) throws SoneException {
225                 String filenameHint = image.getId() + "." + temporaryImage.getMimeType().substring(temporaryImage.getMimeType().lastIndexOf("/") + 1);
226                 InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, "");
227                 FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint);
228                 InsertContext insertContext = client.getInsertContext(true);
229                 RandomAccessBucket bucket = new ArrayBucket(temporaryImage.getImageData());
230                 insertToken.setBucket(bucket);
231                 ClientMetadata metadata = new ClientMetadata(temporaryImage.getMimeType());
232                 InsertBlock insertBlock = new InsertBlock(bucket, metadata, targetUri);
233                 try {
234                         ClientPutter clientPutter = client.insert(insertBlock, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
235                         insertToken.setClientPutter(clientPutter);
236                 } catch (InsertException ie1) {
237                         throw new SoneInsertException("Could not start image insert.", ie1);
238                 }
239         }
240
241         /**
242          * Inserts a directory into Freenet.
243          *
244          * @param insertUri
245          *            The insert URI
246          * @param manifestEntries
247          *            The directory entries
248          * @param defaultFile
249          *            The name of the default file
250          * @return The generated URI
251          * @throws SoneException
252          *             if an insert error occurs
253          */
254         public FreenetURI insertDirectory(FreenetURI insertUri, HashMap<String, Object> manifestEntries, String defaultFile) throws SoneException {
255                 try {
256                         return client.insertManifest(insertUri, manifestEntries, defaultFile);
257                 } catch (InsertException ie1) {
258                         throw new SoneException(ie1);
259                 }
260         }
261
262         public void registerActiveUsk(FreenetURI requestUri,
263                         USKCallback uskCallback) {
264                 try {
265                         soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback);
266                         uskManager.subscribe(create(requestUri),
267                                         uskCallback, true, requestClient);
268                 } catch (MalformedURLException mue1) {
269                         logger.log(WARNING, format("Could not subscribe USK â€ś%s”!",
270                                         requestUri), mue1);
271                 }
272         }
273
274         public void registerPassiveUsk(FreenetURI requestUri,
275                         USKCallback uskCallback) {
276                 try {
277                         soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback);
278                         uskManager.subscribe(create(requestUri), uskCallback, false, requestClient);
279                 } catch (MalformedURLException mue1) {
280                         logger.log(WARNING,
281                                         format("Could not subscribe USK â€ś%s”!", requestUri),
282                                         mue1);
283                 }
284         }
285
286         /**
287          * Unsubscribes the request URI of the given Sone.
288          *
289          * @param sone
290          *            The Sone to unregister
291          */
292         public void unregisterUsk(Sone sone) {
293                 Collection<USKCallback> uskCallbacks = soneUskCallbacks.removeAll(sone.getId());
294                 if (uskCallbacks.isEmpty()) {
295                         return;
296                 }
297                 logger.log(Level.FINE, String.format("Unsubscribing %d from USK for %s…", uskCallbacks.size(), sone));
298                 logger.log(Level.FINEST, String.format("USKs left: %d", soneUskCallbacks.size()));
299                 uskCallbacks.forEach(uskCallback -> {
300                         try {
301                                 uskManager.unsubscribe(USK.create(soneUriCreator.getRequestUri(sone)), uskCallback);
302                         } catch (MalformedURLException mue1) {
303                                 logger.log(Level.FINE, String.format("Could not unsubscribe USK â€ś%s”!", soneUriCreator.getRequestUri(sone)), mue1);
304                         }
305                 });
306         }
307
308         /**
309          * Registers an arbitrary URI and calls the given callback if a new edition
310          * is found.
311          *
312          * @param uri
313          *            The URI to watch
314          * @param callback
315          *            The callback to call
316          */
317         public void registerUsk(FreenetURI uri, final Callback callback) {
318                 USKCallback uskCallback = new USKCallback() {
319
320                         @Override
321                         public void onFoundEdition(long edition, USK key, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
322                                 callback.editionFound(key.getURI(), edition, newKnownGood, newSlotToo);
323                         }
324
325                         @Override
326                         public short getPollingPriorityNormal() {
327                                 return RequestStarter.PREFETCH_PRIORITY_CLASS;
328                         }
329
330                         @Override
331                         public short getPollingPriorityProgress() {
332                                 return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
333                         }
334
335                 };
336                 try {
337                         uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient);
338                         uriUskCallbacks.put(USK.create(uri).clearCopy().getURI(), uskCallback);
339                 } catch (MalformedURLException mue1) {
340                         logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
341                 }
342         }
343
344         /**
345          * Unregisters the USK watcher for the given URI.
346          *
347          * @param uri
348          *            The URI to unregister the USK watcher for
349          */
350         public void unregisterUsk(FreenetURI uri) {
351                 try {
352                         USKCallback uskCallback = uriUskCallbacks.remove(USK.create(uri).clearCopy().getURI());
353                         if (uskCallback == null) {
354                                 logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri));
355                                 return;
356                         }
357                         uskManager.unsubscribe(USK.create(uri), uskCallback);
358                 } catch (MalformedURLException mue1) {
359                         logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1);
360                 }
361         }
362
363         /**
364          * Callback for USK watcher events.
365          */
366         public static interface Callback {
367
368                 /**
369                  * Notifies a listener that a new edition was found for a URI.
370                  *
371                  * @param uri
372                  *            The URI that a new edition was found for
373                  * @param edition
374                  *            The found edition
375                  * @param newKnownGood
376                  *            Whether the found edition was actually fetched
377                  * @param newSlot
378                  *            Whether the found edition is higher than all previously
379                  *            found editions
380                  */
381                 public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot);
382
383         }
384
385         /**
386          * Insert token that can cancel a running insert and sends events.
387          *
388          * @see ImageInsertAbortedEvent
389          * @see ImageInsertStartedEvent
390          * @see ImageInsertFailedEvent
391          * @see ImageInsertFinishedEvent
392          */
393         public class InsertToken implements ClientPutCallback {
394
395                 /** The image being inserted. */
396                 private final Image image;
397
398                 /** The client putter. */
399                 private ClientPutter clientPutter;
400                 private Bucket bucket;
401
402                 /** The final URI. */
403                 private volatile FreenetURI resultingUri;
404
405                 /**
406                  * Creates a new insert token for the given image.
407                  *
408                  * @param image
409                  *            The image being inserted
410                  */
411                 public InsertToken(Image image) {
412                         this.image = image;
413                 }
414
415                 //
416                 // ACCESSORS
417                 //
418
419                 /**
420                  * Sets the client putter that is inserting the image. This will also
421                  * signal all registered listeners that the image has started.
422                  *
423                  * @param clientPutter
424                  *            The client putter
425                  */
426                 @SuppressWarnings("synthetic-access")
427                 public void setClientPutter(ClientPutter clientPutter) {
428                         this.clientPutter = clientPutter;
429                         eventBus.post(new ImageInsertStartedEvent(image));
430                 }
431
432                 public void setBucket(Bucket bucket) {
433                         this.bucket = bucket;
434                 }
435
436                 //
437                 // ACTIONS
438                 //
439
440                 /**
441                  * Cancels the running insert.
442                  */
443                 @SuppressWarnings("synthetic-access")
444                 public void cancel() {
445                         clientPutter.cancel(clientContext);
446                         eventBus.post(new ImageInsertAbortedEvent(image));
447                         bucket.free();
448                 }
449
450                 //
451                 // INTERFACE ClientPutCallback
452                 //
453
454                 @Override
455                 public RequestClient getRequestClient() {
456                         return imageInserts;
457                 }
458
459                 @Override
460                 public void onResume(ClientContext context) throws ResumeFailedException {
461                         /* ignore. */
462                 }
463
464                 /**
465                  * {@inheritDoc}
466                  */
467                 @Override
468                 @SuppressWarnings("synthetic-access")
469                 public void onFailure(InsertException insertException, BaseClientPutter clientPutter) {
470                         if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
471                                 eventBus.post(new ImageInsertAbortedEvent(image));
472                         } else {
473                                 eventBus.post(new ImageInsertFailedEvent(image, insertException));
474                         }
475                         bucket.free();
476                 }
477
478                 /**
479                  * {@inheritDoc}
480                  */
481                 @Override
482                 public void onFetchable(BaseClientPutter clientPutter) {
483                         /* ignore, we don’t care. */
484                 }
485
486                 /**
487                  * {@inheritDoc}
488                  */
489                 @Override
490                 public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter) {
491                         /* ignore, we don’t care. */
492                 }
493
494                 /**
495                  * {@inheritDoc}
496                  */
497                 @Override
498                 public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter) {
499                         resultingUri = generatedUri;
500                 }
501
502                 /**
503                  * {@inheritDoc}
504                  */
505                 @Override
506                 @SuppressWarnings("synthetic-access")
507                 public void onSuccess(BaseClientPutter clientPutter) {
508                         eventBus.post(new ImageInsertFinishedEvent(image, resultingUri));
509                         bucket.free();
510                 }
511
512         }
513
514         public static class InsertTokenSupplier implements Function<Image, InsertToken> {
515
516                 private final FreenetInterface freenetInterface;
517
518                 @Inject
519                 public InsertTokenSupplier(FreenetInterface freenetInterface) {
520                         this.freenetInterface = freenetInterface;
521                 }
522
523                 @Override
524                 public InsertToken apply(Image image) {
525                         return freenetInterface.new InsertToken(image);
526                 }
527
528         }
529
530 }