private JsonObject updateStatus(
      JsonObject notif, HashMap<String, JsonObject> response, JsonObject multicastResult) {
    JsonArray returned = multicastResult.getArray("results");
    JsonArray regIds = notif.getArray("registration_ids");
    // should never happen, unless there is a flaw in gcm algorithm
    if (returned.size() != regIds.size()) {
      throw new RuntimeException(
          "Internal error: sizes do not match. regIds: "
              + regIds.size()
              + "; returned: "
              + returned.size());
    }

    JsonArray reTryRegIds = new JsonArray();
    for (int i = returned.size() - 1; i >= 0; i--) {
      response.put((String) regIds.get(i), (JsonObject) returned.get(i));
      boolean resend = doResubmit((JsonObject) returned.get(i));
      if (resend) {
        reTryRegIds.addString((String) regIds.get(i));
      }
    }
    notif.putArray("registration_ids", reTryRegIds);

    return notif;
  }
 private JsonObject setDefaults(JsonObject config) {
   config = config.copy();
   // Set the defaults
   if (config.getNumber("session_timeout") == null) {
     config.putNumber("session_timeout", 5l * 1000);
   }
   if (config.getBoolean("insert_JSESSIONID") == null) {
     config.putBoolean("insert_JSESSIONID", true);
   }
   if (config.getNumber("heartbeat_period") == null) {
     config.putNumber("heartbeat_period", 25l * 1000);
   }
   if (config.getNumber("max_bytes_streaming") == null) {
     config.putNumber("max_bytes_streaming", 128 * 1024);
   }
   if (config.getString("prefix") == null) {
     config.putString("prefix", "/");
   }
   if (config.getString("library_url") == null) {
     config.putString("library_url", "http://cdn.sockjs.org/sockjs-0.2.1.min.js");
   }
   if (config.getArray("disabled_transports") == null) {
     config.putArray("disabled_transports", new JsonArray());
   }
   return config;
 }
Exemple #3
0
  @Override
  public void get(String sid, final Handler<JsonObject> callback) {
    sid = this.prefix + sid;

    JsonObject redis = new JsonObject();
    redis.putString("command", "get");
    redis.putArray("args", new JsonArray().add(sid));

    eventBus.send(
        redisAddress,
        redis,
        new Handler<Message<JsonObject>>() {
          @Override
          public void handle(Message<JsonObject> reply) {
            if ("ok".equals(reply.body().getString("status"))) {
              String value = reply.body().getString("value");
              if (value == null || "".equals(value)) {
                callback.handle(null);
                return;
              }
              callback.handle(new JsonObject(value));
            } else {
              callback.handle(null);
            }
          }
        });
  }
  private JsonObject calculateSummary(
      JsonArray regIds, HashMap<String, JsonObject> response, long multicastId) {
    int success = 0;
    int failure = 0;
    int canonicalIds = 0;
    JsonArray deliveries = new JsonArray();
    for (Object regId : regIds) {
      JsonObject result = response.get(regId);
      if (!voidNull(result.getString("message_id")).isEmpty()) {
        success++;
        if (!voidNull(result.getString("registration_id")).isEmpty()) {
          canonicalIds++;
        }
      } else {
        failure++;
      }
      // add results, in the same order as the input
      deliveries.add(result);
    }
    // build a new object with the overall result
    JsonObject reply = new JsonObject();
    reply.putNumber("multicast_id", multicastId);
    reply.putNumber("success", success);
    reply.putNumber("failure", failure);
    reply.putNumber("canonical_ids", canonicalIds);
    reply.putArray("results", deliveries);

    return reply;
  }
Exemple #5
0
  public static String getLogList(String userID) {

    DBCursor cursor =
        collLog
            .find(
                (DBObject)
                    JSON.parse(
                        "{ $or : [ { userID : \"" + userID + "\"}, {users: \"" + userID + "\" }]}"),
                (DBObject) JSON.parse("{ _id : 0, nodeID : 0, users : 0 }"))
            .sort((DBObject) JSON.parse("{ time : -1 }"));

    JsonArray logArray = new JsonArray();

    try {
      while (cursor.hasNext()) {
        String data = cursor.next().toString();
        JsonObject jsonObj = new JsonObject(data);
        System.out.println(data);
        logArray.addObject(jsonObj);
      }
    } finally {
      cursor.close();
    }

    JsonObject logList = new JsonObject();
    logList.putArray("log", logArray);

    return logList.encode();
  }
