From 7bf3f34153d68c6c74245e24d0957aa11e93b056 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 24 Jan 2013 06:41:29 +0100 Subject: [PATCH] Make post returned by post provider optional. --- src/main/java/net/pterodactylus/sone/core/Core.java | 10 +++++----- .../java/net/pterodactylus/sone/core/PostProvider.java | 4 +++- .../net/pterodactylus/sone/data/impl/PostReplyImpl.java | 2 +- .../net/pterodactylus/sone/fcp/AbstractSoneCommand.java | 6 +++--- .../java/net/pterodactylus/sone/text/SoneTextParser.java | 8 +++++--- .../java/net/pterodactylus/sone/web/CreateReplyPage.java | 9 +++++++-- .../java/net/pterodactylus/sone/web/DeletePostPage.java | 15 ++++++++++----- .../java/net/pterodactylus/sone/web/MarkAsKnownPage.java | 6 +++--- .../java/net/pterodactylus/sone/web/ViewPostPage.java | 14 ++++++++------ .../pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java | 8 +++++--- .../pterodactylus/sone/web/ajax/DeletePostAjaxPage.java | 10 ++++++---- .../net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java | 7 +++++-- .../net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java | 8 +++++--- .../net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java | 8 ++++---- .../pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java | 6 +++--- 15 files changed, 73 insertions(+), 48 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 70d9ea2..9bc31bb 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -541,9 +541,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * {@inheritDoc} */ @Override - public Post getPost(String postId) { + public Optional 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 posts = new HashSet(); synchronized (bookmarkedPosts) { for (String bookmarkedPostId : bookmarkedPosts) { - Post post = getPost(bookmarkedPostId); - if (post != null) { - posts.add(post); + Optional post = getPost(bookmarkedPostId); + if (!post.isPresent()) { + posts.add(post.get()); } } } diff --git a/src/main/java/net/pterodactylus/sone/core/PostProvider.java b/src/main/java/net/pterodactylus/sone/core/PostProvider.java index e7167cd..19a0e5d 100644 --- a/src/main/java/net/pterodactylus/sone/core/PostProvider.java +++ b/src/main/java/net/pterodactylus/sone/core/PostProvider.java @@ -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 getPost(String postId); } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java index 361a2e9..017f019 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java @@ -57,7 +57,7 @@ public class PostReplyImpl extends ReplyImpl implements PostReply { */ @Override public Post getPost() { - return postProvider.getPost(postId); + return postProvider.getPost(postId).get(); } /** diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index 1617523..368f49a 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -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 = 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); } diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index a093e46..6e84c70 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -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 { 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 = postProvider.getPost(postId); + if (post.isPresent()) { + parts.add(new PostPart(post.get())); } else { parts.add(new PlainTextPart(line.substring(0, 43))); } diff --git a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java index e6a5735..c8979c1 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java @@ -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 = 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); diff --git a/src/main/java/net/pterodactylus/sone/web/DeletePostPage.java b/src/main/java/net/pterodactylus/sone/web/DeletePostPage.java index 1363ac9..6ef86f0 100644 --- a/src/main/java/net/pterodactylus/sone/web/DeletePostPage.java +++ b/src/main/java/net/pterodactylus/sone/web/DeletePostPage.java @@ -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 = 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 = 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); diff --git a/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java b/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java index 04ad304..f183ff4 100644 --- a/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java +++ b/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java @@ -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 = webInterface.getCore().getPost(id); + if (!post.isPresent()) { continue; } - webInterface.getCore().markPostKnown(post); + webInterface.getCore().markPostKnown(post.get()); } else if (type.equals("reply")) { Optional reply = webInterface.getCore().getPostReply(id); if (!reply.isPresent()) { diff --git a/src/main/java/net/pterodactylus/sone/web/ViewPostPage.java b/src/main/java/net/pterodactylus/sone/web/ViewPostPage.java index ca78d99..50719ee 100644 --- a/src/main/java/net/pterodactylus/sone/web/ViewPostPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ViewPostPage.java @@ -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 = 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 = webInterface.getCore().getPost(postId); + templateContext.set("post", post.get()); templateContext.set("raw", raw); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java index 164ec19..8a35185 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java @@ -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 = 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()); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java index 3beb85d..71b0016 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java @@ -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 = 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(); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java index f4a20fe..1a5286f 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java @@ -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 sones = webInterface.getCore().getLikes(post); + Optional post = webInterface.getCore().getPost(id); + if (!post.isPresent()) { + return createErrorJsonObject("invalid-post-id"); + } + Set sones = webInterface.getCore().getLikes(post.get()); return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); } else if ("reply".equals(type)) { Optional reply = webInterface.getCore().getPostReply(id); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java index 0c2311e..e32573d 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java @@ -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 = 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()))); } /** diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java index 7aa6779..4f54571 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java @@ -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 = 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); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java index fec5800..997c9dc 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java @@ -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 = core.getPost(id); + if (!post.isPresent()) { continue; } - core.markPostKnown(post); + core.markPostKnown(post.get()); } else if (type.equals("reply")) { Optional reply = core.getPostReply(id); if (!reply.isPresent()) { -- 2.7.4