use of com.weibo.api.motan.codec.Serialization in project motan by weibocom.
the class CompressRpcCodec method encodeRequest.
/**
* request body 数据:
*
* <pre>
*
* body:
*
* byte[] data :
*
* serialize(interface_name, method_name, method_param_desc, method_param_value, attachments_size, attachments_value)
*
* method_param_desc: for_each (string.append(method_param_interface_name))
*
* method_param_value: for_each (method_param_name, method_param_value)
*
* attachments_value: for_each (attachment_name, attachment_value)
*
* </pre>
*
* @param request
* @return
* @throws IOException
*/
private byte[] encodeRequest(Channel channel, Request request) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutput output = createOutput(outputStream);
addMethodInfo(output, request);
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
if (request.getArguments() != null && request.getArguments().length > 0) {
for (Object obj : request.getArguments()) {
serialize(output, obj, serialization);
}
}
if (request.getAttachments() == null || request.getAttachments().isEmpty()) {
// empty attachments
output.writeShort(0);
} else {
// 需要copy一份attachment进行签名替换,这样在失败重试时原始的request信息不会变更
Map<String, String> attachments = copyMap(request.getAttachments());
replaceAttachmentParamsBySign(channel, attachments);
addAttachment(output, attachments);
}
output.flush();
byte[] body = outputStream.toByteArray();
byte flag = MotanConstants.FLAG_REQUEST;
output.close();
Boolean usegz = channel.getUrl().getBooleanParameter(URLParamType.usegz.getName(), URLParamType.usegz.getBooleanValue());
int minGzSize = channel.getUrl().getIntParameter(URLParamType.mingzSize.getName(), URLParamType.mingzSize.getIntValue());
return encode(compress(body, usegz, minGzSize), flag, request.getRequestId());
}
use of com.weibo.api.motan.codec.Serialization in project motan by weibocom.
the class CompressRpcCodec method decodeV2.
/**
* decode data
*
* <pre>
* 对于client端:主要是来自server端的response or exception
* 对于server端: 主要是来自client端的request
* </pre>
*
* @param data
* @return
* @throws IOException
*/
public Object decodeV2(Channel channel, String remoteIp, byte[] data) throws IOException {
if (data.length <= RpcProtocolVersion.VERSION_2.getHeaderLength()) {
throw new MotanFrameworkException("decode error: format problem", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
short type = ByteUtil.bytes2short(data, 0);
if (type != MAGIC) {
throw new MotanFrameworkException("decode error: magic error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
int bodyLength = ByteUtil.bytes2int(data, 12);
if (RpcProtocolVersion.VERSION_2.getHeaderLength() + bodyLength != data.length) {
throw new MotanFrameworkException("decode error: content length error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
byte flag = data[3];
byte dataType = (byte) (flag & MASK);
boolean isResponse = (dataType != MotanConstants.FLAG_REQUEST);
byte[] body = new byte[bodyLength];
System.arraycopy(data, RpcProtocolVersion.VERSION_1.getHeaderLength(), body, 0, bodyLength);
long requestId = ByteUtil.bytes2long(data, 4);
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
try {
if (isResponse) {
return decodeResponse(body, dataType, requestId, data[2], serialization);
} else {
return decodeRequest(body, requestId, remoteIp, serialization);
}
} catch (ClassNotFoundException e) {
throw new MotanFrameworkException("decode " + (isResponse ? "response" : "request") + " error: class not found", e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
} catch (Exception e) {
if (ExceptionUtil.isMotanException(e)) {
throw (RuntimeException) e;
} else {
throw new MotanFrameworkException("decode error: isResponse=" + isResponse, e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
}
}
use of com.weibo.api.motan.codec.Serialization in project motan by weibocom.
the class DefaultRpcCodec method encodeResponse.
/**
* response body 数据:
*
* <pre>
*
* body:
*
* byte[] : serialize (result) or serialize (exception)
*
* </pre>
*
* @param channel
* @param value
* @return
* @throws IOException
*/
private byte[] encodeResponse(Channel channel, Response value) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutput output = createOutput(outputStream);
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
byte flag = 0;
output.writeLong(value.getProcessTime());
if (value.getException() != null) {
output.writeUTF(value.getException().getClass().getName());
serialize(output, value.getException(), serialization);
flag = MotanConstants.FLAG_RESPONSE_EXCEPTION;
} else if (value.getValue() == null) {
flag = MotanConstants.FLAG_RESPONSE_VOID;
} else {
output.writeUTF(value.getValue().getClass().getName());
serialize(output, value.getValue(), serialization);
flag = MotanConstants.FLAG_RESPONSE;
}
output.flush();
byte[] body = outputStream.toByteArray();
output.close();
return encode(body, flag, value.getRequestId());
}
use of com.weibo.api.motan.codec.Serialization in project motan by weibocom.
the class CompressRpcCodec method encodeResponse.
/**
* response body 数据:
*
* <pre>
*
* body:
*
* byte[] : serialize (result) or serialize (exception)
*
* </pre>
*
* @param channel
* @param value
* @return
* @throws IOException
*/
private byte[] encodeResponse(Channel channel, Response value) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutput output = createOutput(outputStream);
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
byte flag = 0;
output.writeLong(value.getProcessTime());
if (value.getException() != null) {
output.writeUTF(value.getException().getClass().getName());
serialize(output, value.getException(), serialization);
flag = MotanConstants.FLAG_RESPONSE_EXCEPTION;
} else if (value.getValue() == null) {
flag = MotanConstants.FLAG_RESPONSE_VOID;
} else {
output.writeUTF(value.getValue().getClass().getName());
serialize(output, value.getValue(), serialization);
// v2版本可以在response中添加attachment
Map<String, String> attachments = value.getAttachments();
if (attachments != null) {
String signed = attachments.get(ATTACHMENT_SIGN);
String unSigned = attachments.get(UN_ATTACHMENT_SIGN);
// 除了attachment签名外不返回其他信息。
attachments.clear();
if (StringUtils.isNotBlank(signed)) {
attachments.put(ATTACHMENT_SIGN, signed);
}
if (StringUtils.isNotBlank(unSigned)) {
attachments.put(UN_ATTACHMENT_SIGN, unSigned);
}
}
if (attachments != null && !attachments.isEmpty()) {
// 需要回传附加数据
addAttachment(output, attachments);
} else {
// empty attachments
output.writeShort(0);
}
// v2版本flag
flag = MotanConstants.FLAG_RESPONSE_ATTACHMENT;
}
output.flush();
byte[] body = outputStream.toByteArray();
output.close();
Boolean usegz = channel.getUrl().getBooleanParameter(URLParamType.usegz.getName(), URLParamType.usegz.getBooleanValue());
int minGzSize = channel.getUrl().getIntParameter(URLParamType.mingzSize.getName(), URLParamType.mingzSize.getIntValue());
return encode(compress(body, usegz, minGzSize), flag, value.getRequestId());
}
use of com.weibo.api.motan.codec.Serialization in project motan by weibocom.
the class DefaultRpcCodec method encodeRequest.
/**
* request body 数据:
*
* <pre>
*
* body:
*
* byte[] data :
*
* serialize(interface_name, method_name, method_param_desc, method_param_value, attachments_size, attachments_value)
*
* method_param_desc: for_each (string.append(method_param_interface_name))
*
* method_param_value: for_each (method_param_name, method_param_value)
*
* attachments_value: for_each (attachment_name, attachment_value)
*
* </pre>
*
* @param request
* @return
* @throws IOException
*/
private byte[] encodeRequest(Channel channel, Request request) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutput output = createOutput(outputStream);
output.writeUTF(request.getInterfaceName());
output.writeUTF(request.getMethodName());
output.writeUTF(request.getParamtersDesc());
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
if (request.getArguments() != null && request.getArguments().length > 0) {
for (Object obj : request.getArguments()) {
serialize(output, obj, serialization);
}
}
if (request.getAttachments() == null || request.getAttachments().isEmpty()) {
// empty attachments
output.writeInt(0);
} else {
output.writeInt(request.getAttachments().size());
for (Map.Entry<String, String> entry : request.getAttachments().entrySet()) {
output.writeUTF(entry.getKey());
output.writeUTF(entry.getValue());
}
}
output.flush();
byte[] body = outputStream.toByteArray();
byte flag = MotanConstants.FLAG_REQUEST;
output.close();
return encode(body, flag, request.getRequestId());
}
Aggregations