diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2013-09-10 00:06:54 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2013-09-10 00:06:54 -0700 |
commit | 64c13e3235e32344a0c7c5febbefe802e9102ace (patch) | |
tree | d54ba5593c63b806906533b57345c39a8c344e5c /src/com/p4square/grow/backend/resources | |
parent | 46c3639530d200b70f63994d4016fae37dc2e994 (diff) |
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.
Diffstat (limited to 'src/com/p4square/grow/backend/resources')
-rw-r--r-- | src/com/p4square/grow/backend/resources/AccountResource.java | 71 |
1 files changed, 71 insertions, 0 deletions
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 <jesse@jesterpm.net> + */ +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; + } +} |