diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2013-08-04 17:56:45 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2013-08-04 17:56:45 -0700 |
commit | 86a6d6cdbb46faae969dff1e27b6b52c23012174 (patch) | |
tree | d6286016a331c6e9cd9bd0465b5392c20f4d7138 /src/com/p4square/grow/frontend | |
parent | 52539d7aaba96b7997a3c5a07e4a1ad234af7d04 (diff) |
Adding VideosResource to return JSON blobs of video data for the training page.
Diffstat (limited to 'src/com/p4square/grow/frontend')
-rw-r--r-- | src/com/p4square/grow/frontend/GrowFrontend.java | 1 | ||||
-rw-r--r-- | src/com/p4square/grow/frontend/TrainingPageResource.java | 8 | ||||
-rw-r--r-- | src/com/p4square/grow/frontend/VideosResource.java | 120 |
3 files changed, 124 insertions, 5 deletions
diff --git a/src/com/p4square/grow/frontend/GrowFrontend.java b/src/com/p4square/grow/frontend/GrowFrontend.java index 74cd704..5c49fe2 100644 --- a/src/com/p4square/grow/frontend/GrowFrontend.java +++ b/src/com/p4square/grow/frontend/GrowFrontend.java @@ -77,6 +77,7 @@ public class GrowFrontend extends FMFacade { final Router accountRouter = new Router(getContext()); accountRouter.attach("/assessment/question/{questionId}", SurveyPageResource.class); accountRouter.attach("/assessment", SurveyPageResource.class); + accountRouter.attach("/training/{chapter}/videos/{videoId}.json", VideosResource.class); accountRouter.attach("/training/{chapter}", TrainingPageResource.class); accountRouter.attach("/training", TrainingPageResource.class); diff --git a/src/com/p4square/grow/frontend/TrainingPageResource.java b/src/com/p4square/grow/frontend/TrainingPageResource.java index 6493638..6c89ac9 100644 --- a/src/com/p4square/grow/frontend/TrainingPageResource.java +++ b/src/com/p4square/grow/frontend/TrainingPageResource.java @@ -28,13 +28,11 @@ import net.jesterpm.fmfacade.FreeMarkerPageResource; import com.p4square.grow.config.Config; /** - * SurveyPageResource handles rendering the survey and processing user's answers. + * TrainingPageResource handles rendering the training page. * * This resource expects the user to be authenticated and the ClientInfo User object - * to be populated. Each question is requested from the backend along with the - * user's previous answer. Each answer is sent to the backend and the user is redirected - * to the next question. After the last question the user is sent to his results. - * + * to be populated. + * * @author Jesse Morgan <jesse@jesterpm.net> */ public class TrainingPageResource extends FreeMarkerPageResource { diff --git a/src/com/p4square/grow/frontend/VideosResource.java b/src/com/p4square/grow/frontend/VideosResource.java new file mode 100644 index 0000000..fed315b --- /dev/null +++ b/src/com/p4square/grow/frontend/VideosResource.java @@ -0,0 +1,120 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.frontend; + +import java.util.Map; + +import freemarker.template.Template; + +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Status; +import org.restlet.ext.jackson.JacksonRepresentation; +import org.restlet.representation.Representation; +import org.restlet.resource.ServerResource; + +import org.apache.log4j.Logger; + +import net.jesterpm.fmfacade.json.JsonRequestClient; +import net.jesterpm.fmfacade.json.JsonResponse; + +import com.p4square.grow.config.Config; + +/** + * VideosResource returns JSON blobs with video information and records watched + * videos. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class VideosResource extends ServerResource { + private static Logger cLog = Logger.getLogger(VideosResource.class); + + private Config mConfig; + private JsonRequestClient mJsonClient; + + // Fields pertaining to this request. + private String mChapter; + private String mVideoId; + private String mUserId; + + @Override + public void doInit() { + super.doInit(); + + GrowFrontend growFrontend = (GrowFrontend) getApplication(); + mConfig = growFrontend.getConfig(); + + mJsonClient = new JsonRequestClient(getContext().getClientDispatcher()); + + mChapter = getAttribute("chapter"); + mVideoId = getAttribute("videoId"); + mUserId = getRequest().getClientInfo().getUser().getIdentifier(); + } + + /** + * Fetch a video record from the backend. + */ + @Override + protected Representation get() { + try { + JsonResponse response = backendGet("/training/" + mChapter + "/videos/" + mVideoId); + + if (response.getStatus().isSuccess()) { + return new JacksonRepresentation<Map>(response.getMap()); + + } else { + setStatus(response.getStatus()); + return null; + } + + } catch (Exception e) { + cLog.fatal("Could not render page: " + e.getMessage(), e); + setStatus(Status.SERVER_ERROR_INTERNAL); + return null; + } + } + + /** + * Mark a video as completed. + */ + @Override + protected Representation post(Representation entity) { + return null; + } + + /** + * @return The backend endpoint URI + */ + private String getBackendEndpoint() { + return mConfig.getString("backendUri", "riap://component/backend"); + } + + /** + * Helper method to send a GET to the backend. + */ + private JsonResponse backendGet(final String uri) { + cLog.debug("Sending backend GET " + uri); + + final JsonResponse response = mJsonClient.get(getBackendEndpoint() + uri); + final Status status = response.getStatus(); + if (!status.isSuccess() && !Status.CLIENT_ERROR_NOT_FOUND.equals(status)) { + cLog.warn("Error making backend request for '" + uri + "'. status = " + response.getStatus().toString()); + } + + return response; + } + + private JsonResponse backendPut(final String uri, final Map data) { + cLog.debug("Sending backend PUT " + uri); + + final JsonResponse response = mJsonClient.put(getBackendEndpoint() + uri, data); + final Status status = response.getStatus(); + if (!status.isSuccess() && !Status.CLIENT_ERROR_NOT_FOUND.equals(status)) { + cLog.warn("Error making backend request for '" + uri + "'. status = " + response.getStatus().toString()); + } + + return response; + } +} |