use of org.structr.web.entity.dom.Template in project structr by structr.
the class UiScriptingTest method testMultiRequestParameters.
@Test
public void testMultiRequestParameters() {
try (final Tx tx = app.tx()) {
Page page = (Page) app.create(Page.class, new NodeAttribute(Page.name, "test"), new NodeAttribute(Page.visibleToPublicUsers, true));
Template template = (Template) app.create(Template.class, new NodeAttribute(Page.visibleToPublicUsers, true));
template.setContent("${each(request.param, print(data))}");
page.appendChild(template);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body(equalTo("abc")).when().get("http://localhost:8875/test?param=a¶m=b¶m=c");
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
}
use of org.structr.web.entity.dom.Template in project structr by structr.
the class UiScriptingTest method testSingleRequestParameters.
@Test
public void testSingleRequestParameters() {
try (final Tx tx = app.tx()) {
Page page = (Page) app.create(Page.class, new NodeAttribute(Page.name, "test"), new NodeAttribute(Page.visibleToPublicUsers, true));
Template template = (Template) app.create(Template.class, new NodeAttribute(Page.visibleToPublicUsers, true));
template.setContent("${request.param}");
page.appendChild(template);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body(equalTo("a")).when().get("http://localhost:8875/test?param=a");
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
}
use of org.structr.web.entity.dom.Template in project structr by structr.
the class DeployCommand method exportTemplateSource.
private void exportTemplateSource(final Path target, final DOMNode template, final Map<String, Object> configuration) throws FrameworkException {
final Map<String, Object> properties = new TreeMap<>();
boolean doExport = true;
final String content = template.getProperty(StructrApp.key(Template.class, "content"));
if (content != null) {
// name with uuid or just uuid
String name = template.getProperty(AbstractNode.name);
if (name != null) {
name += "-" + template.getUuid();
} else {
name = template.getUuid();
}
final Path targetFile = target.resolve(name + ".html");
if (Files.exists(targetFile)) {
try {
final String existingContent = new String(Files.readAllBytes(targetFile), "utf-8");
doExport = !existingContent.equals(content);
} catch (IOException ignore) {
}
}
configuration.put(name, properties);
exportConfiguration(template, properties);
if (doExport) {
try (final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(targetFile.toFile()))) {
writer.write(content);
writer.flush();
writer.close();
} catch (IOException ioex) {
logger.warn("", ioex);
}
}
}
}
use of org.structr.web.entity.dom.Template in project structr by structr.
the class CreateAndAppendDOMNodeCommand 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 childContent = (String) nodeData.get("childContent");
final String pageId = webSocketData.getPageId();
Boolean inheritVisibilityFlags = (Boolean) nodeData.get("inheritVisibilityFlags");
if (inheritVisibilityFlags == null) {
inheritVisibilityFlags = false;
}
// remove configuration elements from the nodeData so we don't set it on the node
nodeData.remove("parentId");
nodeData.remove("inheritVisibilityFlags");
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");
nodeData.remove("tagName");
try {
DOMNode newNode;
if (tagName != null && "comment".equals(tagName)) {
newNode = (DOMNode) document.createComment("#comment");
} else if (tagName != null && "template".equals(tagName)) {
newNode = (DOMNode) document.createTextNode("#template");
try {
newNode.unlockSystemPropertiesOnce();
newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(NodeInterface.type, Template.class.getSimpleName()));
} catch (FrameworkException fex) {
logger.warn("Unable to set type of node {} to Template: {}", new Object[] { newNode.getUuid(), fex.getMessage() });
}
} else if (tagName != null && !tagName.isEmpty()) {
if ("custom".equals(tagName)) {
try {
// experimental: create DOM element with literal tag
newNode = (DOMElement) StructrApp.getInstance(webSocket.getSecurityContext()).create(DOMElement.class, new NodeAttribute(StructrApp.key(DOMElement.class, "tag"), "custom"), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnDetail"), false), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnIndex"), false));
if (newNode != null && document != null) {
newNode.doAdopt((Page) document);
}
} catch (FrameworkException fex) {
// abort
getWebSocket().send(MessageBuilder.status().code(422).message(fex.getMessage()).build(), true);
return;
}
} else {
newNode = (DOMNode) document.createElement(tagName);
}
} else {
newNode = (DOMNode) document.createTextNode("#text");
}
// Instantiate node again to get correct class
newNode = getDOMNode(newNode.getUuid());
// append new node to parent
if (newNode != null) {
parentNode.appendChild(newNode);
for (Entry entry : nodeData.entrySet()) {
final String key = (String) entry.getKey();
final 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.setProperties(newNode.getSecurityContext(), new PropertyMap(propertyKey, convertedValue));
} catch (FrameworkException fex) {
logger.warn("Unable to set node property {} of node {} to {}: {}", new Object[] { propertyKey, newNode.getUuid(), val, fex.getMessage() });
}
}
}
PropertyMap visibilityFlags = null;
if (inheritVisibilityFlags) {
visibilityFlags = new PropertyMap();
visibilityFlags.put(DOMNode.visibleToAuthenticatedUsers, parentNode.getProperty(DOMNode.visibleToAuthenticatedUsers));
visibilityFlags.put(DOMNode.visibleToPublicUsers, parentNode.getProperty(DOMNode.visibleToPublicUsers));
try {
newNode.setProperties(newNode.getSecurityContext(), visibilityFlags);
} catch (FrameworkException fex) {
logger.warn("Unable to inherit visibility flags for node {} from parent node {}", newNode, parentNode);
}
}
// create a child text node if content is given
if (StringUtils.isNotBlank(childContent)) {
final DOMNode childNode = (DOMNode) document.createTextNode(childContent);
newNode.appendChild(childNode);
if (inheritVisibilityFlags) {
try {
childNode.setProperties(childNode.getSecurityContext(), visibilityFlags);
} catch (FrameworkException fex) {
logger.warn("Unable to inherit visibility flags for node {} from parent node {}", childNode, newNode);
}
}
}
}
} 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);
}
}
use of org.structr.web.entity.dom.Template in project structr by structr.
the class CreateComponentCommand method create.
public DOMNode create(final DOMNode node) throws FrameworkException {
final DOMNode clonedNode = (DOMNode) node.cloneNode(false);
// Child nodes of a template must stay in page tree
if (!(clonedNode instanceof Template)) {
moveChildNodes(node, clonedNode);
}
final ShadowDocument hiddenDoc = CreateComponentCommand.getOrCreateHiddenDocument();
clonedNode.setOwnerDocument(hiddenDoc);
// Change page (owner document) of all children recursively
for (DOMNode child : DOMNode.getAllChildNodes(clonedNode)) {
child.setOwnerDocument(hiddenDoc);
}
node.setSharedComponent(clonedNode);
return clonedNode;
}
Aggregations