Search in sources :

Example 6 with ServiceRepository

use of org.apache.dubbo.rpc.model.ServiceRepository in project dubbo by alibaba.

the class CodecSupport method checkSerialization.

public static void checkSerialization(String path, String version, Byte id) throws IOException {
    ServiceRepository repository = ApplicationModel.getServiceRepository();
    ProviderModel providerModel = repository.lookupExportedServiceWithoutGroup(path + ":" + version);
    if (providerModel == null) {
        throw new IOException("Service " + path + " with version " + version + " not found, invocation rejected.");
    } else {
        List<URL> urls = providerModel.getServiceConfig().getExportedUrls();
        if (CollectionUtils.isNotEmpty(urls)) {
            URL url = urls.get(0);
            String serializationName = url.getParameter(org.apache.dubbo.remoting.Constants.SERIALIZATION_KEY, Constants.DEFAULT_REMOTING_SERIALIZATION);
            Byte localId = SERIALIZATIONNAME_ID_MAP.get(serializationName);
            if (localId != null && !localId.equals(id)) {
                throw new IOException("Unexpected serialization id:" + id + " received from network, please check if the peer send the right id.");
            }
        }
    }
}
Also used : IOException(java.io.IOException) ServiceRepository(org.apache.dubbo.rpc.model.ServiceRepository) URL(org.apache.dubbo.common.URL) ProviderModel(org.apache.dubbo.rpc.model.ProviderModel)

Example 7 with ServiceRepository

use of org.apache.dubbo.rpc.model.ServiceRepository in project dubbo by alibaba.

the class DecodeableRpcInvocation method decode.

@Override
public Object decode(Channel channel, InputStream input) throws IOException {
    ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType).deserialize(channel.getUrl(), input);
    this.put(SERIALIZATION_ID_KEY, serializationType);
    String dubboVersion = in.readUTF();
    request.setVersion(dubboVersion);
    setAttachment(DUBBO_VERSION_KEY, dubboVersion);
    String path = in.readUTF();
    setAttachment(PATH_KEY, path);
    String version = in.readUTF();
    setAttachment(VERSION_KEY, version);
    setMethodName(in.readUTF());
    String desc = in.readUTF();
    setParameterTypesDesc(desc);
    try {
        if (ConfigurationUtils.getSystemConfiguration().getBoolean(SERIALIZATION_SECURITY_CHECK_KEY, false)) {
            CodecSupport.checkSerialization(path, version, serializationType);
        }
        Object[] args = DubboCodec.EMPTY_OBJECT_ARRAY;
        Class<?>[] pts = DubboCodec.EMPTY_CLASS_ARRAY;
        if (desc.length() > 0) {
            // if (RpcUtils.isGenericCall(path, getMethodName()) || RpcUtils.isEcho(path, getMethodName())) {
            // pts = ReflectUtils.desc2classArray(desc);
            // } else {
            ServiceRepository repository = ApplicationModel.getServiceRepository();
            ServiceDescriptor serviceDescriptor = repository.lookupService(path);
            if (serviceDescriptor != null) {
                MethodDescriptor methodDescriptor = serviceDescriptor.getMethod(getMethodName(), desc);
                if (methodDescriptor != null) {
                    pts = methodDescriptor.getParameterClasses();
                    this.setReturnTypes(methodDescriptor.getReturnTypes());
                }
            }
            if (pts == DubboCodec.EMPTY_CLASS_ARRAY) {
                if (!RpcUtils.isGenericCall(desc, getMethodName()) && !RpcUtils.isEcho(desc, getMethodName())) {
                    throw new IllegalArgumentException("Service not found:" + path + ", " + getMethodName());
                }
                pts = ReflectUtils.desc2classArray(desc);
            }
            // }
            args = new Object[pts.length];
            for (int i = 0; i < args.length; i++) {
                try {
                    args[i] = in.readObject(pts[i]);
                } catch (Exception e) {
                    if (log.isWarnEnabled()) {
                        log.warn("Decode argument failed: " + e.getMessage(), e);
                    }
                }
            }
        }
        setParameterTypes(pts);
        Map<String, Object> map = in.readAttachments();
        if (map != null && map.size() > 0) {
            Map<String, Object> attachment = getObjectAttachments();
            if (attachment == null) {
                attachment = new HashMap<>();
            }
            attachment.putAll(map);
            setObjectAttachments(attachment);
        }
        // decode argument ,may be callback
        for (int i = 0; i < args.length; i++) {
            args[i] = decodeInvocationArgument(channel, this, pts, i, args[i]);
        }
        setArguments(args);
        String targetServiceName = buildKey(getAttachment(PATH_KEY), getAttachment(GROUP_KEY), getAttachment(VERSION_KEY));
        setTargetServiceUniqueName(targetServiceName);
    } catch (ClassNotFoundException e) {
        throw new IOException(StringUtils.toString("Read invocation data failed.", e));
    } finally {
        if (in instanceof Cleanable) {
            ((Cleanable) in).cleanup();
        }
    }
    return this;
}
Also used : IOException(java.io.IOException) ServiceRepository(org.apache.dubbo.rpc.model.ServiceRepository) MethodDescriptor(org.apache.dubbo.rpc.model.MethodDescriptor) IOException(java.io.IOException) ServiceDescriptor(org.apache.dubbo.rpc.model.ServiceDescriptor) ObjectInput(org.apache.dubbo.common.serialize.ObjectInput) Cleanable(org.apache.dubbo.common.serialize.Cleanable)

Example 8 with ServiceRepository

use of org.apache.dubbo.rpc.model.ServiceRepository in project dubbo by alibaba.

the class RpcUtilsTest method testIsReturnTypeFuture.

@Test
public void testIsReturnTypeFuture() {
    Class<?> demoServiceClass = DemoService.class;
    String serviceName = demoServiceClass.getName();
    Invoker invoker = mock(Invoker.class);
    given(invoker.getUrl()).willReturn(URL.valueOf("test://127.0.0.1:1/org.apache.dubbo.rpc.support.DemoService?interface=org.apache.dubbo.rpc.support.DemoService"));
    RpcInvocation inv = new RpcInvocation("testReturnType", serviceName, "", new Class<?>[] { String.class }, null, null, invoker, null);
    Assertions.assertFalse(RpcUtils.isReturnTypeFuture(inv));
    ServiceRepository repository = ApplicationModel.getServiceRepository();
    repository.registerService(demoServiceClass);
    inv = new RpcInvocation("testReturnType4", serviceName, "", new Class<?>[] { String.class }, null, null, invoker, null);
    Assertions.assertTrue(RpcUtils.isReturnTypeFuture(inv));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) ServiceRepository(org.apache.dubbo.rpc.model.ServiceRepository) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ServiceRepository (org.apache.dubbo.rpc.model.ServiceRepository)8 ServiceDescriptor (org.apache.dubbo.rpc.model.ServiceDescriptor)5 IOException (java.io.IOException)3 URL (org.apache.dubbo.common.URL)2 MethodDescriptor (org.apache.dubbo.rpc.model.MethodDescriptor)2 ProviderModel (org.apache.dubbo.rpc.model.ProviderModel)2 Server (io.grpc.Server)1 NettyServerBuilder (io.grpc.netty.NettyServerBuilder)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Cleanable (org.apache.dubbo.common.serialize.Cleanable)1 ObjectInput (org.apache.dubbo.common.serialize.ObjectInput)1 Invoker (org.apache.dubbo.rpc.Invoker)1 ProtocolServer (org.apache.dubbo.rpc.ProtocolServer)1 RpcException (org.apache.dubbo.rpc.RpcException)1 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1