Java Code Examples for io.grpc.stub.MetadataUtils#attachHeaders()
The following examples show how to use
io.grpc.stub.MetadataUtils#attachHeaders() .
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: AbstractInteropTest.java From grpc-nebula-java with Apache License 2.0 | 6 votes |
@Test public void exchangeMetadataUnaryCall() throws Exception { TestServiceGrpc.TestServiceBlockingStub stub = blockingStub; // Capture the metadata exchange Metadata fixedHeaders = new Metadata(); // Send a context proto (as it's in the default extension registry) Messages.SimpleContext contextValue = Messages.SimpleContext.newBuilder().setValue("dog").build(); fixedHeaders.put(Util.METADATA_KEY, contextValue); stub = MetadataUtils.attachHeaders(stub, fixedHeaders); // .. and expect it to be echoed back in trailers AtomicReference<Metadata> trailersCapture = new AtomicReference<>(); AtomicReference<Metadata> headersCapture = new AtomicReference<>(); stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture); assertNotNull(stub.emptyCall(EMPTY)); // Assert that our side channel object is echoed back in both headers and trailers Assert.assertEquals(contextValue, headersCapture.get().get(Util.METADATA_KEY)); Assert.assertEquals(contextValue, trailersCapture.get().get(Util.METADATA_KEY)); }
Example 2
Source File: DgraphAsyncClient.java From dgraph4j with Apache License 2.0 | 6 votes |
/** * getStubWithJwt adds an AttachHeadersInterceptor to the stub, which will eventually attach a * header whose key is accessJwt and value is the access JWT stored in the current * DgraphAsyncClient object. * * @param stub the original stub that we should attach JWT to * @return the augmented stub with JWT */ protected DgraphGrpc.DgraphStub getStubWithJwt(DgraphGrpc.DgraphStub stub) { Lock readLock = jwtLock.readLock(); readLock.lock(); try { if (jwt != null && !jwt.getAccessJwt().isEmpty()) { Metadata metadata = new Metadata(); metadata.put( Metadata.Key.of("accessJwt", Metadata.ASCII_STRING_MARSHALLER), jwt.getAccessJwt()); return MetadataUtils.attachHeaders(stub, metadata); } return stub; } finally { readLock.unlock(); } }
Example 3
Source File: BrokerServerTest.java From gcp-token-broker with Apache License 2.0 | 5 votes |
public BrokerBlockingStub addSPNEGOTokenToMetadata(BrokerBlockingStub stub, String principal) { Metadata metadata = new Metadata(); Metadata.Key<String> key = Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER); metadata.put(key, "Negotiate " + principal); stub = MetadataUtils.attachHeaders(stub, metadata); return stub; }
Example 4
Source File: BrokerServerTest.java From gcp-token-broker with Apache License 2.0 | 5 votes |
public BrokerBlockingStub addSessionTokenToMetadata(BrokerBlockingStub stub, Session session) { Metadata metadata = new Metadata(); Metadata.Key<String> key = Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER); metadata.put(key, "BrokerSession " + SessionTokenUtils.marshallSessionToken(session)); stub = MetadataUtils.attachHeaders(stub, metadata); return stub; }
Example 5
Source File: GrpcHeaderHandler.java From buck with Apache License 2.0 | 5 votes |
/** Appends RemoteExecutionMetadata to the GRPC headers. */ public static <Stub extends AbstractStub<Stub>> Stub wrapStubToSendMetadata( Stub grpcStub, RemoteExecutionMetadata metadata) { Metadata extraHeaders = new Metadata(); extraHeaders.put(REMOTE_EXECUTION_METADATA_KEY, metadata.toByteArray()); return MetadataUtils.attachHeaders(grpcStub, extraHeaders); }
Example 6
Source File: BrokerGateway.java From gcp-token-broker with Apache License 2.0 | 5 votes |
public void setSessionToken(String sessionToken) { // Set the session token in the 'authorization' header Metadata metadata = new Metadata(); Metadata.Key<String> key = Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER); metadata.put(key, REQUEST_AUTH_HEADER + " " + sessionToken); stub = MetadataUtils.attachHeaders(stub, metadata); }
Example 7
Source File: GrpcClientTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void exchangeHeadersUnaryCall_grpcMetadata() throws Exception { final Metadata metadata = new Metadata(); metadata.put(TestServiceImpl.EXTRA_HEADER_KEY, "dog"); TestServiceBlockingStub stub = MetadataUtils.attachHeaders(blockingStub, metadata); final AtomicReference<Metadata> headers = new AtomicReference<>(); final AtomicReference<Metadata> trailers = new AtomicReference<>(); stub = MetadataUtils.captureMetadata(stub, headers, trailers); assertThat(stub.emptyCall(EMPTY)).isNotNull(); final HttpHeaders clientHeaders = CLIENT_HEADERS_CAPTURE.get(); assertThat(clientHeaders.get(HttpHeaderNames.TE)) .isEqualTo(HttpHeaderValues.TRAILERS.toString()); // Assert that our side channel object is echoed back in both headers and trailers assertThat(clientHeaders.get(TestServiceImpl.EXTRA_HEADER_NAME)).isEqualTo("dog"); assertThat(SERVER_TRAILERS_CAPTURE.get().get(TestServiceImpl.EXTRA_HEADER_NAME)).isEqualTo("dog"); assertThat(headers.get()).isNull(); assertThat(trailers.get().get(TestServiceImpl.EXTRA_HEADER_KEY)).isEqualTo("dog"); assertThat(trailers.get().getAll(TestServiceImpl.STRING_VALUE_KEY)).containsExactly( StringValue.newBuilder().setValue("hello").build(), StringValue.newBuilder().setValue("world").build()); checkRequestLog((rpcReq, rpcRes, grpcStatus) -> { assertThat(rpcReq.params()).containsExactly(EMPTY); assertThat(rpcRes.get()).isEqualTo(EMPTY); }); }
Example 8
Source File: Tiller.java From microbean-helm with Apache License 2.0 | 5 votes |
public HealthStub getHealthStub() { HealthStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(HealthGrpc.newStub(this.channel), metadata); } return returnValue; }
Example 9
Source File: App.java From dgraph4j with Apache License 2.0 | 5 votes |
private static DgraphClient createDgraphClient(boolean withAuthHeader) { ManagedChannel channel = ManagedChannelBuilder.forAddress(TEST_HOSTNAME, TEST_PORT).usePlaintext().build(); DgraphStub stub = DgraphGrpc.newStub(channel); if (withAuthHeader) { Metadata metadata = new Metadata(); metadata.put( Metadata.Key.of("auth-token", Metadata.ASCII_STRING_MARSHALLER), "the-auth-token-value"); stub = MetadataUtils.attachHeaders(stub, metadata); } return new DgraphClient(stub); }
Example 10
Source File: Tiller.java From microbean-helm with Apache License 2.0 | 5 votes |
public HealthBlockingStub getHealthBlockingStub() { HealthBlockingStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(HealthGrpc.newBlockingStub(this.channel), metadata); } return returnValue; }
Example 11
Source File: JwtAuthBlockingClient.java From product-microgateway with Apache License 2.0 | 5 votes |
public JwtAuthBlockingClient(Channel channel, String token) { JwtAuthTestServiceGrpc.JwtAuthTestServiceBlockingStub stub = JwtAuthTestServiceGrpc.newBlockingStub(channel); //add metadata Metadata metadata = new Metadata(); metadata.put(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), token); blockingStub = MetadataUtils.attachHeaders(stub,metadata); }
Example 12
Source File: ThrottlingBlockingClient.java From product-microgateway with Apache License 2.0 | 5 votes |
public ThrottlingBlockingClient(Channel channel, String token) { ThrottlingTestServiceGrpc.ThrottlingTestServiceBlockingStub stub = ThrottlingTestServiceGrpc.newBlockingStub(channel); //add metadata Metadata metadata = new Metadata(); metadata.put(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), token); blockingStub = MetadataUtils.attachHeaders(stub,metadata); }
Example 13
Source File: AbstractInteropTest.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@Test public void exchangeMetadataStreamingCall() throws Exception { TestServiceGrpc.TestServiceStub stub = asyncStub; // Capture the metadata exchange Metadata fixedHeaders = new Metadata(); // Send a context proto (as it's in the default extension registry) Messages.SimpleContext contextValue = Messages.SimpleContext.newBuilder().setValue("dog").build(); fixedHeaders.put(Util.METADATA_KEY, contextValue); stub = MetadataUtils.attachHeaders(stub, fixedHeaders); // .. and expect it to be echoed back in trailers AtomicReference<Metadata> trailersCapture = new AtomicReference<>(); AtomicReference<Metadata> headersCapture = new AtomicReference<>(); stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture); List<Integer> responseSizes = Arrays.asList(50, 100, 150, 200); Messages.StreamingOutputCallRequest.Builder streamingOutputBuilder = Messages.StreamingOutputCallRequest.newBuilder(); for (Integer size : responseSizes) { streamingOutputBuilder.addResponseParameters( ResponseParameters.newBuilder().setSize(size).setIntervalUs(0)); } final Messages.StreamingOutputCallRequest request = streamingOutputBuilder.build(); StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create(); StreamObserver<Messages.StreamingOutputCallRequest> requestStream = stub.fullDuplexCall(recorder); final int numRequests = 10; for (int ix = numRequests; ix > 0; --ix) { requestStream.onNext(request); } requestStream.onCompleted(); recorder.awaitCompletion(); assertSuccess(recorder); org.junit.Assert.assertEquals(responseSizes.size() * numRequests, recorder.getValues().size()); // Assert that our side channel object is echoed back in both headers and trailers Assert.assertEquals(contextValue, headersCapture.get().get(Util.METADATA_KEY)); Assert.assertEquals(contextValue, trailersCapture.get().get(Util.METADATA_KEY)); }
Example 14
Source File: AbstractInteropTest.java From grpc-java with Apache License 2.0 | 4 votes |
@Test public void exchangeMetadataStreamingCall() throws Exception { TestServiceGrpc.TestServiceStub stub = asyncStub; // Capture the metadata exchange Metadata fixedHeaders = new Metadata(); // Send a context proto (as it's in the default extension registry) Messages.SimpleContext contextValue = Messages.SimpleContext.newBuilder().setValue("dog").build(); fixedHeaders.put(Util.METADATA_KEY, contextValue); stub = MetadataUtils.attachHeaders(stub, fixedHeaders); // .. and expect it to be echoed back in trailers AtomicReference<Metadata> trailersCapture = new AtomicReference<>(); AtomicReference<Metadata> headersCapture = new AtomicReference<>(); stub = MetadataUtils.captureMetadata(stub, headersCapture, trailersCapture); List<Integer> responseSizes = Arrays.asList(50, 100, 150, 200); Messages.StreamingOutputCallRequest.Builder streamingOutputBuilder = Messages.StreamingOutputCallRequest.newBuilder(); for (Integer size : responseSizes) { streamingOutputBuilder.addResponseParameters( ResponseParameters.newBuilder().setSize(size).setIntervalUs(0)); } final Messages.StreamingOutputCallRequest request = streamingOutputBuilder.build(); StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create(); StreamObserver<Messages.StreamingOutputCallRequest> requestStream = stub.fullDuplexCall(recorder); final int numRequests = 10; for (int ix = numRequests; ix > 0; --ix) { requestStream.onNext(request); } requestStream.onCompleted(); recorder.awaitCompletion(); assertSuccess(recorder); org.junit.Assert.assertEquals(responseSizes.size() * numRequests, recorder.getValues().size()); // Assert that our side channel object is echoed back in both headers and trailers Assert.assertEquals(contextValue, headersCapture.get().get(Util.METADATA_KEY)); Assert.assertEquals(contextValue, trailersCapture.get().get(Util.METADATA_KEY)); }
Example 15
Source File: UploadJobService.java From itag with GNU General Public License v3.0 | 4 votes |
private boolean uploadQueue(@NonNull final String tid) { List<Location> pack = new ArrayList<>(); boolean completed = false; for (; ; ) { pack.clear(); int packSize; synchronized (uploadQueue) { packSize = Math.min(uploadQueue.size(), PACK_SIZE); for (int i = 0; i < packSize; i++) { Location head = uploadQueue.peekFirst(); if (head != null) { pack.add(head); } } } if (pack.size() > 0) { TrackerOuterClass.AddLocationsRequest.Builder req = TrackerOuterClass.AddLocationsRequest .newBuilder(); req.setTid(tid); for (Location location : pack) { req.addLocations(marshall(tid, location)); } if (ch == null) ch = grpcChannelProvider.channel(); TrackerGrpc.TrackerBlockingStub grpcStub = getGrpcStub(); try { Metadata headers = new Metadata(); headers.put(Keys.wsseKey, Wsse.getToken()); grpcStub = MetadataUtils.attachHeaders(grpcStub, headers); TrackerOuterClass.AddLocationResponse resp = grpcStub.addLocations(req.build()); if (resp.getOk()) { for (int i = 0; i < packSize; i++) { synchronized (uploadQueue) { uploadQueue.pollFirst(); } } } else { ErrorsObservable.notify(new Exception("upload_failed"), false); break; } } catch (Exception e) { ErrorsObservable.notify(e, true); break; } } synchronized (uploadQueue) { if (uploadQueue.size() == 0) { completed = true; break; } } } return completed; }
Example 16
Source File: InternalBot.java From java-bot-sdk with Apache License 2.0 | 4 votes |
public <T extends AbstractStub<T>, R> R withToken(Metadata meta, T stub, Function<T, R> f) { T newStub = MetadataUtils.attachHeaders(stub, meta); return f.apply(newStub); }
Example 17
Source File: InternalBot.java From java-bot-sdk with Apache License 2.0 | 4 votes |
public <T extends AbstractStub<T>, R> CompletableFuture<R> withToken(T stub, Function<T, ListenableFuture<R>> f) { T newStub = MetadataUtils.attachHeaders(stub, metadata); TaskManager<R> task = new TaskManager<>(FutureConverter.toCompletableFuture(f.apply(newStub)), config.getRetryOptions()); return task.scheduleTask(0); }
Example 18
Source File: Tiller.java From microbean-helm with Apache License 2.0 | 3 votes |
/** * Returns the gRPC-generated {@link ReleaseServiceFutureStub} * object that represents the capabilities of the Tiller server. * * <p>This method will never return {@code null}.</p> * * <p>Overrides of this method must never return {@code null}.</p> * * @return a non-{@code null} {@link ReleaseServiceFutureStub} * * @see ReleaseServiceFutureStub */ public ReleaseServiceFutureStub getReleaseServiceFutureStub() { ReleaseServiceFutureStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(ReleaseServiceGrpc.newFutureStub(this.channel), metadata); } return returnValue; }
Example 19
Source File: Tiller.java From microbean-helm with Apache License 2.0 | 3 votes |
/** * Returns the gRPC-generated {@link ReleaseServiceBlockingStub} * object that represents the capabilities of the Tiller server. * * <p>This method will never return {@code null}.</p> * * <p>Overrides of this method must never return {@code null}.</p> * * @return a non-{@code null} {@link ReleaseServiceBlockingStub} * * @see ReleaseServiceBlockingStub */ public ReleaseServiceBlockingStub getReleaseServiceBlockingStub() { ReleaseServiceBlockingStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(ReleaseServiceGrpc.newBlockingStub(this.channel), metadata); } return returnValue; }
Example 20
Source File: GrpcTokenUtil.java From notes with Apache License 2.0 | 2 votes |
/** * 自定义 token内容 * * @param stub * @return: T * @author: fruiqi * @date: 19-5-15 上午11:02 */ public static <T extends AbstractStub<T>> T blockingStubToken(T stub, String token) { Metadata metadata = new Metadata(); metadata.put(AUTHORIZATION, String.format("%s %s", BEARER_TYPE, token)); return MetadataUtils.attachHeaders(stub, metadata); }