@Override
  public void processMessage(WebSocketMessage webSocketData) {

    AbstractNode obj = getNode(webSocketData.getId());
    boolean rec = (Boolean) webSocketData.getNodeData().get("recursive");
    String principalId = (String) webSocketData.getNodeData().get("principalId");
    String permission = (String) webSocketData.getNodeData().get("permission");
    String action = (String) webSocketData.getNodeData().get("action");

    if (principalId == null) {

      logger.log(Level.SEVERE, "This command needs a principalId");
      getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }

    Principal principal = (Principal) getNode(principalId);

    if (principal == null) {

      logger.log(Level.SEVERE, "No principal found with id {0}", new Object[] {principalId});
      getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }

    webSocketData.getNodeData().remove("recursive");

    if (obj != null) {

      if (!getWebSocket()
          .getSecurityContext()
          .isAllowed(((AbstractNode) obj), Permission.accessControl)) {

        logger.log(
            Level.WARNING,
            "No access control permission for {0} on {1}",
            new Object[] {getWebSocket().getCurrentUser().toString(), obj.toString()});
        getWebSocket()
            .send(
                MessageBuilder.status().message("No access control permission").code(400).build(),
                true);
        return;
      }

      try {
        setPermission(obj, principal, action, permission, rec);

      } catch (FrameworkException ex) {

        logger.log(
            Level.SEVERE, "Unable to set permissions: {0}", ((FrameworkException) ex).toString());
        getWebSocket().send(MessageBuilder.status().code(400).build(), true);
      }

    } else {

      logger.log(Level.WARNING, "Graph object with uuid {0} not found.", webSocketData.getId());
      getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
  }
  @Override
  public void processMessage(final WebSocketMessage webSocketData) {

    final String type = (String) webSocketData.getNodeData().get("type");

    if (type == null) {
      logger.log(Level.WARNING, "Node type given not found");
      getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }

    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);

    final SchemaNode typeNode;
    try {
      typeNode = app.nodeQuery(SchemaNode.class).andName(type).getFirst();

      if (typeNode != null) {

        webSocketData.setResult(Arrays.asList(typeNode));

        // send only over local connection (no broadcast)
        getWebSocket().send(webSocketData, true);
      }
    } catch (FrameworkException ex) {
      logger.log(Level.SEVERE, null, ex);
      getWebSocket().send(MessageBuilder.status().code(500).build(), true);
    }
  }
