Search in sources :

Example 26 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class CompressRpcCodec method decodeRequest.

private Object decodeRequest(byte[] body, long requestId, String remoteIp, Serialization serialization) throws IOException, ClassNotFoundException {
    ObjectInput input = createInput(getInputStream(body));
    String interfaceName = null;
    String methodName = null;
    String paramtersDesc = null;
    String group = null;
    String version = null;
    String flag = input.readUTF();
    if (SIGN_FLAG.equals(flag)) {
        // 方法签名方式
        String sign = input.readUTF();
        MethodInfo mInfo = SIGN_METHOD_MAP.get(sign);
        if (mInfo == null) {
            throw new MotanFrameworkException("decode error: invalid method sign: " + sign, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
        }
        interfaceName = mInfo.getInterfaceName();
        methodName = mInfo.getMethodName();
        paramtersDesc = mInfo.getParamtersDesc();
        group = mInfo.getGroup();
        version = mInfo.getVersion();
    } else {
        interfaceName = flag;
        methodName = input.readUTF();
        paramtersDesc = input.readUTF();
    }
    DefaultRequest rpcRequest = new DefaultRequest();
    rpcRequest.setRequestId(requestId);
    rpcRequest.setInterfaceName(interfaceName);
    rpcRequest.setMethodName(methodName);
    rpcRequest.setParamtersDesc(paramtersDesc);
    rpcRequest.setArguments(decodeRequestParameter(input, paramtersDesc, serialization));
    rpcRequest.setAttachments(decodeRequestAttachments(input));
    rpcRequest.setRpcProtocolVersion(RpcProtocolVersion.VERSION_2.getVersion());
    input.close();
    Map<String, String> attachments = rpcRequest.getAttachments();
    // 根据签名添加client固定参数。
    putSignedAttachment(attachments, remoteIp);
    if (attachments.get(URLParamType.group.name()) == null) {
        // 如果attachment sign失效时,需要使用methodsign中的group信息。
        attachments.put(URLParamType.group.name(), group);
        attachments.put(URLParamType.version.name(), version);
    }
    return rpcRequest;
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) ObjectInput(java.io.ObjectInput)

Example 27 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class DefaultRpcHeartbeatFactory method createRequest.

@Override
public Request createRequest() {
    DefaultRequest request = new DefaultRequest();
    request.setRequestId(RequestIdGenerator.getRequestId());
    request.setInterfaceName(MotanConstants.HEARTBEAT_INTERFACE_NAME);
    request.setMethodName(MotanConstants.HEARTBEAT_METHOD_NAME);
    request.setParamtersDesc(MotanConstants.HHEARTBEAT_PARAM);
    return request;
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Example 28 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class YarProtocolUtil method convert.

/**
 * convert yar request to motan rpc request
 *
 * @param yarRequest
 * @param interfaceClass
 * @return
 */
public static Request convert(YarRequest yarRequest, Class<?> interfaceClass) {
    DefaultRequest request = new DefaultRequest();
    request.setInterfaceName(interfaceClass.getName());
    request.setMethodName(yarRequest.getMethodName());
    request.setRequestId(yarRequest.getId());
    addArguments(request, interfaceClass, yarRequest.getMethodName(), yarRequest.getParameters());
    if (yarRequest instanceof AttachmentRequest) {
        request.setAttachments(((AttachmentRequest) yarRequest).getAttachments());
    }
    return request;
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Example 29 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class MotanServerCallHandler method getBaseMotanRequest.

private DefaultRequest getBaseMotanRequest(Metadata headers) {
    DefaultRequest request = new DefaultRequest();
    request.setMethodName(methodName);
    request.setParamtersDesc(paramsDesc);
    request.setInterfaceName(provider.getInterface().getName());
    String rid = headers.get(REQUEST_ID);
    if (rid == null) {
        rid = headers.get(Metadata.Key.of(URLParamType.requestIdFromClient.getName().toLowerCase(), Metadata.ASCII_STRING_MARSHALLER));
    }
    if (rid != null) {
        request.setAttachment(URLParamType.requestIdFromClient.getName(), rid);
    }
    // fill attachment info from headers
    Set<String> keys = headers.keys();
    for (String key : keys) {
        String value = headers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
        if (value != null) {
            request.setAttachment(key, value);
        }
    }
    return request;
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Example 30 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class UnSerializableClass method testNettyEncodeException.

@Test
public void testNettyEncodeException() throws Exception {
    NettyServer nettyServer;
    nettyServer = new NettyServer(url, new MessageHandler() {

        @Override
        public Object handle(Channel channel, Object message) {
            Request request = (Request) message;
            DefaultResponse response = new DefaultResponse();
            response.setRequestId(request.getRequestId());
            // 序列化错误
            response.setValue(new UnSerializableClass());
            return response;
        }
    });
    nettyServer.open();
    NettyClient nettyClient = new NettyClient(url);
    nettyClient.open();
    DefaultRequest request = new DefaultRequest();
    request.setRequestId(RequestIdGenerator.getRequestId());
    request.setInterfaceName(url.getPath());
    request.setMethodName("helloSerializable");
    request.setParamtersDesc("com.weibo.api.motan.procotol.example.UnSerializableClass");
    request.setArguments(new Object[] { new UnSerializableClass() });
    try {
        nettyClient.request(request);
        Assert.assertFalse(true);
    } catch (Exception e) {
        Assert.assertTrue(true);
    }
    DefaultRequest request1 = new DefaultRequest();
    request1.setRequestId(RequestIdGenerator.getRequestId());
    request1.setInterfaceName(url.getPath());
    request1.setMethodName("helloSerializable");
    request1.setParamtersDesc("void");
    try {
        nettyClient.request(request1);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("error_code: 20002"));
    } finally {
        nettyClient.close();
        nettyServer.close();
    }
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MessageHandler(com.weibo.api.motan.transport.MessageHandler) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Channel(com.weibo.api.motan.transport.Channel) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Aggregations

DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)45 Test (org.junit.Test)26 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)8 Request (com.weibo.api.motan.rpc.Request)7 Model (com.weibo.api.motan.protocol.example.Model)5 Response (com.weibo.api.motan.rpc.Response)5 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)4 Channel (com.weibo.api.motan.transport.Channel)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 IHello (com.weibo.api.motan.protocol.example.IHello)3 URL (com.weibo.api.motan.rpc.URL)3 MessageHandler (com.weibo.api.motan.transport.MessageHandler)3 HashMap (java.util.HashMap)3 MockChannel (com.weibo.api.motan.mock.MockChannel)2 MockReferer (com.weibo.api.motan.mock.MockReferer)2 Referer (com.weibo.api.motan.rpc.Referer)2 Listener (io.grpc.ServerCall.Listener)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Codec (com.weibo.api.motan.codec.Codec)1 Serialization (com.weibo.api.motan.codec.Serialization)1