Exemple #6
0
  @SuppressWarnings("unused")
  private void getKeys(final Handler<JsonArray> next) {
    JsonObject redis = new JsonObject();
    redis.putString("command", "keys");
    redis.putArray("args", new JsonArray().add(prefix + "*"));

    eventBus.send(
        redisAddress,
        redis,
        new Handler<Message<JsonObject>>() {
          @Override
          public void handle(Message<JsonObject> message) {
            if (!"ok".equals(message.body().getString("status"))) {
              next.handle(new JsonArray());
            } else {
              JsonArray keys = message.body().getArray("value");

              JsonArray result = new JsonArray();
              int len = prefix.length();

              for (Object o : keys) {
                String key = (String) o;
                result.add(key.substring(len));
              }

              next.handle(result);
            }
          }
        });
  }
Exemple #7
0
  @Override
  public void all(final Handler<JsonArray> next) {
    JsonObject redis = new JsonObject();
    redis.putString("command", "keys");
    redis.putArray("args", new JsonArray().add(prefix + "*"));

    final JsonArray results = new JsonArray();

    eventBus.send(
        redisAddress,
        redis,
        new Handler<Message<JsonObject>>() {
          @Override
          public void handle(Message<JsonObject> message) {
            if (!"ok".equals(message.body().getString("status"))) {
              next.handle(null);
            } else {
              JsonArray keys = message.body().getArray("value");

              new AsyncIterator<Object>(keys.iterator()) {
                @Override
                public void handle(Object key) {
                  if (hasNext()) {
                    JsonObject redis = new JsonObject();
                    redis.putString("command", "get");
                    redis.putArray("args", new JsonArray().add(key));

                    eventBus.send(
                        redisAddress,
                        redis,
                        new Handler<Message<JsonObject>>() {
                          @Override
                          public void handle(Message<JsonObject> message) {
                            if (!"ok".equals(message.body().getString("status"))) {
                              next.handle(null);
                            } else {
                              String value = message.body().getString("value");
                              if (value != null) {
                                results.add(new JsonObject(value));
                              }
                              next();
                            }
                          }
                        });
                  } else {
                    next.handle(results);
                  }
                }
              };
            }
          }
        });
  }
Exemple #8
0
  @Test
  public void testJsonArrayToClone() {
    JsonArray array = new JsonArray();
    array.add("test");
    JsonObject object = new JsonObject();
    object.putArray("array", array);

    // want to clone
    JsonObject object2 = new JsonObject(object.toMap());
    // this shouldn't throw an exception, it does before patch
    JsonArray array2 = object2.getArray("array");
  }
Exemple #9
0
  public static void logSANodeValue(String nodeID, String nodeName, String nodeValue) {
    JsonObject jsonObj = new JsonObject();
    Long timestamp = System.currentTimeMillis();

    jsonObj.putNumber("time", timestamp);
    jsonObj.putString("type", "SANode");
    jsonObj.putString("nodeID", nodeID);
    jsonObj.putString("nodeName", nodeName);
    jsonObj.putObject("data", new JsonObject(nodeValue));
    jsonObj.putArray("users", AccountManager.getRegisteredUserIDList(nodeID));

    collLog.insert(jsonToDBObject(jsonObj));
  }
