Skip to content

Commit

Permalink
[type: refactor] Optimize how to build rpcType (#3207)
Browse files Browse the repository at this point in the history
  • Loading branch information
yu199195 authored Apr 6, 2022
1 parent de9f077 commit c874d65
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
package org.apache.shenyu.plugin.global;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
import org.apache.shenyu.plugin.api.context.ShenyuContextBuilder;
import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
import org.apache.shenyu.plugin.global.cache.MetaDataCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
Expand All @@ -41,8 +40,6 @@
*/
public class DefaultShenyuContextBuilder implements ShenyuContextBuilder {

private static final Logger LOG = LoggerFactory.getLogger(GlobalPlugin.class);

private static final String RPC_TYPE = "rpc_type";

private static final String UPGRADE = "Upgrade";
Expand All @@ -60,22 +57,28 @@ public DefaultShenyuContextBuilder(final Map<String, ShenyuContextDecorator> dec

@Override
public ShenyuContext build(final ServerWebExchange exchange) {
Pair<String, MetaData> buildData = buildData(exchange);
return decoratorMap.get(buildData.getLeft()).decorator(buildDefaultContext(exchange.getRequest()), buildData.getRight());
}

private Pair<String, MetaData> buildData(final ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
String path = request.getURI().getPath();
MetaData metaData = MetaDataCache.getInstance().obtain(path);
HttpHeaders headers = request.getHeaders();
String rpcType = headers.getFirst(RPC_TYPE);
if (StringUtils.isNotEmpty(rpcType)) {
return Pair.of(rpcType, new MetaData());
}
String upgrade = headers.getFirst(UPGRADE);
String rpcType;
if (StringUtils.isNotEmpty(upgrade) && RpcTypeEnum.WEB_SOCKET.getName().equals(upgrade)) {
return Pair.of(RpcTypeEnum.WEB_SOCKET.getName(), new MetaData());
}
MetaData metaData = MetaDataCache.getInstance().obtain(request.getURI().getPath());
if (Objects.nonNull(metaData) && Boolean.TRUE.equals(metaData.getEnabled())) {
exchange.getAttributes().put(Constants.META_DATA, metaData);
rpcType = metaData.getRpcType();
} else if (StringUtils.isNotEmpty(upgrade) && RpcTypeEnum.WEB_SOCKET.getName().equals(upgrade)) {
rpcType = RpcTypeEnum.WEB_SOCKET.getName();
return Pair.of(metaData.getRpcType(), metaData);
} else {
String rpcTypeParam = request.getHeaders().getFirst(RPC_TYPE);
rpcType = StringUtils.isEmpty(rpcTypeParam) ? RpcTypeEnum.HTTP.getName() : rpcTypeParam;
return Pair.of(RpcTypeEnum.HTTP.getName(), new MetaData());
}
return decoratorMap.get(rpcType).decorator(buildDefaultContext(request), metaData);
}

private ShenyuContext buildDefaultContext(final ServerHttpRequest request) {
Expand Down

0 comments on commit c874d65

Please sign in to comment.