|
@ -1,11 +1,16 @@
|
|
|
package gateway.processor;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.hos.core.datatype.DateUtil;
|
|
|
import com.yihu.hos.core.datatype.StringUtil;
|
|
|
import com.yihu.hos.core.http.HTTPResponse;
|
|
|
import com.yihu.hos.core.http.HttpClientKit;
|
|
|
import org.apache.camel.Body;
|
|
|
import org.apache.camel.Exchange;
|
|
|
import org.apache.camel.Processor;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.sql.Date;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.HashMap;
|
|
@ -15,10 +20,12 @@ import java.util.stream.Stream;
|
|
|
|
|
|
|
|
|
public class GatewayProcessor implements Processor {
|
|
|
static Map<String, ApiInfo> apiMap = new HashMap<>();
|
|
|
|
|
|
public void process(Exchange exchange) throws Exception {
|
|
|
}
|
|
|
|
|
|
public String route(@Body String body) {
|
|
|
public String route(@Body String body, Exchange exchange) {
|
|
|
Map<String, String> params = parseBody(body);
|
|
|
boolean pass = checkParams(params);
|
|
|
if (!pass) {
|
|
@ -40,17 +47,11 @@ public class GatewayProcessor implements Processor {
|
|
|
return "jetty:http://0.0.0.0:9999/error/unauthorizedError"; //TODO:
|
|
|
}
|
|
|
|
|
|
String api = params.get("api");
|
|
|
Map<String, String> apiRouters = new HashMap<>();
|
|
|
if (api == null) {
|
|
|
System.out.println("api参数为null");
|
|
|
} else if (api.equals("secret")) {
|
|
|
apiRouters.put("secret", "restlet:http://sdw2:10000/api/v1.0/admin/apps/Y3hpgpMRyI");
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
String[] requestBody = {""};
|
|
|
String endpoint = genEndpoint(params, requestBody);
|
|
|
exchange.getOut().setBody(requestBody);
|
|
|
|
|
|
return apiRouters.get(api);
|
|
|
return endpoint;
|
|
|
}
|
|
|
|
|
|
private boolean checkParams(Map<String, String> params) {
|
|
@ -69,7 +70,7 @@ public class GatewayProcessor implements Processor {
|
|
|
|
|
|
String timestamp = params.get("timestamp");
|
|
|
Date ts = DateUtil.toDateFromTime(timestamp);
|
|
|
if (ts == null){
|
|
|
if (ts == null) {
|
|
|
return false; //时间格式不正确
|
|
|
}
|
|
|
|
|
@ -113,5 +114,58 @@ public class GatewayProcessor implements Processor {
|
|
|
.collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));
|
|
|
}
|
|
|
|
|
|
private String genEndpoint(Map<String, String> params, String[] requestBody) {
|
|
|
String api = params.get("api"); // API接口名称
|
|
|
String param = params.get("param");
|
|
|
|
|
|
ApiInfo apiInfo = getApiInfo(api);
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
try {
|
|
|
if (apiInfo == null) {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
JsonNode jsonNode = objectMapper.readValue(param, JsonNode.class);
|
|
|
String host = apiInfo.host.split(",")[0];
|
|
|
String endPoint = "restlet:http://" + host + apiInfo.methodName + "restletMethods=" + apiInfo.method;
|
|
|
final String[] body = {""};
|
|
|
apiInfo.paramList.forEach(p -> {
|
|
|
String value = jsonNode.asText(p.name);
|
|
|
if (p.type.equals("2")) { //path param
|
|
|
endPoint.replace("{" + p.name + "}", value);
|
|
|
}
|
|
|
|
|
|
body[0] += ("&" + p.name + "=" + value);
|
|
|
|
|
|
});
|
|
|
|
|
|
if (body[0].length() != 0) {
|
|
|
requestBody[0] = body[0].substring(1);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
private ApiInfo getApiInfo(String api) {
|
|
|
ApiInfo apiInfo = apiMap.get(api);
|
|
|
if (apiInfo != null) {
|
|
|
return apiInfo;
|
|
|
}
|
|
|
|
|
|
|
|
|
HTTPResponse response = HttpClientKit.get("http://192.168.1.221:10000/api/v1.0/admin/appApi?fields=appId%2Cmethod%2Cprotocol%2CmethodName&filters=methodName%3D" + api + "&size=15&page=1");
|
|
|
if (response.getStatusCode() != 200) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
apiInfo = new ApiInfo(response.getBody());
|
|
|
apiMap.put(api, apiInfo);
|
|
|
|
|
|
return apiInfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|