io.grpc.ForwardingServerCall Java Examples

The following examples show how to use io.grpc.ForwardingServerCall. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SafeMethodCachingInterceptorTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
    ServerCall<ReqT, RespT> call,
    final Metadata requestHeaders,
    ServerCallHandler<ReqT, RespT> next) {
  return next.startCall(
      new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
        @Override
        public void sendHeaders(Metadata headers) {
          for (String cacheControlDirective : cacheControlDirectives) {
            headers.put(CACHE_CONTROL_METADATA_KEY, cacheControlDirective);
          }
          super.sendHeaders(headers);
        }
      },
      requestHeaders);
}
 
Example #2
Source File: RPCTracingHelpers.java    From pravega with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, final Metadata headers,
                                                             ServerCallHandler<ReqT, RespT> next) {
    // Check if this RPC has tags to track request (e.g., older clients).
    if (headers != null && headers.containsKey(DESCRIPTOR_HEADER) && headers.containsKey(ID_HEADER)) {
        RequestTag requestTag = new RequestTag(headers.get(DESCRIPTOR_HEADER), Long.parseLong(headers.get(ID_HEADER)));
        requestTracker.trackRequest(requestTag);
        log.debug(requestTag.getRequestId(), "Received tag from RPC request {}.",
                requestTag.getRequestDescriptor());
    } else {
        log.debug("No tags provided for call {} in headers: {}.", call.getMethodDescriptor().getFullMethodName(),
                headers);
    }

    return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
        @Override
        public void sendHeaders(Metadata responseHeaders) {
            super.sendHeaders(responseHeaders);
        }
    }, headers);
}
 
Example #3
Source File: SafeMethodCachingInterceptorTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
    ServerCall<ReqT, RespT> call,
    final Metadata requestHeaders,
    ServerCallHandler<ReqT, RespT> next) {
  return next.startCall(
      new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
        @Override
        public void sendHeaders(Metadata headers) {
          for (String cacheControlDirective : cacheControlDirectives) {
            headers.put(CACHE_CONTROL_METADATA_KEY, cacheControlDirective);
          }
          super.sendHeaders(headers);
        }
      },
      requestHeaders);
}
 
Example #4
Source File: UnknownStatusDescriptionInterceptor.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
    ServerCall<ReqT, RespT> wrappedCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
        @Override
        public void sendMessage(RespT message) {
            super.sendMessage(message);
        }

        @Override
        public void close(Status status, Metadata trailers) {
            Throwable exception;
            Status newStatus;
            if (
                status.getCode() == Status.Code.UNKNOWN
                && status.getDescription() == null
                && (exception = status.getCause()) != null
                && (newStatus = statusForException(exception)) != null
            ) {
                status = newStatus
                    .withCause(exception)
                    .withDescription(stacktraceToString(exception));
            }
            super.close(status, trailers);
        }
    };

    return next.startCall(wrappedCall, headers);
}
 
Example #5
Source File: SentinelGrpcServerInterceptor.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata,
                                                  ServerCallHandler<ReqT, RespT> serverCallHandler) {
    String resourceName = serverCall.getMethodDescriptor().getFullMethodName();
    // Remote address: serverCall.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
    Entry entry = null;
    try {
        ContextUtil.enter(resourceName);
        entry = SphU.entry(resourceName, EntryType.IN);
        // Allow access, forward the call.
        return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(
            serverCallHandler.startCall(
                new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(serverCall) {
                    @Override
                    public void close(Status status, Metadata trailers) {
                        super.close(status, trailers);
                        // Record the exception metrics.
                        if (!status.isOk()) {
                            recordException(status.asRuntimeException());
                        }
                    }
                }, metadata)) {};
    } catch (BlockException e) {
        serverCall.close(FLOW_CONTROL_BLOCK, new Metadata());
        return new ServerCall.Listener<ReqT>() {};
    } finally {
        if (entry != null) {
            entry.exit();
        }
        ContextUtil.exit();
    }
}
 
Example #6
Source File: HeaderServerInterceptor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public <I, O> ServerCall.Listener<I> interceptCall(
        ServerCall<I, O> call,
        final Metadata requestHeaders,
        ServerCallHandler<I, O> next) {
    logger.info("header received from client:" + requestHeaders);
    return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<I, O>(call) {
        @Override
        public void sendHeaders(Metadata responseHeaders) {
            responseHeaders.put(CUSTOM_HEADER_KEY, "customRespondValue");
            super.sendHeaders(responseHeaders);
        }
    }, requestHeaders);
}
 
Example #7
Source File: MyFirstInterceptor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall,
        Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
    return serverCallHandler
            .startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(serverCall) {
                @Override
                public void close(Status status, Metadata trailers) {
                    callTime = System.nanoTime();
                    super.close(status, trailers);
                }
            }, metadata);
}
 
Example #8
Source File: MySecondInterceptor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall,
        Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
    return serverCallHandler
            .startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(serverCall) {
                @Override
                public void close(Status status, Metadata trailers) {
                    callTime = System.nanoTime();
                    super.close(status, trailers);
                }
            }, metadata);
}
 
Example #9
Source File: SimpleLoggingInterceptor.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
    final ServerCall<ReqT, RespT> call,
    final Metadata headers,
    final ServerCallHandler<ReqT, RespT> next
) {

    return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {

        /**
         * {@inheritDoc}
         */
        @Override
        public void request(final int numMessages) {
            log.debug("gRPC call: {}", call.getMethodDescriptor().getFullMethodName());
            super.request(numMessages);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void close(final Status status, final Metadata trailers) {
            if (!status.isOk()) {
                log.warn(
                    "gRPC error: {} -> {}: {}",
                    call.getMethodDescriptor().getFullMethodName(),
                    status.getCode().value(),
                    (status.getCause() != null ? status.getCause().getMessage() : NO_CAUSE),
                    status.getCause()
                );
            }
            super.close(status, trailers);
        }
    }, headers);
}
 
Example #10
Source File: SecondServerInterceptor.java    From java-grpc with Apache License 2.0 4 votes vote down vote up
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(
    ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {

  assertNotNull(tracer.activeSpan());

  call =
      new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {

        @Override
        public void sendHeaders(Metadata headers) {
          assertNotNull(tracer.activeSpan());
          super.sendHeaders(headers);
        }

        @Override
        public void sendMessage(RespT message) {
          assertNotNull(tracer.activeSpan());
          super.sendMessage(message);
        }

        @Override
        public void close(Status status, Metadata trailers) {
          assertNotNull(tracer.activeSpan());
          super.close(status, trailers);
        }
      };

  return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(
      next.startCall(call, headers)) {

    @Override
    public void onReady() {
      assertNotNull(tracer.activeSpan());
      super.onReady();
    }

    @Override
    public void onMessage(ReqT message) {
      assertNotNull(tracer.activeSpan());
      super.onMessage(message);
    }

    @Override
    public void onHalfClose() {
      assertNotNull(tracer.activeSpan());
      super.onHalfClose();
    }

    @Override
    public void onCancel() {
      assertNotNull(tracer.activeSpan());
      super.onCancel();
    }

    @Override
    public void onComplete() {
      assertNotNull(tracer.activeSpan());
      super.onComplete();
    }
  };
}