@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."); } }
@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) { 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(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); } }