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);
}
}
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");
}
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());
}
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());
}
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);
}
Aggregations