Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dubbo支持query转body #336

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.dromara.soul.common.utils;

import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UrlQuerys {

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

/**
* getQuery.
*
* @param uri uri
* @return String string
*/
public static String getQuery(final String uri) {
return GsonUtils.getInstance().toJson(initQueryParams(uri));
}

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;
}

static String decodeQueryParam(final String value) {
try {
return URLDecoder.decode(value, "UTF-8");
} catch (UnsupportedEncodingException exception) {
return URLDecoder.decode(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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.plugin.api.SoulPlugin;
import org.dromara.soul.plugin.api.SoulPluginChain;
import org.dromara.soul.plugin.api.context.SoulContext;
Expand Down Expand Up @@ -60,6 +61,9 @@ public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChai
.flatMap(body -> {
if (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
exchange.getAttributes().put(Constants.DUBBO_PARAMS, body);
} else {
Copy link
Member

@yu199195 yu199195 Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该还可以处理下form表单, from表达,是不是也包含在body里面,不需要加 mdiaType的判断呢?试一试

exchange.getAttributes().put(Constants.DUBBO_PARAMS,
UrlQuerys.getQuery(request.getURI().getQuery()));
}
return chain.execute(exchange);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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.plugin.api.SoulPlugin;
import org.dromara.soul.plugin.api.SoulPluginChain;
import org.dromara.soul.plugin.api.context.SoulContext;
Expand Down Expand Up @@ -62,6 +63,9 @@ public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChai
.flatMap(body -> {
if (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
exchange.getAttributes().put(Constants.DUBBO_PARAMS, body);
} else {
exchange.getAttributes().put(Constants.DUBBO_PARAMS,
UrlQuerys.getQuery(request.getURI().getQuery()));
}
return chain.execute(exchange);
});
Expand Down