Search in sources :

Example 11 with GenericObject

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

the class SofaResponseHessianSerializer method decodeObject.

@Override
public SofaResponse decodeObject(AbstractByteBuf data, Map<String, String> context) throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        // 根据SerializeType信息决定序列化器
        Object object;
        boolean genericSerialize = context != null && isGenericResponse(context.get(RemotingConstants.HEAD_GENERIC_TYPE));
        if (genericSerialize) {
            input.setSerializerFactory(genericSerializerFactory);
            GenericObject genericObject = (GenericObject) input.readObject();
            SofaResponse sofaResponse = new SofaResponse();
            sofaResponse.setErrorMsg((String) genericObject.getField("errorMsg"));
            sofaResponse.setAppResponse(genericObject.getField("appResponse"));
            sofaResponse.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
            object = sofaResponse;
        } else {
            input.setSerializerFactory(serializerFactory);
            object = input.readObject();
        }
        input.close();
        return (SofaResponse) object;
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}
Also used : Hessian2Input(com.caucho.hessian.io.Hessian2Input) GenericObject(com.alipay.hessian.generic.model.GenericObject) UnsafeByteArrayInputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream) GenericObject(com.alipay.hessian.generic.model.GenericObject) IOException(java.io.IOException) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 12 with GenericObject

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

the class GenericTest method isCorrect.

private void isCorrect(GenericObject result) {
    Assert.assertEquals(result.getType(), "com.alipay.sofa.rpc.test.generic.bean.People");
    Assert.assertEquals(result.getField("name"), "Lilei");
    GenericObject genericObject = (GenericObject) result.getField("job");
    Assert.assertEquals(genericObject.getType(), "com.alipay.sofa.rpc.test.generic.bean.Job");
    Assert.assertEquals(genericObject.getField("name"), "coder");
}
Also used : GenericObject(com.alipay.hessian.generic.model.GenericObject)

Example 13 with GenericObject

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

the class GenericTest method assertMyGenericResult.

private void assertMyGenericResult(Object obj) {
    ComplexBean complexBean = createMyComplexBean();
    Assert.assertEquals(GenericObject.class, obj.getClass());
    GenericObject gb = (GenericObject) obj;
    Assert.assertEquals(gb.getType(), ComplexBean.class.getName());
    assertArrayEquals(gb.getField("strs"), complexBean.getStrs());
    Assert.assertEquals(gb.getField("jobs").getClass(), GenericArray.class);
    assertArrayEquals(GenericUtils.convertToObject(gb.getField("jobs")), complexBean.getJobs());
    Assert.assertEquals(gb.getField("list").getClass(), GenericCollection.class);
    Assert.assertEquals(GenericUtils.convertToObject(gb.getField("list")).getClass(), MyList.class);
    assertEquals(GenericUtils.convertToObject(gb.getField("list")), complexBean.getList());
    Assert.assertEquals(gb.getField("map").getClass(), GenericMap.class);
    Assert.assertEquals(GenericUtils.convertToObject(gb.getField("map")).getClass(), MyMap.class);
    assertEquals(GenericUtils.convertToObject(gb.getField("map")), complexBean.getMap());
}
Also used : GenericObject(com.alipay.hessian.generic.model.GenericObject) ComplexBean(com.alipay.sofa.rpc.test.generic.bean.ComplexBean)

Example 14 with GenericObject

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

the class GenericTest method assertJdkGenericResult.

private void assertJdkGenericResult(Object obj) {
    ComplexBean complexBean = createJdkComplexBean();
    Assert.assertEquals(GenericObject.class, obj.getClass());
    GenericObject gb = (GenericObject) obj;
    Assert.assertEquals(gb.getType(), ComplexBean.class.getName());
    assertArrayEquals(gb.getField("strs"), complexBean.getStrs());
    Assert.assertEquals(gb.getField("jobs").getClass(), GenericArray.class);
    assertArrayEquals(GenericUtils.convertToObject(gb.getField("jobs")), complexBean.getJobs());
    Assert.assertEquals(gb.getField("list").getClass(), ArrayList.class);
    assertEquals(gb.getField("list"), complexBean.getList());
    assertEquals(gb.getField("map"), complexBean.getMap());
}
Also used : GenericObject(com.alipay.hessian.generic.model.GenericObject) ComplexBean(com.alipay.sofa.rpc.test.generic.bean.ComplexBean)

Example 15 with GenericObject

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

the class GenericTest method testTimeout.

private void testTimeout(final GenericService proxy, GenericObject genericObject, People people) {
    // 1. 构造GenericContext 对象
    GenericContext genericContext = new GenericContext();
    genericContext.setClientTimeout(5000);
    // 2. 未指定参数,发生超时
    boolean isSuccess = true;
    try {
        proxy.$genericInvoke("helloTimeout", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    } catch (Exception e) {
        isSuccess = false;
    }
    Assert.assertFalse(isSuccess);
    // 3. 指定超时,结果序列化为People 类
    People peopleResult = proxy.$genericInvoke("helloTimeout", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class, genericContext);
    assertEquals(peopleResult, people);
    // 4. 指定超时,结果序列化为GenericObject
    GenericObject result = (GenericObject) proxy.$genericInvoke("helloTimeout", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, genericContext);
    isCorrect(result);
}
Also used : GenericContext(com.alipay.sofa.rpc.api.GenericContext) People(com.alipay.sofa.rpc.test.generic.bean.People) GenericObject(com.alipay.hessian.generic.model.GenericObject) GenericObject(com.alipay.hessian.generic.model.GenericObject) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

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