pull/3/head
tanghc 4 years ago
parent 27d5b0135c
commit 4d260c72ab
  1. 84
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/ApiParam.java
  2. 76
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java
  3. 14
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/ParamBuilder.java

@ -16,7 +16,6 @@ import java.util.UUID;
public class ApiParam extends JSONObject implements Param {
public ApiParam() {
}
@ -25,39 +24,15 @@ public class ApiParam extends JSONObject implements Param {
}
private String requestId = UUID.randomUUID().toString().replace("-", "");
private boolean ignoreSign;
private boolean ignoreValidate;
private String restName;
private String restVersion;
private String serviceId;
private String ip;
private boolean restful;
private boolean mergeResult = true;
private boolean isGrayRequest;
private transient UploadContext uploadContext;
public static ApiParam createRestfulApiParam(String path) {
ApiParam apiParam = new ApiParam();
apiParam.setName(path);
apiParam.setVersion("");
apiParam.setRestful(true);
apiParam.setMergeResult(false);
return apiParam;
}
public void fitNameVersion() {
if (restName != null) {
this.put(ParamNames.API_NAME, restName);
}
if (restVersion != null) {
this.put(ParamNames.VERSION_NAME, restVersion);
}
}
public static ApiParam build(Map<String, ?> map) {
ApiParam apiParam = new ApiParam();
for (Map.Entry<String, ?> entry : map.entrySet()) {
@ -82,25 +57,8 @@ public class ApiParam extends JSONObject implements Param {
return sign;
}
/**
* 是否忽略验证签名
*
* @return 返回true忽略签名
*/
public boolean fetchIgnoreSign() {
return ignoreSign;
}
public void setIgnoreSign(boolean ignoreSign) {
this.ignoreSign = ignoreSign;
}
public boolean fetchIgnoreValidate() {
return ignoreValidate;
}
public void setIgnoreValidate(boolean ignoreValidate) {
this.ignoreValidate = ignoreValidate;
public void setName(String name) {
put(ParamNames.API_NAME, name);
}
/**
@ -108,15 +66,7 @@ public class ApiParam extends JSONObject implements Param {
*/
@Override
public String fetchName() {
String name = getString(ParamNames.API_NAME);
if (name == null) {
name = this.restName;
}
return name;
}
public void setName(String name) {
this.restName = name;
return getString(ParamNames.API_NAME);
}
public String fetchNameVersion() {
@ -139,15 +89,7 @@ public class ApiParam extends JSONObject implements Param {
*/
@Override
public String fetchVersion() {
String version = getString(ParamNames.VERSION_NAME);
if (version == null) {
version = this.restVersion;
}
return version;
}
public void setVersion(String version) {
this.restVersion = version;
return getString(ParamNames.VERSION_NAME);
}
/**
@ -162,6 +104,8 @@ public class ApiParam extends JSONObject implements Param {
put(ParamNames.APP_KEY_NAME, appKey);
}
/**
* 参数,urlencode后的
*/
@ -244,14 +188,6 @@ public class ApiParam extends JSONObject implements Param {
return super.hashCode();
}
public void setRestName(String restName) {
this.restName = restName;
}
public void setRestVersion(String restVersion) {
this.restVersion = restVersion;
}
public void setIp(String ip) {
this.ip = ip;
}
@ -284,12 +220,8 @@ public class ApiParam extends JSONObject implements Param {
this.mergeResult = mergeResult;
}
public boolean fetchRestful() {
return restful;
}
public void setRestful(boolean restful) {
this.restful = restful;
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public String fetchRequestId() {

@ -1,76 +0,0 @@
package com.gitee.sop.gatewaycommon.param;
import com.gitee.sop.gatewaycommon.bean.RouteDefinition;
import com.gitee.sop.gatewaycommon.bean.TargetRoute;
import com.gitee.sop.gatewaycommon.manager.RouteRepository;
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
import com.gitee.sop.gatewaycommon.message.ErrorEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import java.util.Optional;
/**
* @author tanghc
*/
@Slf4j
public abstract class BaseParamBuilder<T> implements ParamBuilder<T> {
/**
* 构建请求参数
*
* @param ctx 请求request
* @return 返回请求参数
*/
public abstract ApiParam buildRequestParams(T ctx);
/**
* 返回客户端ip
*
* @param ctx 请求request
* @return 返回ip
*/
public abstract String getIP(T ctx);
/**
* 将版本号添加到header中
*
* @param ctx 请求request
* @param headerName headerName
* @param version 版本号
*/
public abstract void setVersionInHeader(T ctx, String headerName, String version);
@Override
public ApiParam build(T ctx) {
ApiParam apiParam = this.buildRequestParams(ctx);
this.processApiParam(apiParam, ctx);
this.initOtherProperty(apiParam);
apiParam.setIp(this.getIP(ctx));
this.setVersionInHeader(ctx, ParamNames.HEADER_VERSION_NAME, apiParam.fetchVersion());
return apiParam;
}
protected void processApiParam(ApiParam param, T ctx) {
}
protected void initOtherProperty(ApiParam apiParam) {
RouteRepository<? extends TargetRoute> routeRepository = RouteRepositoryContext.getRouteRepository();
if (routeRepository == null) {
log.error("RouteRepositoryContext.setRouteRepository()方法未使用");
throw ErrorEnum.ISP_UNKNOWN_ERROR.getErrorMeta().getException();
}
String nameVersion = Optional.ofNullable(apiParam.fetchNameVersion()).orElse(String.valueOf(System.currentTimeMillis()));
TargetRoute targetRoute = routeRepository.get(nameVersion);
Integer ignoreValidate = Optional.ofNullable(targetRoute)
.map(TargetRoute::getRouteDefinition)
.map(RouteDefinition::getIgnoreValidate)
// 默认不忽略
.orElse(BooleanUtils.toInteger(false));
apiParam.setIgnoreValidate(BooleanUtils.toBoolean(ignoreValidate));
}
}

@ -1,14 +0,0 @@
package com.gitee.sop.gatewaycommon.param;
/**
* @author tanghc
*/
public interface ParamBuilder<T> {
/**
* 从request提取参数
* @param request
* @return 返回ApiParam
* @throws Exception
*/
ApiParam build(T request);
}
Loading…
Cancel
Save