Search in sources :

Example 1 with GenericObject

use of com.alipay.hessian.generic.model.GenericObject in project incubator-shenyu by apache.

the class SofaProxyService method genericInvoker.

/**
 * Generic invoker object.
 *
 * @param body     the body
 * @param metaData the meta data
 * @param exchange the exchange
 * @return the object
 * @throws ShenyuException the shenyu exception
 */
public Mono<Object> genericInvoker(final String body, final MetaData metaData, final ServerWebExchange exchange) throws ShenyuException {
    ConsumerConfig<GenericService> reference = ApplicationConfigCache.getInstance().get(metaData.getPath());
    if (Objects.isNull(reference) || StringUtils.isEmpty(reference.getInterfaceId())) {
        ApplicationConfigCache.getInstance().invalidate(metaData.getPath());
        reference = ApplicationConfigCache.getInstance().initRef(metaData);
    }
    Pair<String[], Object[]> pair;
    if (StringUtils.isBlank(metaData.getParameterTypes()) || parameterIsNone(body)) {
        pair = new ImmutablePair<>(new String[] {}, new Object[] {});
    } else {
        pair = sofaParamResolveService.buildParameter(body, metaData.getParameterTypes());
    }
    CompletableFuture<Object> future = new CompletableFuture<>();
    RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback<Object>() {

        @Override
        public void onAppResponse(final Object o, final String s, final RequestBase requestBase) {
            future.complete(o);
        }

        @Override
        public void onAppException(final Throwable throwable, final String s, final RequestBase requestBase) {
            future.completeExceptionally(throwable);
        }

        @Override
        public void onSofaException(final SofaRpcException e, final String s, final RequestBase requestBase) {
            future.completeExceptionally(e);
        }
    });
    GenericService genericService = reference.refer();
    genericService.$genericInvoke(metaData.getMethodName(), pair.getLeft(), pair.getRight());
    return Mono.fromFuture(future.thenApply(ret -> {
        if (Objects.isNull(ret)) {
            ret = Constants.SOFA_RPC_RESULT_EMPTY;
        }
        GenericObject genericObject = (GenericObject) ret;
        exchange.getAttributes().put(Constants.RPC_RESULT, genericObject.getFields());
        exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
        return ret;
    })).onErrorMap(ShenyuException::new);
}
Also used : GenericService(com.alipay.sofa.rpc.api.GenericService) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) CompletableFuture(java.util.concurrent.CompletableFuture) GenericObject(com.alipay.hessian.generic.model.GenericObject) GenericObject(com.alipay.hessian.generic.model.GenericObject) ShenyuException(org.apache.shenyu.common.exception.ShenyuException)

Example 2 with GenericObject

use of com.alipay.hessian.generic.model.GenericObject in project incubator-shenyu by apache.

the class SofaBodyParamResolveServiceTest method assertIsObject.

/**
 * assert object is target structure.<br>
 * <ul>
 *     <li>{@link GenericObject} structure is target structure.</li>
 *     <li>{@link Map} structure is target structure.</li>
 *     <li>{@link List} value structure is target structure.</li>
 * </ul>
 *
 * @param object             object.
 * @param clazz              target structure.
 * @param allowValueToBeNull allow value to be Null. If the value is not null, the deep assertion
 */
private void assertIsObject(@NonNull final Object object, @NonNull final Class<?> clazz, final boolean allowValueToBeNull) {
    final Field[] fields = clazz.getDeclaredFields();
    if (object instanceof GenericObject) {
        final GenericObject genericObject = (GenericObject) object;
        for (Field field : fields) {
            if (excludeField(field)) {
                continue;
            }
            if (!genericObject.hasField(field.getName())) {
                fail(genericObject + " fieldName: " + field.getName() + " fieldType => " + field.getType().getName());
            }
            // not allow value is null
            if (!allowValueToBeNull) {
                assertNotNull(genericObject.getField(field.getName()));
                // value is not null, deep assert
                assertIsObject(genericObject.getField(field.getName()), field.getType(), allowValueToBeNull);
            }
        }
    }
    if (object instanceof Map) {
        final Map<?, ?> map = (Map<?, ?>) object;
        for (Field field : fields) {
            if (excludeField(field)) {
                continue;
            }
            if (!map.containsKey(field.getName())) {
                fail(map + " fieldName: " + field.getName() + " fieldType => " + field.getType().getName());
            }
            // not allow value is null
            if (!allowValueToBeNull) {
                assertNotNull(map.get(field.getName()));
                assertIsObject(map.get(field.getName()), field.getType(), allowValueToBeNull);
            }
        }
    }
    if (object instanceof List) {
        for (Object o : (List<?>) object) {
            assertIsObject(o, clazz, allowValueToBeNull);
        }
    }
}
Also used : Field(java.lang.reflect.Field) GenericObject(com.alipay.hessian.generic.model.GenericObject) List(java.util.List) GenericObject(com.alipay.hessian.generic.model.GenericObject) Map(java.util.Map)

