Redirect to index if no phrases have been found.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloader.java
1 /*
2  * Sone - SoneDownloader.java - Copyright © 2010 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 java.io.IOException;
21 import java.io.InputStream;
22 import java.net.MalformedURLException;
23 import java.util.HashSet;
24 import java.util.Set;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import net.pterodactylus.sone.core.Core.SoneStatus;
29 import net.pterodactylus.sone.data.Client;
30 import net.pterodactylus.sone.data.Post;
31 import net.pterodactylus.sone.data.Profile;
32 import net.pterodactylus.sone.data.Reply;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.util.collection.Pair;
35 import net.pterodactylus.util.io.Closer;
36 import net.pterodactylus.util.logging.Logging;
37 import net.pterodactylus.util.number.Numbers;
38 import net.pterodactylus.util.service.AbstractService;
39 import net.pterodactylus.util.xml.SimpleXML;
40 import net.pterodactylus.util.xml.XML;
41
42 import org.w3c.dom.Document;
43
44 import freenet.client.FetchResult;
45 import freenet.keys.FreenetURI;
46 import freenet.support.api.Bucket;
47
48 /**
49  * The Sone downloader is responsible for download Sones as they are updated.
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class SoneDownloader extends AbstractService {
54
55         /** The logger. */
56         private static final Logger logger = Logging.getLogger(SoneDownloader.class);
57
58         /** The maximum protocol version. */
59         private static final int MAX_PROTOCOL_VERSION = 0;
60
61         /** The core. */
62         private final Core core;
63
64         /** The Freenet interface. */
65         private final FreenetInterface freenetInterface;
66
67         /** The sones to update. */
68         private final Set<Sone> sones = new HashSet<Sone>();
69
70         /**
71          * Creates a new Sone downloader.
72          *
73          * @param core
74          *            The core
75          * @param freenetInterface
76          *            The Freenet interface
77          */
78         public SoneDownloader(Core core, FreenetInterface freenetInterface) {
79                 super("Sone Downloader", false);
80                 this.core = core;
81                 this.freenetInterface = freenetInterface;
82         }
83
84         //
85         // ACTIONS
86         //
87
88         /**
89          * Adds the given Sone to the set of Sones that will be watched for updates.
90          *
91          * @param sone
92          *            The Sone to add
93          */
94         public void addSone(Sone sone) {
95                 if (!sones.add(sone)) {
96                         freenetInterface.unregisterUsk(sone);
97                 }
98                 freenetInterface.registerUsk(sone, this);
99         }
100
101         /**
102          * Removes the given Sone from the downloader.
103          *
104          * @param sone
105          *            The Sone to stop watching
106          */
107         public void removeSone(Sone sone) {
108                 if (sones.remove(sone)) {
109                         freenetInterface.unregisterUsk(sone);
110                 }
111         }
112
113         /**
114          * Fetches the updated Sone. This method is a callback method for
115          * {@link FreenetInterface#registerUsk(Sone, SoneDownloader)}.
116          *
117          * @param sone
118          *            The Sone to fetch
119          */
120         public void fetchSone(Sone sone) {
121                 fetchSone(sone, sone.getRequestUri().sskForUSK());
122         }
123
124         /**
125          * Fetches the updated Sone. This method can be used to fetch a Sone from a
126          * specific URI.
127          *
128          * @param sone
129          *            The Sone to fetch
130          * @param soneUri
131          *            The URI to fetch the Sone from
132          */
133         public void fetchSone(Sone sone, FreenetURI soneUri) {
134                 fetchSone(sone, soneUri, false);
135         }
136
137         /**
138          * Fetches the Sone from the given URI.
139          *
140          * @param sone
141          *            The Sone to fetch
142          * @param soneUri
143          *            The URI of the Sone to fetch
144          * @param fetchOnly
145          *            {@code true} to only fetch and parse the Sone, {@code false}
146          *            to {@link Core#updateSone(Sone) update} it in the core
147          * @return The downloaded Sone, or {@code null} if the Sone could not be
148          *         downloaded
149          */
150         public Sone fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly) {
151                 logger.log(Level.FINE, "Starting fetch for Sone “%s” from %s…", new Object[] { sone, soneUri });
152                 FreenetURI requestUri = soneUri.setMetaString(new String[] { "sone.xml" });
153                 core.setSoneStatus(sone, SoneStatus.downloading);
154                 try {
155                         Pair<FreenetURI, FetchResult> fetchResults = freenetInterface.fetchUri(requestUri);
156                         if (fetchResults == null) {
157                                 /* TODO - mark Sone as bad. */
158                                 return null;
159                         }
160                         logger.log(Level.FINEST, "Got %d bytes back.", fetchResults.getRight().size());
161                         Sone parsedSone = parseSone(sone, fetchResults.getRight(), fetchResults.getLeft());
162                         if (parsedSone != null) {
163                                 if (!fetchOnly) {
164                                         core.updateSone(parsedSone);
165                                         addSone(parsedSone);
166                                 }
167                         }
168                         return parsedSone;
169                 } finally {
170                         core.setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
171                 }
172         }
173
174         /**
175          * Parses a Sone from a fetch result.
176          *
177          * @param originalSone
178          *            The sone to parse, or {@code null} if the Sone is yet unknown
179          * @param fetchResult
180          *            The fetch result
181          * @param requestUri
182          *            The requested URI
183          * @return The parsed Sone, or {@code null} if the Sone could not be parsed
184          */
185         public Sone parseSone(Sone originalSone, FetchResult fetchResult, FreenetURI requestUri) {
186                 logger.log(Level.FINEST, "Parsing FetchResult (%d bytes, %s) for %s…", new Object[] { fetchResult.size(), fetchResult.getMimeType(), originalSone });
187                 Bucket soneBucket = fetchResult.asBucket();
188                 InputStream soneInputStream = null;
189                 try {
190                         soneInputStream = soneBucket.getInputStream();
191                         Sone parsedSone = parseSone(originalSone, soneInputStream);
192                         if (parsedSone != null) {
193                                 parsedSone.setLatestEdition(requestUri.getEdition());
194                                 if (requestUri.getKeyType().equals("USK")) {
195                                         parsedSone.setRequestUri(requestUri.setMetaString(new String[0]));
196                                 } else {
197                                         parsedSone.setRequestUri(requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]));
198                                 }
199                         }
200                         return parsedSone;
201                 } catch (IOException ioe1) {
202                         logger.log(Level.WARNING, "Could not parse Sone from " + requestUri + "!", ioe1);
203                 } finally {
204                         Closer.close(soneInputStream);
205                         soneBucket.free();
206                 }
207                 return null;
208         }
209
210         /**
211          * Parses a Sone from the given input stream and creates a new Sone from the
212          * parsed data.
213          *
214          * @param originalSone
215          *            The Sone to update
216          * @param soneInputStream
217          *            The input stream to parse the Sone from
218          * @return The parsed Sone
219          */
220         public Sone parseSone(Sone originalSone, InputStream soneInputStream) {
221                 /* TODO - impose a size limit? */
222
223                 Document document;
224                 /* XML parsing is not thread-safe. */
225                 synchronized (this) {
226                         document = XML.transformToDocument(soneInputStream);
227                 }
228                 if (document == null) {
229                         /* TODO - mark Sone as bad. */
230                         logger.log(Level.WARNING, "Could not parse XML for Sone %s!", new Object[] { originalSone });
231                         return null;
232                 }
233
234                 Sone sone = new Sone(originalSone.getId()).setIdentity(originalSone.getIdentity());
235
236                 SimpleXML soneXml;
237                 try {
238                         soneXml = SimpleXML.fromDocument(document);
239                 } catch (NullPointerException npe1) {
240                         /* for some reason, invalid XML can cause NPEs. */
241                         logger.log(Level.WARNING, "XML for Sone " + sone + " can not be parsed!", npe1);
242                         return null;
243                 }
244
245                 Integer protocolVersion = null;
246                 String soneProtocolVersion = soneXml.getValue("protocol-version", null);
247                 if (soneProtocolVersion != null) {
248                         protocolVersion = Numbers.safeParseInteger(soneProtocolVersion);
249                 }
250                 if (protocolVersion == null) {
251                         logger.log(Level.INFO, "No protocol version found, assuming 0.");
252                         protocolVersion = 0;
253                 }
254
255                 if (protocolVersion < 0) {
256                         logger.log(Level.WARNING, "Invalid protocol version: " + protocolVersion + "! Not parsing Sone.");
257                         return null;
258                 }
259
260                 /* check for valid versions. */
261                 if (protocolVersion > MAX_PROTOCOL_VERSION) {
262                         logger.log(Level.WARNING, "Unknown protocol version: " + protocolVersion + "! Not parsing Sone.");
263                         return null;
264                 }
265
266                 String soneTime = soneXml.getValue("time", null);
267                 if (soneTime == null) {
268                         /* TODO - mark Sone as bad. */
269                         logger.log(Level.WARNING, "Downloaded time for Sone %s was null!", new Object[] { sone });
270                         return null;
271                 }
272                 try {
273                         sone.setTime(Long.parseLong(soneTime));
274                 } catch (NumberFormatException nfe1) {
275                         /* TODO - mark Sone as bad. */
276                         logger.log(Level.WARNING, "Downloaded Sone %s with invalid time: %s", new Object[] { sone, soneTime });
277                         return null;
278                 }
279
280                 SimpleXML clientXml = soneXml.getNode("client");
281                 if (clientXml != null) {
282                         String clientName = clientXml.getValue("name", null);
283                         String clientVersion = clientXml.getValue("version", null);
284                         if ((clientName == null) || (clientVersion == null)) {
285                                 logger.log(Level.WARNING, "Download Sone %s with client XML but missing name or version!", sone);
286                                 return null;
287                         }
288                         sone.setClient(new Client(clientName, clientVersion));
289                 }
290
291                 String soneRequestUri = soneXml.getValue("request-uri", null);
292                 if (soneRequestUri != null) {
293                         try {
294                                 sone.setRequestUri(new FreenetURI(soneRequestUri));
295                         } catch (MalformedURLException mue1) {
296                                 /* TODO - mark Sone as bad. */
297                                 logger.log(Level.WARNING, "Downloaded Sone " + sone + " has invalid request URI: " + soneRequestUri, mue1);
298                                 return null;
299                         }
300                 }
301
302                 String soneInsertUri = soneXml.getValue("insert-uri", null);
303                 if ((soneInsertUri != null) && (sone.getInsertUri() == null)) {
304                         try {
305                                 sone.setInsertUri(new FreenetURI(soneInsertUri));
306                                 sone.setLatestEdition(Math.max(sone.getRequestUri().getEdition(), sone.getInsertUri().getEdition()));
307                         } catch (MalformedURLException mue1) {
308                                 /* TODO - mark Sone as bad. */
309                                 logger.log(Level.WARNING, "Downloaded Sone " + sone + " has invalid insert URI: " + soneInsertUri, mue1);
310                                 return null;
311                         }
312                 }
313
314                 SimpleXML profileXml = soneXml.getNode("profile");
315                 if (profileXml == null) {
316                         /* TODO - mark Sone as bad. */
317                         logger.log(Level.WARNING, "Downloaded Sone %s has no profile!", new Object[] { sone });
318                         return null;
319                 }
320
321                 /* parse profile. */
322                 String profileFirstName = profileXml.getValue("first-name", null);
323                 String profileMiddleName = profileXml.getValue("middle-name", null);
324                 String profileLastName = profileXml.getValue("last-name", null);
325                 Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null));
326                 Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null));
327                 Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null));
328                 Profile profile = new Profile().setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName);
329                 profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear);
330
331                 /* parse profile fields. */
332                 SimpleXML profileFieldsXml = profileXml.getNode("fields");
333                 if (profileFieldsXml != null) {
334                         for (SimpleXML fieldXml : profileFieldsXml.getNodes("field")) {
335                                 String fieldName = fieldXml.getValue("field-name", null);
336                                 String fieldValue = fieldXml.getValue("field-value", null);
337                                 if ((fieldName == null) || (fieldValue == null)) {
338                                         logger.log(Level.WARNING, "Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", new Object[] { sone, fieldName, fieldValue });
339                                         return null;
340                                 }
341                                 try {
342                                         profile.addField(fieldName).setValue(fieldValue);
343                                 } catch (IllegalArgumentException iae1) {
344                                         logger.log(Level.WARNING, "Duplicate field: " + fieldName, iae1);
345                                         return null;
346                                 }
347                         }
348                 }
349
350                 /* parse posts. */
351                 SimpleXML postsXml = soneXml.getNode("posts");
352                 Set<Post> posts = new HashSet<Post>();
353                 if (postsXml == null) {
354                         /* TODO - mark Sone as bad. */
355                         logger.log(Level.WARNING, "Downloaded Sone %s has no posts!", new Object[] { sone });
356                 } else {
357                         for (SimpleXML postXml : postsXml.getNodes("post")) {
358                                 String postId = postXml.getValue("id", null);
359                                 String postRecipientId = postXml.getValue("recipient", null);
360                                 String postTime = postXml.getValue("time", null);
361                                 String postText = postXml.getValue("text", null);
362                                 if ((postId == null) || (postTime == null) || (postText == null)) {
363                                         /* TODO - mark Sone as bad. */
364                                         logger.log(Level.WARNING, "Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", new Object[] { sone, postId, postTime, postText });
365                                         return null;
366                                 }
367                                 try {
368                                         Post post = core.getPost(postId).setSone(sone).setTime(Long.parseLong(postTime)).setText(postText);
369                                         if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
370                                                 post.setRecipient(core.getSone(postRecipientId));
371                                         }
372                                         posts.add(post);
373                                 } catch (NumberFormatException nfe1) {
374                                         /* TODO - mark Sone as bad. */
375                                         logger.log(Level.WARNING, "Downloaded post for Sone %s with invalid time: %s", new Object[] { sone, postTime });
376                                         return null;
377                                 }
378                         }
379                 }
380
381                 /* parse replies. */
382                 SimpleXML repliesXml = soneXml.getNode("replies");
383                 Set<Reply> replies = new HashSet<Reply>();
384                 if (repliesXml == null) {
385                         /* TODO - mark Sone as bad. */
386                         logger.log(Level.WARNING, "Downloaded Sone %s has no replies!", new Object[] { sone });
387                 } else {
388                         for (SimpleXML replyXml : repliesXml.getNodes("reply")) {
389                                 String replyId = replyXml.getValue("id", null);
390                                 String replyPostId = replyXml.getValue("post-id", null);
391                                 String replyTime = replyXml.getValue("time", null);
392                                 String replyText = replyXml.getValue("text", null);
393                                 if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) {
394                                         /* TODO - mark Sone as bad. */
395                                         logger.log(Level.WARNING, "Downloaded reply for Sone %s with missing data! ID: %s, Post: %s, Time: %s, Text: %s", new Object[] { sone, replyId, replyPostId, replyTime, replyText });
396                                         return null;
397                                 }
398                                 try {
399                                         replies.add(core.getReply(replyId).setSone(sone).setPost(core.getPost(replyPostId)).setTime(Long.parseLong(replyTime)).setText(replyText));
400                                 } catch (NumberFormatException nfe1) {
401                                         /* TODO - mark Sone as bad. */
402                                         logger.log(Level.WARNING, "Downloaded reply for Sone %s with invalid time: %s", new Object[] { sone, replyTime });
403                                         return null;
404                                 }
405                         }
406                 }
407
408                 /* parse liked post IDs. */
409                 SimpleXML likePostIdsXml = soneXml.getNode("post-likes");
410                 Set<String> likedPostIds = new HashSet<String>();
411                 if (likePostIdsXml == null) {
412                         /* TODO - mark Sone as bad. */
413                         logger.log(Level.WARNING, "Downloaded Sone %s has no post likes!", new Object[] { sone });
414                 } else {
415                         for (SimpleXML likedPostIdXml : likePostIdsXml.getNodes("post-like")) {
416                                 String postId = likedPostIdXml.getValue();
417                                 likedPostIds.add(postId);
418                         }
419                 }
420
421                 /* parse liked reply IDs. */
422                 SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes");
423                 Set<String> likedReplyIds = new HashSet<String>();
424                 if (likeReplyIdsXml == null) {
425                         /* TODO - mark Sone as bad. */
426                         logger.log(Level.WARNING, "Downloaded Sone %s has no reply likes!", new Object[] { sone });
427                 } else {
428                         for (SimpleXML likedReplyIdXml : likeReplyIdsXml.getNodes("reply-like")) {
429                                 String replyId = likedReplyIdXml.getValue();
430                                 likedReplyIds.add(replyId);
431                         }
432                 }
433
434                 /* okay, apparently everything was parsed correctly. Now import. */
435                 /* atomic setter operation on the Sone. */
436                 synchronized (sone) {
437                         sone.setProfile(profile);
438                         sone.setPosts(posts);
439                         sone.setReplies(replies);
440                         sone.setLikePostIds(likedPostIds);
441                         sone.setLikeReplyIds(likedReplyIds);
442                 }
443
444                 return sone;
445         }
446
447         //
448         // SERVICE METHODS
449         //
450
451         /**
452          * {@inheritDoc}
453          */
454         @Override
455         protected void serviceStop() {
456                 for (Sone sone : sones) {
457                         freenetInterface.unregisterUsk(sone);
458                 }
459         }
460
461 }