Beispiel #1
0
  public void send(final WebSocketMessage message, final boolean clearSessionId) {

    // return session status to client
    message.setSessionValid(isAuthenticated());

    // whether to clear the token (all command except LOGIN (for now) should absolutely do this!)
    if (clearSessionId) {

      message.setSessionId(null);
    }

    // set callback
    message.setCallback(callback);
    if (isAuthenticated() || "STATUS".equals(message.getCommand())) {

      String msg = gson.toJson(message, WebSocketMessage.class);

      logger.log(
          Level.FINE,
          "############################################################ SENDING \n{0}",
          msg);

      try {

        session.getRemote().sendString(msg);

      } catch (Throwable t) {
        logger.log(Level.WARNING, "Unable to send websocket message to remote client");
      }

    } else {

      logger.log(Level.WARNING, "NOT sending message to unauthenticated client.");
    }
  }
  @Override
  public WebSocketMessage deserialize(
      JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException {

    WebSocketMessage webSocketData = new WebSocketMessage();

    if (json instanceof JsonObject) {

      JsonObject root = json.getAsJsonObject();
      JsonObject nodeData = root.getAsJsonObject("data");
      JsonObject relData = root.getAsJsonObject("relData");

      if (root.has("command")) {

        webSocketData.setCommand(root.getAsJsonPrimitive("command").getAsString());
      }

      if (root.has("id")) {

        webSocketData.setId(root.getAsJsonPrimitive("id").getAsString());
      }

      if (root.has("pageId")) {

        webSocketData.setPageId(root.getAsJsonPrimitive("pageId").getAsString());
      }

      if (root.has("sessionId")) {

        webSocketData.setSessionId(root.getAsJsonPrimitive("sessionId").getAsString());
      }

      if (root.has("token")) {

        webSocketData.setToken(root.getAsJsonPrimitive("token").getAsString());
      }

      if (root.has("callback")) {

        webSocketData.setCallback(root.getAsJsonPrimitive("callback").getAsString());
      }

      if (root.has("button")) {

        webSocketData.setButton(root.getAsJsonPrimitive("button").getAsString());
      }

      if (root.has("parent")) {

        webSocketData.setParent(root.getAsJsonPrimitive("parent").getAsString());
      }

      if (root.has("view")) {

        webSocketData.setView(root.getAsJsonPrimitive("view").getAsString());
      }

      if (root.has("sort")) {

        webSocketData.setSortKey(root.getAsJsonPrimitive("sort").getAsString());
      }

      if (root.has("order")) {

        webSocketData.setSortOrder(root.getAsJsonPrimitive("order").getAsString());
      }

      if (root.has("pageSize")) {

        webSocketData.setPageSize(root.getAsJsonPrimitive("pageSize").getAsInt());
      }

      if (root.has("page")) {

        webSocketData.setPage(root.getAsJsonPrimitive("page").getAsInt());
      }

      if (nodeData != null) {

        for (Entry<String, JsonElement> entry : nodeData.entrySet()) {

          webSocketData.setNodeData(entry.getKey(), entry.getValue().getAsString());
        }
      }

      if (relData != null) {

        for (Entry<String, JsonElement> entry : relData.entrySet()) {

          webSocketData.setRelData(entry.getKey(), entry.getValue().getAsString());
        }
      }
    }

    return webSocketData;
  }