Search in sources :

Example 1 with EmptyPropertyToken

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.");
}
Also used : SecurityContext(org.structr.common.SecurityContext) App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) EmptyPropertyToken(org.structr.common.error.EmptyPropertyToken) FrameworkException(org.structr.common.error.FrameworkException) Tx(org.structr.core.graph.Tx) LogEvent(org.structr.rest.logging.entity.LogEvent) Date(java.util.Date) HttpServletRequest(javax.servlet.http.HttpServletRequest) PropertyMap(org.structr.core.property.PropertyMap) ErrorBuffer(org.structr.common.error.ErrorBuffer) File(java.io.File) RestMethodResult(org.structr.rest.RestMethodResult)

Example 2 with EmptyPropertyToken

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;
}
Also used : NullPropertyToken(org.structr.common.error.NullPropertyToken) EmptyPropertyToken(org.structr.common.error.EmptyPropertyToken)

Example 3 with EmptyPropertyToken

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;
}
Also used : EmptyPropertyToken(org.structr.common.error.EmptyPropertyToken) GraphObject(org.structr.core.GraphObject)

Example 4 with EmptyPropertyToken

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;
}
Also used : EmptyPropertyToken(org.structr.common.error.EmptyPropertyToken) TooShortToken(org.structr.common.error.TooShortToken)

Example 5 with EmptyPropertyToken

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);
    }
}
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)

Aggregations

EmptyPropertyToken (org.structr.common.error.EmptyPropertyToken)7 ErrorBuffer (org.structr.common.error.ErrorBuffer)3 FrameworkException (org.structr.common.error.FrameworkException)3 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 PropertyMap (org.structr.core.property.PropertyMap)2 RestMethodResult (org.structr.rest.RestMethodResult)2 File (java.io.File)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 SecurityContext (org.structr.common.SecurityContext)1 ThreadLocalMatcher (org.structr.common.ThreadLocalMatcher)1 NullPropertyToken (org.structr.common.error.NullPropertyToken)1 TooShortToken (org.structr.common.error.TooShortToken)1 GraphObject (org.structr.core.GraphObject)1 Relation (org.structr.core.entity.Relation)1 NodeInterface (org.structr.core.graph.NodeInterface)1 RelationshipInterface (org.structr.core.graph.RelationshipInterface)1 Tx (org.structr.core.graph.Tx)1