summaryrefslogtreecommitdiff
path: root/src/main/java/com/p4square/grow/frontend/FeedResource.java
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2016-04-09 14:22:20 -0700
committerJesse Morgan <jesse@jesterpm.net>2016-04-09 15:48:01 -0700
commit3102d8bce3426d9cf41aeaf201c360d342677770 (patch)
tree38c4f1e8828f9af9c4b77a173bee0d312b321698 /src/main/java/com/p4square/grow/frontend/FeedResource.java
parentbbf907e51dfcf157bdee24dead1d531122aa25db (diff)
Switching from Ivy+Ant to Maven.
Diffstat (limited to 'src/main/java/com/p4square/grow/frontend/FeedResource.java')
-rw-r--r--src/main/java/com/p4square/grow/frontend/FeedResource.java101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/main/java/com/p4square/grow/frontend/FeedResource.java b/src/main/java/com/p4square/grow/frontend/FeedResource.java
new file mode 100644
index 0000000..13d0fa0
--- /dev/null
+++ b/src/main/java/com/p4square/grow/frontend/FeedResource.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2014 Jesse Morgan
+ */
+
+package com.p4square.grow.frontend;
+
+import java.io.IOException;
+
+import org.restlet.data.Form;
+import org.restlet.data.Status;
+import org.restlet.representation.Representation;
+import org.restlet.resource.ServerResource;
+
+import org.apache.log4j.Logger;
+
+import com.p4square.grow.config.Config;
+import com.p4square.grow.model.Message;
+import com.p4square.grow.model.UserRecord;
+
+/**
+ * This resource handles user interactions with the feed.
+ */
+public class FeedResource extends ServerResource {
+ private static final Logger LOG = Logger.getLogger(FeedResource.class);
+
+ private Config mConfig;
+
+ private FeedData mFeedData;
+
+ // Fields pertaining to this request.
+ protected String mTopic;
+ protected String mThread;
+
+ @Override
+ public void doInit() {
+ super.doInit();
+
+ GrowFrontend growFrontend = (GrowFrontend) getApplication();
+ mConfig = growFrontend.getConfig();
+
+ mFeedData = new FeedData(getContext(), mConfig);
+
+ mTopic = getAttribute("topic");
+ if (mTopic != null) {
+ mTopic = mTopic.trim();
+ }
+
+ mThread = getAttribute("thread");
+ if (mThread != null) {
+ mThread = mThread.trim();
+ }
+ }
+
+ /**
+ * Create a new MessageThread.
+ */
+ @Override
+ protected Representation post(Representation entity) {
+ try {
+ if (mTopic == null || mTopic.length() == 0 || !FeedData.TOPICS.contains(mTopic)) {
+ setStatus(Status.CLIENT_ERROR_NOT_FOUND);
+ return ErrorPage.NOT_FOUND;
+ }
+
+ Form form = new Form(entity);
+
+ String question = form.getFirstValue("question");
+
+ Message message = new Message();
+ message.setMessage(question);
+
+ UserRecord user = new UserRecord(getRequest().getClientInfo().getUser());
+ message.setAuthor(user);
+
+ if (mThread != null && mThread.length() != 0) {
+ // Post a response
+ mFeedData.createResponse(mTopic, mThread, message);
+
+ } else {
+ // Post a new thread
+ mFeedData.createThread(mTopic, message);
+ }
+
+ /*
+ * Can't trust the referrer, so we'll send them to the
+ * appropriate part of the training page
+ * TODO: This could be better done.
+ */
+ String nextPage = mConfig.getString("dynamicRoot", "");
+ nextPage += "/account/training/" + mTopic;
+ getResponse().redirectSeeOther(nextPage);
+ return null;
+
+ } catch (IOException e) {
+ LOG.fatal("Could not save message: " + e.getMessage(), e);
+ setStatus(Status.SERVER_ERROR_INTERNAL);
+ return ErrorPage.BACKEND_ERROR;
+
+ }
+ }
+}