Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.api.client.util.Strings;
import com.google.api.core.ApiClock;
import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.core.NanoClock;
import com.google.api.core.ObsoleteApi;
import com.google.api.gax.core.BackgroundResource;
Expand Down Expand Up @@ -292,6 +293,80 @@
.build();
}

/**
* Derives a {@link ClientContext} for a second transport from {@code parent}, replacing the
* transport channel, endpoint and internal headers and inheriting everything else.
*/
@InternalApi
public static ClientContext createWithTransport(
ClientContext parent,
String endpoint,
TransportChannelProvider transportChannelProvider,
HeaderProvider internalHeaderProvider)
throws IOException {
Credentials credentials = parent.getCredentials();
EndpointContext endpointContext = parent.getEndpointContext();

Map<String, String> internal = internalHeaderProvider.getHeaders();
Map<String, String> user = parent.getHeaders();
Map<String, String> conflictResolution = new HashMap<>();

for (String key : Sets.intersection(user.keySet(), internal.keySet())) {

Check warning on line 314 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Reduce the total number of break and continue statements in this loop to use at most one.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AaCtQfNhb7eQSQL38fqj&open=AaCtQfNhb7eQSQL38fqj&pullRequest=14412
if ("user-agent".equals(key)) {
conflictResolution.put(key, user.get(key) + " " + internal.get(key));
continue;
}
if (QUOTA_PROJECT_ID_HEADER_KEY.equals(key) && parent.getQuotaProjectId() != null) {
continue;
}
throw new IllegalArgumentException("Header provider can't override the header: " + key);
}
if (parent.getQuotaProjectId() != null) {
conflictResolution.put(QUOTA_PROJECT_ID_HEADER_KEY, parent.getQuotaProjectId());
}

Map<String, String> effective = new HashMap<>();
effective.putAll(internal);
effective.putAll(user);
effective.putAll(conflictResolution);
Map<String, String> headers = appendCredentialTypeToHeaderIfPresent(effective, credentials);

TransportChannelProvider provider = transportChannelProvider;
if (provider.needsHeaders()) {
provider = provider.withHeaders(headers);
}
if (provider.needsCredentials() && credentials != null) {
provider = provider.withCredentials(credentials);
}
if (provider.needsEndpoint()) {
provider = provider.withEndpoint(endpoint);
}
provider = provider.withUseS2A(endpointContext.useS2A());
if (provider.needsMtlsEndpoint() && endpointContext.mtlsEndpoint() != null) {
provider = provider.withMtlsEndpoint(endpointContext.mtlsEndpoint());

Check warning on line 346 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Annotate the parameter with @javax.annotation.Nullable in method 'withMtlsEndpoint' declaration, or make sure that null can not be passed as argument.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AaCtQfNhb7eQSQL38fqi&open=AaCtQfNhb7eQSQL38fqi&pullRequest=14412
}
TransportChannel channel = provider.getTransportChannel();

ApiCallContext defaultCallContext = channel.getEmptyCallContext().withTransportChannel(channel);
if (credentials != null) {
defaultCallContext = defaultCallContext.withCredentials(credentials);
}
defaultCallContext = defaultCallContext.withEndpointContext(endpointContext);
Comment on lines +344 to +354

This comment was marked as off-topic.


ImmutableList.Builder<BackgroundResource> backgroundResources = ImmutableList.builder();
if (provider.shouldAutoClose()) {
backgroundResources.add(channel);
}

return parent.toBuilder()
.setEndpoint(endpoint)
.setTransportChannel(channel)
.setInternalHeaders(ImmutableMap.copyOf(internal))
.setDefaultCallContext(defaultCallContext)
.setBackgroundResources(backgroundResources.build())
.build();
}

@VisibleForTesting
static ApiTracerFactory getApiTracerFactory(
StubSettings settings, EndpointContext endpointContext) {
Expand Down
Loading