Search in sources :

Example 1 with URIBean

use of info.xiancloud.gateway.controller.URIBean in project xian by happyyangyuan.

the class URIBeanTest method uriBeanTest.

@Test
public void uriBeanTest() {
    String uri = "/group/unit/extension-1/extension-2?a=1&a=1&a=2&c=&d=";
    URIBean bean = URIBean.create(uri);
    if (!"group".equals(bean.getGroup()))
        throw new RuntimeException("group检测失败");
    if (!"unit".equals(bean.getUnit()))
        throw new RuntimeException("unit检测失败");
    if (!"extension-1/extension-2".equals(bean.getUriExtension()))
        throw new RuntimeException("uriExtension检测失败");
    if (!"".equals(bean.getUriParameters().getString("c")))
        throw new RuntimeException("uriParameters检测失败");
    Assert.assertTrue(bean.getUriParameters().getJSONArray("a").size() == 3);
}
Also used : URIBean(info.xiancloud.gateway.controller.URIBean) Test(org.junit.Test)

Example 2 with URIBean

use of info.xiancloud.gateway.controller.URIBean 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

URIBean (info.xiancloud.gateway.controller.URIBean)2 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 TransactionalNotifyHandler (info.xiancloud.gateway.handle.TransactionalNotifyHandler)1 ServerResponseBean (info.xiancloud.gateway.server.ServerResponseBean)1 Test (org.junit.Test)1