com.spring.boxes.mutant.resolver.ResolverTemplate Maven / Gradle / Ivy
The newest version!
package com.spring.boxes.mutant.resolver;
import com.spring.boxes.dollar.JSONUtils;
import com.spring.boxes.dollar.support.mutant.beans.GatewayInfo;
import com.spring.boxes.dollar.support.mutant.beans.MutantInfo;
import com.spring.boxes.dollar.support.mutant.pattern.SyntaxUtility;
import com.spring.boxes.dollar.support.mutant.resolver.ResolverInterface;
import com.spring.boxes.dollar.support.mutant.resolver.data.MixupContext;
import com.spring.boxes.dollar.support.mutant.resolver.data.ScriptContext;
import com.spring.boxes.dollar.support.mutant.resolver.type.*;
import com.spring.boxes.mutant.options.GatewayCacheOptions;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import java.util.Map;
import java.util.Objects;
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class ResolverTemplate {
private GatewayCacheOptions gatewayOptions;
private ApplicationContext applicationContext;
public Map mutantRequest(String path, Map source,
Map internalArgs, Map constantArgs,
boolean debug) {
GatewayInfo gatewayInfo = this.gatewayOptions.getGateWayInfo(path, debug);
log.info("[GatewayMutant][request] path:{} gatewayInfo:{}", path, gatewayInfo);
if (Objects.isNull(gatewayInfo) || Objects.isNull(gatewayInfo.getMutantInfo())) {
log.warn("[GatewayMutant][request] path:{} 无路由命中, 原样返回.", path);
return source;
}
MutantInfo mutantInfo = SyntaxUtility.mutantExpand(gatewayInfo.getMutantInfo(), internalArgs, constantArgs);
if (Objects.isNull(mutantInfo)) {
log.warn("[GatewayMutant][request] path:{} 无路由命中, 原样返回.", path);
return source;
}
return this.execute(mutantInfo.getInputResolver(), source, mutantInfo.getInputMapping(),
mutantInfo.getInternalArgs(), mutantInfo.getConstantArgs());
}
/**
* 处理映射响应的方法
*
* @param path 请求路径
* @param source 源参数
* @param internalArgs 内部参数
* @param constantArgs 常量参数
* @param debug 是否启用调试模式
* @return 处理结果的Map对象
*/
public Map mutantResponse(String path, Map source,
Map internalArgs, Map constantArgs,
boolean debug) {
GatewayInfo gatewayInfo = this.gatewayOptions.getGateWayInfo(path, debug);
log.info("[GatewayMutant][response] path:{} gatewayInfo:{}", path, gatewayInfo);
if (Objects.isNull(gatewayInfo) || Objects.isNull(gatewayInfo.getMutantInfo())) {
log.warn("[GatewayMutant][response] path:{} 无路由命中, 原样返回.", path);
return source;
}
MutantInfo mutantInfo = SyntaxUtility.mutantExpand(gatewayInfo.getMutantInfo(), internalArgs, constantArgs);
if (Objects.isNull(mutantInfo)) {
log.warn("[GatewayMutant][response] path:{} 无路由命中, 原样返回.", path);
return source;
}
return this.execute(mutantInfo.getOutputResolver(), source, mutantInfo.getOutputMapping(),
mutantInfo.getInternalArgs(), mutantInfo.getConstantArgs());
}
public Map execute(String type,
Map source, String sourceArgs,
Map internalMap, Map constantMap) {
String revolverType = StringUtils.uncapitalize(type);
Map resolverInterfaceMap = applicationContext.getBeansOfType(ResolverInterface.class);
ResolverInterface> resolverInterface = MapUtils.getObject(resolverInterfaceMap, revolverType);
if (resolverInterface instanceof DefaultResolver) {
return resolverInterface.revolver(source, null);
}
if (resolverInterface instanceof MixupResolver) {
MixupContext mixupContext = JSONUtils.fromJSON(sourceArgs, MixupContext.class);
if (Objects.isNull(mixupContext)) {
log.error("MixupContext Not Configured");
return source;
}
if (MapUtils.isNotEmpty(internalMap)) {
mixupContext.setInternalArgs(internalMap);
}
if (MapUtils.isNotEmpty(constantMap)) {
mixupContext.setConstantArgs(constantMap);
}
MixupResolver mixupResolver = (MixupResolver) resolverInterface;
return mixupResolver.revolver(source, mixupContext);
}
if (resolverInterface instanceof PathAviResolver) {
MixupContext mixupContext = JSONUtils.fromJSON(sourceArgs, MixupContext.class);
if (Objects.isNull(mixupContext)) {
log.error("PathAviResolver Not Configured");
return source;
}
if (MapUtils.isNotEmpty(internalMap)) {
mixupContext.setInternalArgs(internalMap);
}
if (MapUtils.isNotEmpty(constantMap)) {
mixupContext.setConstantArgs(constantMap);
}
PathAviResolver pathAviResolver = (PathAviResolver) resolverInterface;
return pathAviResolver.revolver(source, mixupContext);
}
if (resolverInterface instanceof Mvel2Resolver) {
ScriptContext scriptContext = JSONUtils.fromJSON(sourceArgs, ScriptContext.class);
Mvel2Resolver mvel2Resolver = (Mvel2Resolver) resolverInterface;
return mvel2Resolver.revolver(source, scriptContext);
}
if (resolverInterface instanceof QLExpressResolver) {
ScriptContext scriptContext = JSONUtils.fromJSON(sourceArgs, ScriptContext.class);
QLExpressResolver qlExpressResolver = (QLExpressResolver) resolverInterface;
return qlExpressResolver.revolver(source, scriptContext);
}
return source;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy