Search in sources :

Example 6 with HeadDto

use of com.duangframework.core.common.dto.result.HeadDto in project duangframework by tcrct.

the class initHandle method builderRequestHeadDto.

/**
 *  构建请求DTO,并将该DTO设置到ThreadLocal变量中
 * @param request       请求对象
 */
private void builderRequestHeadDto(String target, IRequest request, IResponse response) {
    HeadDto headDto = new HeadDto();
    // 设置请求ID
    String requestId = (String) request.getAttribute(Const.DUANG_REQUEST_ID);
    if (ToolsKit.isEmpty(requestId)) {
        requestId = ConfigKit.duang().key("product.code").asString() + "_" + DuangId.get();
        request.setAttribute(Const.DUANG_REQUEST_ID, requestId);
    }
    // 设置客户端IP
    String remoteClientIp = request.getHeader(Const.FORWARDED_FOR);
    // 设置服务器端IP
    if (ToolsKit.isEmpty(LOCALHOST_IP)) {
        LOCALHOST_IP = IpUtils.getLocalHostIP(true, true);
    }
    if (ToolsKit.isNotEmpty(LOCALHOST_IP)) {
        response.setHeader(Const.REMOTE_SERVICE_IP, LOCALHOST_IP);
    }
    if (ToolsKit.isEmpty(remoteClientIp)) {
        remoteClientIp = request.getHeader("X-Real-IP");
    }
    if (ToolsKit.isEmpty(remoteClientIp)) {
        remoteClientIp = request.getHeader("Host");
    }
    if (ToolsKit.isEmpty(remoteClientIp)) {
        remoteClientIp = request.getRemoteHost();
    }
    if (ToolsKit.isNotEmpty(remoteClientIp)) {
        response.setHeader(Const.REMOTE_CLIENT_IP, remoteClientIp.split(",")[0]);
    }
    headDto.setUri(target);
    headDto.setRequestId(requestId);
    headDto.setTimestamp(System.currentTimeMillis());
    headDto.setHeaderMap(request.getHeaderMap());
    headDto.setMethod(request.getMethod());
    headDto.setRequestId((String) request.getAttribute(Const.DUANG_REQUEST_ID));
    headDto.setClientId(response.getHeader(Const.REMOTE_CLIENT_IP));
    ToolsKit.setThreadLocalDto(headDto);
}
Also used : HeadDto(com.duangframework.core.common.dto.result.HeadDto)

Example 7 with HeadDto

use of com.duangframework.core.common.dto.result.HeadDto in project duangframework by tcrct.

the class HttpBaseHandler method channelRead0.

@Override
public void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest request) throws Exception {
    try {
        RequestUtils.verificationRequest(request);
        new ActionHandler(bootStrap, ctx, request).run();
    } catch (VerificationException ve) {
        logger.warn(ve.getMessage());
        ReturnDto<String> returnDto = new ReturnDto<>();
        HeadDto headDto = new HeadDto();
        headDto.setUri(request.uri());
        headDto.setTimestamp(System.currentTimeMillis());
        headDto.setRet(500);
        headDto.setMsg(ve.getMessage());
        returnDto.setData(ve.getMessage());
        returnDto.setHead(headDto);
        ResponseUtils.buildFullHttpResponse(ctx, request, ToolsKit.toJsonString(returnDto));
    }
}
Also used : HeadDto(com.duangframework.core.common.dto.result.HeadDto) VerificationException(com.duangframework.core.exceptions.VerificationException) ReturnDto(com.duangframework.core.common.dto.result.ReturnDto)

Aggregations

HeadDto (com.duangframework.core.common.dto.result.HeadDto)7 ReturnDto (com.duangframework.core.common.dto.result.ReturnDto)6 JSONObject (com.alibaba.fastjson.JSONObject)3 HttpResponse (com.duangframework.core.common.dto.http.response.HttpResponse)1 IEnums (com.duangframework.core.common.enums.IEnums)1 ServiceException (com.duangframework.core.exceptions.ServiceException)1 VerificationException (com.duangframework.core.exceptions.VerificationException)1 JsonRender (com.duangframework.mvc.render.JsonRender)1