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

The following examples show how to use com.alipay.sofa.rpc.core.request.SofaRequest#setTargetAppName() . 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: Http1ServerChannelHandler.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
private void parseHeader(FullHttpRequest httpRequest, SofaRequest sofaRequest) {
    HttpHeaders headers = httpRequest.headers();

    // 获取序列化类型
    byte serializeType;
    if (httpRequest.method() == HttpMethod.GET) {
        serializeType = 0;
    } else {
        String codeName = headers.get(RemotingConstants.HEAD_SERIALIZE_TYPE);
        if (codeName != null) {
            serializeType = HttpTransportUtils.getSerializeTypeByName(codeName);
        } else {
            String contentType = headers.get(HttpHeaderNames.CONTENT_TYPE);
            serializeType = HttpTransportUtils.getSerializeTypeByContentType(contentType);
        }
    }
    sofaRequest.setSerializeType(serializeType);
    // 服务端应用
    sofaRequest.setTargetAppName(headers.get(RemotingConstants.HEAD_TARGET_APP));
}
 
Example 2
Source File: RestLookoutAdapter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
public static void sendRestServerSendEvent(RestServerSendEvent restServerSendEvent) {
    //this is special for rest
    if (EventBus.isEnable(ServerSendEvent.class)) {
        SofaRequest request = new SofaRequest();

        String appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME);
        request.setTargetAppName(appName);
        request.addRequestProp(RemotingConstants.HEAD_APP_NAME, restServerSendEvent.getRequest().getHttpHeaders()
            .getHeaderString(RemotingConstants.HEAD_APP_NAME));
        RpcInternalContext context = RpcInternalContext.getContext();
        request.setTargetServiceUniqueName((String) context.getAttachment(INTERNAL_KEY_PREFIX +
            RestConstants.REST_SERVICE_KEY));

        request.setMethodName((String) context.getAttachment(INTERNAL_KEY_PREFIX +
            RestConstants.REST_METHODNAME_KEY));
        request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST);
        request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
        SofaResponse response = new SofaResponse();

        if (restServerSendEvent.getThrowable() != null) {
            response.setErrorMsg(restServerSendEvent.getThrowable().getMessage());
        }
        final ServerSendEvent event = new ServerSendEvent(request, response, restServerSendEvent.getThrowable());
        EventBus.post(event);
    }
}
 
Example 3
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 4
Source File: Http2ServerChannelHandler.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void parseHttp2Request(Http2Headers headers, SofaRequest sofaRequest) {
    String targetApp = StringUtils.toString(headers.get(RemotingConstants.HEAD_TARGET_APP));
    sofaRequest.setTargetAppName(targetApp);
    // 获取序列化类型
    byte serializeType;
    CharSequence codeName = headers.get(RemotingConstants.HEAD_SERIALIZE_TYPE);
    if (codeName != null) {
        serializeType = HttpTransportUtils.getSerializeTypeByName(codeName.toString());
    } else {
        String contentType = StringUtils.toString(headers.get(HttpHeaderNames.CONTENT_TYPE));
        serializeType = HttpTransportUtils.getSerializeTypeByContentType(contentType);
    }
    sofaRequest.setSerializeType(serializeType);

    // 解析trace信息
    Map<String, String> traceMap = new HashMap<String, String>(8);
    Iterator<Map.Entry<CharSequence, CharSequence>> it = headers.iterator();
    while (it.hasNext()) {
        Map.Entry<CharSequence, CharSequence> entry = it.next();
        String key = entry.getKey().toString();
        if (HttpTracerUtils.isTracerKey(key)) {
            HttpTracerUtils.parseTraceKey(traceMap, key, StringUtils.toString(entry.getValue()));
        } else if (!key.startsWith(":")) {
            sofaRequest.addRequestProp(key, StringUtils.toString(entry.getValue()));
        }
    }
    if (!traceMap.isEmpty()) {
        sofaRequest.addRequestProp(RemotingConstants.RPC_TRACE_NAME, traceMap);
    }
}
 
Example 5
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 6
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 7
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 8
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 9
Source File: ConsumerInvoker.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(SofaRequest sofaRequest) throws SofaRpcException {
    // 设置下服务器应用
    ProviderInfo providerInfo = RpcInternalContext.getContext().getProviderInfo();
    String appName = providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME);
    if (StringUtils.isNotEmpty(appName)) {
        sofaRequest.setTargetAppName(appName);
    }

    // 目前只是通过client发送给服务端
    return consumerBootstrap.getCluster().sendMsg(providerInfo, sofaRequest);
}
 
Example 10
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()});
}