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