Search in sources :

Example 1 with RequestParameterImpl

use of io.vertx.ext.web.api.impl.RequestParameterImpl in project vertx-web by vert-x3.

the class ExpandedObjectFieldValidator method isValid.

/**
 * Function that checks if parameter is valid. It returns a RequestParameter object that will be linked inside
 * {@link RequestParameters}. For more info, check {@link RequestParameter}.
 *
 * @param value value of parameter to test
 * @return request parameter value
 */
@Override
public RequestParameter isValid(String value) throws ValidationException {
    RequestParameter requestParameter = innerValidator.isValid(value);
    requestParameter.setName(fieldName);
    Map<String, RequestParameter> map = new HashMap<>();
    map.put(fieldName, requestParameter);
    return new RequestParameterImpl(objectName, map);
}
Also used : HashMap(java.util.HashMap) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter)

Example 2 with RequestParameterImpl

use of io.vertx.ext.web.api.impl.RequestParameterImpl in project vertx-web by vert-x3.

the class BaseValidationHandler method validateCookieParams.

private Map<String, RequestParameter> validateCookieParams(RoutingContext routingContext) throws ValidationException {
    // Validation process validate only params that are registered in the validation -> extra params are allowed
    if (!routingContext.request().headers().contains("Cookie"))
        return null;
    // Some hack to reuse this object
    QueryStringDecoder decoder = new QueryStringDecoder("/?" + routingContext.request().getHeader("Cookie"));
    Map<String, List<String>> cookies = new HashMap<>();
    for (Map.Entry<String, List<String>> e : decoder.parameters().entrySet()) {
        String key = e.getKey().trim();
        if (cookies.containsKey(key))
            cookies.get(key).addAll(e.getValue());
        else
            cookies.put(key, e.getValue());
    }
    Map<String, RequestParameter> parsedParams = new HashMap<>();
    for (ParameterValidationRule rule : cookieParamsRules.values()) {
        String name = rule.getName().trim();
        if (cookies.containsKey(name)) {
            List<String> p = cookies.get(name);
            if (p.size() != 0) {
                RequestParameter parsedParam = rule.validateArrayParam(p);
                if (parsedParams.containsKey(parsedParam.getName()))
                    parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
                parsedParams.put(parsedParam.getName(), parsedParam);
            } else {
                throw ValidationException.ValidationExceptionFactory.generateNotMatchValidationException(name + " can't be empty");
            }
        } else {
            if (rule.parameterTypeValidator().getDefault() != null) {
                RequestParameter parsedParam = new RequestParameterImpl(name, rule.parameterTypeValidator().getDefault());
                if (parsedParams.containsKey(parsedParam.getName()))
                    parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
                parsedParams.put(parsedParam.getName(), parsedParam);
            } else if (!rule.isOptional())
                throw ValidationException.ValidationExceptionFactory.generateNotFoundValidationException(name, ParameterLocation.COOKIE);
        }
    }
    return parsedParams;
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter) MultiMap(io.vertx.core.MultiMap)

Example 3 with RequestParameterImpl

use of io.vertx.ext.web.api.impl.RequestParameterImpl in project vertx-web by vert-x3.

the class BaseValidationHandler method validateHeaderParams.

private Map<String, RequestParameter> validateHeaderParams(RoutingContext routingContext) throws ValidationException {
    // Validation process validate only params that are registered in the validation -> extra params are allowed
    Map<String, RequestParameter> parsedParams = new HashMap<>();
    MultiMap headersParams = routingContext.request().headers();
    for (ParameterValidationRule rule : headerParamsRules.values()) {
        String name = rule.getName();
        if (headersParams.contains(name)) {
            List<String> p = headersParams.getAll(name);
            if (p.size() != 0) {
                RequestParameter parsedParam = rule.validateArrayParam(p);
                if (parsedParams.containsKey(parsedParam.getName()))
                    parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
                parsedParams.put(parsedParam.getName(), parsedParam);
            } else {
                throw ValidationException.ValidationExceptionFactory.generateNotMatchValidationException(name + " can't be empty");
            }
        } else if (rule.parameterTypeValidator().getDefault() != null) {
            RequestParameter parsedParam = new RequestParameterImpl(name, rule.parameterTypeValidator().getDefault());
            if (parsedParams.containsKey(parsedParam.getName()))
                parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
            parsedParams.put(parsedParam.getName(), parsedParam);
        } else if (!rule.isOptional())
            throw ValidationException.ValidationExceptionFactory.generateNotFoundValidationException(name, ParameterLocation.HEADER);
    }
    return parsedParams;
}
Also used : MultiMap(io.vertx.core.MultiMap) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter)

