Browse Source

修复restlet参数传递问题

Airhead 8 years ago
parent
commit
19013a3d34
1 changed files with 22 additions and 18 deletions
  1. 22 18
      hos-camel2/src/main/java/camel/gateway/processor/GatewayProcessor.java

+ 22 - 18
hos-camel2/src/main/java/camel/gateway/processor/GatewayProcessor.java

@ -28,7 +28,7 @@ public class GatewayProcessor implements Processor {
        String appKey = exchange.getIn().getHeaders().get("appKey").toString();
        String secret = getSecret(appKey);
        exchange.setOut(inMessage);
        exchange.getOut().setHeader("secret",secret);
        exchange.getOut().setHeader("secret", secret);
    }
    public String secret(@Body String body, Exchange exchange) throws IOException, ParseException {
@ -37,11 +37,11 @@ public class GatewayProcessor implements Processor {
        return "restlet:http://sdw2:10000/api/v1.0/admin/apps/" + appKey;
    }
    public String route( Exchange exchange) throws IOException, ParseException {
    public String route(Exchange exchange) throws IOException, ParseException {
//        body = URLDecoder.decode(body, "UTF-8");
        Map<String, Object> params = exchange.getIn().getHeaders();
        String secret = params.get("secret").toString();
        if (secret==null){
        if (secret.equals("")) {
            return "jetty:http://0.0.0.0:9999/error/paramError";    //TODO:
        }
@ -124,7 +124,7 @@ public class GatewayProcessor implements Processor {
    private boolean checkAuthorized(Map<String, Object> params) {
        ObjectMapper objectMapper = new ObjectMapper();
        //TODO 设置固定的验证入口地址
        HTTPResponse response = HttpClientKit.get("http://localhost:10000/api/v1.0/admin/appApiAuth?appId=" + params.get("appKey") + "&apiName="+ params.get("api"));
        HTTPResponse response = HttpClientKit.get("http://192.168.131.109:10000/api/v1.0/admin/appApiAuth?appId=" + params.get("appKey") + "&apiName=" + params.get("api"));
        if (response.getStatusCode() != 200) {
            System.out.println("请求失败!");
            return false;
@ -132,10 +132,10 @@ public class GatewayProcessor implements Processor {
        try {
            Map map = objectMapper.readValue(response.getBody(), Map.class);
            if ((Boolean) map.get("successFlg")){
            if ((Boolean) map.get("successFlg")) {
                return true;
            }else {
                System.out.println("验证失败:"+map.get("errorMsg"));
            } else {
                System.out.println("验证失败:" + map.get("errorMsg"));
                return false;
            }
        } catch (IOException e) {
@ -171,17 +171,21 @@ public class GatewayProcessor implements Processor {
            final String[] endPoint = {"restlet:" + url + appApi.getMsMethodName() + "?socketTimeout=60000&connectionTimeout=60000&restletMethod=" + methodMap.get(appApi.getMethod())};
            final String[] body = {""};
            appApi.getParameters().forEach(p -> {
                String value = jsonNode.get(p.getName()).asText();
                JsonNode paramNode = jsonNode.get(p.getName());
                if (paramNode == null) {
                    return;
                }
                String value = paramNode.asText();
                if (p.getType().equals("0")) { //path param
                    endPoint[0] = endPoint[0].replaceAll("\\{[^}]*\\}", value);
                }
                body[0] += ("&" + p.getName() + "=" + value);
            });
            if (body[0].length() != 0) {
                exchange.getOut().setBody(body[0].substring(1));
                exchange.getOut().setHeader(Exchange.REST_HTTP_QUERY, body[0].substring(1));
            }
            return endPoint[0];
        } catch (IOException e) {
@ -216,28 +220,28 @@ public class GatewayProcessor implements Processor {
    private String getSecret(String appKey) {
        ObjectMapper objectMapper = new ObjectMapper();
        //TODO 设置固定的验证入口地址
        HTTPResponse response = HttpClientKit.get("http://localhost:10000/api/v1.0/admin/apps/" +appKey);
        HTTPResponse response = HttpClientKit.get("http://192.168.131.109:10000/api/v1.0/admin/apps/" + appKey);
        if (response.getStatusCode() != 200) {
            System.out.println("请求失败!");
            return null;
            return "";
        }
        try {
            Map map = objectMapper.readValue(response.getBody(), Map.class);
            if ((Boolean) map.get("successFlg")){
            if ((Boolean) map.get("successFlg")) {
                Map<String, Object> obj = (Map) map.get("obj");
                if (obj == null) {
                    return null;
                    return "";
                }
                String secret = obj.get("secret").toString();
                return secret;
            }else {
                System.out.println("验证失败:"+map.get("errorMsg"));
                return null;
            } else {
                System.out.println("验证失败:" + map.get("errorMsg"));
                return "";
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
            return "";
        }
    }