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;
}
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;
}
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);
}
}
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.");
}
}
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);
}
}
}
Aggregations