@Override public void onCompleted() { if (cancelled) { throw Status.CANCELLED.asRuntimeException(); } else { call.close(Status.OK, new Metadata()); } }
@Override public void onError(Throwable t) { Metadata metadata = Status.trailersFromThrowable(t); if (metadata == null) { metadata = new Metadata(); } if (t instanceof MotanBizException) { call.close(Status.INTERNAL.withDescription(t.getMessage()).withCause(t), metadata); } else { call.close(Status.UNAVAILABLE.withDescription(t.getMessage()).withCause(t), metadata); } }
private <ReqT, RespT> boolean authenticateCall(ServerCall<ReqT, RespT> call, Metadata headers) { // Try to fetch channel Id from the metadata. UUID channelId = headers.get(ChannelIdInjector.S_CLIENT_ID_KEY); boolean callAuthenticated = false; if (channelId != null) { try { // Fetch authenticated username for this channel and set it. String userName = mAuthenticationServer.getUserNameForChannel(channelId); if (userName != null) { AuthenticatedClientUser.set(userName); } else { AuthenticatedClientUser.remove(); } callAuthenticated = true; } catch (UnauthenticatedException e) { LOG.debug("Channel:{} is not authenticated for call:{}", channelId.toString(), call.getMethodDescriptor().getFullMethodName()); call.close(Status.UNAUTHENTICATED, headers); } } else { LOG.debug("Channel Id is missing for call:{}.", call.getMethodDescriptor().getFullMethodName()); call.close(Status.UNAUTHENTICATED, headers); } return callAuthenticated; } }
call.close(statusSupplier.get(), trailerSupplier.get()); return new ServerCall.Listener<ReqT>() {}; });
serverCall.close(FLOW_CONTROL_BLOCK, new Metadata()); return new ServerCall.Listener<ReqT>() {}; } finally {
/** * Close the call with {@link Status#PERMISSION_DENIED}. * * @param call The call to close. * @param aex The exception that was the cause. */ protected void closeCallAccessDenied(final ServerCall<?, ?> call, final AccessDeniedException aex) { call.close(Status.PERMISSION_DENIED.withCause(aex).withDescription(ACCESS_DENIED_DESCRIPTION), new Metadata()); }
/** * Close the call with {@link Status#UNAUTHENTICATED}. * * @param call The call to close. * @param aex The exception that was the cause. */ protected void closeCallUnauthenticated(final ServerCall<?, ?> call, final AuthenticationException aex) { call.close(Status.UNAUTHENTICATED.withCause(aex).withDescription(UNAUTHENTICATED_DESCRIPTION), new Metadata()); }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { System.out.println("执行server拦截器1,获取token"); //获取客户端参数 Metadata.Key<String> token = Metadata.Key.of("token", Metadata.ASCII_STRING_MARSHALLER); String tokenStr = headers.get(token); if (StringUtil.isNullOrEmpty(tokenStr)){ System.out.println("未收到客户端token,关闭此连接"); call.close(Status.DATA_LOSS,headers); } //服务端写回参数 ServerCall<ReqT, RespT> serverCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) { @Override public void sendHeaders(Metadata headers) { System.out.println("执行server拦截器2,写入token"); headers.put(token,tokenStr); super.sendHeaders(headers); } }; return next.startCall(serverCall,headers); } }
@Override public void close(Status status, Metadata trailers) { delegate().close(status, trailers); }
@Override public <ReqT, RespT> Listener<ReqT> interceptCall( ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> nextHandler) { serverCall.close(status, new Metadata()); return new Listener<ReqT>() {}; } }
@Override public void close(Status status, Metadata trailers) { delegate().close(status, trailers); }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { log.info("blocked ips:{}", Arrays.toString(blackIps)); if (isBlocked(getRemoteHost(getRemoteInetSocketString(call)))) { call.close(Status.ABORTED, headers); } return next.startCall(call, headers); }
@Override public void onError(Throwable t) { Metadata metadata = Status.trailersFromThrowable(t); if (metadata == null) { metadata = new Metadata(); } call.close(Status.fromThrowable(t), metadata); }
/** * Close the call with {@link Status#PERMISSION_DENIED}. * * @param call The call to close. * @param aex The exception that was the cause. */ protected void closeCallAccessDenied(final ServerCall<?, ?> call, final AccessDeniedException aex) { call.close(Status.PERMISSION_DENIED.withCause(aex).withDescription(ACCESS_DENIED_DESCRIPTION), new Metadata()); }
@Override public void onCompleted() { if (cancelled) { throw Status.CANCELLED.withDescription("call already cancelled").asRuntimeException(); } else { call.close(Status.OK, new Metadata()); } }
/** * Close the call with {@link Status#UNAUTHENTICATED}. * * @param call The call to close. * @param aex The exception that was the cause. */ protected void closeCallUnauthenticated(final ServerCall<?, ?> call, final AuthenticationException aex) { call.close(Status.UNAUTHENTICATED.withCause(aex).withDescription(UNAUTHENTICATED_DESCRIPTION), new Metadata()); }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { // Throw if ambient context is missing if (!AmbientContext.isPresent()) { call.close(missingContextStatus(), new Metadata()); return new ServerCall.Listener<ReqT>() { }; } // Throw if required context keys are missing for (String requiredKey : requiredContextKeys) { if (!AmbientContext.current().keys().contains(requiredKey)) { call.close(incompleteContextStatus(requiredKey), new Metadata()); return new ServerCall.Listener<ReqT>() { }; } } return next.startCall(call, headers); }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next ) { if (!headers.containsKey(tokenKey)) { call.close(Status.PERMISSION_DENIED.withDescription("no authorization token"), new Metadata()); return new ServerCall.Listener<ReqT>(){}; } final String authToken = headers.get(tokenKey); LOGGER.debug("Got authentication token (" + authToken + ")"); return next.startCall(call, headers); } }
@Override public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { if (headers.containsKey(TenantTokenClientInterceptor.TENANT_ID_KEY)) { String tenantId = headers.get(TenantTokenClientInterceptor.TENANT_ID_KEY); LOGGER.trace("Server received tenant id key: " + tenantId); Context ctx = Context.current().withValue(GrpcContextKeys.TENANT_ID_KEY, tenantId); return Contexts.interceptCall(ctx, call, headers, next); } else { call.close(Status.UNAUTHENTICATED.withDescription("Tenant token not passed in metadata."), headers); return new ServerCall.Listener<ReqT>() { }; } }
@Override public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { if (headers.containsKey(JwtClientInterceptor.JWT_KEY)) { String jwt = headers.get(JwtClientInterceptor.JWT_KEY); LOGGER.trace("Server received jwt key: " + jwt); Context ctx = Context.current().withValue(GrpcContextKeys.JWT_KEY, jwt); return Contexts.interceptCall(ctx, call, headers, next); } else { call.close(Status.UNAUTHENTICATED.withDescription("JWT not passed in metadata."), headers); return new ServerCall.Listener<ReqT>() { }; } }