Skip to content

Commit

Permalink
code modify.
Browse files Browse the repository at this point in the history
  • Loading branch information
yu199195 committed Nov 12, 2020
1 parent c6a4c86 commit 9f29408
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.dromara.soul.common.utils;

import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.SneakyThrows;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

/**
* The type Http param converter.
*/
public final class HttpParamConverter {

private static final Pattern PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");

/**
* of.
*
* @param supplier supplier
* @return String string
*/
public static String ofString(final Supplier<String> supplier) {
return GsonUtils.getInstance().toJson(initQueryParams(supplier.get()));
}

/**
* map.
*
* @param <K> the type parameter
* @param <V> the type parameter
* @param supplier supplier
* @return String string
*/
public static <K, V> String toMap(final Supplier<MultiValueMap<K,V>> supplier) {
return GsonUtils.getInstance().toJson(supplier.get().toSingleValueMap());
}

/**
* Init query params map.
*
* @param query the query
* @return the map
*/
static Map<String, String> initQueryParams(final String query) {
final Map<String, String> queryParams = new LinkedHashMap<>();
if (query != null) {
final Matcher matcher = PATTERN.matcher(query);
while (matcher.find()) {
String name = decodeQueryParam(matcher.group(1));
String eq = matcher.group(2);
String value = matcher.group(3);
value = value != null ? decodeQueryParam(value) : (StringUtils.hasLength(eq) ? "" : null);
queryParams.put(name, value);
}
}
return queryParams;
}

/**
* Decode query param string.
*
* @param value the value
* @return the string
*/
@SneakyThrows
static String decodeQueryParam(final String value) {
return URLDecoder.decode(value, "UTF-8");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.dromara.soul.common.constant.Constants;
import org.dromara.soul.common.enums.PluginEnum;
import org.dromara.soul.common.enums.RpcTypeEnum;
import org.dromara.soul.common.utils.UrlQuerys;
import org.dromara.soul.common.utils.HttpParamConverter;
import org.dromara.soul.plugin.api.SoulPlugin;
import org.dromara.soul.plugin.api.SoulPluginChain;
import org.dromara.soul.plugin.api.context.SoulContext;
Expand All @@ -40,16 +40,16 @@
* The type Body param plugin.
*/
public class BodyParamPlugin implements SoulPlugin {

private final List<HttpMessageReader<?>> messageReaders;

/**
* Instantiates a new Body param plugin.
*/
public BodyParamPlugin() {
this.messageReaders = HandlerStrategies.withDefaults().messageReaders();
}

@Override
public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) {
final ServerHttpRequest request = exchange.getRequest();
Expand All @@ -67,17 +67,17 @@ public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChai
}
return chain.execute(exchange);
}

@Override
public int getOrder() {
return PluginEnum.DUBBO.getCode() - 1;
}

@Override
public String named() {
return "alibaba-dubbo-body-param";
}

Mono<Void> body(ServerWebExchange exchange, ServerRequest serverRequest, SoulPluginChain chain) {
return serverRequest.bodyToMono(String.class)
.switchIfEmpty(Mono.defer(() -> Mono.just("")))
Expand All @@ -91,15 +91,14 @@ Mono<Void> formData(ServerWebExchange exchange, ServerRequest serverRequest, Sou
return serverRequest.formData()
.switchIfEmpty(Mono.defer(() -> Mono.just(new LinkedMultiValueMap<>())))
.flatMap(map -> {
exchange.getAttributes().put(Constants.DUBBO_PARAMS, UrlQuerys.map(() -> map));
exchange.getAttributes().put(Constants.DUBBO_PARAMS, HttpParamConverter.toMap(() -> map));
return chain.execute(exchange);
});
}

Mono<Void> query(ServerWebExchange exchange, ServerRequest serverRequest, SoulPluginChain chain) {
exchange.getAttributes().put(Constants.DUBBO_PARAMS,
UrlQuerys.of(() -> serverRequest.uri().getQuery()));
HttpParamConverter.ofString(() -> serverRequest.uri().getQuery()));
return chain.execute(exchange);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.dromara.soul.common.constant.Constants;
import org.dromara.soul.common.enums.PluginEnum;
import org.dromara.soul.common.enums.RpcTypeEnum;
import org.dromara.soul.common.utils.UrlQuerys;
import org.dromara.soul.common.utils.HttpParamConverter;
import org.dromara.soul.plugin.api.SoulPlugin;
import org.dromara.soul.plugin.api.SoulPluginChain;
import org.dromara.soul.plugin.api.context.SoulContext;
Expand All @@ -42,16 +42,16 @@
* @author xiaoyu
*/
public class BodyParamPlugin implements SoulPlugin {

private final List<HttpMessageReader<?>> messageReaders;

/**
* Instantiates a new Body param plugin.
*/
public BodyParamPlugin() {
this.messageReaders = HandlerStrategies.withDefaults().messageReaders();
}

@Override
public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) {
final ServerHttpRequest request = exchange.getRequest();
Expand All @@ -69,7 +69,7 @@ public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChai
}
return chain.execute(exchange);
}

@Override
public int getOrder() {
return PluginEnum.DUBBO.getCode() - 1;
Expand All @@ -88,21 +88,19 @@ Mono<Void> body(ServerWebExchange exchange, ServerRequest serverRequest, SoulPlu
return chain.execute(exchange);
});
}

Mono<Void> formData(ServerWebExchange exchange, ServerRequest serverRequest, SoulPluginChain chain) {
return serverRequest.formData()
.switchIfEmpty(Mono.defer(() -> Mono.just(new LinkedMultiValueMap<>())))
.flatMap(map -> {
exchange.getAttributes().put(Constants.DUBBO_PARAMS, UrlQuerys.map(() -> map));
exchange.getAttributes().put(Constants.DUBBO_PARAMS, HttpParamConverter.toMap(() -> map));
return chain.execute(exchange);
});
}

Mono<Void> query(ServerWebExchange exchange, ServerRequest serverRequest, SoulPluginChain chain) {
exchange.getAttributes().put(Constants.DUBBO_PARAMS,
UrlQuerys.of(() -> serverRequest.uri().getQuery()));
HttpParamConverter.ofString(() -> serverRequest.uri().getQuery()));
return chain.execute(exchange);
}


}

0 comments on commit 9f29408

Please sign in to comment.