本文整理汇总了Java中io.grpc.ServerCall.Listener方法的典型用法代码示例。如果您正苦于以下问题:Java ServerCall.Listener方法的具体用法?Java ServerCall.Listener怎么用?Java ServerCall.Listener使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.grpc.ServerCall
的用法示例。
在下文中一共展示了ServerCall.Listener方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: echoRequestHeadersInterceptor
import io.grpc.ServerCall; //导入方法依赖的package包/类
/**
* Echo the request headers from a client into response headers and trailers. Useful for
* testing end-to-end metadata propagation.
*/
private static ServerInterceptor echoRequestHeadersInterceptor(final Metadata.Key<?>... keys) {
final Set<Metadata.Key<?>> keySet = new HashSet<Metadata.Key<?>>(Arrays.asList(keys));
return new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
final Metadata requestHeaders,
ServerCallHandler<ReqT, RespT> next) {
return next.startCall(new SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendHeaders(Metadata responseHeaders) {
responseHeaders.merge(requestHeaders, keySet);
super.sendHeaders(responseHeaders);
}
@Override
public void close(Status status, Metadata trailers) {
trailers.merge(requestHeaders, keySet);
super.close(status, trailers);
}
}, requestHeaders);
}
};
}
示例2: interceptCall
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
if (Objects.isNull(SecurityContextHolder.getContext().getAuthentication())) {
SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken(key,
"anonymousUser", Collections.singletonList(new SimpleGrantedAuthority("ROLE_ANONYMOUS"))));
log.debug("Populated SecurityContextHolder with anonymous token: {}",
SecurityContextHolder.getContext().getAuthentication());
} else {
log.debug("SecurityContextHolder not populated with anonymous token, as it already contained: {}",
SecurityContextHolder.getContext().getAuthentication());
}
return next.startCall(call, headers);
}
示例3: cannotDisableAutoFlowControlAfterServiceInvocation
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Test
public void cannotDisableAutoFlowControlAfterServiceInvocation() throws Exception {
final AtomicReference<ServerCallStreamObserver<Integer>> callObserver =
new AtomicReference<ServerCallStreamObserver<Integer>>();
ServerCallHandler<Integer, Integer> callHandler =
ServerCalls.asyncBidiStreamingCall(
new ServerCalls.BidiStreamingMethod<Integer, Integer>() {
@Override
public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
callObserver.set((ServerCallStreamObserver<Integer>) responseObserver);
return new ServerCalls.NoopStreamObserver<Integer>();
}
});
ServerCall.Listener<Integer> callListener =
callHandler.startCall(serverCall, new Metadata());
callListener.onMessage(1);
try {
callObserver.get().disableAutoInboundFlowControl();
fail("Cannot set onCancel handler after service invocation");
} catch (IllegalStateException expected) {
// Expected
}
}
示例4: clientSendsOne_errorMissingRequest_unary
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Test
public void clientSendsOne_errorMissingRequest_unary() {
ServerCallRecorder serverCall = new ServerCallRecorder(UNARY_METHOD);
ServerCallHandler<Integer, Integer> callHandler =
ServerCalls.asyncUnaryCall(
new ServerCalls.UnaryMethod<Integer, Integer>() {
@Override
public void invoke(Integer req, StreamObserver<Integer> responseObserver) {
fail("should not be reached");
}
});
ServerCall.Listener<Integer> listener = callHandler.startCall(serverCall, new Metadata());
listener.onHalfClose();
assertThat(serverCall.responses).isEmpty();
assertEquals(Status.Code.INTERNAL, serverCall.status.getCode());
assertEquals(ServerCalls.MISSING_REQUEST, serverCall.status.getDescription());
}
示例5: clientSendsOne_errorMissingRequest_serverStreaming
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Test
public void clientSendsOne_errorMissingRequest_serverStreaming() {
ServerCallRecorder serverCall = new ServerCallRecorder(SERVER_STREAMING_METHOD);
ServerCallHandler<Integer, Integer> callHandler =
ServerCalls.asyncServerStreamingCall(
new ServerCalls.ServerStreamingMethod<Integer, Integer>() {
@Override
public void invoke(Integer req, StreamObserver<Integer> responseObserver) {
fail("should not be reached");
}
});
ServerCall.Listener<Integer> listener = callHandler.startCall(serverCall, new Metadata());
listener.onHalfClose();
assertThat(serverCall.responses).isEmpty();
assertEquals(Status.Code.INTERNAL, serverCall.status.getCode());
assertEquals(ServerCalls.MISSING_REQUEST, serverCall.status.getDescription());
}
示例6: clientSendsOne_errorTooManyRequests_serverStreaming
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Test
public void clientSendsOne_errorTooManyRequests_serverStreaming() {
ServerCallRecorder serverCall = new ServerCallRecorder(SERVER_STREAMING_METHOD);
ServerCallHandler<Integer, Integer> callHandler =
ServerCalls.asyncServerStreamingCall(
new ServerCalls.ServerStreamingMethod<Integer, Integer>() {
@Override
public void invoke(Integer req, StreamObserver<Integer> responseObserver) {
fail("should not be reached");
}
});
ServerCall.Listener<Integer> listener = callHandler.startCall(serverCall, new Metadata());
listener.onMessage(1);
listener.onMessage(1);
assertThat(serverCall.responses).isEmpty();
assertEquals(Status.Code.INTERNAL, serverCall.status.getCode());
assertEquals(ServerCalls.TOO_MANY_REQUESTS, serverCall.status.getDescription());
// ensure onHalfClose does not invoke
listener.onHalfClose();
}
示例7: startCall
import io.grpc.ServerCall; //导入方法依赖的package包/类
/** Never returns {@code null}. */
private <ReqT, RespT> ServerStreamListener startCall(ServerStream stream, String fullMethodName,
ServerMethodDefinition<ReqT, RespT> methodDef, Metadata headers,
Context.CancellableContext context, StatsTraceContext statsTraceCtx) {
// TODO(ejona86): should we update fullMethodName to have the canonical path of the method?
ServerCallImpl<ReqT, RespT> call = new ServerCallImpl<ReqT, RespT>(
stream, methodDef.getMethodDescriptor(), headers, context,
decompressorRegistry, compressorRegistry);
ServerCallHandler<ReqT, RespT> callHandler = methodDef.getServerCallHandler();
statsTraceCtx.serverCallStarted(
new ServerCallInfoImpl<ReqT, RespT>(
methodDef.getMethodDescriptor(),
call.getAttributes(),
call.getAuthority()));
for (ServerInterceptor interceptor : interceptors) {
callHandler = InternalServerInterceptors.interceptCallHandler(interceptor, callHandler);
}
ServerCall.Listener<ReqT> listener = callHandler.startCall(call, headers);
if (listener == null) {
throw new NullPointerException(
"startCall() returned a null listener for method " + fullMethodName);
}
return call.newServerStreamListener(listener);
}
示例8: clientSendsOne_errorTooManyRequests_unary
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Test
public void clientSendsOne_errorTooManyRequests_unary() {
ServerCallRecorder serverCall = new ServerCallRecorder(UNARY_METHOD);
ServerCallHandler<Integer, Integer> callHandler =
ServerCalls.asyncUnaryCall(
new ServerCalls.UnaryMethod<Integer, Integer>() {
@Override
public void invoke(Integer req, StreamObserver<Integer> responseObserver) {
fail("should not be reached");
}
});
ServerCall.Listener<Integer> listener = callHandler.startCall(serverCall, new Metadata());
listener.onMessage(1);
listener.onMessage(1);
assertThat(serverCall.responses).isEmpty();
assertEquals(Status.Code.INTERNAL, serverCall.status.getCode());
assertEquals(ServerCalls.TOO_MANY_REQUESTS, serverCall.status.getDescription());
// ensure onHalfClose does not invoke
listener.onHalfClose();
}
示例9: interceptCall
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
final Metadata requestHeaders,
ServerCallHandler<ReqT, RespT> next) {
String token = requestHeaders.get(authKey);
String principal = null;
if (StringUtils.isEmpty(token))
{
logger.warn("Failed to find token");
}
else
{
Map<String,String> tokenParams = new HashMap<>();
tokenParams.put(OAuth2AccessToken.ACCESS_TOKEN,token);
OAuth2AccessToken otoken = DefaultOAuth2AccessToken.valueOf(tokenParams);
OAuth2Authentication auth = server.getTokenStore().readAuthentication(otoken);
if (auth != null && auth.isAuthenticated())
{
logger.debug("Principal:"+auth.getPrincipal());
principal = auth.getPrincipal().toString();
}
else
{
logger.warn("Failed to authenticate token "+token);
}
}
return new MessagePrincipalListener<ReqT>(next.startCall(call, requestHeaders),principal,server);
}
示例10: interceptCall
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
ServerCall<RespT> call,
final Metadata requestHeaders,
ServerCallHandler<ReqT, RespT> next) {
logger.info("header received from client:" + requestHeaders);
return next.startCall(method, new SimpleForwardingServerCall<RespT>(call) {
@Override
public void sendHeaders(Metadata responseHeaders) {
responseHeaders.put(customHeadKey, "customRespondValue");
super.sendHeaders(responseHeaders);
}
}, requestHeaders);
}
示例11: interceptCall
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
System.out.println(call.getMethodDescriptor().getFullMethodName());
log.info(call.getMethodDescriptor().getFullMethodName());
return next.startCall(call, headers);
}
示例12: interceptCall
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
final Metadata requestHeaders, ServerCallHandler<ReqT, RespT> next) {
Context context = Context.current().withValue(MINI_DEVICE_INFO, requestHeaders.get(miniDeviceInfoKey));
return Contexts.interceptCall(context, call, requestHeaders, next);
}
示例13: interceptCall
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Override
public <R, S> ServerCall.Listener<R> interceptCall(
ServerCall<R, S> call,
Metadata requestHeaders,
ServerCallHandler<R, S> next) {
MethodDescriptor<R, S> method = call.getMethodDescriptor();
ServerMetrics metrics = serverMetricsFactory.createMetricsForMethod(method);
GrpcMethod grpcMethod = GrpcMethod.of(method);
ServerCall<R,S> monitoringCall = new MonitoringServerCall(call, clock, grpcMethod, metrics, configuration);
return new MonitoringServerCallListener<>(
next.startCall(monitoringCall, requestHeaders), metrics, GrpcMethod.of(method));
}
示例14: startCall
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Nullable
private <I, O> ArmeriaServerCall<I, O> startCall(
String fullMethodName,
ServerMethodDefinition<I, O> methodDef,
ServiceRequestContext ctx,
HttpHeaders headers,
HttpResponseWriter res,
SerializationFormat serializationFormat) {
ArmeriaServerCall<I, O> call = new ArmeriaServerCall<>(
headers,
methodDef.getMethodDescriptor(),
compressorRegistry,
decompressorRegistry,
res,
maxInboundMessageSizeBytes,
maxOutboundMessageSizeBytes,
ctx,
serializationFormat,
jsonMarshaller);
final ServerCall.Listener<I> listener;
try (SafeCloseable ignored = RequestContext.push(ctx)) {
listener = methodDef.getServerCallHandler().startCall(call, EMPTY_METADATA);
} catch (Throwable t) {
call.setListener(new EmptyListener<>());
call.close(Status.fromThrowable(t), EMPTY_METADATA);
logger.warn(
"Exception thrown from streaming request stub method before processing any request data" +
" - this is likely a bug in the stub implementation.");
return null;
}
if (listener == null) {
// This will never happen for normal generated stubs but could conceivably happen for manually
// constructed ones.
throw new NullPointerException(
"startCall() returned a null listener for method " + fullMethodName);
}
call.setListener(listener);
return call;
}
示例15: interceptCall
import io.grpc.ServerCall; //导入方法依赖的package包/类
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
RequestMetadata meta = headers.get(TracingMetadataUtils.METADATA_KEY);
assertThat(meta.getCorrelatedInvocationsId()).isEqualTo("build-req-id");
assertThat(meta.getToolInvocationId()).isEqualTo("command-id");
assertThat(meta.getActionId()).isNotEmpty();
assertThat(meta.getToolDetails().getToolName()).isEqualTo("bazel");
assertThat(meta.getToolDetails().getToolVersion())
.isEqualTo(BlazeVersionInfo.instance().getVersion());
return next.startCall(call, headers);
}