Exemple #3
0
  @Override
  public void processMessage(final WebSocketMessage webSocketData) {

    final GraphObject obj = getGraphObject(webSocketData.getId());
    String key = (String) webSocketData.getNodeData().get("key");

    if (obj != null) {

      PropertyKey propertyKey =
          StructrApp.getConfiguration().getPropertyKeyForJSONName(obj.getClass(), key);
      PropertyConverter converter = propertyKey.inputConverter(getWebSocket().getSecurityContext());

      Object value = obj.getProperty(propertyKey);
      if (converter != null) {

        try {
          value = converter.revert(value);

        } catch (FrameworkException ex) {

          getWebSocket()
              .send(MessageBuilder.status().code(400).message(ex.getMessage()).build(), true);
        }
      }

      webSocketData.setNodeData(key, value);

      // send only over local connection (no broadcast)
      getWebSocket().send(webSocketData, true);

    } else {

      getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
  }
  @Override
  public void processMessage(WebSocketMessage webSocketData) {

    final App app = StructrApp.getInstance(getWebSocket().getSecurityContext());
    final String id = webSocketData.getId();
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String source = (String) nodeData.get("source");
    final String name = (String) nodeData.get("name");

    // check for ID
    if (id == null) {

      getWebSocket()
          .send(
              MessageBuilder.status().code(422).message("Cannot create widget without id").build(),
              true);

      return;
    }

    // check if parent node with given ID exists
    DOMNode node = getDOMNode(id);

    if (node == null) {

      getWebSocket()
          .send(MessageBuilder.status().code(404).message("Node not found").build(), true);

      return;
    }

    try {

      // convertFromInput
      PropertyMap properties = new PropertyMap();

      properties.put(AbstractNode.type, Widget.class.getSimpleName());
      properties.put(AbstractNode.name, name);
      properties.put(Widget.source, source);

      app.create(Widget.class, properties);

    } catch (Throwable t) {

      logger.log(Level.WARNING, t.toString());

      // send exception
      getWebSocket().send(MessageBuilder.status().code(422).message(t.toString()).build(), true);
    }
  }
  @Override
  public void processMessage(WebSocketMessage webSocketData) {

    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String parentId = (String) nodeData.get("parentId");
    final String pageId = webSocketData.getPageId();

    nodeData.remove("parentId");

    if (pageId != null) {

      // check for parent ID before creating any nodes
      if (parentId == null) {

        getWebSocket()
            .send(
                MessageBuilder.status()
                    .code(422)
                    .message("Cannot add node without parentId")
                    .build(),
                true);
        return;
      }

      // check if content node with given ID exists
      final DOMNode contentNode = getDOMNode(parentId);
      if (contentNode == null) {

        getWebSocket()
            .send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
        return;
      }

      final Document document = getPage(pageId);
      if (document != null) {

        final String tagName = (String) nodeData.get("tagName");
        nodeData.remove("tagName");

        final DOMNode parentNode = (DOMNode) contentNode.getParentNode();

        try {

          DOMNode elementNode = null;
          if (tagName != null && !tagName.isEmpty()) {

            elementNode = (DOMNode) document.createElement(tagName);
          }

          // append new node to parent parent node
          if (elementNode != null) {

            parentNode.appendChild(elementNode);
          }

          // append new node to parent parent node
          if (elementNode != null) {

            // append content node to new node
            elementNode.appendChild(contentNode);
          }

        } catch (DOMException dex) {

          // send DOM exception
          getWebSocket()
              .send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
        }

      } else {

        getWebSocket()
            .send(MessageBuilder.status().code(404).message("Page not found").build(), true);
      }

    } else {

      getWebSocket()
          .send(
              MessageBuilder.status()
                  .code(422)
                  .message("Cannot create node without pageId")
                  .build(),
              true);
    }
  }
  @Override
  public void processMessage(final WebSocketMessage webSocketData) {

    String id = webSocketData.getId();
    Map<String, Object> nodeData = webSocketData.getNodeData();
    String parentId = (String) nodeData.get("parentId");

    // check node to append
    if (id == null) {

      getWebSocket()
          .send(
              MessageBuilder.status()
                  .code(422)
                  .message("Cannot append node, no id is given")
                  .build(),
              true);

      return;
    }

    // check for parent ID
    if (parentId == null) {

      getWebSocket()
          .send(
              MessageBuilder.status().code(422).message("Cannot add node without parentId").build(),
              true);

      return;
    }

    // check if parent node with given ID exists
    AbstractNode parentNode = getNode(parentId);

    if (parentNode == null) {

      getWebSocket()
          .send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);

      return;
    }

    if (parentNode instanceof DOMNode) {

      DOMNode parentDOMNode = getDOMNode(parentId);

      if (parentDOMNode == null) {

        getWebSocket()
            .send(
                MessageBuilder.status().code(422).message("Parent node is no DOM node").build(),
                true);

        return;
      }

      DOMNode node = (DOMNode) getDOMNode(id);

      // append node to parent
      if (node != null) {

        parentDOMNode.appendChild(node);
      }

    } else {

      // send exception
      getWebSocket()
          .send(
              MessageBuilder.status()
                  .code(422)
                  .message("Cannot use given node, not instance of DOMNode")
                  .build(),
              true);
    }
  }
  @Override
  public void processMessage(WebSocketMessage webSocketData) {

    String id = webSocketData.getId();
    Map<String, Object> nodeData = webSocketData.getNodeData();
    String parentId = (String) nodeData.get("parentId");

    // check node to append
    if (id == null) {

      getWebSocket()
          .send(
              MessageBuilder.status()
                  .code(422)
                  .message("Cannot append node, no id is given")
                  .build(),
              true);

      return;
    }

    // check for parent ID
    if (parentId == null) {

      getWebSocket()
          .send(
              MessageBuilder.status().code(422).message("Cannot add node without parentId").build(),
              true);

      return;
    }

    // check if parent node with given ID exists
    AbstractNode parentNode = getNode(parentId);

    if (parentNode == null) {

      getWebSocket()
          .send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);

      return;
    }

    if (parentNode instanceof Folder) {

      Folder folder = (Folder) parentNode;

      AbstractFile file = (AbstractFile) getNode(id);

      if (file != null) {
        try {
          // Remove from existing parent
          LinkedTreeNode currentParent = file.treeGetParent(RelType.CONTAINS);
          if (currentParent != null) {

            currentParent.treeRemoveChild(RelType.CONTAINS, file);
          }

          folder.treeAppendChild(RelType.CONTAINS, file);
        } catch (FrameworkException ex) {
          logger.log(Level.SEVERE, null, ex);
          getWebSocket()
              .send(MessageBuilder.status().code(422).message("Cannot append file").build(), true);
        }
      }

    } else {

      // send exception
      getWebSocket()
          .send(
              MessageBuilder.status()
                  .code(422)
                  .message("Parent node is not instance of Folder")
                  .build(),
              true);
    }
  }
  @Override
  public JsonElement serialize(
      WebSocketMessage src, Type typeOfSrc, JsonSerializationContext context) {

    JsonObject root = new JsonObject();
    JsonObject jsonNodeData = new JsonObject();
    JsonObject jsonRelData = new JsonObject();
    JsonArray removedProperties = new JsonArray();
    JsonArray modifiedProperties = new JsonArray();

    if (src.getCommand() != null) {

      root.add("command", new JsonPrimitive(src.getCommand()));
    }

    if (src.getId() != null) {

      root.add("id", new JsonPrimitive(src.getId()));
    }

    if (src.getPageId() != null) {

      root.add("pageId", new JsonPrimitive(src.getPageId()));
    }

    if (src.getMessage() != null) {

      root.add("message", new JsonPrimitive(src.getMessage()));
    }

    if (src.getCode() != 0) {

      root.add("code", new JsonPrimitive(src.getCode()));
    }

    if (src.getSessionId() != null) {

      root.add("sessionId", new JsonPrimitive(src.getSessionId()));
    }

    if (src.getToken() != null) {

      root.add("token", new JsonPrimitive(src.getToken()));
    }

    if (src.getCallback() != null) {

      root.add("callback", new JsonPrimitive(src.getCallback()));
    }

    if (src.getButton() != null) {

      root.add("button", new JsonPrimitive(src.getButton()));
    }

    if (src.getParent() != null) {

      root.add("parent", new JsonPrimitive(src.getParent()));
    }

    if (src.getView() != null) {

      root.add("view", new JsonPrimitive(src.getView()));
    }

    if (src.getSortKey() != null) {

      root.add("sort", new JsonPrimitive(src.getSortKey()));
    }

    if (src.getSortOrder() != null) {

      root.add("order", new JsonPrimitive(src.getSortOrder()));
    }

    if (src.getPageSize() > 0) {

      root.add("pageSize", new JsonPrimitive(src.getPageSize()));
    }

    if (src.getPage() > 0) {

      root.add("page", new JsonPrimitive(src.getPage()));
    }

    JsonArray nodesWithChildren = new JsonArray();
    Set<String> nwc = src.getNodesWithChildren();

    if ((nwc != null) && !src.getNodesWithChildren().isEmpty()) {

      for (String nodeId : nwc) {

        nodesWithChildren.add(new JsonPrimitive(nodeId));
      }

      root.add("nodesWithChildren", nodesWithChildren);
    }

    // serialize session valid flag (output only)
    root.add("sessionValid", new JsonPrimitive(src.isSessionValid()));

    // UPDATE only, serialize only removed and modified properties and use the correct values
    if ((src.getGraphObject() != null)) {

      GraphObject graphObject = src.getGraphObject();

      if (!src.getModifiedProperties().isEmpty()) {

        for (PropertyKey modifiedKey : src.getModifiedProperties()) {

          modifiedProperties.add(toJsonPrimitive(modifiedKey));

          //					Object newValue = graphObject.getProperty(modifiedKey);
          //
          //					if (newValue != null) {
          //
          //						if (graphObject instanceof AbstractNode) {
          //
          //							src.getNodeData().put(modifiedKey.jsonName(), newValue);
          //						} else {
          //
          //							src.getRelData().put(modifiedKey.jsonName(), newValue);
          //						}
          //
          //					}

        }

        root.add("modifiedProperties", modifiedProperties);
      }

      if (!src.getRemovedProperties().isEmpty()) {

        for (PropertyKey removedKey : src.getRemovedProperties()) {

          removedProperties.add(toJsonPrimitive(removedKey));
        }

        root.add("removedProperties", removedProperties);
      }
    }

    // serialize node data
    if (src.getNodeData() != null) {

      for (Entry<String, Object> entry : src.getNodeData().entrySet()) {

        Object value = entry.getValue();
        String key = entry.getKey();

        if (value != null) {

          jsonNodeData.add(key, toJsonPrimitive(value));
        }
      }

      root.add("data", jsonNodeData);
    }

    // serialize relationship data
    if (src.getRelData() != null) {

      for (Entry<String, Object> entry : src.getRelData().entrySet()) {

        Object value = entry.getValue();
        String key = entry.getKey();

        if (value != null) {

          jsonRelData.add(key, toJsonPrimitive(value));
        }
      }

      root.add("relData", jsonRelData);
    }

    // serialize result list
    if (src.getResult() != null) {

      if (src.getView() != null) {

        try {
          propertyView.set(null, src.getView());

        } catch (FrameworkException fex) {

          logger.log(Level.WARNING, "Unable to set property view", fex);
        }

      } else {

        try {
          propertyView.set(null, PropertyView.Ui);

        } catch (FrameworkException fex) {

          logger.log(Level.WARNING, "Unable to set property view", fex);
        }
      }

      JsonArray result = new JsonArray();

      for (GraphObject obj : src.getResult()) {

        result.add(graphObjectSerializer.serialize(obj, System.currentTimeMillis()));
      }

      root.add("result", result);
      root.add("rawResultCount", toJsonPrimitive(src.getRawResultCount()));
    }

    // serialize result tree
    //		if (src.getResultTree() != null) {
    //
    //			TreeNode node = src.getResultTree();
    //
    //			root.add("root", buildTree(node, context));
    //
    //		}

    return root;
  }
  // ~--- methods --------------------------------------------------------
  @Override
  public void processMessage(final WebSocketMessage webSocketData) {

    final String keyString = (String) webSocketData.getNodeData().get("key");
    if (keyString == null) {

      logger.error("Unable to remove given object from collection: key is null");
      getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }

    final String idToRemove = (String) webSocketData.getNodeData().get("idToRemove");
    if (idToRemove == null) {

      logger.error("Unable to remove given object from collection: idToRemove is null");
      getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }

    GraphObject obj = getNode(webSocketData.getId());
    if (obj != null) {

      if (!((AbstractNode) obj).isGranted(Permission.write, getWebSocket().getSecurityContext())) {

        getWebSocket()
            .send(MessageBuilder.status().message("No write permission").code(400).build(), true);
        logger.warn(
            "No write permission for {} on {}",
            new Object[] {getWebSocket().getCurrentUser().toString(), obj.toString()});
        return;
      }
    }

    if (obj == null) {

      // No node? Try to find relationship
      obj = getRelationship(webSocketData.getId());
    }

    GraphObject objToRemove = getNode(idToRemove);

    if (obj != null && objToRemove != null) {

      try {

        PropertyKey key =
            StructrApp.getConfiguration().getPropertyKeyForJSONName(obj.getClass(), keyString);
        if (key != null) {

          List collection = (List) obj.getProperty(key);
          collection.remove(objToRemove);
          obj.setProperties(obj.getSecurityContext(), new PropertyMap(key, collection));

          if (obj instanceof NodeInterface) {

            TransactionCommand.registerNodeCallback((NodeInterface) obj, callback);

          } else if (obj instanceof RelationshipInterface) {

            TransactionCommand.registerRelCallback((RelationshipInterface) obj, callback);
          }
        }

      } catch (FrameworkException ex) {

        logger.error("Unable to set properties: {}", ((FrameworkException) ex).toString());
        getWebSocket().send(MessageBuilder.status().code(400).build(), true);
      }

    } else {

      logger.warn("Graph object with uuid {} not found.", webSocketData.getId());
      getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
  }
  @Override
  public void processMessage(WebSocketMessage webSocketData) {

    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String parentId = (String) nodeData.get("parentId");
    final String childContent = (String) nodeData.get("childContent");
    final String pageId = webSocketData.getPageId();

    nodeData.remove("parentId");

    if (pageId != null) {

      // check for parent ID before creating any nodes
      if (parentId == null) {

        getWebSocket()
            .send(
                MessageBuilder.status()
                    .code(422)
                    .message("Cannot add node without parentId")
                    .build(),
                true);
        return;
      }

      // check if parent node with given ID exists
      final DOMNode parentNode = getDOMNode(parentId);
      if (parentNode == null) {

        getWebSocket()
            .send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
        return;
      }

      final Document document = getPage(pageId);
      if (document != null) {

        final String tagName = (String) nodeData.get("tagName");
        final App app = StructrApp.getInstance();

        nodeData.remove("tagName");

        try {
          app.beginTx();

          DOMNode newNode;

          if (tagName != null && !tagName.isEmpty()) {

            newNode = (DOMNode) document.createElement(tagName);

          } else {

            newNode = (DOMNode) document.createTextNode("#text");
          }

          // append new node to parent
          if (newNode != null) {

            parentNode.appendChild(newNode);

            for (Entry entry : nodeData.entrySet()) {

              String key = (String) entry.getKey();
              Object val = entry.getValue();

              PropertyKey propertyKey =
                  StructrApp.getConfiguration()
                      .getPropertyKeyForDatabaseName(newNode.getClass(), key);
              if (propertyKey != null) {

                try {
                  Object convertedValue = val;

                  PropertyConverter inputConverter =
                      propertyKey.inputConverter(SecurityContext.getSuperUserInstance());
                  if (inputConverter != null) {

                    convertedValue = inputConverter.convert(val);
                  }

                  // newNode.unlockReadOnlyPropertiesOnce();
                  newNode.setProperty(propertyKey, convertedValue);

                } catch (FrameworkException fex) {

                  logger.log(
                      Level.WARNING,
                      "Unable to set node property {0} of node {1} to {2}: {3}",
                      new Object[] {propertyKey, newNode.getUuid(), val, fex.getMessage()});
                }
              }
            }

            // create a child text node if content is given
            if (StringUtils.isNotBlank(childContent)) {

              DOMNode childNode = (DOMNode) document.createTextNode(childContent);

              if (newNode != null) {

                newNode.appendChild(childNode);
              }
            }
          }
          app.commitTx();

        } catch (DOMException dex) {

          // send DOM exception
          getWebSocket()
              .send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);

        } catch (FrameworkException ex) {

          Logger.getLogger(CreateAndAppendDOMNodeCommand.class.getName())
              .log(Level.SEVERE, null, ex);

        } finally {

          app.finishTx();
        }

      } else {

        getWebSocket()
            .send(MessageBuilder.status().code(404).message("Page not found").build(), true);
      }

    } else {

      getWebSocket()
          .send(
              MessageBuilder.status()
                  .code(422)
                  .message("Cannot create node without pageId")
                  .build(),
              true);
    }
  }