Package org.infinispan.server.websocket.json

Examples of org.infinispan.server.websocket.json.JsonObject


    * @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()));
   }
View Full Code Here


    * @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");
         }
View Full Code Here

      return jsonObject;
   }

   private static JsonObject getJsonObject(Object value) {
      JsonObject valueObject;
      try {
         valueObject = JsonObject.fromObject(value);
      } catch (JsonConversionException e) {
         throw logger.unableToGetFieldsFromObject(e);
      }
View Full Code Here

  private void notifyChannels(CacheEntryEvent<Object, Object> event, Event.Type eventType) {
    if(event.isPre()) {
      return;
    }
   
    JsonObject jsonObject;

      Cache<Object, Object> cache = event.getCache();
      Object key = event.getKey();
      Object value;

      switch(eventType) {
      case CACHE_ENTRY_CREATED:
         // TODO: Add optimization ... don't get from cache if non of the channels are interested in creates...
         value = cache.get(key);
         jsonObject = ChannelUtils.toJSON(key.toString(), value, cache.getName());
         break;
      case CACHE_ENTRY_MODIFIED:
         value = event.getValue();
         jsonObject = ChannelUtils.toJSON(key.toString(), value, cache.getName());
         break;
      case CACHE_ENTRY_REMOVED:
         jsonObject = ChannelUtils.toJSON(key.toString(), null, cache.getName());
         break;
      default:
         return;
      }

      jsonObject.put("eventType", eventType.toString());

    String jsonString = jsonObject.toString();
    for(ChannelNotifyParams channel : channels) {
      if(channel.channel.isOpen() && channel.onEvents.contains(eventType)) {
        if(channel.key != null) {
          if(event.getKey().equals(channel.key) || channel.key.equals("*")) {
            channel.channel.writeAndFlush(new TextWebSocketFrame(jsonString));
View Full Code Here

         try {
            ByteBuf binaryData = frame.content();
            if (binaryData == null) {
               return;
            }
            JsonObject payload = JsonObject.fromString(binaryData.toString(CharsetUtil.UTF_8));
            String opCode = (String) payload.get(OpHandler.OP_CODE);
            String cacheName = (String) payload.get(OpHandler.CACHE_NAME);
            Cache<Object, Object> cache = getCache(cacheName);

            OpHandler handler = operationHandlers.get(opCode);
            if (handler != null) {
               handler.handleOp(payload, cache, ctx);
View Full Code Here

   public void shouldReturnJsonObjectOnStringValue() throws Exception {
      //given
      String value = "new val";

      //when
      JsonObject jsonObject = ChannelUtils.toJSON("key", value, "cacheName");

      //then
      assertThat(jsonObject).hasValue("new val");
   }
View Full Code Here

   public void shouldReturnJsonObjectOnNumberValue() throws Exception {
      //given
      Integer value = 1;

      //when
      JsonObject jsonObject = ChannelUtils.toJSON("key", value, "cacheName");

      //then
      assertThat(jsonObject).hasValue(1);
   }
View Full Code Here

   public void shouldReturnJsonObjectOnCharacterValue() throws Exception {
      //given
      Character value = 'a';

      //when
      JsonObject jsonObject = ChannelUtils.toJSON("key", value, "cacheName");

      //then
      assertThat(jsonObject).hasValue('a');
   }
View Full Code Here

      CustomValue value = new CustomValue();
      value.field1 = "value";

      //when
      JsonObject jsonObject = ChannelUtils.toJSON("key", value, "cacheName");

      //then
      assertThat(jsonObject).hasValue("{\"field1\":\"value\"}");
   }
View Full Code Here

   private void callHandler(OpHandler handler, JsonObject jsonObj) {
      handler.handleOp(jsonObj, cache, ctx);
   }

   private JsonObject toPut(String key, String value, String mimeType) {
      JsonObject jsonObj = JsonObject.createNew();

      jsonObj.put(OpHandler.OP_CODE, "put");
      jsonObj.put(OpHandler.CACHE_NAME, cacheName);
      jsonObj.put(OpHandler.KEY, key);
      jsonObj.put(OpHandler.VALUE, value);
      jsonObj.put(OpHandler.MIME, mimeType);

      return jsonObj;
   }
View Full Code Here

TOP

Related Classes of org.infinispan.server.websocket.json.JsonObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.