Exemple #1
0
  @Test
  public void testScripting() {

    NodeInterface detailsDataObject = null;
    Page page = null;
    DOMNode html = null;
    DOMNode head = null;
    DOMNode body = null;
    DOMNode title = null;
    DOMNode div = null;
    DOMNode p = null;
    DOMNode text = null;

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

      detailsDataObject = app.create(TestOne.class, "TestOne");
      page = Page.createNewPage(securityContext, "testpage");

      page.setProperties(
          page.getSecurityContext(), new PropertyMap(Page.visibleToPublicUsers, true));

      assertTrue(page != null);
      assertTrue(page instanceof Page);

      html = (DOMNode) page.createElement("html");
      head = (DOMNode) page.createElement("head");
      body = (DOMNode) page.createElement("body");
      title = (DOMNode) page.createElement("title");
      div = (DOMNode) page.createElement("div");
      p = (DOMNode) page.createElement("p");
      text = (DOMNode) page.createTextNode("x");

      // add HTML element to page
      page.appendChild(html);

      // add HEAD and BODY elements to HTML
      html.appendChild(head);
      html.appendChild(body);

      // add TITLE element to HEAD
      head.appendChild(title);

      body.appendChild(div);
      div.appendChild(p);

      final PropertyMap changedProperties = new PropertyMap();
      changedProperties.put(DOMElement.restQuery, "/divs");
      changedProperties.put(DOMElement.dataKey, "div");
      p.setProperties(p.getSecurityContext(), changedProperties);

      p.appendChild(text);

      tx.success();

    } catch (FrameworkException fex) {

      fail("Unexpected exception");
    }

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

      final RenderContext ctx =
          new RenderContext(
              securityContext,
              new RequestMockUp(),
              new ResponseMockUp(),
              RenderContext.EditMode.NONE);
      ctx.setDetailsDataObject(detailsDataObject);
      ctx.setPage(page);

      test(p, text, "${{ return Structr.get('div').id}}", "<p>" + div.getUuid() + "</p>", ctx);
      test(p, text, "${{ return Structr.get('page').id}}", "<p>" + page.getUuid() + "</p>", ctx);
      test(p, text, "${{ return Structr.get('parent').id}}", "<p>" + p.getUuid() + "</p>", ctx);

      tx.success();

    } catch (FrameworkException fex) {

      logger.warn("", fex);
      fail("Unexpected exception.");
    }
  }
Exemple #2
0
  @Override
  public void processMessage(final WebSocketMessage webSocketData) {

    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String id = webSocketData.getId();

    if (id != null) {

      final NodeInterface node = getNode(id);

      if (node != null) {

        if (node instanceof DOMNode) {

          // Use new DOM interface
          DOMNode domNode = (DOMNode) node;

          try {

            domNode.getParentNode().removeChild(domNode);

            // Remove node from page
            final PropertyMap changedProperties = new PropertyMap();
            changedProperties.put(DOMNode.syncedNodes, Collections.EMPTY_LIST);
            changedProperties.put(DOMNode.pageId, null);
            domNode.setProperties(securityContext, changedProperties);

          } catch (DOMException | FrameworkException ex) {

            logger.error("Could not remove node from page " + domNode, ex);
            getWebSocket()
                .send(MessageBuilder.status().code(422).message(ex.getMessage()).build(), true);
          }

        } else {

          final App app = StructrApp.getInstance(securityContext);

          try {

            // Old style: Delete all incoming CONTAINS rels
            for (AbstractRelationship rel : node.getIncomingRelationships()) {

              if ("CONTAINS".equals(rel.getType())) {

                app.delete(rel);
              }
            }

          } catch (Throwable t) {

            logger.error("Could not delete relationship", t);
            getWebSocket()
                .send(
                    MessageBuilder.status()
                        .code(400)
                        .message("Error in RemoveCommand: " + t.getMessage())
                        .build(),
                    true);
          }
        }

      } else {

        getWebSocket().send(MessageBuilder.status().code(404).build(), true);
      }

    } else {

      getWebSocket()
          .send(
              MessageBuilder.status()
                  .code(400)
                  .message("RemoveCommand called with empty id")
                  .build(),
              true);
    }
  }