Throw a different exception if a Sone is too new.
[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                 SimpleXML soneXml = SimpleXML.fromDocument(document);
89                 Optional<Client> parsedClient = parseClient(originalSone, soneXml);
90                 Sone sone = new DefaultSone(database, originalSone.getId(), originalSone.isLocal(), parsedClient.or(originalSone.getClient()));
91
92                 Optional<Integer> protocolVersion = parseProtocolVersion(soneXml);
93                 if (protocolVersion.isPresent()) {
94                         if (protocolVersion.get() < 0) {
95                                 logger.log(Level.WARNING, String.format("Invalid protocol version: %d! Not parsing Sone.", protocolVersion.get()));
96                                 throw new InvalidProtocolVersion();
97                         }
98                         if (protocolVersion.get() > MAX_PROTOCOL_VERSION) {
99                                 logger.log(Level.WARNING, String.format("Unknown protocol version: %d! Not parsing Sone.", protocolVersion.get()));
100                                 throw new SoneTooNew();
101                         }
102                 }
103
104                 String soneTime = soneXml.getValue("time", null);
105                 if (soneTime == null) {
106                         /* TODO - mark Sone as bad. */
107                         logger.log(Level.WARNING, String.format("Downloaded time for Sone %s was null!", sone));
108                         throw new MalformedXml();
109                 }
110                 try {
111                         sone.setTime(Long.parseLong(soneTime));
112                 } catch (NumberFormatException nfe1) {
113                         /* TODO - mark Sone as bad. */
114                         logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", sone, soneTime));
115                         throw new MalformedTime();
116                 }
117
118                 SimpleXML profileXml = soneXml.getNode("profile");
119                 if (profileXml == null) {
120                         /* TODO - mark Sone as bad. */
121                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone));
122                         throw new MalformedXml();
123                 }
124
125                 /* parse profile. */
126                 String profileFirstName = profileXml.getValue("first-name", null);
127                 String profileMiddleName = profileXml.getValue("middle-name", null);
128                 String profileLastName = profileXml.getValue("last-name", null);
129                 Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null));
130                 Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null));
131                 Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null));
132                 Profile profile = new Profile(sone).modify().setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName).update();
133                 profile.modify().setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear).update();
134                 /* avatar is processed after images are loaded. */
135                 String avatarId = profileXml.getValue("avatar", null);
136
137                 /* parse profile fields. */
138                 SimpleXML profileFieldsXml = profileXml.getNode("fields");
139                 if (profileFieldsXml != null) {
140                         for (SimpleXML fieldXml : profileFieldsXml.getNodes("field")) {
141                                 String fieldName = fieldXml.getValue("field-name", null);
142                                 String fieldValue = fieldXml.getValue("field-value", "");
143                                 if (fieldName == null) {
144                                         logger.log(Level.WARNING, String.format("Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", sone, fieldName, fieldValue));
145                                         throw new MalformedXml();
146                                 }
147                                 try {
148                                         profile.setField(profile.addField(fieldName), fieldValue);
149                                 } catch (IllegalArgumentException iae1) {
150                                         logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1);
151                                         throw new DuplicateField();
152                                 }
153                         }
154                 }
155
156                 /* parse posts. */
157                 SimpleXML postsXml = soneXml.getNode("posts");
158                 Set<Post> posts = new HashSet<Post>();
159                 if (postsXml == null) {
160                         /* TODO - mark Sone as bad. */
161                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", sone));
162                 } else {
163                         for (SimpleXML postXml : postsXml.getNodes("post")) {
164                                 String postId = postXml.getValue("id", null);
165                                 String postRecipientId = postXml.getValue("recipient", null);
166                                 String postTime = postXml.getValue("time", null);
167                                 String postText = postXml.getValue("text", null);
168                                 if ((postId == null) || (postTime == null) || (postText == null)) {
169                                         /* TODO - mark Sone as bad. */
170                                         logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", sone, postId, postTime, postText));
171                                         throw new MalformedXml();
172                                 }
173                                 try {
174                                         PostBuilder postBuilder = sone.newPostBuilder();
175                                         /* TODO - parse time correctly. */
176                                         postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText);
177                                         if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
178                                                 postBuilder.to(of(postRecipientId));
179                                         }
180                                         posts.add(postBuilder.build(Optional.<PostCreated>absent()));
181                                 } catch (NumberFormatException nfe1) {
182                                         /* TODO - mark Sone as bad. */
183                                         logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime));
184                                         throw new MalformedTime();
185                                 }
186                         }
187                 }
188
189                 /* parse replies. */
190                 SimpleXML repliesXml = soneXml.getNode("replies");
191                 Set<PostReply> replies = new HashSet<PostReply>();
192                 if (repliesXml == null) {
193                         /* TODO - mark Sone as bad. */
194                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no replies!", sone));
195                 } else {
196                         for (SimpleXML replyXml : repliesXml.getNodes("reply")) {
197                                 String replyId = replyXml.getValue("id", null);
198                                 String replyPostId = replyXml.getValue("post-id", null);
199                                 String replyTime = replyXml.getValue("time", null);
200                                 String replyText = replyXml.getValue("text", null);
201                                 if ((replyId == null) || (replyPostId == null) || (replyTime == null) || (replyText == null)) {
202                                         /* TODO - mark Sone as bad. */
203                                         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));
204                                         throw new MalformedXml();
205                                 }
206                                 try {
207                                         /* TODO - parse time correctly. */
208                                         PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(replyPostId).withId(replyId).withTime(Long.parseLong(replyTime)).withText(replyText);
209                                         replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
210                                 } catch (NumberFormatException nfe1) {
211                                         /* TODO - mark Sone as bad. */
212                                         logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
213                                         throw new MalformedTime();
214                                 }
215                         }
216                 }
217
218                 /* parse liked post IDs. */
219                 SimpleXML likePostIdsXml = soneXml.getNode("post-likes");
220                 Set<String> likedPostIds = new HashSet<String>();
221                 if (likePostIdsXml == null) {
222                         /* TODO - mark Sone as bad. */
223                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no post likes!", sone));
224                 } else {
225                         for (SimpleXML likedPostIdXml : likePostIdsXml.getNodes("post-like")) {
226                                 String postId = likedPostIdXml.getValue();
227                                 likedPostIds.add(postId);
228                         }
229                 }
230
231                 /* parse liked reply IDs. */
232                 SimpleXML likeReplyIdsXml = soneXml.getNode("reply-likes");
233                 Set<String> likedReplyIds = new HashSet<String>();
234                 if (likeReplyIdsXml == null) {
235                         /* TODO - mark Sone as bad. */
236                         logger.log(Level.WARNING, String.format("Downloaded Sone %s has no reply likes!", sone));
237                 } else {
238                         for (SimpleXML likedReplyIdXml : likeReplyIdsXml.getNodes("reply-like")) {
239                                 String replyId = likedReplyIdXml.getValue();
240                                 likedReplyIds.add(replyId);
241                         }
242                 }
243
244                 /* parse albums. */
245                 SimpleXML albumsXml = soneXml.getNode("albums");
246                 Map<String, Album> albums = Maps.newHashMap();
247                 if (albumsXml != null) {
248                         for (SimpleXML albumXml : albumsXml.getNodes("album")) {
249                                 String id = albumXml.getValue("id", null);
250                                 String parentId = albumXml.getValue("parent", null);
251                                 String title = albumXml.getValue("title", null);
252                                 String description = albumXml.getValue("description", "");
253                                 String albumImageId = albumXml.getValue("album-image", null);
254                                 if ((id == null) || (title == null) || (description == null)) {
255                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone));
256                                         throw new MalformedXml();
257                                 }
258                                 Album parent = sone.getRootAlbum();
259                                 if (parentId != null) {
260                                         parent = albums.get(parentId);
261                                         if (parent == null) {
262                                                 logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
263                                                 throw new InvalidParentAlbum();
264                                         }
265                                 }
266                                 Album album = parent.newAlbumBuilder().withId(id).build().modify().setTitle(title).setDescription(description).update();
267                                 albums.put(album.getId(), album);
268                                 SimpleXML imagesXml = albumXml.getNode("images");
269                                 if (imagesXml != null) {
270                                         for (SimpleXML imageXml : imagesXml.getNodes("image")) {
271                                                 String imageId = imageXml.getValue("id", null);
272                                                 String imageCreationTimeString = imageXml.getValue("creation-time", null);
273                                                 String imageKey = imageXml.getValue("key", null);
274                                                 String imageTitle = imageXml.getValue("title", null);
275                                                 String imageDescription = imageXml.getValue("description", "");
276                                                 String imageWidthString = imageXml.getValue("width", null);
277                                                 String imageHeightString = imageXml.getValue("height", null);
278                                                 if ((imageId == null) || (imageCreationTimeString == null) || (imageKey == null) || (imageTitle == null) || (imageWidthString == null) || (imageHeightString == null)) {
279                                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone));
280                                                         throw new MalformedXml();
281                                                 }
282                                                 long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L);
283                                                 int imageWidth = Numbers.safeParseInteger(imageWidthString, 0);
284                                                 int imageHeight = Numbers.safeParseInteger(imageHeightString, 0);
285                                                 if ((imageWidth < 1) || (imageHeight < 1)) {
286                                                         logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
287                                                         throw new MalformedDimension();
288                                                 }
289                                                 Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(Optional.<ImageCreated>absent());
290                                                 image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
291                                         }
292                                 }
293                                 album.modify().setAlbumImage(albumImageId).update();
294                         }
295                 }
296
297                 /* process avatar. */
298                 profile.setAvatar(fromNullable(avatarId));
299
300                 /* okay, apparently everything was parsed correctly. Now import. */
301                 sone.setProfile(profile);
302                 sone.setPosts(posts);
303                 sone.setReplies(replies);
304                 sone.setLikePostIds(likedPostIds);
305                 sone.setLikeReplyIds(likedReplyIds);
306
307                 return sone;
308         }
309
310         private Optional<Integer> parseProtocolVersion(SimpleXML soneXml) {
311                 String soneProtocolVersion = soneXml.getValue("protocol-version", null);
312                 if (soneProtocolVersion == null) {
313                         logger.log(Level.INFO, "No protocol version found, assuming 0.");
314                         return absent();
315                 }
316                 return fromNullable(Ints.tryParse(soneProtocolVersion));
317         }
318
319         private Optional<Client> parseClient(Sone sone, SimpleXML soneXml) {
320                 SimpleXML clientXml = soneXml.getNode("client");
321                 if (clientXml == null) {
322                         return absent();
323                 }
324                 String clientName = clientXml.getValue("name", null);
325                 String clientVersion = clientXml.getValue("version", null);
326                 if ((clientName == null) || (clientVersion == null)) {
327                         logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone));
328                         return absent();
329                 }
330                 return of(new Client(clientName, clientVersion));
331         }
332
333         public static class InvalidXml extends RuntimeException {
334
335         }
336
337         public static class InvalidProtocolVersion extends RuntimeException {
338
339         }
340
341         public static class SoneTooNew extends RuntimeException {
342
343         }
344
345         public static class MalformedXml extends RuntimeException {
346
347         }
348
349         public static class DuplicateField extends RuntimeException {
350
351         }
352
353         public static class MalformedTime extends RuntimeException {
354
355         }
356
357         public static class InvalidParentAlbum extends RuntimeException {
358
359         }
360
361         public static class MalformedDimension extends RuntimeException {
362
363         }
364
365 }