summaryrefslogtreecommitdiff
path: root/src/com/p4square/fmfacade/json/JsonRequestClient.java
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2014-03-31 22:35:43 -0700
committerJesse Morgan <jesse@jesterpm.net>2014-03-31 22:35:43 -0700
commit38c12cf70ef4714a7fc508f7fbaf44487ea971b7 (patch)
tree59d29b82f65952653f789615db1003b431be607a /src/com/p4square/fmfacade/json/JsonRequestClient.java
parentcfb2c5ef6582e51ae9cfdfff35e12b5b7fdc24fb (diff)
Locking down restlet library version.
While trying to fix this issue, I also moved FMFacade into this package and fixed a couple bugs that snuck into the last commit.
Diffstat (limited to 'src/com/p4square/fmfacade/json/JsonRequestClient.java')
-rw-r--r--src/com/p4square/fmfacade/json/JsonRequestClient.java109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/com/p4square/fmfacade/json/JsonRequestClient.java b/src/com/p4square/fmfacade/json/JsonRequestClient.java
new file mode 100644
index 0000000..19a394f
--- /dev/null
+++ b/src/com/p4square/fmfacade/json/JsonRequestClient.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.fmfacade.json;
+
+import java.util.Map;
+
+import java.io.IOException;
+
+import org.apache.log4j.Logger;
+
+import org.restlet.data.Status;
+import org.restlet.data.Method;
+import org.restlet.representation.Representation;
+import org.restlet.Request;
+import org.restlet.Response;
+import org.restlet.Restlet;
+
+import org.restlet.ext.jackson.JacksonRepresentation;
+
+/**
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public class JsonRequestClient {
+ private final Restlet mDispatcher;
+
+ public JsonRequestClient(Restlet dispatcher) {
+ mDispatcher = dispatcher;
+ }
+
+ /**
+ * Perform a GET request for the given URI and parse the response as a
+ * JSON map.
+ *
+ * @return A JsonResponse object which can be used to retrieve the
+ * response as a JSON map.
+ */
+ public JsonResponse get(final String uri) {
+ final Request request = new Request(Method.GET, uri);
+ final Response response = mDispatcher.handle(request);
+
+ return new JsonResponse(response);
+ }
+
+ /**
+ * Perform a PUT request for the given URI and parse the response as a
+ * JSON map.
+ *
+ * @return A JsonResponse object which can be used to retrieve the
+ * response as a JSON map.
+ */
+ public JsonResponse put(final String uri, Representation entity) {
+ final Request request = new Request(Method.PUT, uri);
+ request.setEntity(entity);
+
+ final Response response = mDispatcher.handle(request);
+ return new JsonResponse(response);
+ }
+
+ /**
+ * Perform a PUT request for the given URI and parse the response as a
+ * JSON map.
+ *
+ * @return A JsonResponse object which can be used to retrieve the
+ * response as a JSON map.
+ */
+ public JsonResponse put(final String uri, Map map) {
+ return put(uri, new JacksonRepresentation<Map>(map));
+ }
+
+ /**
+ * Perform a POST request for the given URI and parse the response as a
+ * JSON map.
+ *
+ * @return A JsonResponse object which can be used to retrieve the
+ * response as a JSON map.
+ */
+ public JsonResponse post(final String uri, Representation entity) {
+ final Request request = new Request(Method.POST, uri);
+ request.setEntity(entity);
+
+ final Response response = mDispatcher.handle(request);
+ return new JsonResponse(response);
+ }
+
+ /**
+ * Perform a POST request for the given URI and parse the response as a
+ * JSON map.
+ *
+ * @return A JsonResponse object which can be used to retrieve the
+ * response as a JSON map.
+ */
+ public JsonResponse post(final String uri, Map map) {
+ return post(uri, new JacksonRepresentation<Map>(map));
+ }
+
+ /**
+ * Perform a DELETE request for the given URI.
+ *
+ * @return A JsonResponse object with the status of the request.
+ */
+ public JsonResponse delete(final String uri) {
+ final Request request = new Request(Method.DELETE, uri);
+ final Response response = mDispatcher.handle(request);
+ return new JsonResponse(response);
+ }
+}