Search in sources :

Example 1 with Response

use of com.alibaba.nacos.api.remote.response.Response in project nacos by alibaba.

the class PayloadRegistry method scan.

private static synchronized void scan() {
    if (initialized) {
        return;
    }
    List<String> requestScanPackage = Arrays.asList("com.alibaba.nacos.api.naming.remote.request", "com.alibaba.nacos.api.config.remote.request", "com.alibaba.nacos.api.remote.request", "com.alibaba.nacos.naming.cluster.remote.request");
    for (String pkg : requestScanPackage) {
        Reflections reflections = new Reflections(pkg);
        Set<Class<? extends Request>> subTypesRequest = reflections.getSubTypesOf(Request.class);
        for (Class<?> clazz : subTypesRequest) {
            register(clazz.getSimpleName(), clazz);
        }
    }
    List<String> responseScanPackage = Arrays.asList("com.alibaba.nacos.api.naming.remote.response", "com.alibaba.nacos.api.config.remote.response", "com.alibaba.nacos.api.remote.response", "com.alibaba.nacos.naming.cluster.remote.response");
    for (String pkg : responseScanPackage) {
        Reflections reflections = new Reflections(pkg);
        Set<Class<? extends Response>> subTypesOfResponse = reflections.getSubTypesOf(Response.class);
        for (Class<?> clazz : subTypesOfResponse) {
            register(clazz.getSimpleName(), clazz);
        }
    }
    initialized = true;
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) Request(com.alibaba.nacos.api.remote.request.Request) Reflections(org.reflections.Reflections)

Example 2 with Response

use of com.alibaba.nacos.api.remote.response.Response in project nacos by alibaba.

the class GrpcClient method connectToServer.

@Override
public Connection connectToServer(ServerInfo serverInfo) {
    try {
        if (grpcExecutor == null) {
            this.grpcExecutor = createGrpcExecutor(serverInfo.getServerIp());
        }
        int port = serverInfo.getServerPort() + rpcPortOffset();
        RequestGrpc.RequestFutureStub newChannelStubTemp = createNewChannelStub(serverInfo.getServerIp(), port);
        if (newChannelStubTemp != null) {
            Response response = serverCheck(serverInfo.getServerIp(), port, newChannelStubTemp);
            if (response == null || !(response instanceof ServerCheckResponse)) {
                shuntDownChannel((ManagedChannel) newChannelStubTemp.getChannel());
                return null;
            }
            BiRequestStreamGrpc.BiRequestStreamStub biRequestStreamStub = BiRequestStreamGrpc.newStub(newChannelStubTemp.getChannel());
            GrpcConnection grpcConn = new GrpcConnection(serverInfo, grpcExecutor);
            grpcConn.setConnectionId(((ServerCheckResponse) response).getConnectionId());
            // create stream request and bind connection event to this connection.
            StreamObserver<Payload> payloadStreamObserver = bindRequestStream(biRequestStreamStub, grpcConn);
            // stream observer to send response to server
            grpcConn.setPayloadStreamObserver(payloadStreamObserver);
            grpcConn.setGrpcFutureServiceStub(newChannelStubTemp);
            grpcConn.setChannel((ManagedChannel) newChannelStubTemp.getChannel());
            // send a  setup request.
            ConnectionSetupRequest conSetupRequest = new ConnectionSetupRequest();
            conSetupRequest.setClientVersion(VersionUtils.getFullClientVersion());
            conSetupRequest.setLabels(super.getLabels());
            conSetupRequest.setAbilities(super.clientAbilities);
            conSetupRequest.setTenant(super.getTenant());
            grpcConn.sendRequest(conSetupRequest);
            // wait to register connection setup
            Thread.sleep(100L);
            return grpcConn;
        }
        return null;
    } catch (Exception e) {
        LOGGER.error("[{}]Fail to connect to server!,error={}", GrpcClient.this.getName(), e);
    }
    return null;
}
Also used : ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) ServerCheckResponse(com.alibaba.nacos.api.remote.response.ServerCheckResponse) BiRequestStreamGrpc(com.alibaba.nacos.api.grpc.auto.BiRequestStreamGrpc) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ConnectionSetupRequest(com.alibaba.nacos.api.remote.request.ConnectionSetupRequest) RequestGrpc(com.alibaba.nacos.api.grpc.auto.RequestGrpc) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 3 with Response

use of com.alibaba.nacos.api.remote.response.Response in project nacos by alibaba.

the class GrpcConnection method requestFuture.

@Override
public RequestFuture requestFuture(Request request) throws NacosException {
    Payload grpcRequest = GrpcUtils.convert(request);
    final ListenableFuture<Payload> requestFuture = grpcFutureServiceStub.request(grpcRequest);
    return new RequestFuture() {

        @Override
        public boolean isDone() {
            return requestFuture.isDone();
        }

        @Override
        public Response get() throws Exception {
            Payload grpcResponse = requestFuture.get();
            Response response = (Response) GrpcUtils.parse(grpcResponse);
            if (response instanceof ErrorResponse) {
                throw new NacosException(response.getErrorCode(), response.getMessage());
            }
            return response;
        }

        @Override
        public Response get(long timeout) throws Exception {
            Payload grpcResponse = requestFuture.get(timeout, TimeUnit.MILLISECONDS);
            Response response = (Response) GrpcUtils.parse(grpcResponse);
            if (response instanceof ErrorResponse) {
                throw new NacosException(response.getErrorCode(), response.getMessage());
            }
            return response;
        }
    };
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) RequestFuture(com.alibaba.nacos.api.remote.RequestFuture) Payload(com.alibaba.nacos.api.grpc.auto.Payload) NacosException(com.alibaba.nacos.api.exception.NacosException) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse)

