Java Code Examples for com.alipay.sofa.rpc.core.request.SofaRequest#setMethodArgSigs()

The following examples show how to use com.alipay.sofa.rpc.core.request.SofaRequest#setMethodArgSigs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComplexDataGenerator.java    From sofa-hessian with Apache License 2.0 5 votes vote down vote up
public SofaRequest generateSofaRequest() {
    SofaRequest request = new SofaRequest();

    request.setTargetServiceUniqueName("liqiwei");
    request.setMethodName("sayHello");
    request.setTargetAppName("HelloService");

    request.setMethodArgSigs(new String[] { "com.lqw.testPerson", "int" });
    request.setMethodArgs(new Object[] { generateGenericPerson_6(), 1992 });
    request.setMethod(null);

    request.addRequestProps("trace", new HashMap<String, String>());

    return request;
}
 
Example 2
Source File: SofaRpcUtilsTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMethodResourceName() {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName("com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService");
    request.setMethodName("sayHello");
    request.setMethodArgSigs(new String[]{"java.lang.String", "int"});
    String methodResourceName = SofaRpcUtils.getMethodResourceName(request);
    assertEquals("com.alibaba.csp.sentinel.adapter.sofa.rpc.service.DemoService#sayHello(java.lang.String,int)", methodResourceName);
}
 
Example 3
Source File: ProtobufSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaRequest(AbstractByteBuf data, SofaRequest sofaRequest, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    // 解析request信息
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService != null) {
        sofaRequest.setTargetServiceUniqueName(targetService);
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetService);
        sofaRequest.setInterfaceName(interfaceName);
    } else {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName != null) {
        sofaRequest.setMethodName(methodName);
    } else {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }
    String targetApp = head.remove(RemotingConstants.HEAD_TARGET_APP);
    if (targetApp != null) {
        sofaRequest.setTargetAppName(targetApp);
    }

    // 解析tracer等信息
    parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, head, sofaRequest);
    if (RpcInvokeContext.isBaggageEnable()) {
        parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, head, sofaRequest);
    }
    for (Map.Entry<String, String> entry : head.entrySet()) {
        sofaRequest.addRequestProp(entry.getKey(), entry.getValue());
    }

    // 根据接口+方法名找到参数类型 此处要处理byte[]为空的吗
    Class requestClass = protobufHelper.getReqClass(targetService,
        sofaRequest.getMethodName());
    Object pbReq = decode(data, requestClass, head);
    sofaRequest.setMethodArgs(new Object[] { pbReq });
    sofaRequest.setMethodArgSigs(new String[] { requestClass.getName() });
}
 
Example 4
Source File: SofaRequestHessianSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 5
Source File: JacksonSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaRequest(AbstractByteBuf data, SofaRequest sofaRequest, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    // 解析request信息
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService != null) {
        sofaRequest.setTargetServiceUniqueName(targetService);
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetService);
        sofaRequest.setInterfaceName(interfaceName);
    } else {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName != null) {
        sofaRequest.setMethodName(methodName);
    } else {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }
    String targetApp = head.remove(RemotingConstants.HEAD_TARGET_APP);
    if (targetApp != null) {
        sofaRequest.setTargetAppName(targetApp);
    }

    // parse tracer and baggage
    parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, head, sofaRequest);
    if (RpcInvokeContext.isBaggageEnable()) {
        parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, head, sofaRequest);
    }
    for (Map.Entry<String, String> entry : head.entrySet()) {
        sofaRequest.addRequestProp(entry.getKey(), entry.getValue());
    }

    // according interface and method name to find parameter types
    JavaType[] requestClassList = jacksonHelper.getReqClass(targetService, sofaRequest.getMethodName());
    Object[] reqList = decode(data, requestClassList);
    sofaRequest.setMethodArgs(reqList);
    sofaRequest.setMethodArgSigs(parseArgSigs(requestClassList));
}
 
Example 6
Source File: ProtostuffSerializer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void decodeSofaRequest(AbstractByteBuf data, SofaRequest sofaRequest, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    // 解析request信息
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService != null) {
        sofaRequest.setTargetServiceUniqueName(targetService);
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetService);
        sofaRequest.setInterfaceName(interfaceName);
    } else {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName != null) {
        sofaRequest.setMethodName(methodName);
    } else {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }
    String targetApp = head.remove(RemotingConstants.HEAD_TARGET_APP);
    if (targetApp != null) {
        sofaRequest.setTargetAppName(targetApp);
    }

    // 解析tracer等信息
    parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, head, sofaRequest);
    if (RpcInvokeContext.isBaggageEnable()) {
        parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, head, sofaRequest);
    }
    for (Map.Entry<String, String> entry : head.entrySet()) {
        sofaRequest.addRequestProp(entry.getKey(), entry.getValue());
    }

    // 根据接口+方法名找到参数类型 此处要处理byte[]为空的吗
    Class requestClass = protostuffHelper.getReqClass(targetService,
        sofaRequest.getMethodName());
    Object pbReq = decode(data, requestClass, head);
    sofaRequest.setMethodArgs(new Object[] { pbReq });
    sofaRequest.setMethodArgSigs(new String[] { requestClass.getName() });
}
 
