From 5306005833083589d0f93b0966b458da969e5f38 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Tue, 10 Sep 2013 00:06:54 -0700 Subject: Adding redirect page /account. /account redirects to either the assessment or the training page, as appropriate. This change adds the /accounts/UID backend resource, which simply stores an arbitrary json document for now. --- .../grow/backend/resources/AccountResource.java | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/com/p4square/grow/backend/resources/AccountResource.java (limited to 'src/com/p4square/grow/backend/resources') diff --git a/src/com/p4square/grow/backend/resources/AccountResource.java b/src/com/p4square/grow/backend/resources/AccountResource.java new file mode 100644 index 0000000..f3404c0 --- /dev/null +++ b/src/com/p4square/grow/backend/resources/AccountResource.java @@ -0,0 +1,71 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.backend.resources; + +import java.io.IOException; + +import org.restlet.data.Status; +import org.restlet.resource.ServerResource; +import org.restlet.representation.Representation; +import org.restlet.representation.StringRepresentation; + +import org.apache.log4j.Logger; + +import com.p4square.grow.backend.GrowBackend; +import com.p4square.grow.backend.db.CassandraDatabase; + +/** + * Stores a document about a user. + * + * @author Jesse Morgan + */ +public class AccountResource extends ServerResource { + private static final Logger LOG = Logger.getLogger(AccountResource.class); + + private CassandraDatabase mDb; + + private String mUserId; + + @Override + public void doInit() { + super.doInit(); + + final GrowBackend backend = (GrowBackend) getApplication(); + mDb = backend.getDatabase(); + + mUserId = getAttribute("userId"); + } + + /** + * Handle GET Requests. + */ + @Override + protected Representation get() { + String result = mDb.getKey("accounts", mUserId); + + if (result == null) { + setStatus(Status.CLIENT_ERROR_NOT_FOUND); + return null; + } + + return new StringRepresentation(result); + } + + /** + * Handle PUT requests + */ + @Override + protected Representation put(Representation entity) { + try { + mDb.putKey("accounts", mUserId, entity.getText()); + setStatus(Status.SUCCESS_NO_CONTENT); + + } catch (IOException e) { + setStatus(Status.SERVER_ERROR_INTERNAL); + } + + return null; + } +} -- cgit v1.2.3