Search in sources :

Example 1 with UnlicensedException

use of org.structr.common.error.UnlicensedException in project structr by structr.

the class DOMNode method displayForConditions.

static boolean displayForConditions(final DOMNode thisNode, final RenderContext renderContext) {
    // In raw or widget mode, render everything
    EditMode editMode = renderContext.getEditMode(renderContext.getSecurityContext().getUser(false));
    if (EditMode.DEPLOYMENT.equals(editMode) || EditMode.RAW.equals(editMode) || EditMode.WIDGET.equals(editMode)) {
        return true;
    }
    String _showConditions = thisNode.getProperty(StructrApp.key(DOMNode.class, "showConditions"));
    String _hideConditions = thisNode.getProperty(StructrApp.key(DOMNode.class, "hideConditions"));
    // If both fields are empty, render node
    if (StringUtils.isBlank(_hideConditions) && StringUtils.isBlank(_showConditions)) {
        return true;
    }
    try {
        // If hide conditions evaluate to "true", don't render
        if (StringUtils.isNotBlank(_hideConditions) && Boolean.TRUE.equals(Scripting.evaluate(renderContext, thisNode, "${".concat(_hideConditions).concat("}"), "hide condition"))) {
            return false;
        }
    } catch (UnlicensedException | FrameworkException ex) {
        logger.error("Hide conditions " + _hideConditions + " could not be evaluated.", ex);
    }
    try {
        // If show conditions evaluate to "false", don't render
        if (StringUtils.isNotBlank(_showConditions) && Boolean.FALSE.equals(Scripting.evaluate(renderContext, thisNode, "${".concat(_showConditions).concat("}"), "show condition"))) {
            return false;
        }
    } catch (UnlicensedException | FrameworkException ex) {
        logger.error("Show conditions " + _showConditions + " could not be evaluated.", ex);
    }
    return true;
}
Also used : UnlicensedException(org.structr.common.error.UnlicensedException) FrameworkException(org.structr.common.error.FrameworkException) EditMode(org.structr.web.common.RenderContext.EditMode)

Example 2 with UnlicensedException

use of org.structr.common.error.UnlicensedException in project structr by structr.

the class Scripting method replaceVariables.

public static String replaceVariables(final ActionContext actionContext, final GraphObject entity, final Object rawValue) throws FrameworkException {
    if (rawValue == null) {
        return null;
    }
    String value;
    if (rawValue instanceof String) {
        value = (String) rawValue;
        if (!actionContext.returnRawValue()) {
            final List<Tuple> replacements = new LinkedList<>();
            for (final String expression : extractScripts(value)) {
                try {
                    final Object extractedValue = evaluate(actionContext, entity, expression, "script source");
                    String partValue = extractedValue != null ? formatToDefaultDateOrString(extractedValue) : "";
                    if (partValue != null) {
                        replacements.add(new Tuple(expression, partValue));
                    } else {
                        // all browsers
                        if (!value.equals(expression)) {
                            replacements.add(new Tuple(expression, ""));
                        }
                    }
                } catch (UnlicensedException ex) {
                    ex.log(logger);
                }
            }
            // apply replacements
            for (final Tuple tuple : replacements) {
                // only replace a single occurrence at a time!
                value = StringUtils.replaceOnce(value, tuple.key, tuple.value);
            }
        }
    } else if (rawValue instanceof Boolean) {
        value = Boolean.toString((Boolean) rawValue);
    } else {
        value = rawValue.toString();
    }
    if (Functions.NULL_STRING.equals(value)) {
        // return literal null for a single ___NULL___
        return null;
    } else {
        // Replace ___NULL___ by empty string
        value = StringUtils.replaceAll(value, Functions.NULL_STRING, "");
    }
    return value;
}
Also used : UnlicensedException(org.structr.common.error.UnlicensedException) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList)

Example 3 with UnlicensedException

use of org.structr.common.error.UnlicensedException in project structr by structr.

the class StructrScriptEngine method eval.

@Override
public Object eval(final String script, final ScriptContext context) throws ScriptException {
    try {
        final ActionContext actionContext = (ActionContext) get("_actionContext");
        final GraphObject entity = (GraphObject) get("_entity");
        return Functions.evaluate(actionContext, entity, script);
    } catch (UnlicensedException | FrameworkException fex) {
        // wrap FrameworkException in ScriptException and re-throw
        throw new ScriptException(fex);
    }
}
Also used : ScriptException(javax.script.ScriptException) UnlicensedException(org.structr.common.error.UnlicensedException) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) GraphObject(org.structr.core.GraphObject)

Example 4 with UnlicensedException

use of org.structr.common.error.UnlicensedException in project structr by structr.

the class ScriptingTest method testNonPrimitiveReturnValue.

