summaryrefslogtreecommitdiff
path: root/src/main/java/com/p4square/fmfacade/json/JsonResponse.java
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2016-04-09 15:53:24 -0700
committerJesse Morgan <jesse@jesterpm.net>2016-04-09 15:53:24 -0700
commit371ccae3d1f31ec38f4af77fb7fcd175d49b3cd5 (patch)
tree38c4f1e8828f9af9c4b77a173bee0d312b321698 /src/main/java/com/p4square/fmfacade/json/JsonResponse.java
parentbbf907e51dfcf157bdee24dead1d531122aa25db (diff)
parent3102d8bce3426d9cf41aeaf201c360d342677770 (diff)
Merge pull request #10 from PuyallupFoursquare/maven
Switching from Ivy+Ant to Maven.
Diffstat (limited to 'src/main/java/com/p4square/fmfacade/json/JsonResponse.java')
-rw-r--r--src/main/java/com/p4square/fmfacade/json/JsonResponse.java87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/main/java/com/p4square/fmfacade/json/JsonResponse.java b/src/main/java/com/p4square/fmfacade/json/JsonResponse.java
new file mode 100644
index 0000000..b9cb587
--- /dev/null
+++ b/src/main/java/com/p4square/fmfacade/json/JsonResponse.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.fmfacade.json;
+
+import java.util.Map;
+
+import java.io.IOException;
+
+import org.restlet.data.Status;
+import org.restlet.data.Reference;
+import org.restlet.representation.Representation;
+import org.restlet.Response;
+
+import org.restlet.ext.jackson.JacksonRepresentation;
+
+/**
+ * JsonResponse wraps a Restlet Response object and parses the entity, if any,
+ * as a JSON map.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public class JsonResponse {
+ private final Response mResponse;
+ private final Representation mRepresentation;
+
+ private Map<String, Object> mMap;
+
+ JsonResponse(Response response) {
+ mResponse = response;
+ mRepresentation = response.getEntity();
+ mMap = null;
+
+ if (!response.getStatus().isSuccess()) {
+ if (mRepresentation != null) {
+ mRepresentation.release();
+ }
+ }
+ }
+
+ /**
+ * @return the Status info from the response.
+ */
+ public Status getStatus() {
+ return mResponse.getStatus();
+ }
+
+ /**
+ * @return the Reference for a redirect.
+ */
+ public Reference getRedirectLocation() {
+ return mResponse.getLocationRef();
+ }
+
+ /**
+ * Return the parsed json map from the response.
+ */
+ public Map<String, Object> getMap() throws ClientException {
+ if (mMap == null) {
+ Representation representation = mRepresentation;
+
+ // Parse response
+ if (representation == null) {
+ return null;
+ }
+
+ JacksonRepresentation<Map> mapRepresentation;
+ if (representation instanceof JacksonRepresentation) {
+ mapRepresentation = (JacksonRepresentation<Map>) representation;
+ } else {
+ mapRepresentation = new JacksonRepresentation<Map>(
+ representation, Map.class);
+ }
+
+ try {
+ mMap = (Map<String, Object>) mapRepresentation.getObject();
+
+ } catch (IOException e) {
+ throw new ClientException("Failed to parse response: " + e.getMessage(), e);
+ }
+ }
+
+ return mMap;
+ }
+
+}