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