Example 4 with RequestParameterImpl

use of io.vertx.ext.web.api.impl.RequestParameterImpl in project vertx-web by vert-x3.

the class BaseValidationHandler method validateFormParams.

private Map<String, RequestParameter> validateFormParams(RoutingContext routingContext) throws ValidationException {
    // Validation process validate only params that are registered in the validation -> extra params are allowed
    Map<String, RequestParameter> parsedParams = new HashMap<>();
    MultiMap formParams = routingContext.request().formAttributes();
    for (ParameterValidationRule rule : formParamsRules.values()) {
        String name = rule.getName();
        if (formParams.contains(name)) {
            List<String> p = formParams.getAll(name);
            if (p.size() != 0) {
                RequestParameter parsedParam = rule.validateArrayParam(p);
                if (parsedParams.containsKey(parsedParam.getName()))
                    parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
                parsedParams.put(parsedParam.getName(), parsedParam);
            } else {
                throw ValidationException.ValidationExceptionFactory.generateNotMatchValidationException(name + " can't be empty");
            }
        } else if (rule.parameterTypeValidator().getDefault() != null) {
            RequestParameter parsedParam = new RequestParameterImpl(name, rule.parameterTypeValidator().getDefault());
            if (parsedParams.containsKey(parsedParam.getName()))
                parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
            parsedParams.put(parsedParam.getName(), parsedParam);
        } else if (!rule.isOptional())
            throw ValidationException.ValidationExceptionFactory.generateNotFoundValidationException(name, ParameterLocation.BODY_FORM);
    }
    return parsedParams;
}
Also used : MultiMap(io.vertx.core.MultiMap) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter)

Example 5 with RequestParameterImpl

use of io.vertx.ext.web.api.impl.RequestParameterImpl in project vertx-web by vert-x3.

the class BaseValidationHandler method validateQueryParams.

private Map<String, RequestParameter> validateQueryParams(RoutingContext routingContext) throws ValidationException {
    // Validation process validate only params that are registered in the validation -> extra params are allowed
    Map<String, RequestParameter> parsedParams = new HashMap<>();
    MultiMap queryParams = routingContext.queryParams();
    for (ParameterValidationRule rule : queryParamsRules.values()) {
        String name = rule.getName();
        if (queryParams.contains(name)) {
            List<String> p = queryParams.getAll(name);
            if (p.size() != 0) {
                RequestParameter parsedParam = rule.validateArrayParam(p);
                if (parsedParams.containsKey(parsedParam.getName()))
                    parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
                parsedParams.put(parsedParam.getName(), parsedParam);
            } else {
                throw ValidationException.ValidationExceptionFactory.generateNotMatchValidationException(name + " can't be empty");
            }
        } else if (rule.parameterTypeValidator().getDefault() != null) {
            RequestParameter parsedParam = new RequestParameterImpl(name, rule.parameterTypeValidator().getDefault());
            if (parsedParams.containsKey(parsedParam.getName()))
                parsedParam = parsedParam.merge(parsedParams.get(parsedParam.getName()));
            parsedParams.put(parsedParam.getName(), parsedParam);
        } else if (!rule.isOptional())
            throw ValidationException.ValidationExceptionFactory.generateNotFoundValidationException(name, ParameterLocation.QUERY);
    }
    return parsedParams;
}
Also used : MultiMap(io.vertx.core.MultiMap) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) RequestParameter(io.vertx.ext.web.api.RequestParameter)

Aggregations

RequestParameter (io.vertx.ext.web.api.RequestParameter)5 RequestParameterImpl (io.vertx.ext.web.api.impl.RequestParameterImpl)5 MultiMap (io.vertx.core.MultiMap)4 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)1 HashMap (java.util.HashMap)1