me.doshou.admin.maintain.push.web.controller.PushController.java Source code

Java tutorial

Introduction

Here is the source code for me.doshou.admin.maintain.push.web.controller.PushController.java

Source

/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package me.doshou.admin.maintain.push.web.controller;

import com.google.common.collect.Maps;
import me.doshou.admin.maintain.notification.service.NotificationApi;
import me.doshou.admin.maintain.push.service.PushService;
import me.doshou.admin.personal.message.service.MessageApi;
import me.doshou.admin.sys.user.entity.User;
import me.doshou.admin.sys.user.web.bind.annotation.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 1???
 * <p>User: Zhang Kaitao
 * <p>Date: 13-7-16 ?2:08
 * <p>Version: 1.0
 */
@Controller
public class PushController {

    @Autowired
    private MessageApi messageApi;

    @Autowired
    private NotificationApi notificationApi;

    @Autowired
    private PushService pushService;

    /**
     * ?????
     * @return
     */
    @RequestMapping(value = "/admin/polling")
    @ResponseBody
    public Object polling(HttpServletResponse resp, @CurrentUser User user) {
        resp.setHeader("Connection", "Keep-Alive");
        resp.addHeader("Cache-Control", "private");
        resp.addHeader("Pragma", "no-cache");

        Long userId = user.getId();
        if (userId == null) {
            return null;
        }
        //? ?
        if (!pushService.isOnline(userId)) {
            Long unreadMessageCount = messageApi.countUnread(userId);
            List<Map<String, Object>> notifications = notificationApi.topFiveNotification(user.getId());

            Map<String, Object> data = Maps.newHashMap();
            data.put("unreadMessageCount", unreadMessageCount);
            data.put("notifications", notifications);
            pushService.online(userId);
            return data;
        } else {
            //
            return pushService.newDeferredResult(userId);
        }
    }

}