Search in sources :

Example 36 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project java-chassis by ServiceComb.

the class CseClientHttpRequest method execute.

@Override
public ClientHttpResponse execute() {
    path = findUriPath(uri);
    requestMeta = createRequestMeta(method.name(), uri);
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri.getRawSchemeSpecificPart());
    queryParams = queryStringDecoder.parameters();
    Map<String, Object> swaggerArguments = this.collectArguments();
    // 异常流程,直接抛异常出去
    return this.invoke(swaggerArguments);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder)

Example 37 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder 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 new HashMap<>();
    // 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.remove(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);
        }
    }
    if (cookieAdditionalPropertiesValidator != null) {
        for (Map.Entry<String, List<String>> e : cookies.entrySet()) {
            try {
                Map<String, RequestParameter> r = new HashMap<>();
                r.put(e.getKey(), cookieAdditionalPropertiesValidator.isValidCollection(e.getValue()));
                RequestParameter parsedParam = new RequestParameterImpl(cookieAdditionalPropertiesObjectPropertyName, r);
                if (parsedParams.containsKey(cookieAdditionalPropertiesObjectPropertyName))
                    parsedParam = parsedParam.merge(parsedParams.get(cookieAdditionalPropertiesObjectPropertyName));
                parsedParams.put(parsedParam.getName(), parsedParam);
            } catch (ValidationException ex) {
                ex.setParameterName(cookieAdditionalPropertiesObjectPropertyName);
                e.setValue(e.getValue());
                throw ex;
            }
        }
    }
    return parsedParams;
}
Also used : RequestParameter(io.vertx.ext.web.api.RequestParameter) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) RequestParameterImpl(io.vertx.ext.web.api.impl.RequestParameterImpl) MultiMap(io.vertx.core.MultiMap)

Example 38 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project xian by happyyangyuan.

the class HttpUtil method parseQueryString.

/**
 * parse the given http query string
 *
 * @param queryString the standard http query string
 * @param hasPath     whether the query string contains uri
 * @return the parsed json object. if the given query string is empty then an empty json object is returned.
 */
public static JSONObject parseQueryString(String queryString, boolean hasPath) {
    JSONObject uriParameters = new JSONObject();
    if (queryString == null)
        return uriParameters;
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(queryString, hasPath);
    Map<String, List<String>> parameters = queryStringDecoder.parameters();
    parameters.forEach((key, values) -> {
        if (values == null || values.isEmpty()) {
            LOG.debug("空参数统一对应空字符串");
            uriParameters.put(key, "");
        } else if (values.size() == 1)
            uriParameters.put(key, values.get(0));
        else
            uriParameters.put(key, values);
    });
    return uriParameters;
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) JSONObject(com.alibaba.fastjson.JSONObject) List(java.util.List)

Example 39 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project vertx-web by vert-x3.

the class HttpServerRequestWrapper method params.

@Override
public MultiMap params() {
    if (!modified) {
        return delegate.params();
    }
    if (params == null) {
        params = MultiMap.caseInsensitiveMultiMap();
        // if there is no query it's not really needed to parse it
        if (query != null) {
            QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri);
            Map<String, List<String>> prms = queryStringDecoder.parameters();
            if (!prms.isEmpty()) {
                for (Map.Entry<String, List<String>> entry : prms.entrySet()) {
                    params.add(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    return params;
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) List(java.util.List) Map(java.util.Map)

Example 40 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project xian by happyyangyuan.

the class URIBean method checkUri.

/**
 * Check the uri is xian pattern or not.
 * xian pattern uri must starts with /${group}/${unit}
 * TODO combine URI checking with UriBean creation. And use UriBean reference instead of URI string later on for performance consideration.
 *
 * @param uri the URI to be checked
 * @return true if it is xian pattern false other wise.
 */
public static boolean checkUri(String uri) {
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri, true);
    String path = queryStringDecoder.path();
    int groupIndex = path.indexOf('/') + 1, unitIndex = path.indexOf('/', groupIndex) + 1;
    if (groupIndex == 0 || unitIndex == 0) {
        LOG.warn("URI is illegal: " + uri);
        return false;
    }
    return true;
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder)

Aggregations

QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)74 List (java.util.List)30 Test (org.junit.Test)15 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 Map (java.util.Map)11 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 URI (java.net.URI)7 ByteBuf (io.netty.buffer.ByteBuf)6 HttpContent (io.netty.handler.codec.http.HttpContent)6 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)6 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)6 ArrayList (java.util.ArrayList)6 HttpMethod (io.netty.handler.codec.http.HttpMethod)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 DeviceSession (org.traccar.DeviceSession)5 Position (org.traccar.model.Position)5 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)4 Channel (io.netty.channel.Channel)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3