Search in sources :

Example 1 with UnsafeByteArrayInputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream in project sofa-rpc by sofastack.

the class SimpleMapSerializer method decode.

/**
 * 简单 map 的反序列化过程, 用来反序列化 bolt 的 header
 * <p>
 * {@link SofaRpcSerialization#deserializeHeader(com.alipay.remoting.rpc.RequestCommand)}
 *
 * @param bytes bolt header
 * @return 反序列化后的 Map 对象
 * @throws DeserializationException DeserializationException
 */
public Map<String, String> decode(byte[] bytes) throws DeserializationException {
    Map<String, String> map = new HashMap<String, String>();
    if (bytes == null || bytes.length == 0) {
        return map;
    }
    UnsafeByteArrayInputStream in = new UnsafeByteArrayInputStream(bytes);
    try {
        while (in.available() > 0) {
            String key = readString(in);
            String value = readString(in);
            if (key != null && value != null) {
                map.put(key, value);
            }
        }
        return map;
    } catch (IOException ex) {
        throw new DeserializationException(ex.getMessage(), ex);
    }
}
Also used : HashMap(java.util.HashMap) UnsafeByteArrayInputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream) IOException(java.io.IOException) DeserializationException(com.alipay.remoting.exception.DeserializationException)

Example 2 with UnsafeByteArrayInputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream in project sofa-rpc by sofastack.

the class SofaHessianSerializer method decode.

@Override
public Object decode(AbstractByteBuf data, Class clazz, Map<String, String> context) throws SofaRpcException {
    if (clazz == null) {
        throw buildDeserializeError("class is null!");
    } else {
        CustomHessianSerializer serializer = CustomHessianSerializerManager.getSerializer(clazz);
        if (serializer != null) {
            return serializer.decodeObject(data, context);
        } else {
            try {
                UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
                Hessian2Input input = new Hessian2Input(inputStream);
                input.setSerializerFactory(serializerFactory);
                Object object = input.readObject();
                input.close();
                return object;
            } catch (IOException e) {
                throw buildDeserializeError(e.getMessage(), e);
            }
        }
    }
}
Also used : Hessian2Input(com.caucho.hessian.io.Hessian2Input) UnsafeByteArrayInputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream) IOException(java.io.IOException) CustomHessianSerializer(com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer)

Example 3 with UnsafeByteArrayInputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream in project sofa-rpc by sofastack.

the class SofaRequestHessianSerializer method decodeObjectByTemplate.

@Override
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaRequest template) throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        input.setSerializerFactory(serializerFactory);
        Object object = input.readObject();
        SofaRequest tmp = (SofaRequest) object;
        String targetServiceName = tmp.getTargetServiceUniqueName();
        if (targetServiceName == null) {
            throw buildDeserializeError("Target service name of request is null!");
        }
        // copy values to template
        template.setMethodName(tmp.getMethodName());
        template.setMethodArgSigs(tmp.getMethodArgSigs());
        template.setTargetServiceUniqueName(tmp.getTargetServiceUniqueName());
        template.setTargetAppName(tmp.getTargetAppName());
        template.addRequestProps(tmp.getRequestProps());
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetServiceName);
        template.setInterfaceName(interfaceName);
        // decode args
        String[] sig = template.getMethodArgSigs();
        Class<?>[] classSig = ClassTypeUtils.getClasses(sig);
        final Object[] args = new Object[sig.length];
        for (int i = 0; i < template.getMethodArgSigs().length; ++i) {
            args[i] = input.readObject(classSig[i]);
        }
        template.setMethodArgs(args);
        input.close();
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Hessian2Input(com.caucho.hessian.io.Hessian2Input) UnsafeByteArrayInputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream) IOException(java.io.IOException)

Example 4 with UnsafeByteArrayInputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream 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 5 with UnsafeByteArrayInputStream

use of com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream in project sofa-rpc by sofastack.

the class SofaRequestHessianSerializer method decodeObject.

@Override
public SofaRequest decodeObject(AbstractByteBuf data, Map<String, String> context) throws SofaRpcException {
    try {
        UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
        Hessian2Input input = new Hessian2Input(inputStream);
        input.setSerializerFactory(serializerFactory);
        Object object = input.readObject();
        SofaRequest sofaRequest = (SofaRequest) object;
        String targetServiceName = sofaRequest.getTargetServiceUniqueName();
        if (targetServiceName == null) {
            throw buildDeserializeError("Target service name of request is null!");
        }
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetServiceName);
        sofaRequest.setInterfaceName(interfaceName);
        String[] sig = sofaRequest.getMethodArgSigs();
        Class<?>[] classSig = ClassTypeUtils.getClasses(sig);
        final Object[] args = new Object[sig.length];
        for (int i = 0; i < sofaRequest.getMethodArgSigs().length; ++i) {
            args[i] = input.readObject(classSig[i]);
        }
        sofaRequest.setMethodArgs(args);
        input.close();
        return sofaRequest;
    } catch (IOException e) {
        throw buildDeserializeError(e.getMessage(), e);
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Hessian2Input(com.caucho.hessian.io.Hessian2Input) UnsafeByteArrayInputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream) IOException(java.io.IOException)

Aggregations

UnsafeByteArrayInputStream (com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream)6 IOException (java.io.IOException)6 Hessian2Input (com.caucho.hessian.io.Hessian2Input)5 GenericObject (com.alipay.hessian.generic.model.GenericObject)2 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 DeserializationException (com.alipay.remoting.exception.DeserializationException)1 CustomHessianSerializer (com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer)1 HashMap (java.util.HashMap)1