Search in sources :

Example 41 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class RenderContextTest method testVariableReplacement.

@Test
public void testVariableReplacement() {
    NodeInterface detailsDataObject = null;
    Page page = null;
    DOMNode html = null;
    DOMNode head = null;
    DOMNode body = null;
    DOMNode title = null;
    DOMNode h1 = null;
    DOMNode div1 = null;
    DOMNode p1 = null;
    DOMNode div2 = null;
    DOMNode p2 = null;
    DOMNode div3 = null;
    DOMNode p3 = null;
    A a = null;
    DOMNode div4 = null;
    DOMNode p4 = null;
    TestOne testOne = 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");
        h1 = (DOMNode) page.createElement("h1");
        div1 = (DOMNode) page.createElement("div");
        p1 = (DOMNode) page.createElement("p");
        div2 = (DOMNode) page.createElement("div");
        p2 = (DOMNode) page.createElement("p");
        div3 = (DOMNode) page.createElement("div");
        p3 = (DOMNode) page.createElement("p");
        a = (A) page.createElement("a");
        div4 = (DOMNode) page.createElement("div");
        p4 = (DOMNode) page.createElement("p");
        // 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);
        // add H1 element to BODY
        body.appendChild(h1);
        // add DIV element 1 to BODY
        body.appendChild(div1);
        div1.appendChild(p1);
        // add DIV element 2 to DIV
        div1.appendChild(div2);
        div2.appendChild(p2);
        // add DIV element 3 to DIV
        div2.appendChild(div3);
        div3.appendChild(p3);
        // add link to p3
        p3.appendChild(a);
        a.setLinkable(page);
        body.appendChild(div4);
        div4.appendChild(p4);
        final PropertyMap p4Properties = new PropertyMap();
        p4Properties.put(StructrApp.key(DOMElement.class, "restQuery"), "/divs");
        p4Properties.put(StructrApp.key(DOMElement.class, "dataKey"), "div");
        p4.setProperties(p4.getSecurityContext(), p4Properties);
        NodeList paragraphs = page.getElementsByTagName("p");
        assertEquals(p1, paragraphs.item(0));
        assertEquals(p2, paragraphs.item(1));
        assertEquals(p3, paragraphs.item(2));
        assertEquals(p4, paragraphs.item(3));
        // create users
        final User tester1 = app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester1"), new NodeAttribute<>(StructrApp.key(User.class, "eMail"), "tester1@test.com"));
        final User tester2 = app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester2"), new NodeAttribute<>(StructrApp.key(User.class, "eMail"), "tester2@test.com"));
        assertNotNull("User tester1 should exist.", tester1);
        assertNotNull("User tester2 should exist.", tester2);
        // create admin user for later use
        final PropertyMap adminProperties = new PropertyMap();
        adminProperties.put(StructrApp.key(User.class, "name"), "admin");
        adminProperties.put(StructrApp.key(User.class, "password"), "admin");
        adminProperties.put(StructrApp.key(User.class, "isAdmin"), true);
        app.create(User.class, adminProperties);
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        final RenderContext ctx = new RenderContext(securityContext);
        ctx.setDetailsDataObject(detailsDataObject);
        ctx.setPage(page);
        // test for "empty" return value
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${err}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${this.error}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${this.this.this.error}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${parent.error}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${this.owner}"));
        assertEquals("", Scripting.replaceVariables(ctx, p1, "${parent.owner}"));
        // other functions are tested in the ActionContextTest in structr-core, see there.
        assertEquals("true", Scripting.replaceVariables(ctx, p1, "${true}"));
        assertEquals("false", Scripting.replaceVariables(ctx, p1, "${false}"));
        assertEquals("yes", Scripting.replaceVariables(ctx, p1, "${if(true, \"yes\", \"no\")}"));
        assertEquals("no", Scripting.replaceVariables(ctx, p1, "${if(false, \"yes\", \"no\")}"));
        assertEquals("true", Scripting.replaceVariables(ctx, p1, "${if(true, true, false)}"));
        assertEquals("false", Scripting.replaceVariables(ctx, p1, "${if(false, true, false)}"));
        // test keywords
        assertEquals("${id} should evaluate to the ID if the current details object", detailsDataObject.getUuid(), Scripting.replaceVariables(ctx, p1, "${id}"));
        ctx.setDetailsDataObject(null);
        assertEquals("${id} should evaluate to the ID if the current details object", "abc12345", Scripting.replaceVariables(ctx, p1, "${id!abc12345}"));
        ctx.setDetailsDataObject(detailsDataObject);
        assertEquals("${id} should be equal to ${current.id}", "true", Scripting.replaceVariables(ctx, p1, "${equal(id, current.id)}"));
        assertEquals("${element} should evaluate to the current DOM node", p1.toString(), Scripting.replaceVariables(ctx, p1, "${element}"));
        assertNull(Scripting.replaceVariables(ctx, p1, "${if(true, null, \"no\")}"));
        assertNull(Scripting.replaceVariables(ctx, p1, "${null}"));
        assertEquals("Invalid replacement result", "/testpage?" + page.getUuid(), Scripting.replaceVariables(ctx, p1, "/${page.name}?${page.id}"));
        assertEquals("Invalid replacement result", "/testpage?" + page.getUuid(), Scripting.replaceVariables(ctx, a, "/${link.name}?${link.id}"));
        // these tests find single element => success
        assertEquals("Invalid replacement result", page.getUuid(), Scripting.replaceVariables(ctx, a, "${get(find('Page', 'name', 'testpage'), 'id')}"));
        assertEquals("Invalid replacement result", a.getUuid(), Scripting.replaceVariables(ctx, a, "${get(find('A'), 'id')}"));
        // this test finds multiple <p> elements => error
        assertEquals("Invalid replacement result", GetFunction.ERROR_MESSAGE_GET_ENTITY, Scripting.replaceVariables(ctx, a, "${get(find('P'), 'id')}"));
        // more complex replacement
        // assertEquals("Invalid replacement result", "", a.replaceVariables(ctx, securityContext, "${get(find('P'), 'id')}"));
        // String default value
        assertEquals("bar", Scripting.replaceVariables(ctx, p1, "${request.foo!bar}"));
        // Number default value (will be evaluated to a string)
        assertEquals("1", Scripting.replaceVariables(ctx, p1, "${page.position!1}"));
        // Number default value
        assertEquals("true", Scripting.replaceVariables(ctx, p1, "${equal(42, this.null!42)}"));
        final User tester1 = app.nodeQuery(User.class).andName("tester1").getFirst();
        final User tester2 = app.nodeQuery(User.class).andName("tester2").getFirst();
        assertNotNull("User tester1 should exist.", tester1);
        assertNotNull("User tester2 should exist.", tester2);
        final ActionContext tester1Context = new ActionContext(SecurityContext.getInstance(tester1, AccessMode.Backend));
        final ActionContext tester2Context = new ActionContext(SecurityContext.getInstance(tester2, AccessMode.Backend));
        // users
        assertEquals("tester1", Scripting.replaceVariables(tester1Context, p1, "${me.name}"));
        assertEquals("tester2", Scripting.replaceVariables(tester2Context, p2, "${me.name}"));
        // allow unauthenticated GET on /pages
        grant("Page/_Ui", 16, true);
        // test GET REST access
        assertEquals("Invalid GET notation result", page.getName(), Scripting.replaceVariables(ctx, p1, "${from_json(GET('http://localhost:" + httpPort + "/structr/rest/pages/ui')).result[0].name}"));
        grant("Folder", 64, true);
        grant("_login", 64, false);
        assertEquals("Invalid POST result", "201", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:status}').status}"));
        assertEquals("Invalid POST result", "1.0", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:result_count}').body.result_count}"));
        assertEquals("Invalid POST result", "application/json;charset=utf-8", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:content-type}').headers.Content-Type}"));
        // test POST with invalid name containing curly braces to provoke 422
        assertEquals("Invalid POST result", "422", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:\"ShouldFail/xyz\"}').status}"));
        // test login and sessions
        final String sessionIdCookie = Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/login', '{name:admin,password:admin}').headers.Set-Cookie}");
        final String sessionId = HttpCookie.parse(sessionIdCookie).get(0).getValue();
        // test authenticated GET request using session ID cookie
        assertEquals("Invalid authenticated GET result", "admin", Scripting.replaceVariables(ctx, page, "${add_header('Cookie', 'JSESSIONID=" + sessionId + ";Path=/')}${from_json(GET('http://localhost:" + httpPort + "/structr/rest/users')).result[0].name}"));
        assertEquals("Invalid authenticated GET result", "tester1", Scripting.replaceVariables(ctx, page, "${add_header('Cookie', 'JSESSIONID=" + sessionId + ";Path=/')}${from_json(GET('http://localhost:" + httpPort + "/structr/rest/users')).result[1].name}"));
        assertEquals("Invalid authenticated GET result", "tester2", Scripting.replaceVariables(ctx, page, "${add_header('Cookie', 'JSESSIONID=" + sessionId + ";Path=/')}${from_json(GET('http://localhost:" + httpPort + "/structr/rest/users')).result[2].name}"));
        // locale
        final String localeString = ctx.getLocale().toString();
        assertEquals("Invalid locale result", localeString, Scripting.replaceVariables(ctx, page, "${locale}"));
        // set new details object
        final TestOne detailsDataObject2 = app.create(TestOne.class, "TestOne");
        Scripting.replaceVariables(ctx, p1, "${set_details_object(first(find('TestOne', 'id', '" + detailsDataObject2.getUuid() + "')))}");
        assertEquals("${current.id} should resolve to new details object", detailsDataObject2.getUuid(), Scripting.replaceVariables(ctx, p1, "${current.id}"));
        // test values() with single parameter
        assertEquals("Invalid values() result", "[test]", Scripting.replaceVariables(ctx, page, "${values(from_json('{name:test}'))}"));
        testOne = createTestNode(TestOne.class);
        testOne.setProperty(TestOne.htmlString, "<a b=\"c\">&d</a>");
        // escape_html
        assertEquals("Invalid escape_html() result", "&lt;a b=&quot;c&quot;&gt;&amp;d&lt;/a&gt;", Scripting.replaceVariables(ctx, testOne, "${escape_html(this.htmlString)}"));
        testOne.setProperty(TestOne.htmlString, "&lt;a b=&quot;c&quot;&gt;&amp;d&lt;/a&gt;");
        // unescape_html
        assertEquals("Invalid unescape_html() result", "<a b=\"c\">&d</a>", Scripting.replaceVariables(ctx, testOne, "${unescape_html(this.htmlString)}"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : A(org.structr.web.entity.html.A) RenderContext(org.structr.web.common.RenderContext) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeList(org.w3c.dom.NodeList) Page(org.structr.web.entity.dom.Page) DOMElement(org.structr.web.entity.dom.DOMElement) ActionContext(org.structr.schema.action.ActionContext) PropertyMap(org.structr.core.property.PropertyMap) TestOne(org.structr.web.entity.TestOne) DOMNode(org.structr.web.entity.dom.DOMNode) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 42 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class CloneNodeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final String id = webSocketData.getId();
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String parentId = (String) nodeData.get("parentId");
    final boolean deep = (Boolean) nodeData.get("deep");
    if (id != null) {
        DOMNode parent = null;
        final DOMNode node = getDOMNode(id);
        if (parentId != null) {
            // check if parent node with given ID exists
            parent = getDOMNode(parentId);
            if (parent == null) {
                getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
                return;
            }
        }
        Page ownerPage = null;
        if (parent != null) {
            if (parent instanceof Page) {
                ownerPage = (Page) parent;
            } else {
                ownerPage = parent.getOwnerDocument();
            }
        } else {
            ownerPage = node.getOwnerDocument();
        }
        if (ownerPage == null) {
            getWebSocket().send(MessageBuilder.status().code(422).message("Cannot clone node without a target page").build(), true);
            return;
        }
        try {
            DOMNode clonedNode = (DOMNode) node.cloneNode(deep);
            if (parent != null) {
                parent.appendChild(clonedNode);
            }
            setOwnerPageRecursively(clonedNode, clonedNode.getSecurityContext(), ownerPage);
        } catch (DOMException | FrameworkException ex) {
            getWebSocket().send(MessageBuilder.status().code(422).message(ex.getMessage()).build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node without id").build(), true);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) FrameworkException(org.structr.common.error.FrameworkException) Page(org.structr.web.entity.dom.Page) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 43 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class CreateAndReplaceDOMNodeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String parentId = (String) nodeData.get("parentId");
    final String refId = (String) nodeData.get("refId");
    final String pageId = webSocketData.getPageId();
    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;
        }
        // check for ref ID before creating any nodes
        if (refId == null) {
            getWebSocket().send(MessageBuilder.status().code(422).message("Cannot add node without refId").build(), true);
            return;
        }
        // check if ref node with given ID exists
        final DOMNode refNode = getDOMNode(refId);
        if (refNode == null) {
            getWebSocket().send(MessageBuilder.status().code(404).message("Reference node not found").build(), true);
            return;
        }
        final Document document = getPage(pageId);
        if (document != null) {
            String tagName = (String) nodeData.get("tagName");
            DOMNode newNode = null;
            try {
                if (tagName != null && !tagName.isEmpty()) {
                    newNode = (DOMNode) document.createElement(tagName);
                } else {
                    newNode = (DOMNode) document.createTextNode("");
                }
                // append new node to parent
                if (newNode != null) {
                    parentNode.replaceChild(newNode, refNode);
                }
            } 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);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) DOMNode(org.structr.web.entity.dom.DOMNode) Document(org.w3c.dom.Document)

Example 44 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class RemoveCommand method processMessage.

@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 pageId from node and all children ("move to trash")
                    recursivelyRemoveNodesFromPage(domNode, securityContext);
                } 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 {
                // is this even used any more?
                logger.warn("Deprecated use of RemoveCommand, please report this error and the following stack trace to the Structr team on https://github.com/structr/structr. Thanks!");
                Thread.dumpStack();
                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);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) DOMException(org.w3c.dom.DOMException) FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) SecurityContext(org.structr.common.SecurityContext) DOMNode(org.structr.web.entity.dom.DOMNode) NodeInterface(org.structr.core.graph.NodeInterface)