@Test
public void testNonPrimitiveReturnValue() {
    try (final Tx tx = app.tx()) {
        app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.name, "testReturnValueOfGlobalSchemaMethod"), new NodeAttribute<>(SchemaMethod.source, "{ return { name: 'test', value: 123, me: Structr.me }; }"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"))), new NodeAttribute<>(SchemaProperty.name, "returnTest"), new NodeAttribute<>(SchemaProperty.propertyType, "Function"), new NodeAttribute<>(SchemaProperty.readFunction, "{ return { name: 'test', value: 123, me: Structr.this }; }"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        final ActionContext ctx = new ActionContext(securityContext, null);
        final Map map = (Map) Scripting.evaluate(ctx, null, "${{return Structr.call('testReturnValueOfGlobalSchemaMethod')}}", "test");
        final Object name = map.get("name");
        final Object value = map.get("value");
        final Object me = map.get("me");
        assertEquals("Invalid non-primitive scripting return value result, name should be of type string.", "test", name);
        assertEquals("Invalid non-primitive scripting return value result, value should be of type integer", Integer.valueOf(123), value);
        assertTrue("Invalid non-primitive scripting return value result,   me should be of type SuperUser", me instanceof SuperUser);
        tx.success();
    } catch (UnlicensedException | FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        final Class type = StructrApp.getConfiguration().getNodeEntityClass("Test");
        final NodeInterface obj = app.create(type, "test");
        final Map map = (Map) obj.getProperty(StructrApp.key(type, "returnTest"));
        final Object name = map.get("name");
        final Object value = map.get("value");
        final Object me = map.get("me");
        assertEquals("Invalid non-primitive scripting return value result, name should be of type string.", "test", name);
        assertEquals("Invalid non-primitive scripting return value result, value should be of type integer", Integer.valueOf(123), value);
        assertEquals("Invalid non-primitive scripting return value result, me should be the entity", obj, me);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : UnlicensedException(org.structr.common.error.UnlicensedException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) ActionContext(org.structr.schema.action.ActionContext) Map(java.util.Map) PropertyMap(org.structr.core.property.PropertyMap) SuperUser(org.structr.core.entity.SuperUser) NodeInterface(org.structr.core.graph.NodeInterface) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 5 with UnlicensedException

use of org.structr.common.error.UnlicensedException in project structr by structr.

the class LoginCommand method processMessage.

// ~--- methods --------------------------------------------------------
@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final String username = (String) webSocketData.getNodeData().get("username");
    final String password = (String) webSocketData.getNodeData().get("password");
    Principal user;
    if ((username != null) && (password != null)) {
        try {
            StructrWebSocket socket = this.getWebSocket();
            Authenticator auth = socket.getAuthenticator();
            user = auth.doLogin(socket.getRequest(), username, password);
            if (user != null) {
                String sessionId = webSocketData.getSessionId();
                if (sessionId == null) {
                    logger.debug("Unable to login {}: No sessionId found", new Object[] { username, password });
                    getWebSocket().send(MessageBuilder.status().code(403).build(), true);
                    return;
                }
                sessionId = SessionHelper.getShortSessionId(sessionId);
                try {
                    Actions.call(Actions.NOTIFICATION_LOGIN, user);
                } catch (UnlicensedException ex) {
                    ex.log(logger);
                }
                // Clear possible existing sessions
                SessionHelper.clearSession(sessionId);
                user.addSessionId(sessionId);
                // store token in response data
                webSocketData.getNodeData().clear();
                webSocketData.setSessionId(sessionId);
                webSocketData.getNodeData().put("username", user.getProperty(AbstractNode.name));
                // authenticate socket
                socket.setAuthenticated(sessionId, user);
                // send data..
                socket.send(webSocketData, false);
            }
        } catch (AuthenticationException e) {
            logger.info("Unable to login {}, probably wrong password", username);
            getWebSocket().send(MessageBuilder.status().code(403).build(), true);
        } catch (FrameworkException fex) {
            logger.warn("Unable to execute command", fex);
        }
    }
}
Also used : UnlicensedException(org.structr.common.error.UnlicensedException) FrameworkException(org.structr.common.error.FrameworkException) AuthenticationException(org.structr.core.auth.exception.AuthenticationException) StructrWebSocket(org.structr.websocket.StructrWebSocket) Principal(org.structr.core.entity.Principal) Authenticator(org.structr.core.auth.Authenticator)

Aggregations

UnlicensedException (org.structr.common.error.UnlicensedException)17 FrameworkException (org.structr.common.error.FrameworkException)13 Tx (org.structr.core.graph.Tx)11 ActionContext (org.structr.schema.action.ActionContext)11 Test (org.junit.Test)10 StructrTest (org.structr.common.StructrTest)10 TestOne (org.structr.core.entity.TestOne)7 GraphObject (org.structr.core.GraphObject)6 LinkedList (java.util.LinkedList)3 Principal (org.structr.core.entity.Principal)3 NodeInterface (org.structr.core.graph.NodeInterface)3 Date (java.util.Date)2 List (java.util.List)2 Locale (java.util.Locale)2 Group (org.structr.core.entity.Group)2 TestFour (org.structr.core.entity.TestFour)2 TestSix (org.structr.core.entity.TestSix)2 TestThree (org.structr.core.entity.TestThree)2 TestTwo (org.structr.core.entity.TestTwo)2 PropertyMap (org.structr.core.property.PropertyMap)2