summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/frontend
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/p4square/grow/frontend')
-rw-r--r--src/com/p4square/grow/frontend/GrowFrontend.java8
-rw-r--r--src/com/p4square/grow/frontend/JsonRequestProvider.java64
-rw-r--r--src/com/p4square/grow/frontend/SurveyPageResource.java25
3 files changed, 82 insertions, 15 deletions
diff --git a/src/com/p4square/grow/frontend/GrowFrontend.java b/src/com/p4square/grow/frontend/GrowFrontend.java
index 327554e..6a74bda 100644
--- a/src/com/p4square/grow/frontend/GrowFrontend.java
+++ b/src/com/p4square/grow/frontend/GrowFrontend.java
@@ -156,10 +156,10 @@ public class GrowFrontend extends FMFacade {
// Static content
try {
- component.getDefaultHost().attach("/images/", new FileServingApp("./build/images/"));
- component.getDefaultHost().attach("/scripts", new FileServingApp("./build/scripts"));
- component.getDefaultHost().attach("/style.css", new FileServingApp("./build/style.css"));
- component.getDefaultHost().attach("/favicon.ico", new FileServingApp("./build/favicon.ico"));
+ component.getDefaultHost().attach("/images/", new FileServingApp("./build/root/images/"));
+ component.getDefaultHost().attach("/scripts", new FileServingApp("./build/root/scripts"));
+ component.getDefaultHost().attach("/style.css", new FileServingApp("./build/root/style.css"));
+ component.getDefaultHost().attach("/favicon.ico", new FileServingApp("./build/root/favicon.ico"));
} catch (IOException e) {
LOG.error("Could not create directory for static resources: "
+ e.getMessage(), e);
diff --git a/src/com/p4square/grow/frontend/JsonRequestProvider.java b/src/com/p4square/grow/frontend/JsonRequestProvider.java
new file mode 100644
index 0000000..c372251
--- /dev/null
+++ b/src/com/p4square/grow/frontend/JsonRequestProvider.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.grow.frontend;
+
+import java.io.IOException;
+
+import org.restlet.Request;
+import org.restlet.Response;
+import org.restlet.Restlet;
+import org.restlet.data.Method;
+import org.restlet.data.Status;
+import org.restlet.representation.Representation;
+import org.restlet.representation.StringRepresentation;
+
+import com.p4square.grow.provider.JsonEncodedProvider;
+
+/**
+ * Fetch a JSON object via a Request.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public class JsonRequestProvider<V> extends JsonEncodedProvider<String, V> {
+
+ private final Restlet mDispatcher;
+
+ public JsonRequestProvider(Restlet dispatcher, Class<V> clazz) {
+ super(clazz);
+
+ mDispatcher = dispatcher;
+ }
+
+ @Override
+ public V get(String url) throws IOException {
+ Request request = new Request(Method.GET, url);
+ Response response = mDispatcher.handle(request);
+ Representation representation = response.getEntity();
+
+ if (!response.getStatus().isSuccess()) {
+ if (representation != null) {
+ representation.release();
+ }
+
+ throw new IOException("Could not get object. " + response.getStatus());
+ }
+
+ return decode(representation.getText());
+ }
+
+ @Override
+ public void put(String url, V obj) throws IOException {
+ final Request request = new Request(Method.PUT, url);
+ request.setEntity(new StringRepresentation(encode(obj)));
+
+ final Response response = mDispatcher.handle(request);
+
+ if (!response.getStatus().isSuccess()) {
+ throw new IOException("Could not put object. " + response.getStatus());
+ }
+
+ }
+
+}
diff --git a/src/com/p4square/grow/frontend/SurveyPageResource.java b/src/com/p4square/grow/frontend/SurveyPageResource.java
index 415b46c..f864014 100644
--- a/src/com/p4square/grow/frontend/SurveyPageResource.java
+++ b/src/com/p4square/grow/frontend/SurveyPageResource.java
@@ -4,6 +4,8 @@
package com.p4square.grow.frontend;
+import java.io.IOException;
+
import java.util.Map;
import java.util.HashMap;
@@ -27,6 +29,8 @@ import com.p4square.fmfacade.FreeMarkerPageResource;
import com.p4square.grow.config.Config;
import com.p4square.grow.model.Question;
+import com.p4square.grow.provider.QuestionProvider;
+import com.p4square.grow.provider.Provider;
/**
* SurveyPageResource handles rendering the survey and processing user's answers.
@@ -44,6 +48,7 @@ public class SurveyPageResource extends FreeMarkerPageResource {
private Config mConfig;
private Template mSurveyTemplate;
private JsonRequestClient mJsonClient;
+ private Provider<String, Question> mQuestionProvider;
// Fields pertaining to this request.
private String mQuestionId;
@@ -62,6 +67,12 @@ public class SurveyPageResource extends FreeMarkerPageResource {
}
mJsonClient = new JsonRequestClient(getContext().getClientDispatcher());
+ mQuestionProvider = new QuestionProvider<String>(new JsonRequestProvider<Question>(getContext().getClientDispatcher(), Question.class)) {
+ @Override
+ public String makeKey(String questionId) {
+ return getBackendEndpoint() + "/assessment/question/" + questionId;
+ }
+ };
mQuestionId = getAttribute("questionId");
mUserId = getRequest().getClientInfo().getUser().getIdentifier();
@@ -102,7 +113,7 @@ public class SurveyPageResource extends FreeMarkerPageResource {
String selectedAnswer = getAnswer(mQuestionId);
Map root = getRootObject();
- root.put("question", question.getMap());
+ root.put("question", question);
root.put("selectedAnswerId", selectedAnswer);
// Get the question count and compute progress
@@ -214,17 +225,9 @@ public class SurveyPageResource extends FreeMarkerPageResource {
private Question getQuestion(String id) {
try {
- Map<?, ?> questionData = null;
-
- JsonResponse response = backendGet("/assessment/question/" + id);
- if (!response.getStatus().isSuccess()) {
- return null;
- }
- questionData = response.getMap();
+ return mQuestionProvider.get(id);
- return new Question((Map<String, Object>) questionData);
-
- } catch (ClientException e) {
+ } catch (IOException e) {
LOG.warn("Error fetching question.", e);
return null;
}