use of org.structr.common.error.EmptyPropertyToken in project structr by structr.
the class LogResource method doPost.
@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
final HttpServletRequest request = securityContext.getRequest();
if (request != null) {
// initialize?!
if ("true".equals(request.getParameter("initialize"))) {
final String filesPath = Settings.FilesPath.getValue();
try (final Context context = new Context(1000)) {
collectFilesAndStore(context, new File(filesPath + SUBJECTS).toPath(), 0);
} catch (FrameworkException fex) {
logger.warn("", fex);
}
return new RestMethodResult(200);
}
final String subjectId = (String) propertySet.get(subjectProperty.jsonName());
final String objectId = (String) propertySet.get(objectProperty.jsonName());
final String action = (String) propertySet.get(actionProperty.jsonName());
final String message = (String) propertySet.get(messageProperty.jsonName());
if (subjectId != null && objectId != null && action != null) {
final App app = StructrApp.getInstance(securityContext);
LogEvent event = null;
try (final Tx tx = app.tx()) {
final PropertyMap properties = new PropertyMap();
properties.put(LogEvent.timestampProperty, new Date());
properties.put(LogEvent.actionProperty, action);
properties.put(LogEvent.subjectProperty, subjectId);
properties.put(LogEvent.objectProperty, objectId);
properties.put(LogEvent.messageProperty, message);
properties.put(LogEvent.visibleToPublicUsers, true);
properties.put(LogEvent.visibleToAuthenticatedUsers, true);
event = app.create(LogEvent.class, properties);
tx.success();
}
final RestMethodResult result = new RestMethodResult(201);
result.addContent(event);
return result;
} else {
final ErrorBuffer errorBuffer = new ErrorBuffer();
if (StringUtils.isEmpty(subjectId)) {
errorBuffer.add(new EmptyPropertyToken("LogFile", subjectProperty));
}
if (StringUtils.isEmpty(objectId)) {
errorBuffer.add(new EmptyPropertyToken("LogFile", objectProperty));
}
if (StringUtils.isEmpty(action)) {
errorBuffer.add(new EmptyPropertyToken("LogFile", actionProperty));
}
throw new FrameworkException(422, "Log entry must consist of at least subjectId, objectId and action", errorBuffer);
}
}
// no request object, this is fatal
throw new FrameworkException(500, "No request object present, aborting.");
}
use of org.structr.common.error.EmptyPropertyToken in project structr by structr.
the class ValidatedNode method nonEmpty.
protected boolean nonEmpty(Property<String> property, ErrorBuffer errorBuffer) {
String value = getProperty(property);
boolean valid = true;
if (value == null) {
errorBuffer.add(new NullPropertyToken(getClass().getSimpleName(), property));
valid = false;
} else if (value.isEmpty()) {
errorBuffer.add(new EmptyPropertyToken(getClass().getSimpleName(), property));
valid = false;
}
return valid;
}
use of org.structr.common.error.EmptyPropertyToken in project structr by structr.
the class ValidationHelper method isValidPropertyNotNull.
/**
* Checks whether the value for the given property key of the given node
* is a non-empty string.
*
* @param node the node
* @param key the property key
* @param errorBuffer the error buffer
*
* @return true if the condition is valid
*/
public static boolean isValidPropertyNotNull(final GraphObject node, final PropertyKey key, final ErrorBuffer errorBuffer) {
final String type = node.getType();
if (key == null) {
errorBuffer.add(new EmptyPropertyToken(type, UnknownType));
return false;
}
final Object value = node.getProperty(key);
if (value != null) {
if (value instanceof Iterable) {
if (((Iterable) value).iterator().hasNext()) {
return true;
}
} else {
return true;
}
}
errorBuffer.add(new EmptyPropertyToken(type, key));
return false;
}
use of org.structr.common.error.EmptyPropertyToken in project structr by structr.
the class ValidationHelper method isValidStringMinLength.
// ----- public static methods -----
/**
* Checks whether the value for the given property key of the given node
* has at least the given length.
*
* @param node the node
* @param key the property key whose value should be checked
* @param minLength the min length
* @param errorBuffer the error buffer
*
* @return true if the condition is valid
*/
public static boolean isValidStringMinLength(final GraphObject node, final PropertyKey<String> key, final int minLength, final ErrorBuffer errorBuffer) {
String value = node.getProperty(key);
String type = node.getType();
if (StringUtils.isNotBlank(value)) {
if (value.length() >= minLength) {
return true;
}
errorBuffer.add(new TooShortToken(type, key, minLength));
return false;
}
errorBuffer.add(new EmptyPropertyToken(type, key));
return false;
}
use of org.structr.common.error.EmptyPropertyToken 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);
}
}
Aggregations