Example 4 with Response

use of com.alibaba.nacos.api.remote.response.Response in project nacos by alibaba.

the class GrpcConnection method request.

@Override
public Response request(Request request, long timeouts) throws NacosException {
    Payload grpcRequest = GrpcUtils.convert(request);
    ListenableFuture<Payload> requestFuture = grpcFutureServiceStub.request(grpcRequest);
    Payload grpcResponse;
    try {
        grpcResponse = requestFuture.get(timeouts, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        throw new NacosException(NacosException.SERVER_ERROR, e);
    }
    return (Response) GrpcUtils.parse(grpcResponse);
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) Payload(com.alibaba.nacos.api.grpc.auto.Payload) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 5 with Response

use of com.alibaba.nacos.api.remote.response.Response in project nacos by alibaba.

the class RemoteRequestAuthFilter method filter.

@Override
public Response filter(Request request, RequestMeta meta, Class handlerClazz) throws NacosException {
    try {
        Method method = getHandleMethod(handlerClazz);
        if (method.isAnnotationPresent(Secured.class) && authConfigs.isAuthEnabled()) {
            if (Loggers.AUTH.isDebugEnabled()) {
                Loggers.AUTH.debug("auth start, request: {}", request.getClass().getSimpleName());
            }
            Secured secured = method.getAnnotation(Secured.class);
            if (!protocolAuthService.enableAuth(secured)) {
                return null;
            }
            String clientIp = meta.getClientIp();
            request.putHeader(Constants.Identity.X_REAL_IP, clientIp);
            Resource resource = protocolAuthService.parseResource(request, secured);
            IdentityContext identityContext = protocolAuthService.parseIdentity(request);
            boolean result = protocolAuthService.validateIdentity(identityContext, resource);
            if (!result) {
                // TODO Get reason of failure
                throw new AccessException("Validate Identity failed.");
            }
            String action = secured.action().toString();
            result = protocolAuthService.validateAuthority(identityContext, new Permission(resource, action));
            if (!result) {
                // TODO Get reason of failure
                throw new AccessException("Validate Authority failed.");
            }
        }
    } catch (AccessException e) {
        if (Loggers.AUTH.isDebugEnabled()) {
            Loggers.AUTH.debug("access denied, request: {}, reason: {}", request.getClass().getSimpleName(), e.getErrMsg());
        }
        Response defaultResponseInstance = getDefaultResponseInstance(handlerClazz);
        defaultResponseInstance.setErrorInfo(NacosException.NO_RIGHT, e.getErrMsg());
        return defaultResponseInstance;
    } catch (Exception e) {
        Response defaultResponseInstance = getDefaultResponseInstance(handlerClazz);
        defaultResponseInstance.setErrorInfo(NacosException.SERVER_ERROR, ExceptionUtil.getAllExceptionMsg(e));
        return defaultResponseInstance;
    }
    return null;
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) AccessException(com.alibaba.nacos.plugin.auth.exception.AccessException) Secured(com.alibaba.nacos.auth.annotation.Secured) Resource(com.alibaba.nacos.plugin.auth.api.Resource) Permission(com.alibaba.nacos.plugin.auth.api.Permission) IdentityContext(com.alibaba.nacos.plugin.auth.api.IdentityContext) Method(java.lang.reflect.Method) AccessException(com.alibaba.nacos.plugin.auth.exception.AccessException) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

Response (com.alibaba.nacos.api.remote.response.Response)31 NacosException (com.alibaba.nacos.api.exception.NacosException)23 Payload (com.alibaba.nacos.api.grpc.auto.Payload)9 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)9 Member (com.alibaba.nacos.core.cluster.Member)8 Test (org.junit.Test)7 Request (com.alibaba.nacos.api.remote.request.Request)6 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)6 RequestCallBack (com.alibaba.nacos.api.remote.RequestCallBack)5 ConnectionSetupRequest (com.alibaba.nacos.api.remote.request.ConnectionSetupRequest)4 ServerCheckResponse (com.alibaba.nacos.api.remote.response.ServerCheckResponse)4 DistroDataRequest (com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest)4 DistroDataResponse (com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse)4 ConnectResetRequest (com.alibaba.nacos.api.remote.request.ConnectResetRequest)3 HealthCheckRequest (com.alibaba.nacos.api.remote.request.HealthCheckRequest)3 ServerCheckRequest (com.alibaba.nacos.api.remote.request.ServerCheckRequest)3 ConnectResetResponse (com.alibaba.nacos.api.remote.response.ConnectResetResponse)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)2