1 /* 2 * Copyright 2016 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc; 18 19 import static com.google.common.base.MoreObjects.firstNonNull; 20 import static com.google.common.base.Preconditions.checkNotNull; 21 22 import java.util.concurrent.Executor; 23 24 /** 25 * The new interface for {@link CallCredentials}. 26 * 27 * <p>THIS CLASS NAME IS TEMPORARY and is part of a migration. This class will BE DELETED as it 28 * replaces {@link CallCredentials} in short-term. THIS CLASS SHOULD ONLY BE REFERENCED BY 29 * IMPLEMENTIONS. All consumers should still reference {@link CallCredentials}. 30 */ 31 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4901") 32 public abstract class CallCredentials2 implements CallCredentials { 33 /** 34 * Pass the credential data to the given {@link CallCredentials.MetadataApplier}, which will 35 * propagate it to the request metadata. 36 * 37 * <p>It is called for each individual RPC, within the {@link Context} of the call, before the 38 * stream is about to be created on a transport. Implementations should not block in this 39 * method. If metadata is not immediately available, e.g., needs to be fetched from network, the 40 * implementation may give the {@code applier} to an asynchronous task which will eventually call 41 * the {@code applier}. The RPC proceeds only after the {@code applier} is called. 42 * 43 * @param requestInfo request-related information 44 * @param appExecutor The application thread-pool. It is provided to the implementation in case it 45 * needs to perform blocking operations. 46 * @param applier The outlet of the produced headers. It can be called either before or after this 47 * method returns. 48 */ 49 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1914") applyRequestMetadata( RequestInfo requestInfo, Executor appExecutor, MetadataApplier applier)50 public abstract void applyRequestMetadata( 51 RequestInfo requestInfo, Executor appExecutor, MetadataApplier applier); 52 53 @Override 54 @SuppressWarnings("deprecation") applyRequestMetadata( final MethodDescriptor<?, ?> method, final Attributes attrs, Executor appExecutor, final CallCredentials.MetadataApplier applier)55 public final void applyRequestMetadata( 56 final MethodDescriptor<?, ?> method, final Attributes attrs, 57 Executor appExecutor, final CallCredentials.MetadataApplier applier) { 58 final String authority = checkNotNull(attrs.get(ATTR_AUTHORITY), "authority"); 59 final SecurityLevel securityLevel = 60 firstNonNull(attrs.get(ATTR_SECURITY_LEVEL), SecurityLevel.NONE); 61 RequestInfo requestInfo = new RequestInfo() { 62 @Override 63 public MethodDescriptor<?, ?> getMethodDescriptor() { 64 return method; 65 } 66 67 @Override 68 public SecurityLevel getSecurityLevel() { 69 return securityLevel; 70 } 71 72 @Override 73 public String getAuthority() { 74 return authority; 75 } 76 77 @Override 78 public Attributes getTransportAttrs() { 79 return attrs; 80 } 81 }; 82 MetadataApplier applierAdapter = new MetadataApplier() { 83 @Override 84 public void apply(Metadata headers) { 85 applier.apply(headers); 86 } 87 88 @Override 89 public void fail(Status status) { 90 applier.fail(status); 91 } 92 }; 93 applyRequestMetadata(requestInfo, appExecutor, applierAdapter); 94 } 95 96 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1914") 97 @SuppressWarnings("deprecation") 98 public abstract static class MetadataApplier implements CallCredentials.MetadataApplier {} 99 } 100