Make post returned by post provider optional.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Jan 2013 05:41:29 +0000 (06:41 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Jan 2013 05:41:29 +0000 (06:41 +0100)
15 files changed:
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/PostProvider.java
src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java
src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java
src/main/java/net/pterodactylus/sone/text/SoneTextParser.java
src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java
src/main/java/net/pterodactylus/sone/web/DeletePostPage.java
src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java
src/main/java/net/pterodactylus/sone/web/ViewPostPage.java
src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java

index 70d9ea2..9bc31bb 100644 (file)
@@ -541,9 +541,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * {@inheritDoc}
         */
        @Override
-       public Post getPost(String postId) {
+       public Optional<Post> getPost(String postId) {
                synchronized (posts) {
-                       return posts.get(postId);
+                       return Optional.fromNullable(posts.get(postId));
                }
        }
 
@@ -676,9 +676,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                Set<Post> posts = new HashSet<Post>();
                synchronized (bookmarkedPosts) {
                        for (String bookmarkedPostId : bookmarkedPosts) {
-                               Post post = getPost(bookmarkedPostId);
-                               if (post != null) {
-                                       posts.add(post);
+                               Optional<Post> post = getPost(bookmarkedPostId);
+                               if (!post.isPresent()) {
+                                       posts.add(post.get());
                                }
                        }
                }
index e7167cd..19a0e5d 100644 (file)
@@ -19,6 +19,8 @@ package net.pterodactylus.sone.core;
 
 import net.pterodactylus.sone.data.Post;
 
+import com.google.common.base.Optional;
+
 /**
  * Interface for objects that can provide {@link Post}s by their ID.
  *
@@ -33,6 +35,6 @@ public interface PostProvider {
         *            The ID of the post to return
         * @return The post with the given ID, or {@code null}
         */
-       public Post getPost(String postId);
+       public Optional<Post> getPost(String postId);
 
 }
index 361a2e9..017f019 100644 (file)
@@ -57,7 +57,7 @@ public class PostReplyImpl extends ReplyImpl<PostReply> implements PostReply {
         */
        @Override
        public Post getPost() {
-               return postProvider.getPost(postId);
+               return postProvider.getPost(postId).get();
        }
 
        /**
index 1617523..368f49a 100644 (file)
@@ -186,11 +186,11 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
        protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
                try {
                        String postId = simpleFieldSet.getString(parameterName);
-                       Post post = core.getPost(postId);
-                       if (post == null) {
+                       Optional<Post> post = core.getPost(postId);
+                       if (!post.isPresent()) {
                                throw new FcpException("Could not load post from “" + postId + "”.");
                        }
-                       return post;
+                       return post.get();
                } catch (FSParseException fspe1) {
                        throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
                }
index a093e46..6e84c70 100644 (file)
@@ -26,6 +26,8 @@ import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.core.PostProvider;
 import net.pterodactylus.sone.core.SoneProvider;
 import net.pterodactylus.sone.data.Post;
@@ -258,9 +260,9 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        if (linkType == LinkType.POST) {
                                                if (line.length() >= (7 + 36)) {
                                                        String postId = line.substring(7, 43);
-                                                       Post post = postProvider.getPost(postId);
-                                                       if ((post != null) && (post.getSone() != null)) {
-                                                               parts.add(new PostPart(post));
+                                                       Optional<Post> post = postProvider.getPost(postId);
+                                                       if (post.isPresent()) {
+                                                               parts.add(new PostPart(post.get()));
                                                        } else {
                                                                parts.add(new PlainTextPart(line.substring(0, 43)));
                                                        }
index e6a5735..c8979c1 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.web;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.text.TextFilter;
@@ -58,7 +60,10 @@ public class CreateReplyPage extends SoneTemplatePage {
                String text = request.getHttpRequest().getPartAsStringFailsafe("text", 65536).trim();
                String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                if (request.getMethod() == Method.POST) {
-                       Post post = webInterface.getCore().getPost(postId);
+                       Optional<Post> post = webInterface.getCore().getPost(postId);
+                       if (!post.isPresent()) {
+                               throw new RedirectException("noPermission.html");
+                       }
                        if (text.length() > 0) {
                                String senderId = request.getHttpRequest().getPartAsStringFailsafe("sender", 43);
                                Sone sender = webInterface.getCore().getLocalSone(senderId, false);
@@ -66,7 +71,7 @@ public class CreateReplyPage extends SoneTemplatePage {
                                        sender = getCurrentSone(request.getToadletContext());
                                }
                                text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
-                               webInterface.getCore().createReply(sender, post, text);
+                               webInterface.getCore().createReply(sender, post.get(), text);
                                throw new RedirectException(returnPage);
                        }
                        templateContext.set("errorTextEmpty", true);
index 1363ac9..6ef86f0 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.web;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Template;
@@ -55,19 +57,22 @@ public class DeletePostPage extends SoneTemplatePage {
                if (request.getMethod() == Method.GET) {
                        String postId = request.getHttpRequest().getParam("post");
                        String returnPage = request.getHttpRequest().getParam("returnPage");
-                       Post post = webInterface.getCore().getPost(postId);
-                       templateContext.set("post", post);
+                       Optional<Post> post = webInterface.getCore().getPost(postId);
+                       if (!post.isPresent()) {
+                               throw new RedirectException("noPermission.html");
+                       }
+                       templateContext.set("post", post.get());
                        templateContext.set("returnPage", returnPage);
                        return;
                } else if (request.getMethod() == Method.POST) {
                        String postId = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
-                       Post post = webInterface.getCore().getPost(postId);
-                       if (!post.getSone().isLocal()) {
+                       Optional<Post> post = webInterface.getCore().getPost(postId);
+                       if (!post.isPresent() || !post.get().getSone().isLocal()) {
                                throw new RedirectException("noPermission.html");
                        }
                        if (request.getHttpRequest().isPartSet("confirmDelete")) {
-                               webInterface.getCore().deletePost(post);
+                               webInterface.getCore().deletePost(post.get());
                                throw new RedirectException(returnPage);
                        } else if (request.getHttpRequest().isPartSet("abortDelete")) {
                                throw new RedirectException(returnPage);
index 04ad304..f183ff4 100644 (file)
@@ -67,11 +67,11 @@ public class MarkAsKnownPage extends SoneTemplatePage {
                for (StringTokenizer idTokenizer = new StringTokenizer(ids); idTokenizer.hasMoreTokens();) {
                        String id = idTokenizer.nextToken();
                        if (type.equals("post")) {
-                               Post post = webInterface.getCore().getPost(id);
-                               if (post == null) {
+                               Optional<Post> post = webInterface.getCore().getPost(id);
+                               if (!post.isPresent()) {
                                        continue;
                                }
-                               webInterface.getCore().markPostKnown(post);
+                               webInterface.getCore().markPostKnown(post.get());
                        } else if (type.equals("reply")) {
                                Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
                                if (!reply.isPresent()) {
index ca78d99..50719ee 100644 (file)
@@ -19,6 +19,8 @@ package net.pterodactylus.sone.web;
 
 import java.net.URI;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.template.SoneAccessor;
 import net.pterodactylus.sone.web.page.FreenetRequest;
@@ -54,11 +56,11 @@ public class ViewPostPage extends SoneTemplatePage {
        @Override
        protected String getPageTitle(FreenetRequest request) {
                String postId = request.getHttpRequest().getParam("post");
-               Post post = webInterface.getCore().getPost(postId);
+               Optional<Post> post = webInterface.getCore().getPost(postId);
                String title = "";
-               if ((post != null) && (post.getSone() != null)) {
-                       title = post.getText().substring(0, Math.min(20, post.getText().length())) + "…";
-                       title += " - " + SoneAccessor.getNiceName(post.getSone()) + " - ";
+               if (post.isPresent()) {
+                       title = post.get().getText().substring(0, Math.min(20, post.get().getText().length())) + "…";
+                       title += " - " + SoneAccessor.getNiceName(post.get().getSone()) + " - ";
                }
                title += webInterface.getL10n().getString("Page.ViewPost.Title");
                return title;
@@ -72,8 +74,8 @@ public class ViewPostPage extends SoneTemplatePage {
                super.processTemplate(request, templateContext);
                String postId = request.getHttpRequest().getParam("post");
                boolean raw = request.getHttpRequest().getParam("raw").equals("true");
-               Post post = webInterface.getCore().getPost(postId);
-               templateContext.set("post", post);
+               Optional<Post> post = webInterface.getCore().getPost(postId);
+               templateContext.set("post", post.get());
                templateContext.set("raw", raw);
        }
 
index 164ec19..8a35185 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.web.ajax;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
@@ -58,12 +60,12 @@ public class CreateReplyAjaxPage extends JsonPage {
                if (sender == null) {
                        sender = getCurrentSone(request.getToadletContext());
                }
-               Post post = webInterface.getCore().getPost(postId);
-               if ((post == null) || (post.getSone() == null)) {
+               Optional<Post> post = webInterface.getCore().getPost(postId);
+               if (!post.isPresent()) {
                        return createErrorJsonObject("invalid-post-id");
                }
                text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
-               PostReply reply = webInterface.getCore().createReply(sender, post, text);
+               PostReply reply = webInterface.getCore().createReply(sender, post.get(), text);
                return createSuccessJsonObject().put("reply", reply.getId()).put("sone", sender.getId());
        }
 
index 3beb85d..71b0016 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.web.ajax;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
@@ -49,14 +51,14 @@ public class DeletePostAjaxPage extends JsonPage {
        @Override
        protected JsonObject createJsonObject(FreenetRequest request) {
                String postId = request.getHttpRequest().getParam("post");
-               Post post = webInterface.getCore().getPost(postId);
-               if ((post == null) || (post.getSone() == null)) {
+               Optional<Post> post = webInterface.getCore().getPost(postId);
+               if (!post.isPresent()) {
                        return createErrorJsonObject("invalid-post-id");
                }
-               if (!post.getSone().isLocal()) {
+               if (!post.get().getSone().isLocal()) {
                        return createErrorJsonObject("not-authorized");
                }
-               webInterface.getCore().deletePost(post);
+               webInterface.getCore().deletePost(post.get());
                return createSuccessJsonObject();
        }
 
index f4a20fe..1a5286f 100644 (file)
@@ -65,8 +65,11 @@ public class GetLikesAjaxPage extends JsonPage {
                        return createErrorJsonObject("invalid-" + type + "-id");
                }
                if ("post".equals(type)) {
-                       Post post = webInterface.getCore().getPost(id);
-                       Set<Sone> sones = webInterface.getCore().getLikes(post);
+                       Optional<Post> post = webInterface.getCore().getPost(id);
+                       if (!post.isPresent()) {
+                               return createErrorJsonObject("invalid-post-id");
+                       }
+                       Set<Sone> sones = webInterface.getCore().getLikes(post.get());
                        return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
                } else if ("reply".equals(type)) {
                        Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
index 0c2311e..e32573d 100644 (file)
@@ -19,6 +19,8 @@ package net.pterodactylus.sone.web.ajax;
 
 import java.io.StringWriter;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.WebInterface;
@@ -59,11 +61,11 @@ public class GetPostAjaxPage extends JsonPage {
        @Override
        protected JsonObject createJsonObject(FreenetRequest request) {
                String postId = request.getHttpRequest().getParam("post");
-               Post post = webInterface.getCore().getPost(postId);
-               if (post == null) {
+               Optional<Post> post = webInterface.getCore().getPost(postId);
+               if (!post.isPresent()) {
                        return createErrorJsonObject("invalid-post-id");
                }
-               return createSuccessJsonObject().put("post", createJsonPost(request, post, getCurrentSone(request.getToadletContext())));
+               return createSuccessJsonObject().put("post", createJsonPost(request, post.get(), getCurrentSone(request.getToadletContext())));
        }
 
        /**
index 7aa6779..4f54571 100644 (file)
@@ -60,16 +60,16 @@ public class GetTimesAjaxPage extends JsonPage {
                if (allIds.length() > 0) {
                        String[] ids = allIds.split(",");
                        for (String id : ids) {
-                               Post post = webInterface.getCore().getPost(id);
-                               if (post == null) {
+                               Optional<Post> post = webInterface.getCore().getPost(id);
+                               if (!post.isPresent()) {
                                        continue;
                                }
                                JsonObject postTime = new JsonObject();
-                               Time time = getTime(post.getTime());
+                               Time time = getTime(post.get().getTime());
                                postTime.put("timeText", time.getText());
                                postTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
                                synchronized (dateFormat) {
-                                       postTime.put("tooltip", dateFormat.format(new Date(post.getTime())));
+                                       postTime.put("tooltip", dateFormat.format(new Date(post.get().getTime())));
                                }
                                postTimes.put(id, postTime);
                        }
index fec5800..997c9dc 100644 (file)
@@ -59,11 +59,11 @@ public class MarkAsKnownAjaxPage extends JsonPage {
                Core core = webInterface.getCore();
                for (String id : ids) {
                        if (type.equals("post")) {
-                               Post post = core.getPost(id);
-                               if (post == null) {
+                               Optional<Post> post = core.getPost(id);
+                               if (!post.isPresent()) {
                                        continue;
                                }
-                               core.markPostKnown(post);
+                               core.markPostKnown(post.get());
                        } else if (type.equals("reply")) {
                                Optional<PostReply> reply = core.getPostReply(id);
                                if (!reply.isPresent()) {