cloudstack-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jburwell <...@git.apache.org>
Subject [GitHub] cloudstack pull request: CLOUDSTACK-8562: Dynamic Role-Based API C...
Date Fri, 22 Apr 2016 13:34:18 GMT
Github user jburwell commented on a diff in the pull request:

    https://github.com/apache/cloudstack/pull/1489#discussion_r60738990
  
    --- Diff: server/src/com/cloud/api/dispatch/ParamProcessWorker.java ---
    @@ -92,6 +94,55 @@ public void handle(final DispatchTask task) {
             processParameters(task.getCmd(), task.getParams());
         }
     
    +    private void validateNonEmptyString(final Object param, final String argName) {
    +        if (param == null || Strings.isNullOrEmpty(param.toString())) {
    +            throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Empty
or null value provided for API arg: %s", argName));
    +        }
    +    }
    +
    +    private void validateNaturalNumber(final Object param, final String argName) {
    +        Long value = null;
    +        if (param != null && param instanceof Long) {
    +            value = (Long) param;
    +        } else if (param != null) {
    +            value = Long.valueOf(param.toString());
    +        }
    +        if (value == null || value < 1L) {
    +            throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Invalid
value provided for API arg: %s", argName));
    +        }
    +    }
    +
    +    private void validateField(final Object paramObj, final Parameter annotation) throws
ServerApiException {
    +        if (annotation == null) {
    +            return;
    +        }
    +        final String argName = annotation.name();
    +        for (final ApiArgValidator validator : annotation.validations()) {
    +            if (validator == null) {
    +                continue;
    +            }
    +            switch (validator) {
    +                case NotNullOrEmpty:
    +                    switch (annotation.type()) {
    +                        case UUID:
    +                        case STRING:
    +                            validateNonEmptyString(paramObj, argName);
    +                            break;
    +                    }
    +                    break;
    +                case PositiveNumber:
    +                    switch (annotation.type()) {
    +                        case SHORT:
    +                        case INTEGER:
    +                        case LONG:
    +                            validateNaturalNumber(paramObj, argName);
    +                            break;
    +                    }
    --- End diff --
    
    Why not place these validation methods on the ``ApiArgValidator`` instances?  Not only
would it simplify this code (i.e. reducing the switch to a single line), but it would also
make it easier to add new validators without changing this class.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message