|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.rpc; |
| 31 | + |
| 32 | +import com.google.api.core.BetaApi; |
| 33 | +import com.google.api.core.InternalApi; |
| 34 | +import com.google.common.annotations.VisibleForTesting; |
| 35 | +import java.io.IOException; |
| 36 | +import org.jspecify.annotations.NullMarked; |
| 37 | +import org.jspecify.annotations.Nullable; |
| 38 | + |
| 39 | +/** |
| 40 | + * Helpers for bridging a stub's transport configuration to the HTTP/JSON {@link ClientContext} used |
| 41 | + * for resumable uploads. Designed for use by generated code. |
| 42 | + */ |
| 43 | +@BetaApi |
| 44 | +@InternalApi |
| 45 | +@NullMarked |
| 46 | +public final class ResumableUploadTransportBridge { |
| 47 | + |
| 48 | + private static final ApiCallContext.Key<Boolean> NON_SETTINGS_TRANSPORT_CUSTOMIZATION_KEY = |
| 49 | + ApiCallContext.Key.create("resumable_upload_non_settings_transport_customization"); |
| 50 | + |
| 51 | + private static final String CLIENT_CONTEXT_FAILURE_MESSAGE = |
| 52 | + "Failed to initialize the HTTP/JSON client context required for resumable uploads." |
| 53 | + + " Resumable uploads run over HTTP/JSON using only the endpoint, credentials, and" |
| 54 | + + " headers configured on the service Settings; create the client or stub from Settings" |
| 55 | + + " rather than a ClientContext."; |
| 56 | + |
| 57 | + static final String NON_SETTINGS_TRANSPORT_CUSTOMIZATION_HINT = |
| 58 | + "This client configures the endpoint, credentials, or headers outside the service Settings" |
| 59 | + + " (on the TransportChannelProvider or via a pre-built channel). Resumable uploads do not" |
| 60 | + + " use the TransportChannelProvider or pre-built channel; configure the endpoint," |
| 61 | + + " credentials, and headers on the Settings instead."; |
| 62 | + |
| 63 | + private ResumableUploadTransportBridge() {} |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates the HTTP/JSON {@link ClientContext} used for resumable uploads. If {@code settings} |
| 67 | + * already uses an HTTP/JSON transport, the context is created from {@code settings} unchanged. |
| 68 | + * Otherwise the transport channel provider and internal headers are replaced with the given |
| 69 | + * HTTP/JSON equivalents, and if the original transport channel provider was customized outside |
| 70 | + * the settings, the context is marked so that upload failures can surface an actionable hint. |
| 71 | + */ |
| 72 | + public static ClientContext createUploadClientContext( |
| 73 | + StubSettings<?> settings, |
| 74 | + TransportChannelProvider httpJsonChannelProvider, |
| 75 | + HeaderProvider httpJsonInternalHeaderProvider) |
| 76 | + throws IOException { |
| 77 | + TransportChannelProvider original = settings.getTransportChannelProvider(); |
| 78 | + if (original.getTransportName().equals(httpJsonChannelProvider.getTransportName())) { |
| 79 | + return ClientContext.create(settings); |
| 80 | + } |
| 81 | + StubSettings.Builder<?, ?> builder = settings.toBuilder(); |
| 82 | + builder.setInternalHeaderProvider(httpJsonInternalHeaderProvider); |
| 83 | + builder.setTransportChannelProvider(httpJsonChannelProvider); |
| 84 | + ClientContext uploadContext = ClientContext.create(builder.build()); |
| 85 | + if (!isCustomizedOutsideSettings(original)) { |
| 86 | + return uploadContext; |
| 87 | + } |
| 88 | + return markNonSettingsTransportCustomization(uploadContext); |
| 89 | + } |
| 90 | + |
| 91 | + @VisibleForTesting |
| 92 | + static ClientContext markNonSettingsTransportCustomization(ClientContext uploadContext) { |
| 93 | + return uploadContext.toBuilder() |
| 94 | + .setDefaultCallContext( |
| 95 | + uploadContext |
| 96 | + .getDefaultCallContext() |
| 97 | + .withOption(NON_SETTINGS_TRANSPORT_CUSTOMIZATION_KEY, true)) |
| 98 | + .build(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Creates a {@link ResumableUploadCallable} that synchronously throws an {@link |
| 103 | + * IllegalStateException} with the given cause when invoked because the HTTP/JSON client context |
| 104 | + * required for resumable uploads could not be created. |
| 105 | + */ |
| 106 | + public static <RequestT, ResponseT> ResumableUploadCallable<RequestT, ResponseT> failingCallable( |
| 107 | + Throwable cause) { |
| 108 | + return new FailingResumableUploadCallable<>(cause); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns {@code true} if {@code callContext} was marked by {@link #createUploadClientContext} |
| 113 | + * because the original transport channel provider was customized outside the stub's settings. |
| 114 | + */ |
| 115 | + static boolean hasNonSettingsTransportCustomization(ApiCallContext callContext) { |
| 116 | + return Boolean.TRUE.equals(callContext.getOption(NON_SETTINGS_TRANSPORT_CUSTOMIZATION_KEY)); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns {@code true} if {@code provider} does not need the endpoint, credentials, or headers |
| 121 | + * from the stub's settings, meaning they were configured on the provider directly or the provider |
| 122 | + * wraps a pre-built channel. |
| 123 | + */ |
| 124 | + private static boolean isCustomizedOutsideSettings(TransportChannelProvider provider) { |
| 125 | + return !provider.needsHeaders() || !provider.needsCredentials() || !provider.needsEndpoint(); |
| 126 | + } |
| 127 | + |
| 128 | + private static final class FailingResumableUploadCallable<RequestT, ResponseT> |
| 129 | + extends ResumableUploadCallable<RequestT, ResponseT> { |
| 130 | + |
| 131 | + private final Throwable cause; |
| 132 | + |
| 133 | + private FailingResumableUploadCallable(Throwable cause) { |
| 134 | + this.cause = cause; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public ResumableUploadFuture<ResponseT> futureCall( |
| 139 | + RequestT request, |
| 140 | + InputStreamSupplier payload, |
| 141 | + @Nullable ApiCallContext context, |
| 142 | + @Nullable ResumableUploadOptions options) { |
| 143 | + throw new IllegalStateException(CLIENT_CONTEXT_FAILURE_MESSAGE, cause); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public ResumableUploadFuture<ResponseT> resumeCall( |
| 148 | + String sessionUrl, InputStreamSupplier payload, @Nullable ResumableUploadOptions options) { |
| 149 | + throw new IllegalStateException(CLIENT_CONTEXT_FAILURE_MESSAGE, cause); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments