private void invokeOnComplete() { try (SafeCloseable ignored = ctx.push()) { listener.onComplete(); } catch (Throwable t) { // This should not be possible with normal generated stubs which do not implement // onComplete, but is conceivable for a completely manually constructed stub. logger.warn("Error in gRPC onComplete handler.", t); } }
/** * Invokes the service method with arguments. */ private Object invoke(ServiceRequestContext ctx, HttpRequest req, @Nullable AggregatedHttpMessage message) { try (SafeCloseable ignored = ctx.push(false)) { final ResolverContext resolverContext = new ResolverContext(ctx, req, message); final Object[] arguments = toArguments(resolvers, resolverContext); return method.invoke(object, arguments); } catch (Throwable cause) { return exceptionHandler.handleException(ctx, req, cause); } }
@Override public HttpResponse convertResponse(ServiceRequestContext ctx, HttpHeaders headers, @Nullable Object result, HttpHeaders trailingHeaders) throws Exception { try (SafeCloseable ignored = ctx.push(false)) { for (final ResponseConverterFunction func : functions) { try { return func.convertResponse(ctx, headers, result, trailingHeaders); } catch (FallthroughException ignore) { // Do nothing. } catch (Exception e) { throw new IllegalStateException( "Response converter " + func.getClass().getName() + " cannot convert a result to HttpResponse: " + result, e); } } } // There is no response converter which is able to convert 'null' result to a response. // In this case, a response with the specified HTTP headers would be sent. // If you want to force to send '204 No Content' for this case, add // 'NullToNoContentResponseConverterFunction' to the list of response converters. if (result == null) { return HttpResponse.of(headers, HttpData.EMPTY_DATA, trailingHeaders); } throw new IllegalStateException( "No response converter exists for a result: " + result.getClass().getName()); } }
private void invokeOnCancel() { try (SafeCloseable ignored = ctx.push()) { listener.onCancel(); } catch (Throwable t) { if (!closeCalled) { // A custom error when dealing with client cancel or transport issues should be // returned. We have already closed the listener, so it will not receive any more // callbacks as designed. close(Status.fromThrowable(t), EMPTY_METADATA); } } }
private void invokeOnMessage(I request) { try (SafeCloseable ignored = ctx.push()) { listener.onMessage(request); } catch (Throwable t) { close(Status.fromThrowable(t), EMPTY_METADATA); } }
private void invokeHalfClose() { try (SafeCloseable ignored = ctx.push()) { listener.onHalfClose(); } catch (Throwable t) { close(Status.fromThrowable(t), EMPTY_METADATA); } }
advertisedEncodingsHeader); final ServerCall.Listener<I> listener; try (SafeCloseable ignored = ctx.push()) { listener = methodDef.getServerCallHandler().startCall(call, EMPTY_METADATA); } catch (Throwable t) {
@Override public O serve(ServiceRequestContext ctx, I req) throws Exception { final PathMappingContext mappingCtx = ctx.pathMappingContext(); final PathMapped<Service<I, O>> mapped = findService(mappingCtx.overridePath(ctx.mappedPath())); if (!mapped.isPresent()) { throw HttpStatusException.of(HttpStatus.NOT_FOUND); } final Optional<String> childPrefix = mapped.mapping().prefix(); if (childPrefix.isPresent()) { final PathMapping newMapping = PathMapping.ofPrefix(ctx.pathMapping().prefix().get() + childPrefix.get().substring(1)); final ServiceRequestContext newCtx = new CompositeServiceRequestContext( ctx, newMapping, mapped.mappingResult().path()); try (SafeCloseable ignored = newCtx.push(false)) { return mapped.value().serve(newCtx, req); } } else { return mapped.value().serve(ctx, req); } }
@Test public void testMdcPropertyPreservation() throws Exception { final List<ILoggingEvent> events = prepare(a -> a.addBuiltIn(BuiltInProperty.REQ_DIRECTION)); MDC.put("some-prop", "some-value"); final ServiceRequestContext ctx = newServiceContext("/foo", null); try (SafeCloseable ignored = ctx.push()) { final ILoggingEvent e = log(events); final Map<String, String> mdc = e.getMDCPropertyMap(); assertThat(mdc).containsEntry("req.direction", "INBOUND") .containsEntry("some-prop", "some-value") .hasSize(2); } finally { MDC.remove("some-prop"); } }
private void invoke( ServiceRequestContext ctx, SerializationFormat serializationFormat, int seqId, ThriftFunction func, RpcRequest call, CompletableFuture<HttpResponse> res) { final RpcResponse reply; try (SafeCloseable ignored = ctx.push()) { reply = delegate.serve(ctx, call); } catch (Throwable cause) { handleException(ctx, new DefaultRpcResponse(cause), res, serializationFormat, seqId, func, cause); return; } reply.handle((result, cause) -> { if (func.isOneWay()) { handleOneWaySuccess(ctx, reply, res, serializationFormat); return null; } if (cause != null) { handleException(ctx, reply, res, serializationFormat, seqId, func, cause); return null; } try { handleSuccess(ctx, reply, res, serializationFormat, seqId, func, result); } catch (Throwable t) { handleException(ctx, new DefaultRpcResponse(t), res, serializationFormat, seqId, func, t); } return null; }).exceptionally(CompletionActions::log); }
try (SafeCloseable ignored = ctx.push()) { final ILoggingEvent e = log(events); final Map<String, String> mdc = e.getMDCPropertyMap();
try (SafeCloseable ignored = ctx.push()) { final RequestLogBuilder log = ctx.logBuilder(); log.endRequest();
try (SafeCloseable ignored = ctx.push()) { final RequestLogBuilder log = ctx.logBuilder(); log.serializationFormat(ThriftSerializationFormats.BINARY);
private void invokeOnComplete() { try (SafeCloseable ignored = ctx.push()) { listener.onComplete(); } catch (Throwable t) { // This should not be possible with normal generated stubs which do not implement // onComplete, but is conceivable for a completely manually constructed stub. logger.warn("Error in gRPC onComplete handler.", t); } }
private void invokeHalfClose() { try (SafeCloseable ignored = ctx.push()) { listener.onHalfClose(); } catch (Throwable t) { close(Status.fromThrowable(t), EMPTY_METADATA); } }
private void invokeOnCancel() { try (SafeCloseable ignored = ctx.push()) { listener.onCancel(); } catch (Throwable t) { if (!closeCalled) { // A custom error when dealing with client cancel or transport issues should be // returned. We have already closed the listener, so it will not receive any more // callbacks as designed. close(Status.fromThrowable(t), EMPTY_METADATA); } } }
private void invokeOnMessage(I request) { try (SafeCloseable ignored = ctx.push()) { listener.onMessage(request); } catch (Throwable t) { close(Status.fromThrowable(t), EMPTY_METADATA); } }
private void invoke( ServiceRequestContext ctx, SerializationFormat serializationFormat, int seqId, ThriftFunction func, RpcRequest call, CompletableFuture<HttpResponse> res) { final RpcResponse reply; try (SafeCloseable ignored = ctx.push()) { reply = delegate.serve(ctx, call); } catch (Throwable cause) { handleException(ctx, new DefaultRpcResponse(cause), res, serializationFormat, seqId, func, cause); return; } reply.handle((result, cause) -> { if (func.isOneWay()) { handleOneWaySuccess(ctx, reply, res, serializationFormat); return null; } if (cause != null) { handleException(ctx, reply, res, serializationFormat, seqId, func, cause); return null; } try { handleSuccess(ctx, reply, res, serializationFormat, seqId, func, result); } catch (Throwable t) { handleException(ctx, new DefaultRpcResponse(t), res, serializationFormat, seqId, func, t); } return null; }).exceptionally(CompletionActions::log); }
private void invoke( ServiceRequestContext ctx, SerializationFormat serializationFormat, int seqId, ThriftFunction func, RpcRequest call, CompletableFuture<HttpResponse> res) { final RpcResponse reply; try (SafeCloseable ignored = ctx.push()) { reply = delegate.serve(ctx, call); } catch (Throwable cause) { handleException(ctx, new DefaultRpcResponse(cause), res, serializationFormat, seqId, func, cause); return; } reply.handle((result, cause) -> { if (func.isOneWay()) { handleOneWaySuccess(ctx, reply, res, serializationFormat); return null; } if (cause != null) { handleException(ctx, reply, res, serializationFormat, seqId, func, cause); return null; } try { handleSuccess(ctx, reply, res, serializationFormat, seqId, func, result); } catch (Throwable t) { handleException(ctx, new DefaultRpcResponse(t), res, serializationFormat, seqId, func, t); } return null; }).exceptionally(CompletionActions::log); }
private void invoke( ServiceRequestContext ctx, SerializationFormat serializationFormat, int seqId, ThriftFunction func, RpcRequest call, CompletableFuture<HttpResponse> res) { final RpcResponse reply; try (SafeCloseable ignored = ctx.push()) { reply = delegate.serve(ctx, call); } catch (Throwable cause) { handleException(ctx, new DefaultRpcResponse(cause), res, serializationFormat, seqId, func, cause); return; } reply.handle(voidFunction((result, cause) -> { if (func.isOneWay()) { handleOneWaySuccess(ctx, reply, res, serializationFormat); return; } if (cause != null) { handleException(ctx, reply, res, serializationFormat, seqId, func, cause); return; } try { handleSuccess(ctx, reply, res, serializationFormat, seqId, func, result); } catch (Throwable t) { handleException(ctx, new DefaultRpcResponse(t), res, serializationFormat, seqId, func, t); } })).exceptionally(CompletionActions::log); }