Search in sources :

Example 1 with BaseController

use of info.xiancloud.gateway.controller.BaseController 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.createError(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 = IControllerMapper.getController(controllerRequest, new TransactionalNotifyHandler() {

        @Override
        protected void handle(UnitResponse unitResponse) {
            if (unitResponse == null) {
                Throwable emptyOutputException = new RuntimeException("UnitResponse is not allowed to be null.");
                LOG.error(emptyOutputException);
                unitResponse = UnitResponse.createException(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.createException(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.controller.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.controller.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.controller.BaseController)1 URIBean (info.xiancloud.gateway.controller.URIBean)1 TransactionalNotifyHandler (info.xiancloud.gateway.handle.TransactionalNotifyHandler)1 ServerResponseBean (info.xiancloud.gateway.server.ServerResponseBean)1