|
| 1 | +/* |
| 2 | + * Copyright 2020 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 com.google.common.base.Preconditions; |
| 20 | +import java.util.concurrent.Executor; |
| 21 | + |
| 22 | +/** |
| 23 | + * Uses multiple {@code CallCredentials} as if they were one. If the first credential fails, the |
| 24 | + * second will not be used. Both must succeed to allow the RPC. |
| 25 | + */ |
| 26 | +@ExperimentalApi("https://github.com/grpc/grpc-java/issues/7479") |
| 27 | +public final class CompositeCallCredentials extends CallCredentials { |
| 28 | + private final CallCredentials credentials1; |
| 29 | + private final CallCredentials credentials2; |
| 30 | + |
| 31 | + public CompositeCallCredentials(CallCredentials creds1, CallCredentials creds2) { |
| 32 | + this.credentials1 = Preconditions.checkNotNull(creds1, "creds1"); |
| 33 | + this.credentials2 = Preconditions.checkNotNull(creds2, "creds2"); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void applyRequestMetadata( |
| 38 | + RequestInfo requestInfo, Executor appExecutor, MetadataApplier applier) { |
| 39 | + credentials1.applyRequestMetadata(requestInfo, appExecutor, |
| 40 | + new WrappingMetadataApplier(requestInfo, appExecutor, applier, Context.current())); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void thisUsesUnstableApi() {} |
| 45 | + |
| 46 | + private final class WrappingMetadataApplier extends MetadataApplier { |
| 47 | + private final RequestInfo requestInfo; |
| 48 | + private final Executor appExecutor; |
| 49 | + private final MetadataApplier delegate; |
| 50 | + private final Context context; |
| 51 | + |
| 52 | + public WrappingMetadataApplier( |
| 53 | + RequestInfo requestInfo, Executor appExecutor, MetadataApplier delegate, Context context) { |
| 54 | + this.requestInfo = requestInfo; |
| 55 | + this.appExecutor = appExecutor; |
| 56 | + this.delegate = Preconditions.checkNotNull(delegate, "delegate"); |
| 57 | + this.context = Preconditions.checkNotNull(context, "context"); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void apply(Metadata headers) { |
| 62 | + Preconditions.checkNotNull(headers, "headers"); |
| 63 | + Context previous = context.attach(); |
| 64 | + try { |
| 65 | + credentials2.applyRequestMetadata( |
| 66 | + requestInfo, appExecutor, new CombiningMetadataApplier(delegate, headers)); |
| 67 | + } finally { |
| 68 | + context.detach(previous); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void fail(Status status) { |
| 74 | + delegate.fail(status); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static final class CombiningMetadataApplier extends MetadataApplier { |
| 79 | + private final MetadataApplier delegate; |
| 80 | + private final Metadata firstHeaders; |
| 81 | + |
| 82 | + public CombiningMetadataApplier(MetadataApplier delegate, Metadata firstHeaders) { |
| 83 | + this.delegate = delegate; |
| 84 | + this.firstHeaders = firstHeaders; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void apply(Metadata headers) { |
| 89 | + Preconditions.checkNotNull(headers, "headers"); |
| 90 | + Metadata combined = new Metadata(); |
| 91 | + combined.merge(firstHeaders); |
| 92 | + combined.merge(headers); |
| 93 | + delegate.apply(combined); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void fail(Status status) { |
| 98 | + delegate.fail(status); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments