Search in sources :

Example 61 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class DefaultShenyuContextBuilder method build.

@Override
public ShenyuContext build(final ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    String path = request.getURI().getPath();
    MetaData metaData = MetaDataCache.getInstance().obtain(path);
    HttpHeaders headers = request.getHeaders();
    String upgrade = headers.getFirst(UPGRADE);
    String rpcType;
    if (Objects.nonNull(metaData) && Boolean.TRUE.equals(metaData.getEnabled())) {
        exchange.getAttributes().put(Constants.META_DATA, metaData);
        rpcType = metaData.getRpcType();
    } else if (StringUtils.isNotEmpty(upgrade) && RpcTypeEnum.WEB_SOCKET.getName().equals(upgrade)) {
        rpcType = RpcTypeEnum.WEB_SOCKET.getName();
    } else {
        String rpcTypeParam = request.getHeaders().getFirst(RPC_TYPE);
        rpcType = StringUtils.isEmpty(rpcTypeParam) ? RpcTypeEnum.HTTP.getName() : rpcTypeParam;
    }
    return decoratorMap.get(rpcType).decorator(buildDefaultContext(request), metaData);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MetaData(org.apache.shenyu.common.dto.MetaData) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 62 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class ApacheDubboMetaDataSubscriberTest method setUp.

@BeforeEach
public void setUp() {
    apacheDubboMetaDataSubscriber = new ApacheDubboMetaDataSubscriber();
    metaData = new MetaData();
    metaData.setId("1332017966661636096");
    metaData.setAppName("dubbo");
    metaData.setPath("/dubbo/findAll");
    metaData.setServiceName("org.apache.shenyu.test.dubbo.api.service.DubboTestService");
    metaData.setMethodName("findAll");
    metaData.setRpcType(RpcTypeEnum.DUBBO.getName());
    metaData.setRpcExt("{\"group\":\"Group\",\"version\":\"2.7.5\",\"loadbalance\":\"Balance\",\"url\":\"http://192.168.55.113/dubbo\"}");
    metaData.setParameterTypes("parameterTypes");
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 63 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class AbstractDubboPlugin method doExecute.

/**
 * this is Template Method child has Implement your own logic.
 *
 * @param exchange exchange the current server exchange {@linkplain ServerWebExchange}
 * @param chain    chain the current chain  {@linkplain ServerWebExchange}
 * @param selector selector    {@linkplain SelectorData}
 * @param rule     rule    {@linkplain RuleData}
 * @return {@code Mono<Void>} to indicate when request handling is complete
 */
@Override
public Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
    String param = exchange.getAttribute(Constants.PARAM_TRANSFORM);
    ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
    assert shenyuContext != null;
    MetaData metaData = exchange.getAttribute(Constants.META_DATA);
    if (!checkMetaData(metaData)) {
        LOG.error(" path is : {}, meta data have error : {}", shenyuContext.getPath(), metaData);
        exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.META_DATA_ERROR, null);
        return WebFluxResultUtils.result(exchange, error);
    }
    if (Objects.nonNull(metaData) && StringUtils.isNoneBlank(metaData.getParameterTypes()) && StringUtils.isBlank(param)) {
        exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.DUBBO_HAVE_BODY_PARAM, null);
        return WebFluxResultUtils.result(exchange, error);
    }
    this.rpcContext(exchange);
    return this.doDubboInvoker(exchange, chain, selector, rule, metaData, param);
}
Also used : ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) MetaData(org.apache.shenyu.common.dto.MetaData)

Example 64 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class GrpcShenyuContextDecoratorTest method testDecorator.

@Test
public void testDecorator() {
    MetaData metaData = new MetaData();
    metaData.setAppName("grpc");
    metaData.setServiceName("echo");
    metaData.setRpcType(PluginEnum.GRPC.getName());
    metaData.setContextPath("/grpc");
    final ShenyuContext shenyuContext = grpcShenyuContextDecorator.decorator(new ShenyuContext(), metaData);
    assertNotNull(shenyuContext);
    assertNotNull(shenyuContext.getContextPath());
    assertNotNull(shenyuContext.getMethod());
    assertNotNull(shenyuContext.getModule());
}
Also used : ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) MetaData(org.apache.shenyu.common.dto.MetaData) Test(org.junit.jupiter.api.Test)

Example 65 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class AlibabaDubboMetaDataSubscriberTest method setUp.

@BeforeEach
public void setUp() {
    alibabaDubboMetaDataSubscriber = new AlibabaDubboMetaDataSubscriber();
    metaData = new MetaData();
    metaData.setId("1332017966661636096");
    metaData.setAppName("dubbo");
    metaData.setPath("/dubbo/findAll");
    metaData.setServiceName("org.apache.shenyu.test.dubbo.api.service.DubboTestService");
    metaData.setMethodName("findAll");
    metaData.setRpcType(RpcTypeEnum.DUBBO.getName());
    metaData.setRpcExt("{\"group\":\"Group\",\"version\":\"2.6.5\",\"url\":\"http://192.168.55.113/dubbo\"}");
    metaData.setParameterTypes("parameterTypes");
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MetaData (org.apache.shenyu.common.dto.MetaData)70 Test (org.junit.jupiter.api.Test)31 BeforeEach (org.junit.jupiter.api.BeforeEach)14 RuleData (org.apache.shenyu.common.dto.RuleData)11 SelectorData (org.apache.shenyu.common.dto.SelectorData)10 ShenyuContext (org.apache.shenyu.plugin.api.context.ShenyuContext)9 ArrayList (java.util.ArrayList)8 MetaDataSubscriber (org.apache.shenyu.sync.data.api.MetaDataSubscriber)8 ServerWebExchange (org.springframework.web.server.ServerWebExchange)7 Objects (java.util.Objects)6 ShenyuException (org.apache.shenyu.common.exception.ShenyuException)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Optional (java.util.Optional)5 Constants (org.apache.shenyu.common.constant.Constants)5 ShenyuPluginChain (org.apache.shenyu.plugin.api.ShenyuPluginChain)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ResultEnum (org.apache.shenyu.common.enums.ResultEnum)4 RpcTypeEnum (org.apache.shenyu.common.enums.RpcTypeEnum)4 GsonUtils (org.apache.shenyu.common.utils.GsonUtils)4