2 * Sone - FcpInterface.java - Copyright © 2011 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.Reply;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
28 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
29 import net.pterodactylus.sone.freenet.fcp.Command;
30 import net.pterodactylus.sone.freenet.fcp.FcpException;
31 import net.pterodactylus.sone.template.SoneAccessor;
32 import net.pterodactylus.util.filter.Filters;
33 import freenet.node.FSParseException;
34 import freenet.support.SimpleFieldSet;
37 * Abstract base implementation of a {@link Command} with Sone-related helper
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public abstract class AbstractSoneCommand extends AbstractCommand {
45 private final Core core;
47 /** Whether this command needs write access. */
48 private final boolean writeAccess;
51 * Creates a new abstract Sone FCP command.
56 protected AbstractSoneCommand(Core core) {
61 * Creates a new abstract Sone FCP command.
66 * {@code true} if this command requires write access,
67 * {@code false} otherwise
69 protected AbstractSoneCommand(Core core, boolean writeAccess) {
71 this.writeAccess = writeAccess;
79 * Returns the Sone core.
81 * @return The Sone core
83 protected Core getCore() {
88 * Returns whether this command requires write access.
90 * @return {@code true} if this command require write access, {@code false}
93 public boolean requiresWriteAccess() {
102 * Encodes text in a way that makes it possible for the text to be stored in
103 * a {@link SimpleFieldSet}. Backslashes, CR, and LF are prepended with a
108 * @return The encoded text
110 protected String encodeString(String text) {
111 return text.replaceAll("\\\\", "\\\\").replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
115 * Returns a Sone whose ID is a parameter in the given simple field set.
117 * @param simpleFieldSet
118 * The simple field set containing the ID of the Sone
119 * @param parameterName
120 * The name under which the Sone ID is stored in the simple field
123 * {@code true} to only return local Sones, {@code false} to
126 * @throws FcpException
127 * if there is no Sone ID stored under the given parameter name,
128 * or if the Sone ID is invalid
130 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException {
131 return getSone(simpleFieldSet, parameterName, localOnly, true);
135 * Returns a Sone whose ID is a parameter in the given simple field set.
137 * @param simpleFieldSet
138 * The simple field set containing the ID of the Sone
139 * @param parameterName
140 * The name under which the Sone ID is stored in the simple field
143 * {@code true} to only return local Sones, {@code false} to
146 * {@code true} if a valid Sone ID is required, {@code false}
148 * @return The Sone, or {@code null} if {@code mandatory} is {@code false}
149 * and the Sone ID is invalid
150 * @throws FcpException
151 * if there is no Sone ID stored under the given parameter name,
152 * or if {@code mandatory} is {@code true} and the Sone ID is
155 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly, boolean mandatory) throws FcpException {
156 String soneId = simpleFieldSet.get(parameterName);
157 if (mandatory && (soneId == null)) {
158 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.");
160 Sone sone = localOnly ? core.getLocalSone(soneId, false) : core.getSone(soneId, false);
161 if (mandatory && (sone == null)) {
162 throw new FcpException("Could not load Sone from “" + soneId + "”.");
168 * Returns a post whose ID is a parameter in the given simple field set.
170 * @param simpleFieldSet
171 * The simple field set containing the ID of the post
172 * @param parameterName
173 * The name under which the post ID is stored in the simple field
176 * @throws FcpException
177 * if there is no post ID stored under the given parameter name,
178 * or if the post ID is invalid
180 protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
182 String postId = simpleFieldSet.getString(parameterName);
183 Post post = core.getPost(postId, false);
185 throw new FcpException("Could not load post from “" + postId + "”.");
188 } catch (FSParseException fspe1) {
189 throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
194 * Returns a reply whose ID is a parameter in the given simple field set.
196 * @param simpleFieldSet
197 * The simple field set containing the ID of the reply
198 * @param parameterName
199 * The name under which the reply ID is stored in the simple
202 * @throws FcpException
203 * if there is no reply ID stored under the given parameter
204 * name, or if the reply ID is invalid
206 protected Reply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
208 String replyId = simpleFieldSet.getString(parameterName);
209 Reply reply = core.getReply(replyId, false);
211 throw new FcpException("Could not load reply from “" + replyId + "”.");
214 } catch (FSParseException fspe1) {
215 throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
220 * Creates a simple field set from the given collection of Sones.
223 * The Sones to encode
225 * The prefix for the field names (may be empty but not
227 * @return The simple field set containing the given Sones
229 protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
230 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
233 soneBuilder.put(prefix + "Count", sones.size());
234 for (Sone sone : sones) {
235 String sonePrefix = prefix + soneIndex++ + ".";
236 soneBuilder.put(sonePrefix + "ID", sone.getId());
237 soneBuilder.put(sonePrefix + "Name", sone.getName());
238 soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone));
239 soneBuilder.put(sonePrefix + "Time", sone.getTime());
242 return soneBuilder.get();
246 * Creates a simple field set from the given post.
251 * The prefix for the field names (may be empty but not
253 * @param includeReplies
254 * {@code true} to include replies, {@code false} to not include
256 * @return The simple field set containing the post
258 protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
259 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
261 postBuilder.put(prefix + "ID", post.getId());
262 postBuilder.put(prefix + "Sone", post.getSone().getId());
263 if (post.getRecipient() != null) {
264 postBuilder.put(prefix + "Recipient", post.getRecipient().getId());
266 postBuilder.put(prefix + "Time", post.getTime());
267 postBuilder.put(prefix + "Text", encodeString(post.getText()));
268 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
270 if (includeReplies) {
271 List<Reply> replies = core.getReplies(post);
272 postBuilder.put(encodeReplies(replies, prefix));
275 return postBuilder.get();
279 * Creates a simple field set from the given collection of posts.
282 * The posts to encode
284 * The prefix for the field names (may be empty but not
286 * @param includeReplies
287 * {@code true} to include the replies, {@code false} to not
288 * include the replies
289 * @return The simple field set containing the posts
291 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
292 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
295 postBuilder.put(prefix + "Count", posts.size());
296 for (Post post : posts) {
297 String postPrefix = prefix + postIndex++;
298 postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
299 if (includeReplies) {
300 postBuilder.put(encodeReplies(Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLIES_FILTER), postPrefix + "."));
304 return postBuilder.get();
308 * Creates a simple field set from the given collection of replies.
311 * The replies to encode
313 * The prefix for the field names (may be empty, but not
315 * @return The simple field set containing the replies
317 protected SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
318 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
321 replyBuilder.put(prefix + "Replies.Count", replies.size());
322 for (Reply reply : replies) {
323 String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
324 replyBuilder.put(replyPrefix + "ID", reply.getId());
325 replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
326 replyBuilder.put(replyPrefix + "Time", reply.getTime());
327 replyBuilder.put(replyPrefix + "Text", encodeString(reply.getText()));
330 return replyBuilder.get();
334 * Creates a simple field set from the given collection of Sones that like
340 * The prefix for the field names (may be empty but not
342 * @return The simple field set containing the likes
344 protected SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
345 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
348 likesBuilder.put(prefix + "Count", likes.size());
349 for (Sone sone : likes) {
350 String sonePrefix = prefix + likeIndex++ + ".";
351 likesBuilder.put(sonePrefix + "ID", sone.getId());
354 return likesBuilder.get();
365 public String toString() {
366 return getClass().getName() + "[writeAccess=" + writeAccess + "]";