Search in sources :

Example 11 with SelectorData

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

the class CryptorRequestPluginTest method encryptTest.

@Test
public void encryptTest() {
    this.ruleData.setHandle("{\"strategyName\":\"rsa\"," + "\"fieldNames\":\"inputToken\"," + "\"decryptKey\":\"MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAtrfolGUtLhZVSpd5L/oAXbGW9Rn54mD96Ny" + "uWsxp/KCscDoeFScN7uSc3LwKk14wrC4X0+fSDxm0kMPTvgNBywIDAQABAkBFPvt4ycNOlQ4r364A3akn2PbR2s9V2NZBW" + "ukE5jVAlOvgCn6L/+tsVDSQgeVtOPd6rwM2a24iASDsNEbnVrwBAiEA34DwAmsa1phE5aGKM1bPHJiGgM8yolIYDWBaBCu" + "PTgECIQDRSOWA8rLJWP+Vijm/QB8C41Gw1V7WXC2Kuj07Jv5nywIgTDKCIODw8m5RNtRe8GfNDlu1p158TbidOJo7tiY/og" + "ECIQCaj0tvP83qBWA8AClFpQVCDL936RxxEwJPQduWo+WeoQIhAN7HKEW0E97il2RvCsgeArdt83WjZh7OhMhW6MLPrMjs\"," + "\"encryptKey\":\"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALa36JRlLS4WVUqXeS/6AF2xlvUZ+eJg/ejcrlrMafygrHA6Hh" + "UnDe7knNy8CpNeMKwuF9Pn0g8ZtJDD074DQcsCAwEAAQ\\u003d\\u003d\"," + "\"way\":\"encrypt\"" + "}\n");
    this.exchange = MockServerWebExchange.from(MockServerHttpRequest.method(HttpMethod.POST, "/test").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).body("{\"inputToken\": " + "\"shenyu\"" + "}"));
    SelectorData selectorData = mock(SelectorData.class);
    when(this.chain.execute(any())).thenReturn(Mono.empty());
    cryptorRequestPluginDataHandler.handlerRule(ruleData);
    StepVerifier.create(cryptorRequestPlugin.doExecute(this.exchange, this.chain, selectorData, this.ruleData)).expectSubscription().verifyComplete();
}
Also used : SelectorData(org.apache.shenyu.common.dto.SelectorData) Test(org.junit.jupiter.api.Test)

Example 12 with SelectorData

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

the class CryptorRequestPluginTest method multiJsonDecryptTest.

@Test
public void multiJsonDecryptTest() {
    this.ruleData.setHandle("{\"strategyName\":\"rsa\"," + "\"fieldNames\":\"inputToken.test\"," + "\"decryptKey\":\"MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAtrfolGUtLhZVSpd5L/oAXbGW9Rn54mD96Ny" + "uWsxp/KCscDoeFScN7uSc3LwKk14wrC4X0+fSDxm0kMPTvgNBywIDAQABAkBFPvt4ycNOlQ4r364A3akn2PbR2s9V2NZBW" + "ukE5jVAlOvgCn6L/+tsVDSQgeVtOPd6rwM2a24iASDsNEbnVrwBAiEA34DwAmsa1phE5aGKM1bPHJiGgM8yolIYDWBaBCu" + "PTgECIQDRSOWA8rLJWP+Vijm/QB8C41Gw1V7WXC2Kuj07Jv5nywIgTDKCIODw8m5RNtRe8GfNDlu1p158TbidOJo7tiY/og" + "ECIQCaj0tvP83qBWA8AClFpQVCDL936RxxEwJPQduWo+WeoQIhAN7HKEW0E97il2RvCsgeArdt83WjZh7OhMhW6MLPrMjs\"," + "\"encryptKey\":\"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALa36JRlLS4WVUqXeS/6AF2xlvUZ+eJg/ejcrlrMafygrHA6Hh" + "UnDe7knNy8CpNeMKwuF9Pn0g8ZtJDD074DQcsCAwEAAQ\\u003d\\u003d\"," + "\"way\":\"decrypt\"" + "}\n");
    this.exchange = MockServerWebExchange.from(MockServerHttpRequest.method(HttpMethod.POST, "/test").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).body("{\"inputToken\": " + "{\"test\":\"kYPZgOAR2pEipskl5WURW/r3CMxNQJwbs4jbTAOfZNV39L4WkaTOqAeolV+rlKCKiXKvhfHWaxQOTMm9hQBxLA==\"}" + "}"));
    SelectorData selectorData = mock(SelectorData.class);
    when(this.chain.execute(any())).thenReturn(Mono.empty());
    cryptorRequestPluginDataHandler.handlerRule(ruleData);
    StepVerifier.create(cryptorRequestPlugin.doExecute(this.exchange, this.chain, selectorData, this.ruleData)).expectSubscription().verifyComplete();
}
Also used : SelectorData(org.apache.shenyu.common.dto.SelectorData) Test(org.junit.jupiter.api.Test)