Example 7
Source File: MessageBuilder.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 构建请求,常用于代理类拦截
 *
 * @param clazz    接口类
 * @param method   方法
 * @param argTypes 方法参数类型
 * @param args     方法参数值
 * @return 远程调用请求
 */
public static SofaRequest buildSofaRequest(Class<?> clazz, Method method, Class[] argTypes, Object[] args) {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(clazz.getName());
    request.setMethodName(method.getName());
    request.setMethod(method);
    request.setMethodArgs(args == null ? CodecUtils.EMPTY_OBJECT_ARRAY : args);
    request.setMethodArgSigs(ClassTypeUtils.getTypeStrs(argTypes, true));
    return request;
}
 
Example 8
Source File: RestTracerAdapter.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
/**
 * 适配服务端serverSend
 */
public static void serverSend(NettyHttpResponse response, Throwable throwable) {
    try {
        SofaRequest sofaRequest = new SofaRequest();
        SofaResponse sofaResponse = new SofaResponse();

        if (response == null) {
            sofaResponse.setErrorMsg("rest path ends with /favicon.ico");
        } else if (throwable != null) {
            if (response.getStatus() == 500) {
                sofaResponse.setAppResponse(throwable);
            } else {
                sofaResponse.setErrorMsg(throwable.getMessage());
            }

            Object method = RpcInternalContext.getContext().getAttachment(METHOD_TYPE_STRING);
            if (method != null) {
                Class[] parameterTypes = ((Method) method).getParameterTypes();
                String[] methodTypeString = new String[parameterTypes.length];
                for (int i = 0; i < methodTypeString.length; i++) {
                    methodTypeString[i] = (parameterTypes[i].getName());
                }
                sofaRequest.setMethodArgSigs(methodTypeString);
            }
        }

        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();

        RpcInternalContext context = RpcInternalContext.getContext();

        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME,
                (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }

        RestBaggageItemsHandler.encodeBaggageItemsToResponse(response, sofaResponse);
        Tracers.serverSend(sofaRequest, sofaResponse, null);
    } catch (Throwable t) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("the process of rest tracer server send occur error ", t);
        }
    }
}
 
Example 9
Source File: MsgPackSerializer.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
private void decodeSofaRequest(AbstractByteBuf data, SofaRequest sofaRequest, Map<String, String> head) {
    if (head == null) {
        throw buildDeserializeError("head is null!");
    }
    // 解析request信息
    String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE);
    if (targetService != null) {
        sofaRequest.setTargetServiceUniqueName(targetService);
        String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetService);
        sofaRequest.setInterfaceName(interfaceName);
    } else {
        throw buildDeserializeError("HEAD_TARGET_SERVICE is null");
    }
    String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME);
    if (methodName != null) {
        sofaRequest.setMethodName(methodName);
    } else {
        throw buildDeserializeError("HEAD_METHOD_NAME is null");
    }
    String targetApp = head.remove(RemotingConstants.HEAD_TARGET_APP);
    if (targetApp != null) {
        sofaRequest.setTargetAppName(targetApp);
    }

    // parse tracer and baggage
    parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, head, sofaRequest);
    if (RpcInvokeContext.isBaggageEnable()) {
        parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, head, sofaRequest);
    }
    for (Map.Entry<String, String> entry : head.entrySet()) {
        sofaRequest.addRequestProp(entry.getKey(), entry.getValue());
    }

    // according interface and method name to find paramter types
    Class requestClass = helper.getReqClass(targetService,
            sofaRequest.getMethodName());

    Object pbReq = decode(data, requestClass, head);
    sofaRequest.setMethodArgs(new Object[]{pbReq});
    sofaRequest.setMethodArgSigs(new String[]{requestClass.getName()});
}
 
Example 10
Source File: MessageBuilder.java    From sofa-rpc with Apache License 2.0 3 votes vote down vote up
/**
 * 构建请求,常用于代理类拦截
 *
 * @param clazz    接口类
 * @param method   方法名
 * @param argTypes 方法参数类型
 * @param args     方法参数值
 * @return 远程调用请求
 * @deprecated use {@link #buildSofaRequest(Class, Method, Class[], Object[])}
 */
@Deprecated
public static SofaRequest buildSofaRequest(Class<?> clazz, String method, Class[] argTypes, Object[] args) {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(clazz.getName());
    request.setMethodName(method);
    request.setMethodArgs(args == null ? CodecUtils.EMPTY_OBJECT_ARRAY : args);
    request.setMethodArgSigs(ClassTypeUtils.getTypeStrs(argTypes, true));
    return request;
}