com.zepben.vertxutils.routing.handlers.QueryParamsHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vertx-utils Show documentation
Show all versions of vertx-utils Show documentation
Helpers and utils for working with Vert.x in Zepben projects.
/*
* Copyright 2020 Zeppelin Bend Pty Ltd
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.zepben.vertxutils.routing.handlers;
import com.zepben.annotations.EverythingIsNonnullByDefault;
import com.zepben.vertxutils.routing.ErrorFormatter;
import com.zepben.vertxutils.routing.Respond;
import com.zepben.vertxutils.routing.RoutingContextEx;
import com.zepben.vertxutils.routing.handlers.params.*;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import java.util.*;
import static java.util.stream.Collectors.toMap;
@EverythingIsNonnullByDefault
public class QueryParamsHandler implements Handler {
private final Map> rules;
public QueryParamsHandler(QueryParamRule>... rules) {
this(Arrays.asList(rules));
}
@SuppressWarnings("WeakerAccess")
public QueryParamsHandler(Collection> rules) {
if (rules.stream().map(ParamRule::name).distinct().count() != rules.size())
throw new IllegalArgumentException("INTERNAL ERROR: The rules you have passed have a duplicate key.");
this.rules = rules.stream().collect(toMap(ParamRule::name, r -> r));
}
@SuppressWarnings("ConstantConditions")
@Override
public void handle(RoutingContext context) {
Map> params = new HashMap<>();
List errors = new ArrayList<>();
for (QueryParamRule> rule : rules.values()) {
try {
List strValues = context.queryParam(rule.name());
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy