2 * Sone - SoneDownloader.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.core;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.HashSet;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.util.io.Closer;
29 import net.pterodactylus.util.logging.Logging;
30 import net.pterodactylus.util.service.AbstractService;
31 import net.pterodactylus.util.xml.SimpleXML;
32 import net.pterodactylus.util.xml.XML;
34 import org.w3c.dom.Document;
36 import freenet.client.FetchResult;
37 import freenet.support.api.Bucket;
40 * The Sone downloader is responsible for download Sones as they are updated.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class SoneDownloader extends AbstractService {
47 private static final Logger logger = Logging.getLogger(SoneDownloader.class);
49 /** The Freenet interface. */
50 private final FreenetInterface freenetInterface;
52 /** The sones to update. */
53 private final Set<Sone> sones = new HashSet<Sone>();
56 * Creates a new Sone downloader.
58 * @param freenetInterface
59 * The Freenet interface
61 public SoneDownloader(FreenetInterface freenetInterface) {
62 super("Sone Downloader");
63 this.freenetInterface = freenetInterface;
71 * Adds the given Sone to the set of Sones that will be watched for updates.
76 public void addSone(Sone sone) {
77 if (sones.add(sone)) {
78 freenetInterface.registerUsk(sone, this);
83 * Fetches the updated Sone. This method is a callback method for
84 * {@link FreenetInterface#registerUsk(Sone, SoneDownloader)}.
89 public void fetchSone(Sone sone) {
90 logger.log(Level.FINE, "Starting fetch for Sone “%s” from %s…", new Object[] { sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" }) });
91 FetchResult fetchResult = freenetInterface.fetchUri(sone.getRequestUri().setMetaString(new String[] { "sone.xml" }));
92 logger.log(Level.FINEST, "Got %d bytes back.", fetchResult.size());
93 updateSoneFromXml(sone, fetchResult);
104 protected void serviceStop() {
105 for (Sone sone : sones) {
106 freenetInterface.unregisterUsk(sone);
115 * Updates the contents of the given Sone from the given fetch result.
122 private void updateSoneFromXml(Sone sone, FetchResult fetchResult) {
123 logger.log(Level.FINEST, "Persing FetchResult (%d bytes, %s) for %s…", new Object[] { fetchResult.size(), fetchResult.getMimeType(), sone });
124 /* TODO - impose a size limit? */
125 InputStream xmlInputStream = null;
126 Bucket xmlBucket = null;
128 xmlBucket = fetchResult.asBucket();
129 xmlInputStream = xmlBucket.getInputStream();
130 Document document = XML.transformToDocument(xmlInputStream);
131 SimpleXML soneXml = SimpleXML.fromDocument(document);
134 String soneId = soneXml.getValue("id", null);
135 if (!sone.getId().equals(soneId)) {
136 /* TODO - mark Sone as bad. */
137 logger.log(Level.WARNING, "Downloaded ID for Sone %s (%s) does not match known ID (%s)!", new Object[] { sone, sone.getId(), soneId });
141 String soneName = soneXml.getValue("name", null);
142 if (soneName == null) {
143 /* TODO - mark Sone as bad. */
144 logger.log(Level.WARNING, "Downloaded name for Sone %s was null!", new Object[] { sone });
148 SimpleXML profileXml = soneXml.getNode("profile");
149 if (profileXml == null) {
150 /* TODO - mark Sone as bad. */
151 logger.log(Level.WARNING, "Downloaded Sone %s has no profile!", new Object[] { sone });
157 } catch (IOException ioe1) {
158 logger.log(Level.WARNING, "Could not read XML file from " + sone + "!", ioe1);
160 if (xmlBucket != null) {
163 Closer.close(xmlInputStream);