Package org.structr.websocket.message

Examples of org.structr.websocket.message.WebSocketMessage


  }

  @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")) {
        JsonPrimitive sessionId = root.getAsJsonPrimitive("sessionId");
        if (sessionId != null) {
          webSocketData.setSessionId(sessionId.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()) {

          JsonElement obj = entry.getValue();
          webSocketData.setNodeData(entry.getKey(),
            (obj instanceof JsonNull ? null :
            (obj instanceof JsonPrimitive ? JsonInputGSONAdapter.fromPrimitive(obj.getAsJsonPrimitive()) : obj.getAsString())));
        }

      }

      if (relData != null) {

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

          JsonElement obj = entry.getValue();
          webSocketData.setRelData(entry.getKey(), obj instanceof JsonNull ? null : obj.getAsString());
        }

      }

    }
View Full Code Here


        List<? extends GraphObject> result = webSocketData.getResult();

        if ((result != null) && (result.size() > 0)
          && (webSocketData.getCommand().equals("UPDATE") || webSocketData.getCommand().equals("ADD") || webSocketData.getCommand().equals("CREATE"))) {

          WebSocketMessage clientData = webSocketData.copy();
          SecurityContext securityContext = socket.getSecurityContext();

          // For non-authenticated clients, construct a security context without user
          if (securityContext == null) {

            try {

              securityContext = SecurityContext.getInstance(null, AccessMode.Frontend);

            } catch (FrameworkException ex) {

              continue;
            }
          }

          clientData.setResult(filter(securityContext, result));

          message = gson.toJson(clientData, WebSocketMessage.class);

        } else {
View Full Code Here

    try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {

      for (final ModificationEvent event : modificationEvents) {

        try {
          final WebSocketMessage message = getMessageForEvent(securityContext, event);
          if (message != null) {
            logger.log(Level.FINE, "################### Broadcast message: {0}", message.getCommand());
            broadcast(message);
          }

        } catch (FrameworkException ignore) {
        }
View Full Code Here

      final NodeInterface node = (NodeInterface) modificationEvent.getGraphObject();

      if (modificationEvent.isDeleted()) {

        final WebSocketMessage message = createMessage("DELETE");

        message.setId(modificationEvent.getRemovedProperties().get(GraphObject.id));

        return message;
      }

      if (modificationEvent.isCreated()) {

        final WebSocketMessage message = createMessage("CREATE");

        message.setGraphObject(node);
        message.setResult(Arrays.asList(new GraphObject[]{node}));

        return message;
      }

      if (modificationEvent.isModified()) {

        final WebSocketMessage message = createMessage("UPDATE");

        message.setGraphObject(node);
        message.setResult(Arrays.asList(new GraphObject[]{node}));
        message.setId(node.getUuid());
        message.getModifiedProperties().addAll(modificationEvent.getModifiedProperties().keySet());
        message.getRemovedProperties().addAll(modificationEvent.getRemovedProperties().keySet());
        message.setNodeData(modificationEvent.getData(securityContext));

        return message;
      }

    } else {

      // handle relationship
      final RelationshipInterface relationship = (RelationshipInterface) modificationEvent.getGraphObject();
      final RelationshipType relType = modificationEvent.getRelationshipType();

      // only interested in CONTAINS relationships
      if (!("CONTAINS".equals(relType.name()))) {
        return null;
      }

      if (modificationEvent.isDeleted()) { // && "CONTAINS".equals(relType.name())) {

        final WebSocketMessage message = createMessage("REMOVE_CHILD");

        message.setNodeData("parentId", relationship.getSourceNodeId());
        message.setId(relationship.getTargetNodeId());

        return message;
      }

      if (modificationEvent.isCreated()) {

        final WebSocketMessage message = new WebSocketMessage();
        final NodeInterface startNode = relationship.getSourceNode();
        final NodeInterface endNode = relationship.getTargetNode();

        message.setResult(Arrays.asList(new GraphObject[]{endNode}));
        message.setId(endNode.getUuid());
        message.setNodeData("parentId", startNode.getUuid());

        message.setCommand("APPEND_CHILD");

        if (endNode instanceof DOMNode) {

          org.w3c.dom.Node refNode = ((DOMNode) endNode).getNextSibling();
          if (refNode != null) {

            message.setCommand("INSERT_BEFORE");
            message.setNodeData("refId", ((AbstractNode) refNode).getUuid());
          }

        } else if (endNode instanceof User) {

          message.setCommand("APPEND_USER");
          message.setNodeData("refId", startNode.getUuid());
        }

        return message;
      }

      if (modificationEvent.isModified()) {

        final WebSocketMessage message = createMessage("UPDATE");

        message.setGraphObject(relationship);
        message.setId(relationship.getUuid());
        message.getModifiedProperties().addAll(modificationEvent.getModifiedProperties().keySet());
        message.getRemovedProperties().addAll(modificationEvent.getRemovedProperties().keySet());
        message.setNodeData(modificationEvent.getData(securityContext));

        final PropertyMap relProperties = relationship.getProperties();
        final NodeInterface startNode = relationship.getSourceNode();
        final NodeInterface endNode = relationship.getTargetNode();

        relProperties.put(new StringProperty("startNodeId"), startNode.getUuid());
        relProperties.put(new StringProperty("endNodeId"), endNode.getUuid());

        final Map<String, Object> properties = PropertyMap.javaTypeToInputType(securityContext, relationship.getClass(), relProperties);

        message.setRelData(properties);

        return message;
      }

    }
View Full Code Here

    return null;
  }

  private WebSocketMessage createMessage(final String command) {

    final WebSocketMessage newMessage = new WebSocketMessage();

    newMessage.setCommand(command);

    return newMessage;
  }
View Full Code Here

    logger.log(Level.FINE, "############################################################ RECEIVED \n{0}", data.substring(0, Math.min(data.length(), 1000)));


    // parse web socket data from JSON
    final WebSocketMessage webSocketData = gson.fromJson(data, WebSocketMessage.class);

    final App app = StructrApp.getInstance(securityContext);

    this.callback = webSocketData.getCallback();

    final String command = webSocketData.getCommand();
    final Class type = commandSet.get(command);

    final String sessionIdFromMessage = webSocketData.getSessionId();

    if (type != null) {

      try (final Tx tx = app.tx()) {

        if (sessionIdFromMessage != null) {

          // try to authenticated this connection by sessionId
          authenticate(sessionIdFromMessage);
        }

        // we only permit LOGIN commands if authentication based on sessionId was not successful
        if (!isAuthenticated() && !type.equals(LoginCommand.class)) {

          // send 401 Authentication Required
          send(MessageBuilder.status().code(401).message("").build(), true);

          return;
        }

        tx.success();

      } catch (FrameworkException t) {

        logger.log(Level.WARNING, "Unable to parse message.", t);

      }

      // process message
      try {

        AbstractCommand abstractCommand = (AbstractCommand) type.newInstance();

        abstractCommand.setWebSocket(this);
        abstractCommand.setSession(session);
        abstractCommand.setIdProperty(idProperty);

        // The below blocks allow a websocket command to manage its own
        // transactions in case of bulk processing commands etc.

        if (abstractCommand.requiresEnclosingTransaction()) {

          try (final Tx tx = app.tx()) {

            // store authenticated-Flag in webSocketData
            // so the command can access it
            webSocketData.setSessionValid(isAuthenticated());

            abstractCommand.processMessage(webSocketData);

            // commit transaction
            tx.success();
          }

        } else {

          try (final Tx tx = app.tx()) {

            // store authenticated-Flag in webSocketData
            // so the command can access it
            webSocketData.setSessionValid(isAuthenticated());

            // commit transaction
            tx.success();
          }
View Full Code Here

TOP

Related Classes of org.structr.websocket.message.WebSocketMessage

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.