Esempio n. 1
0
  @Action("msgpush")
  public String msgpush() throws Exception {
    BufferedReader r = ServletActionContext.getRequest().getReader();
    String ret = null;
    StringBuffer sb = new StringBuffer();
    String s;
    while ((s = r.readLine()) != null) {
      sb.append(s);
    }
    DBObject req = (DBObject) JSON.parse(sb.toString());
    DBObject head = (DBObject) req.get("head");
    timestamp = (long) head.get("timestamp");
    clientid = String.valueOf(head.get("clientid"));
    userenc = String.valueOf(head.get("userenc"));
    String touser = String.valueOf(head.get("touser"));
    DB db = MongoUtil.getInstance().getDB();
    if (checkTime(timestamp)) {
      if (checkenc(db, timestamp, clientid, userenc)) {
        if (checkmsgsum(req.get("data"), touser, userenc, String.valueOf(head.get("checksum")))) {
          Cache cache = CacheManager.getInstance().getCache("MsgCheck");
          if (cache.get(head.get("checksum")) == null) {
            cache.put(new Element(head.get("checksum"), null));
            DBObject user =
                db.getCollection("Bindings")
                    .findOne(
                        new BasicDBObject(
                            "binds",
                            new BasicDBObject("$elemMatch", new BasicDBObject("uisid", touser))));
            if (!CommonUtil.isEmpty(user) && !CommonUtil.isEmpty(user.get("openid"))) {
              // template白名单
              if (Config.getInstance()
                          .get("push.whitelist")
                          .indexOf(head.get("template").toString())
                      >= 0
                  || db.getCollection("Books")
                          .findOne(
                              new BasicDBObject("openid", user.get("openid"))
                                  .append("item", head.get("template"))
                                  .append("book", true))
                      != null) {
                String cret =
                    TemplateMessage.send(
                        String.valueOf(head.get("template")),
                        String.valueOf(user.get("openid")),
                        (DBObject) req.get("data"));
                if (cret != null && cret.startsWith("{")) {
                  DBObject retobj = (DBObject) JSON.parse(cret);
                  retobj.put("touser", touser);
                  retobj.put("timestamp", timestamp);
                  retobj.put("clientid", clientid);
                  db.getCollection("Pushmsgs").save(retobj);
                  ret = cret;
                } else errormsg = cret;
              } else {
                errormsg = "Message not booked";
              }
            } else {
              errormsg = "Touser not binded";
            }

          } else {
            errormsg = "Same message is sent too frequently";
          }
        } else {
          errormsg = "Message checksum error";
        }

      } else {
        errormsg = "User not authorized";
      }
    } else errormsg = "Timestamp outof range";

    HttpServletResponse resp = ServletActionContext.getResponse();
    resp.setCharacterEncoding("utf-8");
    resp.setContentType("application/json");
    if (!CommonUtil.isEmpty(ret)) resp.getWriter().print(ret);
    else resp.getWriter().write("{\"errcode\":50000,\"errmsg\":\"" + errormsg + "\"}");

    return NONE;
  }