use of com.weibo.api.motan.rpc.DefaultResponse in project motan by weibocom.
the class CompressRpcCodec method decodeResponse.
/**
*
* @param body
* @param dataType
* @param requestId
* @param rpcProtocolVersion rpc协议的版本号,不同版本可能有不同的序列化方式
* @param serialization
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
private Object decodeResponse(byte[] body, byte dataType, long requestId, byte rpcProtocolVersion, Serialization serialization) throws IOException, ClassNotFoundException {
ObjectInput input = createInput(getInputStream(body));
long processTime = input.readLong();
DefaultResponse response = new DefaultResponse();
response.setRequestId(requestId);
response.setProcessTime(processTime);
if (dataType == MotanConstants.FLAG_RESPONSE_VOID) {
return response;
}
String className = input.readUTF();
Class<?> clz = ReflectUtil.forName(className);
Object result = deserialize((byte[]) input.readObject(), clz, serialization);
if (dataType == MotanConstants.FLAG_RESPONSE) {
response.setValue(result);
} else if (dataType == MotanConstants.FLAG_RESPONSE_ATTACHMENT) {
response.setValue(result);
Map<String, String> attachment = decodeRequestAttachments(input);
checkAttachment(attachment);
} else if (dataType == MotanConstants.FLAG_RESPONSE_EXCEPTION) {
response.setException((Exception) result);
} else {
throw new MotanFrameworkException("decode error: response dataType not support " + dataType, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
response.setRequestId(requestId);
input.close();
return response;
}
use of com.weibo.api.motan.rpc.DefaultResponse in project motan by weibocom.
the class DefaultRpcCodec method decodeResponse.
private Object decodeResponse(byte[] body, byte dataType, long requestId, Serialization serialization) throws IOException, ClassNotFoundException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(body);
ObjectInput input = createInput(inputStream);
long processTime = input.readLong();
DefaultResponse response = new DefaultResponse();
response.setRequestId(requestId);
response.setProcessTime(processTime);
if (dataType == MotanConstants.FLAG_RESPONSE_VOID) {
return response;
}
String className = input.readUTF();
Class<?> clz = ReflectUtil.forName(className);
Object result = deserialize((byte[]) input.readObject(), clz, serialization);
if (dataType == MotanConstants.FLAG_RESPONSE) {
response.setValue(result);
} else if (dataType == MotanConstants.FLAG_RESPONSE_EXCEPTION) {
response.setException((Exception) result);
} else {
throw new MotanFrameworkException("decode error: response dataType not support " + dataType, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
response.setRequestId(requestId);
input.close();
return response;
}
use of com.weibo.api.motan.rpc.DefaultResponse in project motan by weibocom.
the class NettyChannelHandler method processRequest.
private void processRequest(ChannelHandlerContext ctx, Request request, long processStartTime) {
Object result = messageHandler.handle(serverChannel, request);
DefaultResponse response = null;
if (!(result instanceof DefaultResponse)) {
response = new DefaultResponse(result);
} else {
response = (DefaultResponse) result;
}
response.setRequestId(request.getRequestId());
response.setProcessTime(System.currentTimeMillis() - processStartTime);
if (ctx.getChannel().isConnected()) {
ctx.getChannel().write(response);
}
}
use of com.weibo.api.motan.rpc.DefaultResponse in project motan by weibocom.
the class NettyDecoder method buildExceptionResponse.
private Response buildExceptionResponse(long requestId, Exception e) {
DefaultResponse response = new DefaultResponse();
response.setRequestId(requestId);
response.setException(e);
return response;
}
use of com.weibo.api.motan.rpc.DefaultResponse in project motan by weibocom.
the class MockDefaultRpcCodec method decode.
@Override
public Object decode(Channel channel, String remoteIp, byte[] buffer) throws IOException {
Object result = codec.decode(channel, remoteIp, buffer);
if (result instanceof Response) {
DefaultResponse object = (DefaultResponse) result;
byte flag = buffer[3];
byte dataType = (byte) (flag & MASK);
boolean isResponse = (dataType != MotanConstants.FLAG_REQUEST);
if (object.getException() == null) {
if (isResponse && object.getValue().equals("error")) {
DefaultResponse response = (DefaultResponse) object;
response.setException(new MotanFrameworkException("decode error: response dataType not support " + dataType, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR));
return response;
} else {
throw new MotanFrameworkException(MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
}
}
return object;
}
return result;
}
Aggregations