Example #1
0
  /**
   * Cache key, value and cache-name to JSON string.
   *
   * <p>Note that value objects (like String, Numbers, Characters) are not being converted.
   *
   * @param key The cache key.
   * @param value The cache value.
   * @param cacheName The cache name.
   * @return JSON Object representing a cache entry payload for transmission to the browser channel.
   * @throws java.lang.IllegalStateException In case of complex object which can not be converted to
   *     JSON.
   */
  public static JsonObject toJSON(String key, Object value, String cacheName) {
    JsonObject jsonObject = JsonObject.createNew();

    jsonObject.put(OpHandler.CACHE_NAME, cacheName);
    jsonObject.put(OpHandler.KEY, key);

    if (value != null) {
      if (needsJsonConversion(value)) {
        JsonObject valueObject = getJsonObject(value);
        jsonObject.put(OpHandler.VALUE, valueObject.toString());
        jsonObject.put(OpHandler.MIME, "application/json");
      } else {
        jsonObject.put(OpHandler.VALUE, value);
        jsonObject.put(OpHandler.MIME, "text/plain");
      }
    } else {
      jsonObject.put(OpHandler.VALUE, null);
    }

    return jsonObject;
  }
Example #2
0
 public static void pushErrorMessage(String errorMessage, ChannelHandlerContext ctx) {
   JsonObject errorObject = JsonObject.createNew();
   errorObject.put(OpHandler.ERROR, errorMessage);
   ctx.channel()
       .writeAndFlush(new TextWebSocketFrame(errorObject.toString()), ctx.channel().voidPromise());
 }