Search in sources :

Example 1 with ServerResponseBean

use of info.xiancloud.gateway.server.ServerResponseBean in project xian by happyyangyuan.

the class AbstractAsyncForwarder method forward.

@Override
public void forward(String uri, String ip, String msgId, Map<String, String> header, String body) {
    LOG.debug("this method is running in the netty http server's io thread.");
    UnitRequest controllerRequest;
    try {
        controllerRequest = bodyParams(body, header);
    } catch (BadRequestException badRequestException) {
        LOG.warn(badRequestException);
        ServerResponseBean responseBean = new ServerResponseBean();
        responseBean.setMsgId(msgId);
        responseBean.setHttpContentType(HttpContentType.APPLICATION_JSON);
        responseBean.setResponseBody(UnitResponse.error(Group.CODE_BAD_REQUEST, null, badRequestException.getLocalizedMessage()).toVoJSONString());
        IServerResponder.singleton.response(responseBean);
        return;
    }
    RequestContext context = controllerRequest.getContext();
    URIBean uriBean = URIBean.create(uri);
    context.setUri(uri).setIp(ip).setHeader(header).setUnit(uriBean.getUnit()).setGroup(uriBean.getGroup()).setUriExtension(uriBean.getUriExtension()).setMsgId(msgId).setUriParameters(uriBean.getUriParameters());
    // uri parameters overwrite body parameters.
    controllerRequest.getArgMap().putAll(uriBean.getUriParameters());
    BaseController controller = IControllerMapping.getController(controllerRequest, new TransactionalNotifyHandler() {

        protected void handle(UnitResponse unitResponse) {
            if (unitResponse == null) {
                Throwable emptyOutputException = new RuntimeException("UnitResponse is not allowed to be null.");
                LOG.error(emptyOutputException);
                unitResponse = UnitResponse.exception(emptyOutputException);
            } else if (StringUtil.isEmpty(unitResponse.getCode())) {
                Throwable emptyOutputException = new RuntimeException("UnitResponse's code is not allowed to be empty: " + unitResponse);
                LOG.error(emptyOutputException);
                unitResponse = UnitResponse.exception(emptyOutputException);
            }
            ServerResponseBean responseBean = new ServerResponseBean();
            responseBean.setResponseBody(extractContext(unitResponse));
            responseBean.setMsgId(msgId);
            responseBean.setHttpContentType(unitResponse.getContext().getHttpContentType());
            IServerResponder.singleton.response(responseBean);
        }
    });
    ThreadPoolManager.execute(controller, msgId);
}
Also used : UnitRequest(info.xiancloud.core.message.UnitRequest) BaseController(info.xiancloud.gateway.executor.BaseController) UnitResponse(info.xiancloud.core.message.UnitResponse) BadRequestException(info.xiancloud.core.distribution.exception.BadRequestException) ServerResponseBean(info.xiancloud.gateway.server.ServerResponseBean) TransactionalNotifyHandler(info.xiancloud.gateway.handle.TransactionalNotifyHandler) RequestContext(info.xiancloud.core.message.RequestContext) URIBean(info.xiancloud.gateway.executor.URIBean)

Aggregations

BadRequestException (info.xiancloud.core.distribution.exception.BadRequestException)1 RequestContext (info.xiancloud.core.message.RequestContext)1 UnitRequest (info.xiancloud.core.message.UnitRequest)1 UnitResponse (info.xiancloud.core.message.UnitResponse)1 BaseController (info.xiancloud.gateway.executor.BaseController)1 URIBean (info.xiancloud.gateway.executor.URIBean)1 TransactionalNotifyHandler (info.xiancloud.gateway.handle.TransactionalNotifyHandler)1 ServerResponseBean (info.xiancloud.gateway.server.ServerResponseBean)1