Example 13 with SelectorData

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

the class CryptorRequestPluginTest method multiJsonEncryptTest.

@Test
public void multiJsonEncryptTest() {
    this.ruleData.setHandle("{\"strategyName\":\"rsa\"," + "\"fieldNames\":\"inputToken.test\"," + "\"decryptKey\":\"MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAtrfolGUtLhZVSpd5L/oAXbGW9Rn54mD96Ny" + "uWsxp/KCscDoeFScN7uSc3LwKk14wrC4X0+fSDxm0kMPTvgNBywIDAQABAkBFPvt4ycNOlQ4r364A3akn2PbR2s9V2NZBW" + "ukE5jVAlOvgCn6L/+tsVDSQgeVtOPd6rwM2a24iASDsNEbnVrwBAiEA34DwAmsa1phE5aGKM1bPHJiGgM8yolIYDWBaBCu" + "PTgECIQDRSOWA8rLJWP+Vijm/QB8C41Gw1V7WXC2Kuj07Jv5nywIgTDKCIODw8m5RNtRe8GfNDlu1p158TbidOJo7tiY/og" + "ECIQCaj0tvP83qBWA8AClFpQVCDL936RxxEwJPQduWo+WeoQIhAN7HKEW0E97il2RvCsgeArdt83WjZh7OhMhW6MLPrMjs\"," + "\"encryptKey\":\"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALa36JRlLS4WVUqXeS/6AF2xlvUZ+eJg/ejcrlrMafygrHA6Hh" + "UnDe7knNy8CpNeMKwuF9Pn0g8ZtJDD074DQcsCAwEAAQ\\u003d\\u003d\"," + "\"way\":\"encrypt\"" + "}\n");
    this.exchange = MockServerWebExchange.from(MockServerHttpRequest.method(HttpMethod.POST, "/test").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).body("{\"inputToken\": " + "{\"test\":\"shenyu\"}" + "}"));
    SelectorData selectorData = mock(SelectorData.class);
    when(this.chain.execute(any())).thenReturn(Mono.empty());
    cryptorRequestPluginDataHandler.handlerRule(ruleData);
    StepVerifier.create(cryptorRequestPlugin.doExecute(this.exchange, this.chain, selectorData, this.ruleData)).expectSubscription().verifyComplete();
}
Also used : SelectorData(org.apache.shenyu.common.dto.SelectorData) Test(org.junit.jupiter.api.Test)

Example 14 with SelectorData

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

the class AlibabaDubboPlugin method doDubboInvoker.

/**
 * do dubbo invoker.
 *
 * @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}
 * @param metaData the medata
 * @param param    the param
 * @return {@code Mono<Void>} to indicate when request handling is complete
 */
