예제 #1
0
  /**
   * Push a cache entry value out onto the websocket channel (to the browser).
   *
   * @param key The cache entry key whose value is to be pushed to the browser.
   * @param cache The cache containing the key.
   * @param socket The channel context associated with the browser websocket channel..
   */
  public static void pushCacheValue(
      String key, Cache<Object, Object> cache, WebSocketChannel socket) {
    Object value = cache.get(key);

    JsonObject responseObject = toJSON(key, value, cache.getName());

    // Write the JSON response out onto the channel...
    WebSockets.sendText(responseObject.toString(), socket, null);

    // socket. channel().writeAndFlush(new TextWebSocketFrame(responseObject.toString()));
  }
예제 #2
0
  /**
   * Push a cache entry value out onto the websocket channel (to the browser).
   *
   * @param key The cache entry key whose value is to be pushed to the browser.
   * @param cache The cache containing the key.
   * @param ctx The channel context associated with the browser websocket channel..
   */
  public static void pushCacheValue(
      String key, Cache<Object, Object> cache, ChannelHandlerContext ctx) {
    Object value = cache.get(key);

    JsonObject responseObject = toJSON(key, value, cache.getName());

    // Write the JSON response out onto the channel...
    ctx.channel()
        .writeAndFlush(
            new TextWebSocketFrame(responseObject.toString()), ctx.channel().voidPromise());
  }
예제 #3
0
 private static JsonObject getJsonObject(Object value) {
   JsonObject valueObject;
   try {
     valueObject = JsonObject.fromObject(value);
   } catch (JsonConversionException e) {
     throw logger.unableToGetFieldsFromObject(e);
   }
   return valueObject;
 }
예제 #4
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;
  }
예제 #5
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());
 }
예제 #6
0
 public JsonPayloadAssertion hasFields(String key, String value) {
   assertTrue(assertedNode.containsKey(key));
   assertEquals(value, assertedNode.get(key));
   return this;
 }
예제 #7
0
 public JsonPayloadAssertion hasMimeType(String mimeType) {
   assertEquals(mimeType, assertedNode.get(OpHandler.MIME));
   return this;
 }
예제 #8
0
 public JsonPayloadAssertion hasValue(Object value) {
   assertEquals(value, assertedNode.get(OpHandler.VALUE));
   return this;
 }
예제 #9
0
 public JsonPayloadAssertion hasKey(String key) {
   assertEquals(key, assertedNode.get(OpHandler.KEY));
   return this;
 }
예제 #10
0
 public JsonPayloadAssertion hasCacheName(String cacheName) {
   assertEquals(cacheName, assertedNode.get(OpHandler.CACHE_NAME));
   return this;
 }