* {@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));
}
}
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());
}
}
}
import net.pterodactylus.sone.data.Post;
+import com.google.common.base.Optional;
+
/**
* Interface for objects that can provide {@link Post}s by their ID.
*
* 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);
}
*/
@Override
public Post getPost() {
- return postProvider.getPost(postId);
+ return postProvider.getPost(postId).get();
}
/**
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);
}
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;
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)));
}
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;
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);
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);
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;
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);
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()) {
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;
@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;
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);
}
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;
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());
}
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;
@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();
}
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);
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;
@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())));
}
/**
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);
}
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()) {