Example 3 with GenericObject

use of com.alipay.hessian.generic.model.GenericObject in project sofa-rpc by sofastack.

the class GenericTest method assertGenericBasicBean.

private void assertGenericBasicBean(Object go) {
    assertEquals(go.getClass(), GenericObject.class);
    GenericObject genericObject = (GenericObject) go;
    assertEquals(BasicBean.class.getName(), genericObject.getType());
    BasicBean bb = new BasicBean((short) 12, new Short((short) 32), 21, new Integer(43), (byte) 12, new Byte((byte) 13), 1274646l, 873763l, (float) 1456.9877, (float) 1456.9877, 82837.93883, 82837.88, true, false);
    assertEquals(bb.getB(), genericObject.getField("b"));
    assertEquals(bb.getBb(), genericObject.getField("bb"));
    assertEquals(bb.getS(), genericObject.getField("s"));
    assertEquals(bb.getSs(), genericObject.getField("ss"));
    assertEquals(bb.getF(), genericObject.getField("f"));
    assertEquals(bb.getFf(), genericObject.getField("ff"));
    assertEquals(bb.getD(), genericObject.getField("d"));
    assertEquals(bb.getDd(), genericObject.getField("dd"));
    assertEquals(bb.getL(), genericObject.getField("l"));
    assertEquals(bb.getLl(), genericObject.getField("ll"));
    assertEquals(bb.getI(), genericObject.getField("i"));
    assertEquals(bb.getIi(), genericObject.getField("ii"));
    assertEquals(bb.isBo(), true);
    assertEquals(bb.getBbo(), false);
}
Also used : GenericObject(com.alipay.hessian.generic.model.GenericObject) BasicBean(com.alipay.sofa.rpc.test.generic.bean.BasicBean)

Example 4 with GenericObject

use of com.alipay.hessian.generic.model.GenericObject in project sofa-rpc by sofastack.

the class GenericTest method testAll.

@Test
public void testAll() throws SofaRpcException, InterruptedException {
    // 发布服务
    ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    MethodConfig config1 = new MethodConfig().setName("helloFuture").setInvokeType("future");
    methodConfigs.add(config1);
    MethodConfig config2 = new MethodConfig().setName("helloCallback").setInvokeType("callback").setOnReturn(new TestCallback());
    methodConfigs.add(config2);
    MethodConfig config21 = new MethodConfig().setName("helloCallbackException").setInvokeType("callback").setOnReturn(new TestCallback());
    methodConfigs.add(config21);
    MethodConfig config3 = new MethodConfig().setName("helloOneway").setInvokeType("oneway");
    methodConfigs.add(config3);
    MethodConfig config4 = new MethodConfig().setName("helloTimeout").setInvokeType("sync");
    methodConfigs.add(config4);
    // C服务的服务端
    ProviderConfig<TestInterface> CProvider = new ProviderConfig<TestInterface>().setInterfaceId(TestInterface.class.getName()).setRef(new TestClass()).setServer(serverConfig2);
    CProvider.export();
    // 引用服务
    ConsumerConfig<GenericService> BConsumer = new ConsumerConfig<GenericService>().setInterfaceId(TestInterface.class.getName()).setGeneric(true).setMethods(methodConfigs).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(3000).setRetries(2);
    GenericService proxy = BConsumer.refer();
    GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.test.generic.bean.People");
    genericObject.putField("name", "Lilei");
    genericObject.putField("job", new Job("coder"));
    People people = new People();
    people.setName("Lilei");
    people.setJob(new Job("coder"));
    // sync 调用
    assertEquals(proxy.$invoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people }), new TestClass().hello(people));
    People peopleResult = proxy.$genericInvoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    assertEquals(peopleResult, new TestClass().hello(people));
    GenericObject result = (GenericObject) proxy.$genericInvoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    isCorrect(result);
    // future 调用
    proxy.$invoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    assertEquals(SofaResponseFuture.getResponse(1000, true), new TestClass().helloFuture(people));
    proxy.$genericInvoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    assertEquals(SofaResponseFuture.getResponse(1000, true), new TestClass().helloFuture(people));
    proxy.$genericInvoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    result = (GenericObject) SofaResponseFuture.getResponse(1000, true);
    isCorrect(result);
    // callback调用
    proxy.$invoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    TestCallback.startLatach();
    assertEquals(TestCallback.result, new TestClass().helloCallback(people));
    proxy.$genericInvoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    TestCallback.startLatach();
    assertEquals(TestCallback.result, new TestClass().helloCallback(people));
    proxy.$genericInvoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    TestCallback.startLatach();
    isCorrect((GenericObject) TestCallback.result);
    TestCallback.result = null;
    // oneway调用
    proxy.$invoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    proxy.$genericInvoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    proxy.$genericInvoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    // callback出现异常
    proxy.$invoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    TestCallback.startLatach();
    Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
    proxy.$genericInvoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    TestCallback.startLatach();
    Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
    proxy.$genericInvoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    TestCallback.startLatach();
    Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
    testTimeout(proxy, genericObject, people);
    testComplexBean(proxy);
    testBasicBean(proxy);
}
Also used : GenericService(com.alipay.sofa.rpc.api.GenericService) ArrayList(java.util.ArrayList) People(com.alipay.sofa.rpc.test.generic.bean.People) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) GenericObject(com.alipay.hessian.generic.model.GenericObject) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) GenericObject(com.alipay.hessian.generic.model.GenericObject) Job(com.alipay.sofa.rpc.test.generic.bean.Job) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 5 with GenericObject

