瀏覽代碼

Merge branch 'master' of luofaqiang/esb into master

luofaqiang 8 年之前
父節點
當前提交
9b94398384

+ 36 - 0
hos-camel/src/main/java/gateway/processor/ApiInfo.java

@ -0,0 +1,36 @@
package gateway.processor;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
/**
 * @author Airhead
 * @since 2017/3/15.
 */
public class ApiInfo {
    String apiInfo;
    String gatewayApi;
    String methodName;
    String method;
    String host;
    List<Param> paramList;
    public ApiInfo(String apiInfo) {
        this.apiInfo = apiInfo;
        this.parse();
    }
    private void parse() {
        ObjectMapper objectMapper = new ObjectMapper();
    }
    class Param {
        String name;
        String type;
        String dataType;
    }
}

+ 63 - 10
hos-camel/src/main/java/gateway/processor/GatewayProcessor.java

@ -1,6 +1,10 @@
package gateway.processor;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
@ -16,6 +20,8 @@ import java.util.stream.Stream;
public class GatewayProcessor implements Processor {
    static Map<String, ApiInfo> apiMap = new HashMap<>();
    public void process(Exchange exchange) throws Exception {
    }
@ -57,17 +63,11 @@ public class GatewayProcessor implements Processor {
            return "jetty:http://0.0.0.0:9999/error/unauthorizedError";    //TODO:
        }
        String api = params.get("api").toString();
        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, Object> params) {
@ -132,5 +132,58 @@ public class GatewayProcessor implements Processor {
                .collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));
    }
    private String genEndpoint(Map<String, Object> params, String[] requestBody) {
        String api = params.get("api").toString();                     // API接口名称
        String param = params.get("param").toString();
        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;
    }
}