diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2014-02-01 11:18:41 -0800 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2014-02-01 11:18:41 -0800 |
commit | 06dcb39ca435897b082a5d454a11ddb21f230799 (patch) | |
tree | 66bc8c14065c4b947b011c765d677b0b1e2b3c13 /src/com/p4square/grow/backend/feed/TopicResource.java | |
parent | 331f4939eb8f983b5b85663d7549e604ef485b9a (diff) |
Adding the feed backend support.
Diffstat (limited to 'src/com/p4square/grow/backend/feed/TopicResource.java')
-rw-r--r-- | src/com/p4square/grow/backend/feed/TopicResource.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/com/p4square/grow/backend/feed/TopicResource.java b/src/com/p4square/grow/backend/feed/TopicResource.java new file mode 100644 index 0000000..0904baa --- /dev/null +++ b/src/com/p4square/grow/backend/feed/TopicResource.java @@ -0,0 +1,88 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.backend.feed; + +import java.io.IOException; + +import java.util.Map; + +import org.restlet.data.Status; +import org.restlet.resource.ServerResource; +import org.restlet.representation.Representation; + +import org.restlet.ext.jackson.JacksonRepresentation; + +import org.apache.log4j.Logger; + +import com.p4square.grow.model.MessageThread; + +/** + * TopicResource manages the threads contained in a topic. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class TopicResource extends ServerResource { + private static final Logger LOG = Logger.getLogger(TopicResource.class); + + private FeedDataProvider mBackend; + private String mTopic; + + @Override + public void doInit() { + super.doInit(); + + mBackend = (FeedDataProvider) getApplication(); + mTopic = getAttribute("topic"); + } + + /** + * GET a list of threads in the topic. + */ + @Override + protected Representation get() { + // If no topic is provided, return a list of topics. + if (mTopic == null || mTopic.length() == 0) { + return new JacksonRepresentation(FeedDataProvider.TOPICS); + } + + // TODO: Support limit query parameter. + + try { + Map<String, MessageThread> threads = mBackend.getThreadProvider().query(mTopic); + return new JacksonRepresentation(threads.values()); + + } catch (IOException e) { + LOG.error("Unexpected exception: " + e.getMessage(), e); + setStatus(Status.SERVER_ERROR_INTERNAL); + return null; + } + } + + /** + * POST a new thread to the topic. + */ + @Override + protected Representation post(Representation entity) { + // If no topic is provided, respond with not allowed. + if (mTopic == null || !mBackend.TOPICS.contains(mTopic)) { + setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); + return null; + } + + try { + MessageThread newThread = MessageThread.createNew(); + mBackend.getThreadProvider().put(mTopic, newThread.getId(), newThread); + + setStatus(Status.SUCCESS_NO_CONTENT); + setLocationRef(mTopic + "/" + newThread.getId()); + return null; + + } catch (IOException e) { + LOG.error("Unexpected exception: " + e.getMessage(), e); + setStatus(Status.SERVER_ERROR_INTERNAL); + return null; + } + } +} |