Exemple #10
0
 public synchronized Object[] addJointure(String externalId) {
   if (struct != null) {
     JsonArray joinKey = struct.getArray("joinKey");
     if (joinKey == null) {
       joinKey = new JsonArray();
       struct.putArray("joinKey", joinKey);
     }
     joinKey.add(externalId);
     String query =
         "MATCH (s:Structure {externalId: {externalId}}) " + "SET s.joinKey = {joinKey} ";
     JsonObject params =
         new JsonObject().putArray("joinKey", joinKey).putString("externalId", getExternalId());
     getTransaction().add(query, params);
     return joinKey.toArray();
   }
   return null;
 }
  public void act(HashMap<String, Object> cmd) {
    if (cmd == null) return;

    JsonObject notif = new JsonObject();

    for (String key : cmd.keySet()) {
      Object value = cmd.get(key);

      if (value != null) {
        if (value instanceof byte[]) notif.putBinary(key, (byte[]) value);
        else if (value instanceof Boolean) notif.putBoolean(key, (Boolean) value);
        else if (value instanceof Number) notif.putNumber(key, (Number) value);
        else if (value instanceof String) notif.putString(key, (String) value);
        else if (value instanceof JsonArray) notif.putArray(key, (JsonArray) value);
      }
    }
    System.out.println("sent: \n" + notif.encode());
    push(notif);
  }
Exemple #12
0
  @Override
  public void length(final Handler<Integer> next) {
    JsonObject redis = new JsonObject();
    redis.putString("command", "keys");
    redis.putArray("args", new JsonArray().add(prefix + "*"));

    eventBus.send(
        redisAddress,
        redis,
        new Handler<Message<JsonObject>>() {
          @Override
          public void handle(Message<JsonObject> message) {
            if (!"ok".equals(message.body().getString("status"))) {
              next.handle(0);
            } else {
              JsonArray keys = message.body().getArray("value");
              next.handle(keys.size());
            }
          }
        });
  }
Exemple #13
0
  @Override
  public void destroy(String sid, final Handler<Object> callback) {
    sid = this.prefix + sid;

    JsonObject redis = new JsonObject();
    redis.putString("command", "del");
    redis.putArray("args", new JsonArray().add(sid));

    eventBus.send(
        redisAddress,
        redis,
        new Handler<Message<JsonObject>>() {
          @Override
          public void handle(Message<JsonObject> reply) {
            if ("ok".equals(reply.body().getString("status"))) {
              callback.handle(null);
            } else {
              callback.handle(reply.body().getString("message"));
            }
          }
        });
  }
Exemple #14
0
  @Override
  public void set(String sid, JsonObject sess, final Handler<Object> callback) {
    sid = prefix + sid;

    Integer maxAge = null;

    JsonObject obj = sess.getObject("cookie");
    if (obj != null) {
      maxAge = obj.getInteger("maxAge");
    }

    String session = sess.encode();
    int ttl;

    if (maxAge != null) {
      ttl = maxAge / 1000;
    } else {
      ttl = this.ttl;
    }

    JsonObject redis = new JsonObject();
    redis.putString("command", "setex");
    redis.putArray("args", new JsonArray().add(sid).add(ttl).add(session));

    eventBus.send(
        redisAddress,
        redis,
        new Handler<Message<JsonObject>>() {
          @Override
          public void handle(Message<JsonObject> reply) {
            if ("ok".equals(reply.body().getString("status"))) {
              callback.handle(null);
            } else {
              callback.handle(reply.body().getString("message"));
            }
          }
        });
  }
Exemple #15
0
 public void addAttachment() {
   JsonArray functionalAttachment = struct.getArray("functionalAttachment");
   if (functionalAttachment != null
       && functionalAttachment.size() > 0
       && !externalId.equals(functionalAttachment.get(0))) {
     JsonObject params = new JsonObject().putString("externalId", externalId);
     String query;
     if (functionalAttachment.size() == 1) {
       query =
           "MATCH (s:Structure { externalId : {externalId}}), "
               + "(ps:Structure { externalId : {functionalAttachment}}) "
               + "CREATE UNIQUE s-[:HAS_ATTACHMENT]->ps";
       params.putString("functionalAttachment", (String) functionalAttachment.get(0));
     } else {
       query =
           "MATCH (s:Structure { externalId : {externalId}}), (ps:Structure) "
               + "WHERE ps.externalId IN {functionalAttachment} "
               + "CREATE UNIQUE s-[:HAS_ATTACHMENT]->ps";
       params.putArray("functionalAttachment", functionalAttachment);
     }
     getTransaction().add(query, params);
   }
 }