Search in sources :

Example 1 with MessageSender

use of com.google.samples.apps.iosched.server.gcm.device.MessageSender in project iosched by google.

the class SendMessageServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Extract URL components
    String result = req.getPathInfo();
    if (result == null) {
        send(resp, 400, "Bad request (check request format)");
        return;
    }
    String[] components = result.split("/");
    if (components.length != 3) {
        send(resp, 400, "Bad request (check request format)");
        return;
    }
    String target = components[1];
    String action = components[2];
    // Let's see what this user is authorized to do
    AuthInfo authInfo = AuthHelper.processAuthorization(req);
    // If no auth info or non-admin trying to run non-whitelisted actions, no access.
    if (authInfo == null || action == null || (!UNPRIVILEGED_ACTIONS.contains(action) && !authInfo.permAdmin)) {
        send(resp, 403, "Not authorized");
        return;
    }
    // Extract extraData
    String payload = readBody(req);
    // Request decoding complete. Log request parameters
    LOG.info("Authorized User: " + authInfo.clientName + "\nTarget: " + target + "\nAction: " + action + "\nExtra Data: " + payload);
    MessageSender sender = new MessageSender(getServletConfig());
    // what's the audience of the message?
    if ("global".equals(target)) {
        // Only admins can spam the world
        if (!authInfo.permAdmin) {
            LOG.info("Attempt to send global message, but no admin perm.");
            send(resp, 403, "Not authorized");
            return;
        }
        List<Device> allDevices = DeviceStore.getAllDevices();
        if (allDevices == null || allDevices.isEmpty()) {
            send(resp, 404, "No devices registered");
        } else {
            int resultCount = allDevices.size();
            LOG.info("Selected " + resultCount + " devices");
            sender.multicastSend(allDevices, action, payload);
            send(resp, 200, "Message queued: " + resultCount + " devices");
        }
    } else {
        // If target is SELF, the GCM Group ID will be the auth key in header
        if (SELF.equals(target)) {
            // do we have permission to send message to self?
            if (!authInfo.permSendSelfMessage) {
                LOG.info("Attempt to send self message, but no self message perm.");
                send(resp, 403, "Not authorized");
                return;
            }
            // the target is the auth key (it represents the group)
            target = authInfo.authKey;
        } else {
            // sending message to a specific user. Only admin can do it.
            if (!authInfo.permAdmin) {
                LOG.info("Attempt to send message to specific target, but no admin perm.");
                send(resp, 403, "Not authorized");
                return;
            }
        }
        List<Device> userDevices = DeviceStore.findDevicesByGcmGroupId(target);
        if (userDevices == null || userDevices.isEmpty()) {
            send(resp, 404, "User not found");
        } else {
            int resultCount = userDevices.size();
            LOG.info("Selected " + resultCount + " devices");
            sender.multicastSend(userDevices, action, payload);
            send(resp, 200, "Message queued: " + resultCount + " devices");
        }
    }
}
Also used : AuthInfo(com.google.samples.apps.iosched.server.gcm.AuthHelper.AuthInfo) MessageSender(com.google.samples.apps.iosched.server.gcm.device.MessageSender) Device(com.google.samples.apps.iosched.server.gcm.db.models.Device)

Aggregations

AuthInfo (com.google.samples.apps.iosched.server.gcm.AuthHelper.AuthInfo)1 Device (com.google.samples.apps.iosched.server.gcm.db.models.Device)1 MessageSender (com.google.samples.apps.iosched.server.gcm.device.MessageSender)1