use of com.alipay.hessian.generic.model.GenericObject in project sofa-rpc by sofastack.

the class SofaHessianSerializerTest method encodeSofaRequest.

@Test
public void encodeSofaRequest() throws Exception {
    SofaRequest request = buildRequest();
    AbstractByteBuf data = serializer.encode(request, null);
    SofaRequest newRequest = (SofaRequest) serializer.decode(data, SofaRequest.class, null);
    Assert.assertEquals(newRequest.getInterfaceName(), request.getInterfaceName());
    Assert.assertEquals(newRequest.getMethodName(), request.getMethodName());
    Assert.assertArrayEquals(newRequest.getMethodArgSigs(), request.getMethodArgSigs());
    Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length);
    Assert.assertEquals(newRequest.getTargetServiceUniqueName(), request.getTargetServiceUniqueName());
    Assert.assertEquals(newRequest.getTargetAppName(), request.getTargetAppName());
    request = buildRequest();
    GenericObject genericObject = new GenericObject(TestGenericBean.class.getCanonicalName());
    genericObject.putField("name", "Lilei");
    genericObject.putField("age", 123);
    // change to generic request
    request.setMethodArgSigs(new String[] { TestGenericBean.class.getCanonicalName() });
    request.setMethodArgs(new Object[] { genericObject });
    data = serializer.encode(request, Collections.singletonMap(RemotingConstants.HEAD_GENERIC_TYPE, RemotingConstants.SERIALIZE_FACTORY_GENERIC));
    newRequest = (SofaRequest) serializer.decode(data, SofaRequest.class, null);
    Assert.assertEquals(newRequest.getInterfaceName(), request.getInterfaceName());
    Assert.assertEquals(newRequest.getMethodName(), request.getMethodName());
    Assert.assertArrayEquals(newRequest.getMethodArgSigs(), request.getMethodArgSigs());
    Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length);
    Assert.assertEquals(TestGenericBean.class.getCanonicalName(), newRequest.getMethodArgs()[0].getClass().getCanonicalName());
    Assert.assertEquals(newRequest.getTargetServiceUniqueName(), request.getTargetServiceUniqueName());
    Assert.assertEquals(newRequest.getTargetAppName(), request.getTargetAppName());
    // error request has no target service name
    request = buildRequest();
    request.setTargetServiceUniqueName(null);
    data = serializer.encode(request, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaRequest.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) GenericObject(com.alipay.hessian.generic.model.GenericObject) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Aggregations

GenericObject (com.alipay.hessian.generic.model.GenericObject)16 Test (org.junit.Test)4 GenericService (com.alipay.sofa.rpc.api.GenericService)3 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)3 UnsafeByteArrayInputStream (com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 ComplexBean (com.alipay.sofa.rpc.test.generic.bean.ComplexBean)2 People (com.alipay.sofa.rpc.test.generic.bean.People)2 Hessian2Input (com.caucho.hessian.io.Hessian2Input)2 JsonObject (com.google.gson.JsonObject)2 IOException (java.io.IOException)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 GenericCollection (com.alipay.hessian.generic.model.GenericCollection)1 GenericMap (com.alipay.hessian.generic.model.GenericMap)1 GenericContext (com.alipay.sofa.rpc.api.GenericContext)1 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)1