@Override
protected Mono<Void> doDubboInvoker(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule, final MetaData metaData, final String param) {
    RpcContext.getContext().setAttachment(Constants.DUBBO_SELECTOR_ID, selector.getId());
    RpcContext.getContext().setAttachment(Constants.DUBBO_RULE_ID, rule.getId());
    RpcContext.getContext().setAttachment(Constants.DUBBO_REMOTE_ADDRESS, Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getAddress().getHostAddress());
    return Mono.create(monoSink -> {
        ResponseFuture future = alibabaDubboProxyService.genericInvoker(param, metaData);
        future.setCallback(new ResponseCallback() {

            @Override
            public void done(final Object resultObj) {
                assert resultObj instanceof Result;
                Result result = (Result) resultObj;
                if (result.hasException()) {
                    this.caught(result.getException());
                    return;
                }
                monoSink.success(result.getValue());
            }

            @Override
            public void caught(final Throwable ex) {
                LOG.error("dubbo failed using async genericInvoker() metaData={} param={}", metaData, param, ex);
                monoSink.error(ex);
            }
        });
    }).flatMap(response -> {
        exchange.getAttributes().put(Constants.RPC_RESULT, Objects.nonNull(response) ? response : Constants.DUBBO_RPC_RESULT_EMPTY);
        exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
        return chain.execute(exchange);
    });
}
Also used : AlibabaDubboProxyService(org.apache.shenyu.plugin.alibaba.dubbo.proxy.AlibabaDubboProxyService) AbstractDubboPlugin(org.apache.shenyu.plugin.dubbo.common.AbstractDubboPlugin) Constants(org.apache.shenyu.common.constant.Constants) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) Logger(org.slf4j.Logger) Result(com.alibaba.dubbo.rpc.Result) ShenyuPluginChain(org.apache.shenyu.plugin.api.ShenyuPluginChain) ResponseCallback(com.alibaba.dubbo.remoting.exchange.ResponseCallback) LoggerFactory(org.slf4j.LoggerFactory) RuleData(org.apache.shenyu.common.dto.RuleData) Mono(reactor.core.publisher.Mono) ResultEnum(org.apache.shenyu.common.enums.ResultEnum) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Objects(java.util.Objects) Map(java.util.Map) RpcContext(com.alibaba.dubbo.rpc.RpcContext) MetaData(org.apache.shenyu.common.dto.MetaData) SelectorData(org.apache.shenyu.common.dto.SelectorData) ResponseCallback(com.alibaba.dubbo.remoting.exchange.ResponseCallback) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) Result(com.alibaba.dubbo.rpc.Result)

Example 15 with SelectorData

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

the class AlibabaDubboPluginTest method testAlibabaDubboPlugin.

@Test
public void testAlibabaDubboPlugin() {
    final ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("localhost").remoteAddress(new InetSocketAddress("127.0.0.1", 20880)).build());
    ShenyuContext context = mock(ShenyuContext.class);
    when(context.getRpcType()).thenReturn(RpcTypeEnum.DUBBO.getName());
    exchange.getAttributes().put(Constants.CONTEXT, context);
    exchange.getAttributes().put(Constants.META_DATA, metaData);
    ShenyuPluginChain chain = mock(ShenyuPluginChain.class);
    when(chain.execute(exchange)).thenReturn(Mono.empty());
    RuleData data = mock(RuleData.class);
    SelectorData selectorData = mock(SelectorData.class);
    Mono<Void> voidMono = alibabaDubboPluginUnderTest.doExecute(exchange, chain, selectorData, data);
    StepVerifier.create(voidMono).expectSubscription().verifyComplete();
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) RuleData(org.apache.shenyu.common.dto.RuleData) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) InetSocketAddress(java.net.InetSocketAddress) ShenyuPluginChain(org.apache.shenyu.plugin.api.ShenyuPluginChain) SelectorData(org.apache.shenyu.common.dto.SelectorData) Test(org.junit.jupiter.api.Test)

Aggregations

SelectorData (org.apache.shenyu.common.dto.SelectorData)96 Test (org.junit.jupiter.api.Test)60 RuleData (org.apache.shenyu.common.dto.RuleData)47 ShenyuContext (org.apache.shenyu.plugin.api.context.ShenyuContext)18 ShenyuPluginChain (org.apache.shenyu.plugin.api.ShenyuPluginChain)16 ServerWebExchange (org.springframework.web.server.ServerWebExchange)16 List (java.util.List)14 Objects (java.util.Objects)12 PluginData (org.apache.shenyu.common.dto.PluginData)12 Mono (reactor.core.publisher.Mono)12 Map (java.util.Map)10 MetaData (org.apache.shenyu.common.dto.MetaData)10 PluginEnum (org.apache.shenyu.common.enums.PluginEnum)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 ArrayList (java.util.ArrayList)8 Optional (java.util.Optional)8 CollectionUtils (org.apache.commons.collections4.CollectionUtils)7 AbstractShenyuPlugin (org.apache.shenyu.plugin.base.AbstractShenyuPlugin)7 Collectors (java.util.stream.Collectors)6