|
| 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.httpjson; |
| 31 | + |
| 32 | +import com.google.api.client.http.HttpMethods; |
| 33 | +import com.google.api.core.ApiFuture; |
| 34 | +import com.google.api.core.InternalApi; |
| 35 | +import com.google.api.core.SettableApiFuture; |
| 36 | +import com.google.api.gax.resumable.ResumableUploadClient; |
| 37 | +import com.google.api.gax.resumable.ResumableUploadSession; |
| 38 | +import com.google.api.gax.rpc.ApiCallContext; |
| 39 | +import com.google.api.gax.rpc.ApiException; |
| 40 | +import com.google.api.gax.rpc.ApiExceptionFactory; |
| 41 | +import com.google.api.gax.rpc.ClientContext; |
| 42 | +import com.google.api.gax.rpc.StatusCode; |
| 43 | +import com.google.api.gax.rpc.UnaryCallable; |
| 44 | +import com.google.common.base.Preconditions; |
| 45 | +import com.google.common.base.Strings; |
| 46 | +import com.google.common.collect.ImmutableList; |
| 47 | +import com.google.common.collect.ImmutableMap; |
| 48 | +import java.util.Collections; |
| 49 | +import java.util.List; |
| 50 | +import java.util.Map; |
| 51 | +import org.jspecify.annotations.NullMarked; |
| 52 | +import org.jspecify.annotations.Nullable; |
| 53 | + |
| 54 | +/** |
| 55 | + * Implementation of {@link ResumableUploadClient} using HTTP/JSON transport. |
| 56 | + * |
| 57 | + * <p>Executes the low-level HTTP wire calls for managing resumable upload sessions. |
| 58 | + * |
| 59 | + * @param <RequestT> request type for starting an upload |
| 60 | + * @param <ResponseT> response type of the upload method |
| 61 | + */ |
| 62 | +@NullMarked |
| 63 | +@InternalApi |
| 64 | +public final class HttpJsonResumableUploadClient<RequestT, ResponseT> |
| 65 | + implements ResumableUploadClient<RequestT, ResponseT> { |
| 66 | + |
| 67 | + private static final HttpJsonApiExceptionFactory API_EXCEPTION_FACTORY = |
| 68 | + new HttpJsonApiExceptionFactory(Collections.emptySet()); |
| 69 | + |
| 70 | + private static final String UPLOAD_PROTOCOL_HEADER = "X-Goog-Upload-Protocol"; |
| 71 | + private static final String UPLOAD_COMMAND_HEADER = "X-Goog-Upload-Command"; |
| 72 | + private static final String UPLOAD_URL_HEADER = "X-Goog-Upload-URL"; |
| 73 | + private static final String UPLOAD_GRANULARITY_HEADER = "X-Goog-Upload-Chunk-Granularity"; |
| 74 | + |
| 75 | + private static final Map<String, List<String>> START_UPLOAD_HEADERS = |
| 76 | + ImmutableMap.of( |
| 77 | + UPLOAD_PROTOCOL_HEADER, ImmutableList.of("resumable"), |
| 78 | + UPLOAD_COMMAND_HEADER, ImmutableList.of("start")); |
| 79 | + |
| 80 | + private final ClientContext clientContext; |
| 81 | + private final ApiMethodDescriptor<RequestT, String> startUploadDescriptor; |
| 82 | + private final HttpResponseParser<ResponseT> responseParser; |
| 83 | + |
| 84 | + public static <RequestT, ResponseT> HttpJsonResumableUploadClient<RequestT, ResponseT> create( |
| 85 | + ClientContext clientContext, ApiMethodDescriptor<RequestT, ResponseT> methodDescriptor) { |
| 86 | + return new HttpJsonResumableUploadClient<>(clientContext, methodDescriptor); |
| 87 | + } |
| 88 | + |
| 89 | + private HttpJsonResumableUploadClient( |
| 90 | + ClientContext clientContext, ApiMethodDescriptor<RequestT, ResponseT> methodDescriptor) { |
| 91 | + this.clientContext = Preconditions.checkNotNull(clientContext); |
| 92 | + Preconditions.checkNotNull(methodDescriptor); |
| 93 | + this.responseParser = Preconditions.checkNotNull(methodDescriptor.getResponseParser()); |
| 94 | + |
| 95 | + this.startUploadDescriptor = |
| 96 | + ApiMethodDescriptor.<RequestT, String>newBuilder() |
| 97 | + .setFullMethodName(methodDescriptor.getFullMethodName()) |
| 98 | + .setHttpMethod(HttpMethods.POST) |
| 99 | + .setType(ApiMethodDescriptor.MethodType.UNARY) |
| 100 | + .setRequestFormatter(methodDescriptor.getRequestFormatter()) |
| 101 | + .setResponseParser(ResumableUploadResponseParser.create()) |
| 102 | + .build(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public UnaryCallable<RequestT, ResumableUploadSession> startUploadCallable() { |
| 107 | + return new UnaryCallable<RequestT, ResumableUploadSession>() { |
| 108 | + @Override |
| 109 | + public ApiFuture<ResumableUploadSession> futureCall( |
| 110 | + RequestT request, @Nullable ApiCallContext inputContext) { |
| 111 | + Preconditions.checkNotNull(request); |
| 112 | + HttpJsonCallContext context = |
| 113 | + (HttpJsonCallContext) |
| 114 | + HttpJsonCallContext.createDefault() |
| 115 | + .nullToSelf(clientContext.getDefaultCallContext()) |
| 116 | + .merge(inputContext) |
| 117 | + .withExtraHeaders(START_UPLOAD_HEADERS); |
| 118 | + |
| 119 | + HttpJsonClientCall<RequestT, String> clientCall = |
| 120 | + HttpJsonClientCalls.newCall(startUploadDescriptor, context); |
| 121 | + |
| 122 | + SettableApiFuture<ResumableUploadSession> future = SettableApiFuture.create(); |
| 123 | + HttpJsonClientCalls.startUnaryCall( |
| 124 | + clientCall, request, context, new StartUploadResponseListener(future)); |
| 125 | + |
| 126 | + return future; |
| 127 | + } |
| 128 | + }; |
| 129 | + } |
| 130 | + |
| 131 | + private static class StartUploadResponseListener extends HttpJsonClientCall.Listener<String> { |
| 132 | + |
| 133 | + private final SettableApiFuture<ResumableUploadSession> future; |
| 134 | + @Nullable private String uploadUrl; |
| 135 | + private long chunkGranularity = 1L; |
| 136 | + |
| 137 | + StartUploadResponseListener(SettableApiFuture<ResumableUploadSession> future) { |
| 138 | + this.future = future; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onHeaders(HttpJsonMetadata responseHeaders) { |
| 143 | + Map<String, Object> headers = responseHeaders.getHeaders(); |
| 144 | + |
| 145 | + String url = HttpHeadersUtils.getSingleHeader(headers, UPLOAD_URL_HEADER); |
| 146 | + if (!Strings.isNullOrEmpty(url)) { |
| 147 | + this.uploadUrl = url; |
| 148 | + } |
| 149 | + |
| 150 | + String granularityStr = HttpHeadersUtils.getSingleHeader(headers, UPLOAD_GRANULARITY_HEADER); |
| 151 | + if (!Strings.isNullOrEmpty(granularityStr)) { |
| 152 | + try { |
| 153 | + this.chunkGranularity = Long.parseLong(granularityStr); |
| 154 | + } catch (NumberFormatException ignored) { |
| 155 | + this.chunkGranularity = 1L; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void onMessage(@Nullable String message) { |
| 162 | + // Response body is not needed for startUpload; session URL is in headers. |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void onClose(int statusCode, HttpJsonMetadata trailers) { |
| 167 | + try { |
| 168 | + if (statusCode >= 200 && statusCode < 300) { |
| 169 | + if (!Strings.isNullOrEmpty(uploadUrl)) { |
| 170 | + future.set( |
| 171 | + ResumableUploadSession.newBuilder() |
| 172 | + .setUploadUrl(uploadUrl) |
| 173 | + .setChunkGranularity(chunkGranularity) |
| 174 | + .build()); |
| 175 | + } else { |
| 176 | + future.setException( |
| 177 | + ApiExceptionFactory.createException( |
| 178 | + "Start upload response did not contain upload session URL header", |
| 179 | + /* cause= */ null, |
| 180 | + HttpJsonStatusCode.of(StatusCode.Code.INTERNAL), |
| 181 | + /* retryable= */ false)); |
| 182 | + } |
| 183 | + } else { |
| 184 | + Throwable cause = trailers.getException(); |
| 185 | + ApiException apiException = |
| 186 | + cause != null |
| 187 | + ? API_EXCEPTION_FACTORY.create(cause) |
| 188 | + : ApiExceptionFactory.createException( |
| 189 | + "Failed to start upload with status code: " + statusCode, |
| 190 | + /* cause= */ null, |
| 191 | + HttpJsonStatusCode.of(statusCode), |
| 192 | + /* retryable= */ false); |
| 193 | + future.setException(apiException); |
| 194 | + } |
| 195 | + } catch (Throwable t) { |
| 196 | + future.setException( |
| 197 | + ApiExceptionFactory.createException( |
| 198 | + "Internal error processing start upload response", |
| 199 | + t, |
| 200 | + HttpJsonStatusCode.of(StatusCode.Code.INTERNAL), |
| 201 | + /* retryable= */ false)); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments