<scope>provided</scope>
</dependency>
<dependency>
- <groupId>net.pterodactylus</groupId>
- <artifactId>utils.json</artifactId>
- <version>0.1</version>
- </dependency>
- <dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>2.1.2</version>
+ </dependency>
</dependencies>
<repositories>
<repository>
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets the user bookmark a post.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String id = request.getHttpRequest().getParam("post", null);
if ((id == null) || (id.length() == 0)) {
return createErrorJsonObject("invalid-post-id");
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
import com.google.common.base.Optional;
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
Sone sone = getCurrentSone(request.getToadletContext());
if (sone == null) {
return createErrorJsonObject("auth-required");
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* This AJAX page create a reply.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String postId = request.getHttpRequest().getParam("post");
String text = request.getHttpRequest().getParam("text").trim();
String senderId = request.getHttpRequest().getParam("sender");
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* This AJAX page deletes a post.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String postId = request.getHttpRequest().getParam("post");
Optional<Post> post = webInterface.getCore().getPost(postId);
if (!post.isPresent()) {
package net.pterodactylus.sone.web.ajax;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
+
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.TextNode;
/**
* AJAX page that lets the user delete a profile field.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String fieldId = request.getHttpRequest().getParam("field");
Sone currentSone = getCurrentSone(request.getToadletContext());
Profile profile = currentSone.getProfile();
profile.removeField(field);
currentSone.setProfile(profile);
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("field", new JsonObject().put("id", field.getId()));
+ return createSuccessJsonObject().put("field", new ObjectNode(instance).put("id", new TextNode(field.getId())));
}
}
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* This AJAX page deletes a reply.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String replyId = request.getHttpRequest().getParam("reply");
Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
if (!reply.isPresent()) {
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
import net.pterodactylus.util.notify.Notification;
/**
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String notificationId = request.getHttpRequest().getParam("notification");
Notification notification = webInterface.getNotifications().getNotification(notificationId);
if (notification == null) {
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets the user distrust a Sone.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
Sone currentSone = getCurrentSone(request.getToadletContext(), false);
if (currentSone == null) {
return createErrorJsonObject("auth-required");
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* Page that stores a user’s album modifications.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String albumId = request.getHttpRequest().getParam("album");
Album album = webInterface.getCore().getAlbum(albumId, false);
if (album == null) {
import net.pterodactylus.sone.text.TextFilter;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
import net.pterodactylus.util.template.TemplateContext;
import com.google.common.collect.ImmutableMap;
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String imageId = request.getHttpRequest().getParam("image");
Image image = webInterface.getCore().getImage(imageId, false);
if (image == null) {
String description = request.getHttpRequest().getParam("description").trim();
image.setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description));
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", (String) parserFilter.format(new TemplateContext(), image.getDescription(), ImmutableMap.<String, Object> builder().put("sone", image.getSone()).build()));
+ return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", (String) parserFilter.format(new TemplateContext(), image.getDescription(), ImmutableMap.<String, Object>builder().put("sone", image.getSone()).build()));
}
}
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets the user rename a profile field.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String fieldId = request.getHttpRequest().getParam("field");
Sone currentSone = getCurrentSone(request.getToadletContext());
Profile profile = currentSone.getProfile();
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets a Sone follow another Sone.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String soneId = request.getHttpRequest().getParam("sone");
Optional<Sone> sone = webInterface.getCore().getSone(soneId);
if (!sone.isPresent()) {
package net.pterodactylus.sone.web.ajax;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
+import static net.pterodactylus.sone.data.Sone.NICE_NAME_COMPARATOR;
+
import java.util.Set;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.template.SoneAccessor;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonArray;
-import net.pterodactylus.util.json.JsonObject;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Optional;
+import com.google.common.collect.FluentIterable;
/**
* AJAX page that retrieves the number of “likes” a {@link Post} has.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String type = request.getHttpRequest().getParam("type", null);
String id = request.getHttpRequest().getParam(type, null);
if ((id == null) || (id.length() == 0)) {
* The Sones to convert to an array
* @return The Sones, sorted by name
*/
- private static JsonArray getSones(Set<Sone> sones) {
- JsonArray soneArray = new JsonArray();
- List<Sone> sortedSones = new ArrayList<Sone>(sones);
- Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
- for (Sone sone : sortedSones) {
- soneArray.add(new JsonObject().put("id", sone.getId()).put("name", SoneAccessor.getNiceName(sone)));
+ private static JsonNode getSones(Set<Sone> sones) {
+ ArrayNode soneArray = new ArrayNode(instance);
+ for (Sone sone : FluentIterable.from(sones).toSortedList(NICE_NAME_COMPARATOR)) {
+ soneArray.add(new ObjectNode(instance).put("id", sone.getId()).put("name", SoneAccessor.getNiceName(sone)));
}
return soneArray;
}
package net.pterodactylus.sone.web.ajax;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
+
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
import net.pterodactylus.sone.notify.ListNotificationFilters;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonArray;
-import net.pterodactylus.util.json.JsonObject;
import net.pterodactylus.util.notify.Notification;
import net.pterodactylus.util.notify.TemplateNotification;
import net.pterodactylus.util.template.TemplateContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
/**
* AJAX handler to return all current notifications.
*
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
Sone currentSone = getCurrentSone(request.getToadletContext(), false);
Collection<Notification> notifications = webInterface.getNotifications().getNotifications();
List<Notification> filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone);
Collections.sort(filteredNotifications, Notification.CREATED_TIME_SORTER);
- JsonArray jsonNotifications = new JsonArray();
+ ArrayNode jsonNotifications = new ArrayNode(instance);
for (Notification notification : filteredNotifications) {
jsonNotifications.add(createJsonNotification(request, notification));
}
* The notification to create a JSON object
* @return The JSON object
*/
- private JsonObject createJsonNotification(FreenetRequest request, Notification notification) {
- JsonObject jsonNotification = new JsonObject();
+ private JsonNode createJsonNotification(FreenetRequest request, Notification notification) {
+ ObjectNode jsonNotification = new ObjectNode(instance);
jsonNotification.put("id", notification.getId());
StringWriter notificationWriter = new StringWriter();
try {
* The current Sone (may be {@code null})
* @return The current options
*/
- private static JsonObject createJsonOptions(Sone currentSone) {
- JsonObject options = new JsonObject();
+ private static JsonNode createJsonOptions(Sone currentSone) {
+ ObjectNode options = new ObjectNode(instance);
if (currentSone != null) {
options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
package net.pterodactylus.sone.web.ajax;
import java.io.StringWriter;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
import com.google.common.base.Optional;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.io.Closer;
-import net.pterodactylus.util.json.JsonObject;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.template.TemplateException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
/**
* This AJAX handler retrieves information and rendered representation of a
* {@link Post}.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String postId = request.getHttpRequest().getParam("post");
Optional<Post> post = webInterface.getCore().getPost(postId);
if (!post.isPresent()) {
* The currently logged in Sone (to store in the template)
* @return The JSON representation of the post
*/
- private JsonObject createJsonPost(FreenetRequest request, Post post, Sone currentSone) {
- JsonObject jsonPost = new JsonObject();
+ private JsonNode createJsonPost(FreenetRequest request, Post post, Sone currentSone) {
+ ObjectNode jsonPost = new ObjectNode(instance);
jsonPost.put("id", post.getId());
jsonPost.put("sone", post.getSone().getId());
jsonPost.put("recipient", post.getRecipientId().orNull());
package net.pterodactylus.sone.web.ajax;
import java.io.StringWriter;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
import com.google.common.base.Optional;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.io.Closer;
-import net.pterodactylus.util.json.JsonObject;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.template.TemplateException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
/**
* This AJAX page returns the details of a reply.
*
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String replyId = request.getHttpRequest().getParam("reply");
Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
if (!reply.isPresent()) {
* The currently logged in Sone (to store in the template)
* @return The JSON representation of the reply
*/
- private JsonObject createJsonReply(FreenetRequest request, PostReply reply, Sone currentSone) {
- JsonObject jsonReply = new JsonObject();
+ private JsonNode createJsonReply(FreenetRequest request, PostReply reply, Sone currentSone) {
+ ObjectNode jsonReply = new ObjectNode(instance);
jsonReply.put("id", reply.getId());
jsonReply.put("postId", reply.getPostId());
jsonReply.put("soneId", reply.getSone().getId());
package net.pterodactylus.sone.web.ajax;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
+
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import net.pterodactylus.sone.template.SoneAccessor;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonArray;
-import net.pterodactylus.util.json.JsonObject;
import net.pterodactylus.util.notify.Notification;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
/* load Sones. always return the status of the current Sone. */
Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
sones.add(webInterface.getCore().getSone(soneId).orNull());
}
}
- JsonArray jsonSones = new JsonArray();
+ ArrayNode jsonSones = new ArrayNode(instance);
for (Sone sone : sones) {
if (sone == null) {
continue;
}
- JsonObject jsonSone = createJsonSone(sone);
- jsonSones.add(jsonSone);
+ jsonSones.add(createJsonSone(sone));
}
/* load notifications. */
List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
});
}
- JsonArray jsonPosts = new JsonArray();
+ ArrayNode jsonPosts = new ArrayNode(instance);
for (Post post : newPosts) {
- JsonObject jsonPost = new JsonObject();
+ ObjectNode jsonPost = new ObjectNode(instance);
jsonPost.put("id", post.getId());
jsonPost.put("sone", post.getSone().getId());
jsonPost.put("recipient", post.getRecipientId().orNull());
}
/* remove replies to unknown posts. */
newReplies = Collections2.filter(newReplies, PostReply.HAS_POST_FILTER);
- JsonArray jsonReplies = new JsonArray();
+ ArrayNode jsonReplies = new ArrayNode(instance);
for (PostReply reply : newReplies) {
- JsonObject jsonReply = new JsonObject();
+ ObjectNode jsonReply = new ObjectNode(instance);
jsonReply.put("id", reply.getId());
jsonReply.put("sone", reply.getSone().getId());
jsonReply.put("post", reply.getPostId());
* The Sone to convert to a JSON object
* @return The JSON representation of the given Sone
*/
- private JsonObject createJsonSone(Sone sone) {
- JsonObject jsonSone = new JsonObject();
+ private JsonNode createJsonSone(Sone sone) {
+ ObjectNode jsonSone = new ObjectNode(instance);
jsonSone.put("id", sone.getId());
jsonSone.put("name", SoneAccessor.getNiceName(sone));
jsonSone.put("local", sone.getInsertUri() != null);
* The current Sone (may be {@code null})
* @return The current options
*/
- private static JsonObject createJsonOptions(Sone currentSone) {
- JsonObject options = new JsonObject();
+ private static JsonNode createJsonOptions(Sone currentSone) {
+ ObjectNode options = new ObjectNode(instance);
if (currentSone != null) {
options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
package net.pterodactylus.sone.web.ajax;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
+
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Optional;
/**
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String allIds = request.getHttpRequest().getParam("posts");
- JsonObject postTimes = new JsonObject();
+ ObjectNode postTimes = new ObjectNode(instance);
if (allIds.length() > 0) {
String[] ids = allIds.split(",");
for (String id : ids) {
if (!post.isPresent()) {
continue;
}
- JsonObject postTime = new JsonObject();
+ ObjectNode postTime = new ObjectNode(instance);
Time time = getTime(post.get().getTime());
postTime.put("timeText", time.getText());
postTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
postTimes.put(id, postTime);
}
}
- JsonObject replyTimes = new JsonObject();
+ ObjectNode replyTimes = new ObjectNode(instance);
allIds = request.getHttpRequest().getParam("replies");
if (allIds.length() > 0) {
String[] ids = allIds.split(",");
if (!reply.isPresent()) {
continue;
}
- JsonObject replyTime = new JsonObject();
+ ObjectNode replyTime = new ObjectNode(instance);
Time time = getTime(reply.get().getTime());
replyTime.put("timeText", time.getText());
replyTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* Returns the translation for a given key as JSON object.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String key = request.getHttpRequest().getParam("key");
String translation = webInterface.getL10n().getString(key);
return createSuccessJsonObject().put("value", translation);
--- /dev/null
+/*
+ * © 2013 xplosion interactive
+ */
+
+package net.pterodactylus.sone.web.ajax;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+/**
+ * {@link JsonReturnObject} that signals an error has occured.
+ *
+ * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
+ */
+public class JsonErrorReturnObject extends JsonReturnObject {
+
+ /** The error that has occured. */
+ @JsonProperty
+ private final String error;
+
+ /**
+ * Creates a new error JSON return object.
+ *
+ * @param error
+ * The error that occured
+ */
+ public JsonErrorReturnObject(String error) {
+ super(false);
+ this.error = error;
+ }
+
+}
import net.pterodactylus.sone.web.page.FreenetPage;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.io.Closer;
-import net.pterodactylus.util.json.JsonObject;
-import net.pterodactylus.util.json.JsonUtils;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.web.Page;
import net.pterodactylus.util.web.Response;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
import freenet.clients.http.SessionManager.Session;
import freenet.clients.http.ToadletContext;
/** The logger. */
private static final Logger logger = Logging.getLogger(JsonPage.class);
+ /** The JSON serializer. */
+ private static final ObjectMapper objectMapper = new ObjectMapper();
+
/** The path of the page. */
private final String path;
* The request to handle
* @return The created JSON object
*/
- protected abstract JsonObject createJsonObject(FreenetRequest request);
+ protected abstract JsonReturnObject createJsonObject(FreenetRequest request);
/**
* Returns whether this command needs the form password for authentication
*
* @return A reply signaling success
*/
- protected static JsonObject createSuccessJsonObject() {
- return new JsonObject().put("success", true);
+ protected static JsonReturnObject createSuccessJsonObject() {
+ return new JsonReturnObject(true);
}
/**
* The error that has occured
* @return The JSON object, signalling failure and the error code
*/
- protected static JsonObject createErrorJsonObject(String error) {
- return new JsonObject().put("success", false).put("error", error);
+ protected static JsonReturnObject createErrorJsonObject(String error) {
+ return new JsonErrorReturnObject(error);
}
//
@Override
public Response handleRequest(FreenetRequest request, Response response) throws IOException {
if (webInterface.getCore().getPreferences().isRequireFullAccess() && !request.getToadletContext().isAllowedFullAccess()) {
- return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
+ return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required")));
}
if (needsFormPassword()) {
String formPassword = request.getHttpRequest().getParam("formPassword");
if (!webInterface.getFormPassword().equals(formPassword)) {
- return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
+ return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required")));
}
}
if (requiresLogin()) {
if (getCurrentSone(request.getToadletContext(), false) == null) {
- return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
+ return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required")));
}
}
try {
- JsonObject jsonObject = createJsonObject(request);
- return response.setStatusCode(200).setStatusText("OK").setContentType("application/json").write(JsonUtils.format(jsonObject));
+ JsonReturnObject jsonObject = createJsonObject(request);
+ return response.setStatusCode(200).setStatusText("OK").setContentType("application/json").write(objectMapper.writeValueAsString(jsonObject));
} catch (Exception e1) {
logger.log(Level.WARNING, "Error executing JSON page!", e1);
return response.setStatusCode(500).setStatusText(e1.getMessage()).setContentType("text/plain").write(dumpStackTrace(e1));
--- /dev/null
+/*
+ * © 2013 xplosion interactive
+ */
+
+package net.pterodactylus.sone.web.ajax;
+
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonUnwrapped;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.BooleanNode;
+import com.fasterxml.jackson.databind.node.IntNode;
+import com.fasterxml.jackson.databind.node.TextNode;
+import com.google.common.collect.Maps;
+
+/**
+ * JSON return object for AJAX requests.
+ *
+ * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
+ */
+public class JsonReturnObject {
+
+ /** Whether the request was successful. */
+ @JsonProperty
+ private final boolean success;
+
+ /** The returned values. */
+ @JsonUnwrapped
+ private final Map<String, JsonNode> content = Maps.newHashMap();
+
+ /**
+ * Creates a new JSON return object.
+ *
+ * @param success
+ * {@code true} if the request was successful, {@code false} otherwise
+ */
+ public JsonReturnObject(boolean success) {
+ this.success = success;
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Stores the given value under the given key.
+ *
+ * @param key
+ * The key under which to store the value
+ * @param value
+ * The value to store
+ * @return This JSON return object
+ */
+ public JsonReturnObject put(String key, boolean value) {
+ return put(key, BooleanNode.valueOf(value));
+ }
+
+ /**
+ * Stores the given value under the given key.
+ *
+ * @param key
+ * The key under which to store the value
+ * @param value
+ * The value to store
+ * @return This JSON return object
+ */
+ public JsonReturnObject put(String key, int value) {
+ return put(key, new IntNode(value));
+ }
+
+ /**
+ * Stores the given value under the given key.
+ *
+ * @param key
+ * The key under which to store the value
+ * @param value
+ * The value to store
+ * @return This JSON return object
+ */
+ public JsonReturnObject put(String key, String value) {
+ return put(key, new TextNode(value));
+ }
+
+ /**
+ * Stores the given value under the given key.
+ *
+ * @param key
+ * The key under which to store the value
+ * @param value
+ * The value to store
+ * @return This JSON return object
+ */
+ public JsonReturnObject put(String key, JsonNode value) {
+ content.put(key, value);
+ return this;
+ }
+
+}
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets the user like a {@link Post}.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String type = request.getHttpRequest().getParam("type", null);
String id = request.getHttpRequest().getParam(type, null);
if ((id == null) || (id.length() == 0)) {
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* Lets the user {@link Core#lockSone(Sone) lock} a {@link Sone}.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String soneId = request.getHttpRequest().getParam("sone");
Sone sone = webInterface.getCore().getLocalSone(soneId, false);
if (sone == null) {
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
import com.google.common.base.Optional;
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String type = request.getHttpRequest().getParam("type");
if (!type.equals("sone") && !type.equals("post") && !type.equals("reply")) {
return createErrorJsonObject("invalid-type");
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets the user move a profile field up or down.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
Sone currentSone = getCurrentSone(request.getToadletContext());
Profile profile = currentSone.getProfile();
String fieldId = request.getHttpRequest().getParam("field");
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets the user trust a Sone.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
Sone currentSone = getCurrentSone(request.getToadletContext(), false);
if (currentSone == null) {
return createErrorJsonObject("auth-required");
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets the user unbookmark a post.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String id = request.getHttpRequest().getParam("post", null);
if ((id == null) || (id.length() == 0)) {
return createErrorJsonObject("invalid-post-id");
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets a Sone unfollow another Sone.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String soneId = request.getHttpRequest().getParam("sone");
if (!webInterface.getCore().getSone(soneId).isPresent()) {
return createErrorJsonObject("invalid-sone-id");
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets the user unlike a {@link Post}.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String type = request.getHttpRequest().getParam("type", null);
String id = request.getHttpRequest().getParam(type, null);
if ((id == null) || (id.length() == 0)) {
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* Lets the user {@link Core#unlockSone(Sone) unlock} a {@link Sone}.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
String soneId = request.getHttpRequest().getParam("sone");
Sone sone = webInterface.getCore().getLocalSone(soneId, false);
if (sone == null) {
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
/**
* AJAX page that lets the user untrust a Sone.
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
Sone currentSone = getCurrentSone(request.getToadletContext(), false);
if (currentSone == null) {
return createErrorJsonObject("auth-required");