Search in sources :

Example 6 with DefaultResponse

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;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ObjectInput(java.io.ObjectInput) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with DefaultResponse

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;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 8 with DefaultResponse

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);
    }
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse)

Example 9 with DefaultResponse

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;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse)

Example 10 with DefaultResponse

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;
}
Also used : Response(com.weibo.api.motan.rpc.Response) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Aggregations

DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)28 Test (org.junit.Test)12 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)7 Request (com.weibo.api.motan.rpc.Request)7 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)5 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)4 Response (com.weibo.api.motan.rpc.Response)4 Channel (com.weibo.api.motan.transport.Channel)4 MessageHandler (com.weibo.api.motan.transport.MessageHandler)4 MotanBizException (com.weibo.api.motan.exception.MotanBizException)3 URL (com.weibo.api.motan.rpc.URL)2 YarResponse (com.weibo.yar.YarResponse)2 ObjectInput (java.io.ObjectInput)2 Model (com.weibo.api.motan.protocol.example.Model)1 DefaultProvider (com.weibo.api.motan.rpc.DefaultProvider)1 Future (com.weibo.api.motan.rpc.Future)1 FutureListener (com.weibo.api.motan.rpc.FutureListener)1 Provider (com.weibo.api.motan.rpc.Provider)1 YarRequest (com.weibo.yar.YarRequest)1 Tracer (io.opentracing.Tracer)1