2 * Sone - FcpInterface.java - Copyright © 2011–2012 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
20 import java.util.Collection;
21 import java.util.List;
23 import net.pterodactylus.sone.core.Core;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.PostReply;
26 import net.pterodactylus.sone.data.Profile;
27 import net.pterodactylus.sone.data.Profile.Field;
28 import net.pterodactylus.sone.data.Reply;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
31 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
32 import net.pterodactylus.sone.freenet.fcp.Command;
33 import net.pterodactylus.sone.freenet.fcp.FcpException;
34 import net.pterodactylus.sone.template.SoneAccessor;
35 import net.pterodactylus.util.collection.filter.Filters;
36 import freenet.node.FSParseException;
37 import freenet.support.SimpleFieldSet;
40 * Abstract base implementation of a {@link Command} with Sone-related helper
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public abstract class AbstractSoneCommand extends AbstractCommand {
48 private final Core core;
50 /** Whether this command needs write access. */
51 private final boolean writeAccess;
54 * Creates a new abstract Sone FCP command.
59 protected AbstractSoneCommand(Core core) {
64 * Creates a new abstract Sone FCP command.
69 * {@code true} if this command requires write access,
70 * {@code false} otherwise
72 protected AbstractSoneCommand(Core core, boolean writeAccess) {
74 this.writeAccess = writeAccess;
82 * Returns the Sone core.
84 * @return The Sone core
86 protected Core getCore() {
91 * Returns whether this command requires write access.
93 * @return {@code true} if this command require write access, {@code false}
96 public boolean requiresWriteAccess() {
105 * Encodes text in a way that makes it possible for the text to be stored in
106 * a {@link SimpleFieldSet}. Backslashes, CR, and LF are prepended with a
111 * @return The encoded text
113 protected String encodeString(String text) {
114 return text.replaceAll("\\\\", "\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
118 * Returns a Sone whose ID is a parameter in the given simple field set.
120 * @param simpleFieldSet
121 * The simple field set containing the ID of the Sone
122 * @param parameterName
123 * The name under which the Sone ID is stored in the simple field
126 * {@code true} to only return local Sones, {@code false} to
129 * @throws FcpException
130 * if there is no Sone ID stored under the given parameter name,
131 * or if the Sone ID is invalid
133 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException {
134 return getSone(simpleFieldSet, parameterName, localOnly, true);
138 * Returns a Sone whose ID is a parameter in the given simple field set.
140 * @param simpleFieldSet
141 * The simple field set containing the ID of the Sone
142 * @param parameterName
143 * The name under which the Sone ID is stored in the simple field
146 * {@code true} to only return local Sones, {@code false} to
149 * {@code true} if a valid Sone ID is required, {@code false}
151 * @return The Sone, or {@code null} if {@code mandatory} is {@code false}
152 * and the Sone ID is invalid
153 * @throws FcpException
154 * if there is no Sone ID stored under the given parameter name,
155 * or if {@code mandatory} is {@code true} and the Sone ID is
158 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException {
159 String soneId = simpleFieldSet.get(parameterName);
160 if (mandatory && (soneId == null)) {
161 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.");
163 Sone sone = localOnly ? core.getLocalSone(soneId, false) : core.getSone(soneId, false);
164 if (mandatory && (sone == null)) {
165 throw new FcpException("Could not load Sone from “" + soneId + "”.");
171 * Returns a post whose ID is a parameter in the given simple field set.
173 * @param simpleFieldSet
174 * The simple field set containing the ID of the post
175 * @param parameterName
176 * The name under which the post ID is stored in the simple field
179 * @throws FcpException
180 * if there is no post ID stored under the given parameter name,
181 * or if the post ID is invalid
183 protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
185 String postId = simpleFieldSet.getString(parameterName);
186 Post post = core.getPost(postId, false);
188 throw new FcpException("Could not load post from “" + postId + "”.");
191 } catch (FSParseException fspe1) {
192 throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
197 * Returns a reply whose ID is a parameter in the given simple field set.
199 * @param simpleFieldSet
200 * The simple field set containing the ID of the reply
201 * @param parameterName
202 * The name under which the reply ID is stored in the simple
205 * @throws FcpException
206 * if there is no reply ID stored under the given parameter
207 * name, or if the reply ID is invalid
209 protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
211 String replyId = simpleFieldSet.getString(parameterName);
212 PostReply reply = core.getReply(replyId, false);
214 throw new FcpException("Could not load reply from “" + replyId + "”.");
217 } catch (FSParseException fspe1) {
218 throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
223 * Creates a simple field set from the given Sone, including {@link Profile}
229 * The prefix for the field names (may be empty but not {@code
232 * An optional local Sone that is used for Sone-specific data,
233 * such as if the Sone is followed by the local Sone
234 * @return The simple field set containing the given Sone
236 protected SimpleFieldSet encodeSone(Sone sone, String prefix, Sone localSone) {
237 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
239 soneBuilder.put(prefix + "Name", sone.getName());
240 soneBuilder.put(prefix + "NiceName", SoneAccessor.getNiceName(sone));
241 soneBuilder.put(prefix + "LastUpdated", sone.getTime());
242 if (localSone != null) {
243 soneBuilder.put(prefix + "Followed", String.valueOf(localSone.hasFriend(sone.getId())));
245 Profile profile = sone.getProfile();
246 soneBuilder.put(prefix + "Field.Count", profile.getFields().size());
248 for (Field field : profile.getFields()) {
249 soneBuilder.put(prefix + "Field." + fieldIndex + ".Name", field.getName());
250 soneBuilder.put(prefix + "Field." + fieldIndex + ".Value", field.getValue());
254 return soneBuilder.get();
258 * Creates a simple field set from the given collection of Sones.
261 * The Sones to encode
263 * The prefix for the field names (may be empty but not
265 * @return The simple field set containing the given Sones
267 protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
268 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
271 soneBuilder.put(prefix + "Count", sones.size());
272 for (Sone sone : sones) {
273 String sonePrefix = prefix + soneIndex++ + ".";
274 soneBuilder.put(sonePrefix + "ID", sone.getId());
275 soneBuilder.put(sonePrefix + "Name", sone.getName());
276 soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone));
277 soneBuilder.put(sonePrefix + "Time", sone.getTime());
280 return soneBuilder.get();
284 * Creates a simple field set from the given post.
289 * The prefix for the field names (may be empty but not
291 * @param includeReplies
292 * {@code true} to include replies, {@code false} to not include
294 * @return The simple field set containing the post
296 protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
297 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
299 postBuilder.put(prefix + "ID", post.getId());
300 postBuilder.put(prefix + "Sone", post.getSone().getId());
301 if (post.getRecipient() != null) {
302 postBuilder.put(prefix + "Recipient", post.getRecipient().getId());
304 postBuilder.put(prefix + "Time", post.getTime());
305 postBuilder.put(prefix + "Text", encodeString(post.getText()));
306 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
308 if (includeReplies) {
309 List<PostReply> replies = core.getReplies(post);
310 postBuilder.put(encodeReplies(replies, prefix));
313 return postBuilder.get();
317 * Creates a simple field set from the given collection of posts.
320 * The posts to encode
322 * The prefix for the field names (may be empty but not
324 * @param includeReplies
325 * {@code true} to include the replies, {@code false} to not
326 * include the replies
327 * @return The simple field set containing the posts
329 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
330 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
333 postBuilder.put(prefix + "Count", posts.size());
334 for (Post post : posts) {
335 String postPrefix = prefix + postIndex++;
336 postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
337 if (includeReplies) {
338 postBuilder.put(encodeReplies(Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLY_FILTER), postPrefix + "."));
342 return postBuilder.get();
346 * Creates a simple field set from the given collection of replies.
349 * The replies to encode
351 * The prefix for the field names (may be empty, but not
353 * @return The simple field set containing the replies
355 protected SimpleFieldSet encodeReplies(Collection<? extends PostReply> replies, String prefix) {
356 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
359 replyBuilder.put(prefix + "Replies.Count", replies.size());
360 for (PostReply reply : replies) {
361 String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
362 replyBuilder.put(replyPrefix + "ID", reply.getId());
363 replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
364 replyBuilder.put(replyPrefix + "Time", reply.getTime());
365 replyBuilder.put(replyPrefix + "Text", encodeString(reply.getText()));
368 return replyBuilder.get();
372 * Creates a simple field set from the given collection of Sones that like
378 * The prefix for the field names (may be empty but not
380 * @return The simple field set containing the likes
382 protected SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
383 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
386 likesBuilder.put(prefix + "Count", likes.size());
387 for (Sone sone : likes) {
388 String sonePrefix = prefix + likeIndex++ + ".";
389 likesBuilder.put(sonePrefix + "ID", sone.getId());
392 return likesBuilder.get();
403 public String toString() {
404 return getClass().getName() + "[writeAccess=" + writeAccess + "]";