Example 45 with DOMNode

use of org.structr.web.entity.dom.DOMNode in project structr by structr.

the class RemoveCommand method recursivelyRemoveNodesFromPage.

// ----- private methods -----
private void recursivelyRemoveNodesFromPage(final DOMNode parent, final SecurityContext securityContext) throws FrameworkException {
    // Remove node from page
    final PropertyMap changedProperties = new PropertyMap();
    changedProperties.put(StructrApp.key(DOMNode.class, "syncedNodes"), Collections.EMPTY_LIST);
    changedProperties.put(StructrApp.key(DOMNode.class, "pageId"), null);
    parent.setProperties(securityContext, changedProperties);
    // recurse
    for (final DOMNode child : parent.getChildren()) {
        recursivelyRemoveNodesFromPage(child, securityContext);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) DOMNode(org.structr.web.entity.dom.DOMNode)

Aggregations

DOMNode (org.structr.web.entity.dom.DOMNode)61 FrameworkException (org.structr.common.error.FrameworkException)26 Tx (org.structr.core.graph.Tx)19 PropertyMap (org.structr.core.property.PropertyMap)19 Page (org.structr.web.entity.dom.Page)19 DOMException (org.w3c.dom.DOMException)16 App (org.structr.core.app.App)14 StructrApp (org.structr.core.app.StructrApp)14 SecurityContext (org.structr.common.SecurityContext)12 GraphObject (org.structr.core.GraphObject)9 NodeInterface (org.structr.core.graph.NodeInterface)9 LinkedList (java.util.LinkedList)8 Test (org.junit.Test)8 StructrUiTest (org.structr.web.StructrUiTest)8 ShadowDocument (org.structr.web.entity.dom.ShadowDocument)8 AbstractNode (org.structr.core.entity.AbstractNode)7 Template (org.structr.web.entity.dom.Template)7 Document (org.w3c.dom.Document)6 IOException (java.io.IOException)5 PropertyKey (org.structr.core.property.PropertyKey)5