requestParamsHeader.put(requestParamsKey, "name=" + kmsKeyRingResourcePath); KeyManagementServiceBlockingStub stubForGetKeyRing = MetadataUtils.attachHeaders(kmsStub, requestParamsHeader); stubForGetKeyRing.getKeyRing(getKeyRingRequest); } catch (StatusRuntimeException ex) { requestParamsHeader.put(requestParamsKey, "parent=" + keyRingParent); KeyManagementServiceBlockingStub stubForCreateKeyRing = MetadataUtils.attachHeaders(kmsStub, requestParamsHeader); stubForCreateKeyRing.createKeyRing(createKeyRingRequest); } else {
public Driver(Builder builder) { this.segments = builder.segments; this.runtime = builder.runtimeSeconds; this.latencyAccumulator = builder.latencyAccumulator; Metadata metadata = new Metadata(); metadata.put(ID_HEADER, builder.id); this.channel = ClientInterceptors.intercept(NettyChannelBuilder.forTarget("localhost:" + builder.port) .usePlaintext(true) .build(), MetadataUtils.newAttachHeadersInterceptor(metadata)); }
/** * Captures the last received metadata for a stub. Useful for testing * * @param stub to capture for * @param headersCapture to record the last received headers * @param trailersCapture to record the last received trailers * @return an implementation of the stub that allows to access the last received call's * headers and trailers via {@code headersCapture} and {@code trailersCapture}. */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789") public static <T extends AbstractStub<T>> T captureMetadata( T stub, AtomicReference<Metadata> headersCapture, AtomicReference<Metadata> trailersCapture) { return stub.withInterceptors( newCaptureMetadataInterceptor(headersCapture, trailersCapture)); }
GetCryptoKeyRequest.newBuilder().setName(kmsKeyResourcePath).build(); KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForGetCryptoKey = MetadataUtils.attachHeaders(kmsStub, requestParamsHeader); stubForGetCryptoKey.getCryptoKey(getCryptoKeyRequest); } catch (StatusRuntimeException ex) { MetadataUtils.attachHeaders(kmsStub, requestParamsHeader); stubForCreateCryptoKey.createCryptoKey(createCryptoKeyRequest); } else {
/** * Creates a new {@link ClientInterceptor} that adds the given bearer token as Bearer Authentication to all * requests. The header will be encoded with {@link StandardCharsets#UTF_8 UTF_8}. * * @param token the bearer token * @return The newly created basic auth interceptor. * @deprecated Use {@link StubTransformer}s to set the credentials directly on {@link AbstractStub}s. */ @Deprecated public static ClientInterceptor bearerAuth(final String token) { final Metadata extraHeaders = new Metadata(); extraHeaders.put(AUTHORIZATION_HEADER, "Bearer " + token); return MetadataUtils.newAttachHeadersInterceptor(extraHeaders); }
private static void ensureKmsKeyRingIamPermissionsForTests( IAMPolicyGrpc.IAMPolicyBlockingStub iamStub, String projectId, String location, String keyRingName) throws StatusRuntimeException { ServiceAccount serviceAccount = storage.getServiceAccount(projectId); String kmsKeyRingResourcePath = KeyRingName.of(projectId, location, keyRingName).toString(); Binding binding = Binding.newBuilder() .setRole("roles/cloudkms.cryptoKeyEncrypterDecrypter") .addMembers("serviceAccount:" + serviceAccount.getEmail()) .build(); com.google.iam.v1.Policy policy = com.google.iam.v1.Policy.newBuilder().addBindings(binding).build(); SetIamPolicyRequest setIamPolicyRequest = SetIamPolicyRequest.newBuilder() .setResource(kmsKeyRingResourcePath) .setPolicy(policy) .build(); requestParamsHeader.put(requestParamsKey, "parent=" + kmsKeyRingResourcePath); iamStub = MetadataUtils.attachHeaders(iamStub, requestParamsHeader); iamStub.setIamPolicy(setIamPolicyRequest); }
/** * Creates a new {@link ClientInterceptor} that adds the given username and passwords as basic auth to all requests. * The header will be encoded with {@link StandardCharsets#UTF_8 UTF_8}. * * @param username The username to use. * @param password The password to use. * @return The newly created basic auth interceptor. * @see CallCredentialsHelper#encodeBasicAuth(String, String) * @deprecated Use the (potentially) more secure {@link CallCredentials}. */ @Deprecated public static ClientInterceptor basicAuthInterceptor(final String username, final String password) { final Metadata extraHeaders = new Metadata(); extraHeaders.put(AUTHORIZATION_HEADER, CallCredentialsHelper.encodeBasicAuth(username, password)); return MetadataUtils.newAttachHeadersInterceptor(extraHeaders); }
public HealthStub getHealthStub() { HealthStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(HealthGrpc.newStub(this.channel), metadata); } return returnValue; }
/** * Attaches a set of request headers to a stub. * * @param stub to bind the headers to. * @param extraHeaders the headers to be passed by each call on the returned stub. * @return an implementation of the stub with {@code extraHeaders} bound to each call. */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789") public static <T extends AbstractStub<T>> T attachHeaders(T stub, Metadata extraHeaders) { return stub.withInterceptors(newAttachHeadersInterceptor(extraHeaders)); }
public HealthBlockingStub getHealthBlockingStub() { HealthBlockingStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(HealthGrpc.newBlockingStub(this.channel), metadata); } return returnValue; }
/** * Creates a new {@link ClientInterceptor} that adds the given bearer token as Bearer Authentication to all * requests. The header will be encoded with {@link StandardCharsets#UTF_8 UTF_8}. * * @param token the bearer token * @return The newly created basic auth interceptor. * @deprecated Use {@link StubTransformer}s to set the credentials directly on {@link AbstractStub}s. */ @Deprecated public static ClientInterceptor bearerAuth(final String token) { final Metadata extraHeaders = new Metadata(); extraHeaders.put(AUTHORIZATION_HEADER, "Bearer " + token); return MetadataUtils.newAttachHeadersInterceptor(extraHeaders); }
public HealthFutureStub getHealthFutureStub() { HealthFutureStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(HealthGrpc.newFutureStub(this.channel), metadata); } return returnValue; }
/** * Creates a new {@link ClientInterceptor} that adds the given username and passwords as basic auth to all requests. * The header will be encoded with {@link StandardCharsets#UTF_8 UTF_8}. * * @param username The username to use. * @param password The password to use. * @return The newly created basic auth interceptor. * @see CallCredentialsHelper#encodeBasicAuth(String, String) * @deprecated Use the (potentially) more secure {@link CallCredentials}. */ @Deprecated public static ClientInterceptor basicAuthInterceptor(final String username, final String password) { final Metadata extraHeaders = new Metadata(); extraHeaders.put(AUTHORIZATION_HEADER, CallCredentialsHelper.encodeBasicAuth(username, password)); return MetadataUtils.newAttachHeadersInterceptor(extraHeaders); }
public HealthStub getHealthStub() { HealthStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(HealthGrpc.newStub(this.channel), metadata); } return returnValue; }
/** * Create a new gRPC channel to the Google Genomics API, using the provided credentials for auth. * * @param creds The credential. * @param fields Which fields to return in the partial response, or null for none. * @return The ManagedChannel. * @throws SSLException */ public static ManagedChannel fromCreds(GoogleCredentials creds, String fields) throws SSLException { List<ClientInterceptor> interceptors = new ArrayList(); interceptors.add(new ClientAuthInterceptor(creds.createScoped(Arrays.asList(GENOMICS_SCOPE)), Executors.newSingleThreadExecutor())); if (!Strings.isNullOrEmpty(fields)) { Metadata headers = new Metadata(); Metadata.Key<String> partialResponseHeader = Metadata.Key.of(PARTIAL_RESPONSE_HEADER, Metadata.ASCII_STRING_MARSHALLER); headers.put(partialResponseHeader, fields); interceptors.add(MetadataUtils.newAttachHeadersInterceptor(headers)); } return getGenomicsManagedChannel(interceptors); }
public HealthBlockingStub getHealthBlockingStub() { HealthBlockingStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(HealthGrpc.newBlockingStub(this.channel), metadata); } return returnValue; }
MetadataUtils.newAttachHeadersInterceptor(grpcContext.getMetadata()); channel = ClientInterceptors.intercept(channel, interceptor);
public HealthFutureStub getHealthFutureStub() { HealthFutureStub returnValue = null; if (this.channel != null) { returnValue = MetadataUtils.attachHeaders(HealthGrpc.newFutureStub(this.channel), metadata); } return returnValue; }
MetadataUtils.newAttachHeadersInterceptor(grpcContext.getMetadata()); channel = ClientInterceptors.intercept(channel, interceptor);
public static <T extends AbstractStub<T>> T parseHeaders(HttpHeaders headers, T stub) { return MetadataUtils.attachHeaders(stub, parseHeaders(headers)); }