+ public Fetched fetchUri(FreenetURI uri) {
+ FreenetURI currentUri = new FreenetURI(uri);
+ while (true) {
+ try {
+ FetchResult fetchResult = client.fetch(currentUri);
+ return new Fetched(currentUri, fetchResult);
+ } catch (FetchException fe1) {
+ if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) {
+ currentUri = fe1.newURI;
+ continue;
+ }
+ logger.log(Level.WARNING, String.format("Could not fetch “%s”!", uri), fe1);
+ return null;
+ }
+ }
+ }
+
+ /**
+ * Inserts the image data of the given {@link TemporaryImage} and returns
+ * the given insert token that can be used to add listeners or cancel the
+ * insert.
+ *
+ * @param temporaryImage
+ * The temporary image data
+ * @param image
+ * The image
+ * @param insertToken
+ * The insert token
+ * @throws SoneException
+ * if the insert could not be started
+ */
+ public void insertImage(TemporaryImage temporaryImage, Image image, InsertToken insertToken) throws SoneException {
+ String filenameHint = image.getId() + "." + temporaryImage.getMimeType().substring(temporaryImage.getMimeType().lastIndexOf("/") + 1);
+ InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, "");
+ FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint);
+ InsertContext insertContext = client.getInsertContext(true);
+ Bucket 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);
+ insertToken.setClientPutter(clientPutter);
+ } catch (InsertException ie1) {
+ throw new SoneInsertException("Could not start image insert.", ie1);
+ }
+ }
+
+ /**
+ * Inserts a directory into Freenet.
+ *
+ * @param insertUri
+ * The insert URI
+ * @param manifestEntries
+ * The directory entries
+ * @param defaultFile
+ * The name of the default file
+ * @return The generated URI
+ * @throws SoneException
+ * if an insert error occurs
+ */
+ public FreenetURI insertDirectory(FreenetURI insertUri, HashMap<String, Object> manifestEntries, String defaultFile) throws SoneException {
+ try {
+ return client.insertManifest(insertUri, manifestEntries, defaultFile);
+ } catch (InsertException ie1) {
+ throw new SoneException(ie1);
+ }
+ }
+
+ public void registerActiveUsk(FreenetURI requestUri,
+ USKCallback uskCallback) {
+ try {
+ soneUskCallbacks.put(routingKey(requestUri), uskCallback);
+ node.clientCore.uskManager.subscribe(create(requestUri),
+ uskCallback, true, (RequestClient) client);
+ } catch (MalformedURLException mue1) {
+ logger.log(WARNING, format("Could not subscribe USK “%s”!",
+ requestUri), mue1);
+ }
+ }
+
+ public void registerPassiveUsk(FreenetURI requestUri,
+ USKCallback uskCallback) {
+ try {
+ soneUskCallbacks.put(routingKey(requestUri), uskCallback);
+ node.clientCore
+ .uskManager
+ .subscribe(create(requestUri), uskCallback, false,
+ (RequestClient) client);
+ } catch (MalformedURLException mue1) {
+ logger.log(WARNING,
+ format("Could not subscribe USK “%s”!", requestUri),
+ mue1);
+ }
+ }
+
+ /**
+ * Unsubscribes the request URI of the given Sone.
+ *
+ * @param sone
+ * The Sone to unregister
+ */
+ public void unregisterUsk(Sone sone) {
+ USKCallback uskCallback = soneUskCallbacks.remove(sone.getId());
+ if (uskCallback == null) {
+ return;
+ }
+ try {
+ logger.log(Level.FINEST, String.format("Unsubscribing from USK for %s…", sone));
+ node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.FINE, String.format("Could not unsubscribe USK “%s”!", sone.getRequestUri()), mue1);
+ }
+ }
+
+ /**
+ * Registers an arbitrary URI and calls the given callback if a new edition
+ * is found.
+ *
+ * @param uri
+ * The URI to watch
+ * @param callback
+ * The callback to call
+ */
+ public void registerUsk(FreenetURI uri, final Callback callback) {
+ 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) {
+ callback.editionFound(key.getURI(), edition, newKnownGood, newSlotToo);
+ }
+
+ @Override
+ public short getPollingPriorityNormal() {
+ return RequestStarter.PREFETCH_PRIORITY_CLASS;
+ }
+
+ @Override
+ public short getPollingPriorityProgress() {
+ return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
+ }
+
+ };
+ try {
+ node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (RequestClient) client);
+ uriUskCallbacks.put(uri, uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
+ }
+ }
+
+ /**
+ * Unregisters the USK watcher for the given URI.
+ *
+ * @param uri
+ * The URI to unregister the USK watcher for
+ */
+ public void unregisterUsk(FreenetURI uri) {
+ USKCallback uskCallback = uriUskCallbacks.remove(uri);
+ if (uskCallback == null) {
+ logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri));
+ return;
+ }