use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.
the class GrpcPluginTest method testDoExecuteParaIsBlankError.
@Test
public void testDoExecuteParaIsBlankError() {
ServerWebExchange exchange = getServerWebExchange();
exchange.getAttributes().put(Constants.META_DATA, new MetaData());
RuleData data = mock(RuleData.class);
StepVerifier.create(grpcPlugin.doExecute(exchange, chain, selector, data)).expectSubscription().verifyComplete();
}
use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.
the class TarsPlugin method doExecute.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
String body = exchange.getAttribute(Constants.PARAM_TRANSFORM);
ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
assert shenyuContext != null;
MetaData metaData = exchange.getAttribute(Constants.META_DATA);
if (!checkMetaData(metaData)) {
assert metaData != null;
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 (StringUtils.isNoneBlank(metaData.getParameterTypes()) && StringUtils.isBlank(body)) {
exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.TARS_HAVE_BODY_PARAM, null);
return WebFluxResultUtils.result(exchange, error);
}
TarsInvokePrxList tarsInvokePrxList = ApplicationConfigCache.getInstance().get(metaData.getPath());
int index = RANDOM.nextInt(tarsInvokePrxList.getTarsInvokePrxList().size());
Object prx = tarsInvokePrxList.getTarsInvokePrxList().get(index).getInvokePrx();
Method method = tarsInvokePrxList.getMethod();
CompletableFuture future;
try {
future = (CompletableFuture) method.invoke(prx, PrxInfoUtil.getParamArray(tarsInvokePrxList.getParamTypes(), tarsInvokePrxList.getParamNames(), body));
} catch (Exception e) {
LOG.error("Invoke tars error", e);
exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.TARS_INVOKE, null);
return WebFluxResultUtils.result(exchange, error);
}
return Mono.fromFuture(future.thenApply(ret -> {
if (Objects.isNull(ret)) {
ret = Constants.TARS_RPC_RESULT_EMPTY;
}
exchange.getAttributes().put(Constants.RPC_RESULT, ret);
exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
return ret;
})).onErrorMap(m -> new ShenyuException("failed to invoke tars")).then(chain.execute(exchange));
}
use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.
the class ApplicationConfigCache method initPrxClass.
/**
* initPrxClass.
*
* @param selectorData selectorData
*/
public void initPrxClass(final SelectorData selectorData) {
try {
final List<TarsUpstream> upstreamList = GsonUtils.getInstance().fromList(selectorData.getHandle(), TarsUpstream.class);
if (CollectionUtils.isEmpty(upstreamList)) {
invalidate(selectorData.getName());
return;
}
refreshUpstreamCache.put(selectorData.getName(), upstreamList);
List<MetaData> metaDataList = ctxPathCache.getOrDefault(selectorData.getName(), new ArrayList<>());
for (MetaData metaData : metaDataList) {
refreshTarsInvokePrxList(metaData, upstreamList);
}
} catch (ExecutionException | NoSuchMethodException e) {
throw new ShenyuException(e.getCause());
}
}
use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.
the class SofaPluginTest method setUp.
@BeforeEach
public void setUp() {
exchange = MockServerWebExchange.from(MockServerHttpRequest.get("localhost").build());
metaData = new MetaData();
metaData.setId("1332017966661636096");
metaData.setAppName("sofa");
metaData.setPath("/sofa/findAll");
metaData.setServiceName("org.apache.shenyu.test.dubbo.api.service.DubboTestService");
metaData.setMethodName("findAll");
metaData.setRpcType(RpcTypeEnum.SOFA.getName());
SofaProxyService sofaProxyService = mock(SofaProxyService.class);
when(sofaProxyService.genericInvoker(null, metaData, exchange)).thenReturn(Mono.empty());
sofaPlugin = new SofaPlugin(sofaProxyService);
}
use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.
the class TarsMetaDataSubscriber method onSubscribe.
@Override
public void onSubscribe(final MetaData metaData) {
metaData.updateContextPath();
if (Objects.equals(RpcTypeEnum.TARS.getName(), metaData.getRpcType())) {
MetaData metaExist = META_DATA.get(metaData.getPath());
List<TarsInvokePrx> prxList = ApplicationConfigCache.getInstance().get(metaData.getPath()).getTarsInvokePrxList();
boolean exist = prxList.stream().anyMatch(tarsInvokePrx -> tarsInvokePrx.getHost().equals(metaData.getAppName()));
if (!exist) {
ApplicationConfigCache.getInstance().initPrx(metaData);
}
if (Objects.isNull(metaExist)) {
META_DATA.put(metaData.getPath(), metaData);
}
}
}
Aggregations