summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/backend/feed/ThreadResource.java
blob: e8f46c298d2a5d8553ac62f3a526ad5dc50650de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright 2013 Jesse Morgan
 */

package com.p4square.grow.backend.feed;

import java.io.IOException;

import java.util.Date;
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.Message;

/**
 * ThreadResource manages the messages that make up a thread.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
public class ThreadResource extends ServerResource {
    private static final Logger LOG = Logger.getLogger(ThreadResource.class);

    private FeedDataProvider mBackend;
    private String mTopic;
    private String mThreadId;

    @Override
    public void doInit() {
        super.doInit();

        mBackend = (FeedDataProvider) getApplication();
        mTopic = getAttribute("topic");
        mThreadId = getAttribute("thread");
    }

    /**
     * GET a list of messages in a thread.
     */
    @Override
    protected Representation get() {
        // If the topic or threadId are missing, return a 404.
        if (mTopic == null || mTopic.length() == 0 ||
                mThreadId == null || mThreadId.length() == 0) {
            setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return null;
        }

        // TODO: Support limit query parameter.

        try {
            String collectionKey = mTopic + "/" + mThreadId;
            Map<String, Message> messages = mBackend.getMessageProvider().query(collectionKey);
            return new JacksonRepresentation(messages.values());

        } catch (IOException e) {
            LOG.error("Unexpected exception: " + e.getMessage(), e);
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return null;
        }
    }

    /**
     * POST a new message to the thread.
     */
    @Override
    protected Representation post(Representation entity) {
        // If the topic and thread are not provided, respond with not allowed.
        // TODO: Check if the thread exists.
        if (mTopic == null || !mBackend.TOPICS.contains(mTopic) ||
                mThreadId == null || mThreadId.length() == 0) {
            setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
            return null;
        }

        try {
            JacksonRepresentation<Message> jsonRep = new JacksonRepresentation<Message>(entity, Message.class);
            Message message = jsonRep.getObject();

            // Force the thread id and message to be what we expect.
            message.setThreadId(mThreadId);
            message.setId(Message.generateId());

            if (message.getCreated() == null) {
                message.setCreated(new Date());
            }

            String collectionKey = mTopic + "/" + mThreadId;
            mBackend.getMessageProvider().put(collectionKey, message.getId(), message);

            setLocationRef(mThreadId + "/" + message.getId());
            return new JacksonRepresentation(message);

        } catch (IOException e) {
            LOG.error("Unexpected exception: " + e.getMessage(), e);
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return null;
        }
    }
}