Verify that an invalid image causes an error.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneParser.java
1 /*
2  * Sone - SoneParser.java - Copyright © 2010–2013 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 com.google.common.base.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Optional.of;
23
24 import java.io.InputStream;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
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.PostReply;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Sone;
38 import net.pterodactylus.sone.data.impl.DefaultSone;
39 import net.pterodactylus.sone.database.Database;
40 import net.pterodactylus.sone.database.ImageBuilder.ImageCreated;
41 import net.pterodactylus.sone.database.PostBuilder;
42 import net.pterodactylus.sone.database.PostBuilder.PostCreated;
43 import net.pterodactylus.sone.database.PostReplyBuilder;
44 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
45 import net.pterodactylus.util.number.Numbers;
46 import net.pterodactylus.util.xml.SimpleXML;
47 import net.pterodactylus.util.xml.XML;
48
49 import com.google.common.base.Optional;
50 import com.google.common.collect.Maps;
51 import com.google.common.primitives.Ints;
52 import org.w3c.dom.Document;
53
54 /**
55  * Parses the inserted XML representation of a {@link Sone} into a Sone.
56  *
57  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
58  */
59 public class SoneParser {
60
61         private static final Logger logger = Logger.getLogger(SoneParser.class.getName());
62         private static final int MAX_PROTOCOL_VERSION = 0;
63
64         /**
65          * Parses a Sone from the given input stream and creates a new Sone from the
66          * parsed data.
67          *
68          * @param originalSone
69          *              The Sone to update
70          * @param soneInputStream
71          *              The input stream to parse the Sone from
72          * @return The parsed Sone
73          */
74         public Sone parseSone(Database database, Sone originalSone, InputStream soneInputStream) {
75                 /* TODO - impose a size limit? */
76
77                 Document document;
78                 /* XML parsing is not thread-safe. */
79                 synchronized (this) {
80                         document = XML.transformToDocument(soneInputStream);
81                 }
82                 if (document == null) {
83                         /* TODO - mark Sone as bad. */
84                         logger.log(Level.WARNING, String.format("Could not parse XML for Sone %s!", originalSone.getId()));
85                         throw new InvalidXml();
86                 }
87
88                 Optional<SimpleXML> soneXml = parseXml(originalSone, document);
89                 if (!soneXml.isPresent()) {
90                         logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone.getId()));
91                         throw new InvalidXml();
92                 }
93
94                 Optional<Client> parsedClient = parseClient(originalSone, soneXml.get());
95                 Sone sone = new DefaultSone(database, originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient()));
96
97                 Optional<Integer> protocolVersion = parseProtocolVersion(soneXml.get());
98                 if (protocolVersion.isPresent()) {
99                         if (protocolVersion.get() < 0) {
100                                 logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion.get()));
101                                 throw new InvalidProtocolVersion();
102                         }
103                         if (protocolVersion.get() > MAX_PROTOCOL_VERSION) {
104                                 logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion.get()));
105                                 throw new InvalidProtocolVersion();
106                         }
107                 }
108
109                 String soneTime = soneXml.get().getValue("time", null);
110                 if (soneTime == null) {
111                         /* TODO - mark Sone as bad. */
112                         logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
113                         throw new MalformedXml();
114                 }
115                 try {
116                         sone.setTime(Long.parseLong(soneTime));
117                 } catch (NumberFormatException nfe1) {
118                         /* TODO - mark Sone as bad. */
119                         logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", sone, soneTime));
120                         throw new MalformedXml();
121                 }
122
123                 SimpleXML profileXml = soneXml.get().getNode("profile");
124                 if (profileXml == null) {
125                         /* TODO - mark Sone as bad. */
126                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone));
127                         throw new MalformedXml();
128                 }
129
130                 /* parse profile. */
131                 String profileFirstName = profileXml.getValue("first-name", null);
132                 String profileMiddleName = profileXml.getValue("middle-name", null);
133                 String profileLastName = profileXml.getValue("last-name", null);
134                 Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null));
135                 Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null));
136                 Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null));
137                 Profile profile = new Profile(sone).modify().setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName).update();
138                 profile.modify().setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear).update();
139                 /* avatar is processed after images are loaded. */
140                 String avatarId = profileXml.getValue("avatar", null);
141
142                 /* parse profile fields. */
143                 SimpleXML profileFieldsXml = profileXml.getNode("fields");
144                 if (profileFieldsXml != null) {
145                         for (SimpleXML fieldXml : profileFieldsXml.getNodes("field")) {
146                                 String fieldName = fieldXml.getValue("field-name", null);
147                                 String fieldValue = fieldXml.getValue("field-value", "");
148                                 if (fieldName == null) {
149                                         logger.log(Level.WARNING, String.format("Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", sone, fieldName, fieldValue));
150                                         throw new MalformedXml();
151                                 }
152                                 try {
153                                         profile.setField(profile.addField(fieldName), fieldValue);
154                                 } catch (IllegalArgumentException iae1) {
155                                         logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1);
156                                         throw new DuplicateField();
157                                 }
158                         }
159                 }
160
161                 /* parse posts. */
162                 SimpleXML postsXml = soneXml.get().getNode("posts");
163                 Set<Post> posts = new HashSet<Post>();
164                 if (postsXml == null) {
165                         /* TODO - mark Sone as bad. */
166                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", sone));
167                 } else {
168                         for (SimpleXML postXml : postsXml.getNodes("post")) {
169                                 String postId = postXml.getValue("id", null);
170                                 String postRecipientId = postXml.getValue("recipient", null);
171                                 String postTime = postXml.getValue("time", null);
172                                 String postText = postXml.getValue("text", null);
173                                 if ((postId == null) || (postTime == null) || (postText == null)) {
174                                         /* TODO - mark Sone as bad. */
175                                         logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", sone, postId, postTime, postText));
176                                         throw new MalformedXml();
177                                 }
178                                 try {
179                                         PostBuilder postBuilder = sone.newPostBuilder();
180                                         /* TODO - parse time correctly. */
181                                         postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText);
182                                         if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
183                                                 postBuilder.to(of(postRecipientId));
184                                         }
185                                         posts.add(postBuilder.build(Optional.<PostCreated>absent()));
186                                 } catch (NumberFormatException nfe1) {
187                                         /* TODO - mark Sone as bad. */
188                                         logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime));
189                                         throw new MalformedTime();
190                                 }
191                         }
192                 }
193
194                 /* parse replies. */
195                 SimpleXML repliesXml = soneXml.get().getNode("replies");
196                 Set<PostReply> replies = new HashSet<PostReply>();
197                 if (repliesXml == null) {
198                         /* TODO - mark Sone as bad. */
199                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", sone));
200                 } else {
201                         for (SimpleXML replyXml : repliesXml.getNodes("reply")) {
202                                 String replyId = replyXml.getValue("id", null);
203                                 String replyPostId = replyXml.getValue("post-id", null);
204                                 String replyTime = replyXml.getValue("time", null);
205                                 String replyText = replyXml.getValue("text", null);
206                                 if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) {
207                                         /* TODO - mark Sone as bad. */
208                                         logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with missing data! ID: %s, Post: %s, Time: %s, Text: %s", sone, replyId, replyPostId, replyTime, replyText));
209                                         throw new MalformedXml();
210                                 }
211                                 try {
212                                         /* TODO - parse time correctly. */
213                                         PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(replyPostId).withId(replyId).withTime(Long.parseLong(replyTime)).withText(replyText);
214                                         replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
215                                 } catch (NumberFormatException nfe1) {
216                                         /* TODO - mark Sone as bad. */
217                                         logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
218                                         throw new MalformedTime();
219                                 }
220                         }
221                 }
222
223                 /* parse liked post IDs. */
224                 SimpleXML likePostIdsXml = soneXml.get().getNode("post-likes");
225                 Set<String> likedPostIds = new HashSet<String>();
226                 if (likePostIdsXml == null) {
227                         /* TODO - mark Sone as bad. */
228                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no post likes!", sone));
229                 } else {
230                         for (SimpleXML likedPostIdXml : likePostIdsXml.getNodes("post-like")) {
231                                 String postId = likedPostIdXml.getValue();
232                                 likedPostIds.add(postId);
233                         }
234                 }
235
236                 /* parse liked reply IDs. */
237                 SimpleXML likeReplyIdsXml = soneXml.get().getNode("reply-likes");
238                 Set<String> likedReplyIds = new HashSet<String>();
239                 if (likeReplyIdsXml == null) {
240                         /* TODO - mark Sone as bad. */
241                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no reply likes!", sone));
242                 } else {
243                         for (SimpleXML likedReplyIdXml : likeReplyIdsXml.getNodes("reply-like")) {
244                                 String replyId = likedReplyIdXml.getValue();
245                                 likedReplyIds.add(replyId);
246                         }
247                 }
248
249                 /* parse albums. */
250                 SimpleXML albumsXml = soneXml.get().getNode("albums");
251                 Map<String, Album> albums = Maps.newHashMap();
252                 if (albumsXml != null) {
253                         for (SimpleXML albumXml : albumsXml.getNodes("album")) {
254                                 String id = albumXml.getValue("id", null);
255                                 String parentId = albumXml.getValue("parent", null);
256                                 String title = albumXml.getValue("title", null);
257                                 String description = albumXml.getValue("description", "");
258                                 String albumImageId = albumXml.getValue("album-image", null);
259                                 if ((id == null) || (title == null) || (description == null)) {
260                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone));
261                                         throw new MalformedXml();
262                                 }
263                                 Album parent = sone.getRootAlbum();
264                                 if (parentId != null) {
265                                         parent = albums.get(parentId);
266                                         if (parent == null) {
267                                                 logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
268                                                 throw new InvalidParentAlbum();
269                                         }
270                                 }
271                                 Album album = parent.newAlbumBuilder().withId(id).build().modify().setTitle(title).setDescription(description).update();
272                                 albums.put(album.getId(), album);
273                                 SimpleXML imagesXml = albumXml.getNode("images");
274                                 if (imagesXml != null) {
275                                         for (SimpleXML imageXml : imagesXml.getNodes("image")) {
276                                                 String imageId = imageXml.getValue("id", null);
277                                                 String imageCreationTimeString = imageXml.getValue("creation-time", null);
278                                                 String imageKey = imageXml.getValue("key", null);
279                                                 String imageTitle = imageXml.getValue("title", null);
280                                                 String imageDescription = imageXml.getValue("description", "");
281                                                 String imageWidthString = imageXml.getValue("width", null);
282                                                 String imageHeightString = imageXml.getValue("height", null);
283                                                 if ((imageId == null) || (imageCreationTimeString == null) || (imageKey == null) || (imageTitle == null) || (imageWidthString == null) || (imageHeightString == null)) {
284                                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone));
285                                                         throw new MalformedXml();
286                                                 }
287                                                 long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L);
288                                                 int imageWidth = Numbers.safeParseInteger(imageWidthString, 0);
289                                                 int imageHeight = Numbers.safeParseInteger(imageHeightString, 0);
290                                                 if ((imageWidth < 1) || (imageHeight < 1)) {
291                                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
292                                                         return null;
293                                                 }
294                                                 Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(Optional.<ImageCreated>absent());
295                                                 image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
296                                         }
297                                 }
298                                 album.modify().setAlbumImage(albumImageId).update();
299                         }
300                 }
301
302                 /* process avatar. */
303                 profile.setAvatar(fromNullable(avatarId));
304
305                 /* okay, apparently everything was parsed correctly. Now import. */
306                 sone.setProfile(profile);
307                 sone.setPosts(posts);
308                 sone.setReplies(replies);
309                 sone.setLikePostIds(likedPostIds);
310                 sone.setLikeReplyIds(likedReplyIds);
311
312                 return sone;
313         }
314
315         private Optional<Integer> parseProtocolVersion(SimpleXML soneXml) {
316                 String soneProtocolVersion = soneXml.getValue("protocol-version", null);
317                 if (soneProtocolVersion == null) {
318                         logger.log(Level.INFO, "No protocol version found, assuming 0.");
319                         return absent();
320                 }
321                 return fromNullable(Ints.tryParse(soneProtocolVersion));
322         }
323
324         private Optional<SimpleXML> parseXml(Sone originalSone, Document document) {
325                 try {
326                         return fromNullable(SimpleXML.fromDocument(document));
327                 } catch (NullPointerException npe1) {
328                         /* for some reason, invalid XML can cause NPEs. */
329                         return absent();
330                 }
331         }
332
333         private Optional<Client> parseClient(Sone sone, SimpleXML soneXml) {
334                 SimpleXML clientXml = soneXml.getNode("client");
335                 if (clientXml == null) {
336                         return absent();
337                 }
338                 String clientName = clientXml.getValue("name", null);
339                 String clientVersion = clientXml.getValue("version", null);
340                 if ((clientName == null) || (clientVersion == null)) {
341                         logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone));
342                         return absent();
343                 }
344                 return of(new Client(clientName, clientVersion));
345         }
346
347         public static class InvalidXml extends RuntimeException {
348
349         }
350
351         public static class InvalidProtocolVersion extends RuntimeException {
352
353         }
354
355         public static class MalformedXml extends RuntimeException {
356
357         }
358
359          public static class DuplicateField extends RuntimeException {
360
361         }
362
363         public static class MalformedTime extends RuntimeException {
364
365         }
366
367         public static class InvalidParentAlbum extends RuntimeException {
368
369         }
370
371 }