Browse Source

统一网关错误返回接口

huangzhiyong 7 years ago
parent
commit
819d1908b1

+ 6 - 12
hos-camel2/src/main/java/camel/central/gateway/processor/GatewayProcessor.java

@ -46,42 +46,36 @@ public class GatewayProcessor implements Processor {
//        body = URLDecoder.decode(body, "UTF-8");
        Map<String, Object> params = exchange.getIn().getHeaders();
        if (params.get("appKey") == null) {
            exchange.getOut().setHeader(Exchange.HTTP_URI,"paramError");
            return "direct:errorHandle";    //TODO:
            return "restlet:http://0.0.0.0:8088/error/paramError?socketTimeout=60000&connectionTimeout=60000";    //TODO:
        }
        String appKey = params.get("appKey").toString();
        String secret = getSecret(appKey);
        if (secret.equals("")) {
            exchange.getOut().setHeader(Exchange.HTTP_URI,"paramError");
            return "direct:errorHandle";    //TODO:
            return "restlet:http://0.0.0.0:8088/error/paramError?socketTimeout=60000&connectionTimeout=60000";    //TODO:
        }
        boolean pass = checkParams(params);
        if (!pass) {
            exchange.getOut().setHeader(Exchange.HTTP_URI,"paramError");
            return "direct:errorHandle";    //TODO:
            return "jetty:http://0.0.0.0:8088/error/paramError?socketTimeout=60000&connectionTimeout=60000";    //TODO:
        }
        pass = checkTimeStamp(params);
        if (!pass) {
            exchange.getOut().setHeader(Exchange.HTTP_URI,"outdateError");
            return "direct:errorHandle";    //TODO:
            return "restlet:http://0.0.0.0:8088/error/outdataError?socketTimeout=60000&connectionTimeout=60000";    //TODO:
        }
        //获取secret接口,不做sign验证
        if (!"admin.apps.get".equals(params.get("api"))) {
            pass = checkSign(params, secret);
            if (!pass) {
                exchange.getOut().setHeader(Exchange.HTTP_URI,"signValidError");
                return "direct:errorHandle";    //TODO:
                return "restlet:http://0.0.0.0:8088/error/signValidError?socketTimeout=60000&connectionTimeout=60000";    //TODO:
            }
        }
        pass = checkAuthorized(params);
        if (!pass) {
            exchange.getOut().setHeader(Exchange.HTTP_URI,"unauthorizedError");
            return "direct:errorHandle";    //TODO:
            return "restlet:http://0.0.0.0:8088/error/unauthorizedError";    //TODO:
        }
        return genEndpoint(params, exchange);

+ 7 - 8
hos-camel2/src/main/java/camel/central/gateway/route/GatewayRouterBuilder.java

@ -1,6 +1,5 @@
package camel.central.gateway.route;
import camel.central.gateway.processor.ErrorHandle;
import camel.central.gateway.processor.GatewayProcessor;
import camel.central.gateway.processor.WsProcessor;
import org.apache.camel.Exchange;
@ -30,13 +29,13 @@ public class GatewayRouterBuilder extends RouteBuilder {
                .routingSlip(method(GatewayProcessor.class, "route"));
        from("direct:errorHandle")
                .choice()
                .when(header(Exchange.HTTP_URI).contains("paramError")).bean(new ErrorHandle(), "paramError")
                .when(header(Exchange.HTTP_URI).contains("outdateError")).bean(new ErrorHandle(), "outdateError")
                .when(header(Exchange.HTTP_URI).contains("signValidError")).bean(new ErrorHandle(), "signValidError")
                .when(header(Exchange.HTTP_URI).contains("unauthorizedError")).bean(new ErrorHandle(), "unauthorizedError")
                .endChoice();
//        from("direct:errorHandle")
//                .choice()
//                .when(header(Exchange.HTTP_URI).contains("paramError")).bean(new ErrorHandle(), "paramError")
//                .when(header(Exchange.HTTP_URI).contains("outdateError")).bean(new ErrorHandle(), "outdateError")
//                .when(header(Exchange.HTTP_URI).contains("signValidError")).bean(new ErrorHandle(), "signValidError")
//                .when(header(Exchange.HTTP_URI).contains("unauthorizedError")).bean(new ErrorHandle(), "unauthorizedError")
//                .endChoice();
        from("jetty:http://0.0.0.0:9999/healthy").routeId("healthy")
                .log("=========================心跳测试=====================");

+ 33 - 0
hos-rest/src/main/java/com/yihu/hos/rest/controllers/ErrorHandleController.java

@ -0,0 +1,33 @@
package com.yihu.hos.rest.controllers;
import com.yihu.hos.rest.services.gateway.ErrorHandleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/6/13.
 */
@RequestMapping("/")
@RestController("errorHandleController")
@Api(protocols = "http", value = "ErrorHandleController", description = "统一网关错误返回", tags = {"统一网关,错误返回"})
public class ErrorHandleController
{
    @Resource(name = ErrorHandleService.BEAN_ID)
    private ErrorHandleService errorHandleService;
    @RequestMapping(value = "/error/{errorCode}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ApiOperation(value = "统一网关错误信息获取", produces = "application/json", notes = "统一网关错误信息获取")
    public String test(HttpServletResponse response,
                       @ApiParam(name = "errorCode", value = "错误代码", required = true)
                       @PathVariable(name = "errorCode") String errorCode) {
        return errorHandleService.getError(response,errorCode);
    }
}

+ 43 - 0
hos-rest/src/main/java/com/yihu/hos/rest/services/gateway/ErrorHandleService.java

@ -0,0 +1,43 @@
package com.yihu.hos.rest.services.gateway;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/6/13.
 */
@Component("errorHandleService")
public class ErrorHandleService {
    public static final String BEAN_ID = "errorHandleService";
    private final static String PARAM_ERROR = "{\"successFlg\":false,\"pageSize\":10,\"currPage\":0,\"totalPage\":0,\"totalCount\":0,\"detailModelList\":null,\"obj\":null,\"errorMsg\":\"paramError!\",\"errorCode\":0}";
    private final static String OUTDATE_ERROR = "{\"successFlg\":false,\"pageSize\":10,\"currPage\":0,\"totalPage\":0,\"totalCount\":0,\"detailModelList\":null,\"obj\":null,\"errorMsg\":\"outdateError!\",\"errorCode\":0}";
    private final static String SIGN_VALID_ERROR = "{\"successFlg\":false,\"pageSize\":10,\"currPage\":0,\"totalPage\":0,\"totalCount\":0,\"detailModelList\":null,\"obj\":null,\"errorMsg\":\"signValidError!\",\"errorCode\":0}";
    private final static String UNAUTHORISED_ERROR = "{\"successFlg\":false,\"pageSize\":10,\"currPage\":0,\"totalPage\":0,\"totalCount\":0,\"detailModelList\":null,\"obj\":null,\"errorMsg\":\"unauthorizedError!\",\"errorCode\":0}";
    private final static String OTHER_ERROR = "{\"successFlg\":false,\"pageSize\":10,\"currPage\":0,\"totalPage\":0,\"totalCount\":0,\"detailModelList\":null,\"obj\":null,\"errorMsg\":\"otherError!\",\"errorCode\":0}";
    public String getError(HttpServletResponse response,String errorCode) {
        switch (errorCode){
            case "paramError":
                response.setHeader("errorCode",PARAM_ERROR);
                return PARAM_ERROR;
            case "outdateError":
                response.setHeader("errorCode",OUTDATE_ERROR);
                return OUTDATE_ERROR;
            case "signValidError":
                response.setHeader("errorCode",SIGN_VALID_ERROR);
                return SIGN_VALID_ERROR;
            case "unauthorizedError":
                response.setHeader("errorCode",UNAUTHORISED_ERROR);
                return UNAUTHORISED_ERROR;
            default:
                response.setHeader("errorCode",UNAUTHORISED_ERROR);
                return UNAUTHORISED_ERROR;
        }
    }
}