Search in sources :

Example 6 with GenericObject

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

the class SofaResponseHessianSerializer method decodeObjectByTemplate.

@Override
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template) throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        // 根据SerializeType信息决定序列化器
        boolean genericSerialize = context != null && isGenericResponse(context.get(RemotingConstants.HEAD_GENERIC_TYPE));
        if (genericSerialize) {
            input.setSerializerFactory(genericSerializerFactory);
            GenericObject genericObject = (GenericObject) input.readObject();
            template.setErrorMsg((String) genericObject.getField("errorMsg"));
            template.setAppResponse(genericObject.getField("appResponse"));
            template.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
        } else {
            input.setSerializerFactory(serializerFactory);
            SofaResponse tmp = (SofaResponse) input.readObject();
            // copy values to template
            template.setErrorMsg(tmp.getErrorMsg());
            template.setAppResponse(tmp.getAppResponse());
            template.setResponseProps(tmp.getResponseProps());
        }
        input.close();
    } 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) IOException(java.io.IOException) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 7 with GenericObject

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

the class SofaBootRpcAllTest method testGeneric.

@Test
public void testGeneric() {
    GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.boot.test.bean.generic.GenericParamModel");
    genericObject.putField("name", "Bible");
    GenericObject result = (GenericObject) genericService.$genericInvoke("sayGeneric", new String[] { "com.alipay.sofa.rpc.boot.test.bean.generic.GenericParamModel" }, new Object[] { genericObject });
    Assert.assertEquals("com.alipay.sofa.rpc.boot.test.bean.generic.GenericResultModel", result.getType());
    Assert.assertEquals("Bible", result.getField("name"));
    Assert.assertEquals("sample generic value", result.getField("value"));
}
Also used : GenericObject(com.alipay.hessian.generic.model.GenericObject) GenericObject(com.alipay.hessian.generic.model.GenericObject) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 8 with GenericObject

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

the class SofaBootRpcAllTest method testGeneric.

@Test
public void testGeneric() {
    GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.boot.test.bean.generic.GenericParamModel");
    genericObject.putField("name", "Bible");
    GenericObject result = (GenericObject) genericService.$genericInvoke("sayGeneric", new String[] { "com.alipay.sofa.rpc.boot.test.bean.generic.GenericParamModel" }, new Object[] { genericObject });
    Assert.assertEquals("com.alipay.sofa.rpc.boot.test.bean.generic.GenericResultModel", result.getType());
    Assert.assertEquals("Bible", result.getField("name"));
    Assert.assertEquals("sample generic value", result.getField("value"));
}
Also used : GenericObject(com.alipay.hessian.generic.model.GenericObject) GenericObject(com.alipay.hessian.generic.model.GenericObject) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 9 with GenericObject

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

the class SofaParamResolveServiceImpl method convertToParameterValue.

/**
 * convert to parameter value.
 *
 * @param value         value support [json object string, json array string,string]
 * @param parameterType parameter type support [javaPackage.ClassName#GenericType1#GenericType2], split is ,
 * @return GenericObject, GenericMap, List, string, array...
 * @see com.alipay.hessian.generic.model
 */
@SuppressWarnings("all")
private Object convertToParameterValue(final Object value, final String[] parameterType) {
    if (isSingleType(parameterType)) {
        return value;
    }
    if (value instanceof JsonObject && parameterType[0].contains("Map")) {
        final Map<String, Object> mapValue = GsonUtils.getInstance().convertToMap(value.toString());
        if (parameterType.length == 1) {
            // no generic info
            return mapValue;
        }
        assert parameterType.length == 3;
        // generic map
        final GenericMap genericMap = new GenericMap(parameterType[2]);
        mapValue.replaceAll((k, v) -> convertToGenericObject(parameterType[2], mapValue.get(k)));
        genericMap.setMap(mapValue);
        return genericMap;
    }
    if (value instanceof JsonObject) {
        return convertToGenericObject(parameterType[0], value);
    }
    if (value instanceof JsonArray) {
        if (parameterType.length == 1) {
            // no generic info
            return GsonUtils.getInstance().fromList(value.toString(), Object.class);
        }
        // generic collection
        final GenericCollection genericCollection = new GenericCollection(parameterType[1]);
        genericCollection.setCollection(convertToGenericObjects(parameterType[1], (Iterable<Object>) value));
        return genericCollection;
    }
    return value;
}
Also used : JsonArray(com.google.gson.JsonArray) GenericMap(com.alipay.hessian.generic.model.GenericMap) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) GenericObject(com.alipay.hessian.generic.model.GenericObject) GenericCollection(com.alipay.hessian.generic.model.GenericCollection)

Example 10 with GenericObject

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

the class SofaParamResolveServiceImpl method convertToGenericObject.

/**
 * convert json object to {@link GenericObject}.
 *
 * @param paramType  param type string
 * @param paramValue param value (if is object, auto to convert string)
 * @return {@link GenericObject},if is single customize type return  paramValue
 * @see GenericObject
 * @see #isSingleType(String)
 */
private static Object convertToGenericObject(final String paramType, final Object paramValue) {
    if (isSingleType(paramType)) {
        return paramValue;
    }
    final Map<String, Object> mapValue = GsonUtils.getInstance().convertToMap(paramValue.toString());
    GenericObject genericObject = new GenericObject(paramType);
    mapValue.forEach(genericObject::putField);
    return genericObject;
}
Also used : GenericObject(com.alipay.hessian.generic.model.GenericObject) JsonObject(com.google.gson.JsonObject) GenericObject(com.alipay.hessian.generic.model.GenericObject)

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