Search in sources :

Example 1 with Importer

use of org.structr.web.importer.Importer in project structr by structr.

the class DeploymentTest method test34WidgetWithTemplate.

@Test
public void test34WidgetWithTemplate() {
    // setup
    try (final Tx tx = app.tx()) {
        final Page testPage = Page.createNewPage(securityContext, "WidgetTestPage");
        final Html html = createElement(testPage, testPage, "html");
        final Head head = createElement(testPage, html, "head");
        final Body body = createElement(testPage, html, "body");
        final Div div = createElement(testPage, body, "div");
        final Div div2 = createElement(testPage, body, "div");
        div.setProperty(AbstractNode.name, "WidgetTestPage-Div");
        div2.setProperty(AbstractNode.name, "WidgetTestPage-Div2");
        Widget widgetToImport = app.create(Widget.class, new NodeAttribute<>(StructrApp.key(Widget.class, "name"), "TestWidget"), new NodeAttribute<>(StructrApp.key(Widget.class, "source"), "<!-- @structr:content(text/html) --><structr:template>${{Structr.print(\"<div>Test</div>\");}}</structr:template>"), new NodeAttribute<>(StructrApp.key(Widget.class, "configuration"), "{\"processDeploymentInfo\": true}"), new NodeAttribute<>(StructrApp.key(Widget.class, "visibleToPublicUsers"), true), new NodeAttribute<>(StructrApp.key(Widget.class, "visibleToAuthenticatedUsers"), true));
        Importer importer = new Importer(securityContext, widgetToImport.getProperty(new StringProperty("source")), null, null, true, true);
        importer.setIsDeployment(true);
        importer.setCommentHandler(new DeploymentCommentHandler());
        importer.parse(true);
        DOMNode template = importer.createComponentChildNodes(div, testPage);
        div.appendChild(template);
        makePublic(testPage, html, head, body, div, div2, template);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // test
    try (final Tx tx = app.tx()) {
        Div div = (Div) app.nodeQuery().andName("WidgetTestPage-Div").getFirst();
        assertEquals(1, div.treeGetChildCount());
        Object obj = div.treeGetFirstChild();
        assertTrue(Template.class.isAssignableFrom(obj.getClass()));
        Template template = (Template) obj;
        assertEquals("${{Structr.print(\"<div>Test</div>\");}}", template.getTextContent());
        assertEquals("text/html", template.getContentType());
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : Head(org.structr.web.entity.html.Head) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Widget(org.structr.web.entity.Widget) Html(org.structr.web.entity.html.Html) StringProperty(org.structr.core.property.StringProperty) Page(org.structr.web.entity.dom.Page) Template(org.structr.web.entity.dom.Template) MailTemplate(org.structr.core.entity.MailTemplate) Div(org.structr.web.entity.html.Div) DeploymentCommentHandler(org.structr.web.maintenance.deploy.DeploymentCommentHandler) GraphObject(org.structr.core.GraphObject) DOMNode(org.structr.web.entity.dom.DOMNode) Body(org.structr.web.entity.html.Body) Importer(org.structr.web.importer.Importer) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with Importer

use of org.structr.web.importer.Importer in project structr by structr.

the class ImporterTest method testImportWidget.

private String testImportWidget(final String code, final RenderContext.EditMode editMode, final String address) {
    String sourceHtml = null;
    try {
        // render page into HTML string
        try (final Tx tx = app.tx()) {
            final Importer importer = new Importer(securityContext, code, address, "widget", true, true);
            importer.parse(true);
            // create page from source
            final Page sourcePage = importer.readPage();
            sourceHtml = sourcePage.getContent(editMode);
            tx.success();
        }
    } catch (Throwable t) {
        logger.warn("", t);
    }
    return sourceHtml;
}
Also used : Tx(org.structr.core.graph.Tx) Page(org.structr.web.entity.dom.Page) Importer(org.structr.web.importer.Importer)

Example 3 with Importer

use of org.structr.web.importer.Importer in project structr by structr.

the class ImportCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final Map<String, Object> properties = webSocketData.getNodeData();
    final String code = (String) properties.get("code");
    final String address = (String) properties.get("address");
    final String name = (String) properties.get("name");
    final boolean publicVisible = (Boolean) properties.get("publicVisible");
    final boolean authVisible = (Boolean) properties.get("authVisible");
    final boolean processDeploymentInfo = (Boolean) properties.get("processDeploymentInfo");
    try {
        final Importer pageImporter = new Importer(securityContext, code, address, name, publicVisible, authVisible);
        if (processDeploymentInfo) {
            pageImporter.setIsDeployment(true);
            pageImporter.setCommentHandler(new DeploymentCommentHandler());
        }
        final boolean parseOk = pageImporter.parse();
        if (parseOk) {
            if (address != null) {
                logger.info("Successfully parsed {}", address);
                getWebSocket().send(MessageBuilder.status().code(200).message("Successfully parsed address " + address).build(), true);
            }
            String pageId = pageImporter.readPage().getUuid();
            Map<String, Object> resultData = new HashMap();
            if (pageId != null) {
                resultData.put("id", pageId);
                getWebSocket().send(MessageBuilder.status().code(200).message("Successfully created page " + name).data(resultData).build(), true);
            } else {
                getWebSocket().send(MessageBuilder.status().code(400).message("Error while creating page " + name).data(resultData).build(), true);
            }
        }
    } catch (FrameworkException fex) {
        logger.warn("Error while importing content", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
    }
}
Also used : DeploymentCommentHandler(org.structr.web.maintenance.deploy.DeploymentCommentHandler) FrameworkException(org.structr.common.error.FrameworkException) HashMap(java.util.HashMap) SecurityContext(org.structr.common.SecurityContext) Importer(org.structr.web.importer.Importer)

Example 4 with Importer

use of org.structr.web.importer.Importer in project structr by structr.

the class Widget method expandWidget.

public static void expandWidget(final SecurityContext securityContext, final Page page, final DOMNode parent, final String baseUrl, final Map<String, Object> parameters, final boolean processDeploymentInfo) throws FrameworkException {
    String _source = (String) parameters.get("source");
    ErrorBuffer errorBuffer = new ErrorBuffer();
    if (_source == null) {
        errorBuffer.add(new EmptyPropertyToken(Widget.class.getSimpleName(), source));
    } else {
        // check source for mandatory parameters
        Matcher matcher = threadLocalTemplateMatcher.get();
        // initialize with source
        matcher.reset(_source);
        while (matcher.find()) {
            final String group = matcher.group();
            final String source = group.substring(1, group.length() - 1);
            final ReplacementInfo info = new ReplacementInfo(source);
            String key = info.getKey();
            Object value = parameters.get(key);
            if (value != null) {
                // replace and restart matching process
                _source = _source.replace(group, value.toString());
                matcher.reset(_source);
            }
        }
    }
    if (!errorBuffer.hasError()) {
        Importer importer = new Importer(securityContext, _source, baseUrl, null, false, false);
        if (processDeploymentInfo) {
            importer.setIsDeployment(true);
            importer.setCommentHandler(new DeploymentCommentHandler());
        }
        importer.parse(true);
        importer.createChildNodes(parent, page, true);
    } else {
        // report error to ui
        throw new FrameworkException(422, "Unable to import the given source code", errorBuffer);
    }
}
Also used : DeploymentCommentHandler(org.structr.web.maintenance.deploy.DeploymentCommentHandler) EmptyPropertyToken(org.structr.common.error.EmptyPropertyToken) ErrorBuffer(org.structr.common.error.ErrorBuffer) FrameworkException(org.structr.common.error.FrameworkException) Matcher(java.util.regex.Matcher) ThreadLocalMatcher(org.structr.common.ThreadLocalMatcher) Importer(org.structr.web.importer.Importer)

Example 5 with Importer

use of org.structr.web.importer.Importer in project structr by structr.

the class ImporterTest method testImport.

private String testImport(final String address, final RenderContext.EditMode editMode) {
    String sourceHtml = null;
    try {
        // render page into HTML string
        try (final Tx tx = app.tx()) {
            final Importer importer = new Importer(securityContext, null, address, "testpage", true, true);
            importer.parse();
            // create page from source
            final Page sourcePage = importer.readPage();
            sourceHtml = sourcePage.getContent(editMode);
            tx.success();
        }
    } catch (Throwable t) {
        logger.warn("", t);
    }
    return sourceHtml;
}
Also used : Tx(org.structr.core.graph.Tx) Page(org.structr.web.entity.dom.Page) Importer(org.structr.web.importer.Importer)

Aggregations

Importer (org.structr.web.importer.Importer)7 Tx (org.structr.core.graph.Tx)5 Page (org.structr.web.entity.dom.Page)4 FrameworkException (org.structr.common.error.FrameworkException)3 DeploymentCommentHandler (org.structr.web.maintenance.deploy.DeploymentCommentHandler)3 PropertyMap (org.structr.core.property.PropertyMap)2 DOMNode (org.structr.web.entity.dom.DOMNode)2 Template (org.structr.web.entity.dom.Template)2 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 Test (org.junit.Test)1 SecurityContext (org.structr.common.SecurityContext)1 ThreadLocalMatcher (org.structr.common.ThreadLocalMatcher)1 EmptyPropertyToken (org.structr.common.error.EmptyPropertyToken)1 ErrorBuffer (org.structr.common.error.ErrorBuffer)1 GraphObject (org.structr.core.GraphObject)1 MailTemplate (org.structr.core.entity.MailTemplate)1 StringProperty (org.structr.core.property.StringProperty)1 StructrUiTest (org.structr.web.StructrUiTest)1 Widget (org.structr.web.entity.Widget)1