|
| 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.resumable.StartUploadRequest; |
| 39 | +import com.google.api.gax.rpc.ApiCallContext; |
| 40 | +import com.google.api.gax.rpc.ClientContext; |
| 41 | +import com.google.api.gax.rpc.UnaryCallable; |
| 42 | +import com.google.api.pathtemplate.PathTemplate; |
| 43 | +import com.google.common.base.Preconditions; |
| 44 | +import com.google.common.base.Strings; |
| 45 | +import com.google.common.collect.ImmutableList; |
| 46 | +import com.google.common.collect.ImmutableMap; |
| 47 | +import java.util.List; |
| 48 | +import java.util.Map; |
| 49 | +import org.jspecify.annotations.NullMarked; |
| 50 | +import org.jspecify.annotations.Nullable; |
| 51 | + |
| 52 | +/** |
| 53 | + * Implementation of {@link ResumableUploadClient} using HTTP/JSON transport. |
| 54 | + * |
| 55 | + * <p>Executes the low-level HTTP wire calls for managing resumable upload sessions. |
| 56 | + */ |
| 57 | +@NullMarked |
| 58 | +@InternalApi |
| 59 | +public final class HttpJsonResumableUploadClient implements ResumableUploadClient { |
| 60 | + |
| 61 | + private static final String UPLOAD_PROTOCOL_HEADER = "X-Goog-Upload-Protocol"; |
| 62 | + private static final String UPLOAD_COMMAND_HEADER = "X-Goog-Upload-Command"; |
| 63 | + private static final String UPLOAD_URL_HEADER = "X-Goog-Upload-URL"; |
| 64 | + private static final String UPLOAD_GRANULARITY_HEADER = "X-Goog-Upload-Chunk-Granularity"; |
| 65 | + |
| 66 | + private static final Map<String, List<String>> START_UPLOAD_HEADERS = |
| 67 | + ImmutableMap.of( |
| 68 | + UPLOAD_PROTOCOL_HEADER, ImmutableList.of("resumable"), |
| 69 | + UPLOAD_COMMAND_HEADER, ImmutableList.of("start")); |
| 70 | + |
| 71 | + private static final ApiMethodDescriptor<StartUploadRequest, String> START_UPLOAD_DESCRIPTOR = |
| 72 | + ApiMethodDescriptor.<StartUploadRequest, String>newBuilder() |
| 73 | + .setFullMethodName("ResumableUpload/StartUpload") |
| 74 | + .setHttpMethod(HttpMethods.POST) |
| 75 | + .setType(ApiMethodDescriptor.MethodType.UNARY) |
| 76 | + .setRequestFormatter( |
| 77 | + new HttpRequestFormatter<StartUploadRequest>() { |
| 78 | + @Override |
| 79 | + public Map<String, List<String>> getQueryParamNames(StartUploadRequest request) { |
| 80 | + return request.getQueryParams(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String getRequestBody(StartUploadRequest request) { |
| 85 | + return request.getJsonPayload(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String getPath(StartUploadRequest request) { |
| 90 | + return request.getPath(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public PathTemplate getPathTemplate() { |
| 95 | + return PathTemplate.create("{+path}"); |
| 96 | + } |
| 97 | + }) |
| 98 | + .setResponseParser(StringHttpResponseParser.create()) |
| 99 | + .build(); |
| 100 | + |
| 101 | + private final ClientContext clientContext; |
| 102 | + |
| 103 | + public static HttpJsonResumableUploadClient create(ClientContext clientContext) { |
| 104 | + return new HttpJsonResumableUploadClient(clientContext); |
| 105 | + } |
| 106 | + |
| 107 | + private HttpJsonResumableUploadClient(ClientContext clientContext) { |
| 108 | + this.clientContext = Preconditions.checkNotNull(clientContext); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public UnaryCallable<StartUploadRequest, ResumableUploadSession> startUploadCallable() { |
| 113 | + return new UnaryCallable<StartUploadRequest, ResumableUploadSession>() { |
| 114 | + @Override |
| 115 | + public ApiFuture<ResumableUploadSession> futureCall( |
| 116 | + StartUploadRequest request, @Nullable ApiCallContext inputContext) { |
| 117 | + Preconditions.checkNotNull(request); |
| 118 | + HttpJsonCallContext context = |
| 119 | + (HttpJsonCallContext) |
| 120 | + HttpJsonCallContext.createDefault() |
| 121 | + .nullToSelf(clientContext.getDefaultCallContext()) |
| 122 | + .merge(inputContext) |
| 123 | + .withExtraHeaders(START_UPLOAD_HEADERS); |
| 124 | + |
| 125 | + HttpJsonClientCall<StartUploadRequest, String> clientCall = |
| 126 | + HttpJsonClientCalls.newCall(START_UPLOAD_DESCRIPTOR, context); |
| 127 | + |
| 128 | + SettableApiFuture<ResumableUploadSession> future = SettableApiFuture.create(); |
| 129 | + HttpJsonClientCalls.startUnaryCall( |
| 130 | + clientCall, request, context, new StartUploadResponseListener(future)); |
| 131 | + |
| 132 | + return future; |
| 133 | + } |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + private static class StartUploadResponseListener extends HttpJsonClientCall.Listener<String> { |
| 138 | + |
| 139 | + private final SettableApiFuture<ResumableUploadSession> future; |
| 140 | + @Nullable private String uploadUrl; |
| 141 | + private long chunkGranularity = 1L; |
| 142 | + |
| 143 | + StartUploadResponseListener(SettableApiFuture<ResumableUploadSession> future) { |
| 144 | + this.future = future; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void onHeaders(HttpJsonMetadata responseHeaders) { |
| 149 | + Map<String, Object> headers = responseHeaders.getHeaders(); |
| 150 | + |
| 151 | + String url = HttpHeadersUtils.getFirstHeader(headers, UPLOAD_URL_HEADER); |
| 152 | + if (Strings.isNullOrEmpty(url)) { |
| 153 | + url = HttpHeadersUtils.getFirstHeader(headers, "Location"); |
| 154 | + } |
| 155 | + if (!Strings.isNullOrEmpty(url)) { |
| 156 | + this.uploadUrl = url; |
| 157 | + } |
| 158 | + |
| 159 | + String granularityStr = HttpHeadersUtils.getFirstHeader(headers, UPLOAD_GRANULARITY_HEADER); |
| 160 | + if (!Strings.isNullOrEmpty(granularityStr)) { |
| 161 | + try { |
| 162 | + this.chunkGranularity = Long.parseLong(granularityStr); |
| 163 | + } catch (NumberFormatException ignored) { |
| 164 | + this.chunkGranularity = 1L; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void onMessage(@Nullable String message) {} |
| 171 | + |
| 172 | + @Override |
| 173 | + public void onClose(int statusCode, HttpJsonMetadata trailers) { |
| 174 | + if (statusCode >= 200 && statusCode < 300) { |
| 175 | + if (!Strings.isNullOrEmpty(uploadUrl)) { |
| 176 | + future.set(ResumableUploadSession.create(uploadUrl, chunkGranularity)); |
| 177 | + } else { |
| 178 | + future.setException( |
| 179 | + new HttpJsonStatusRuntimeException( |
| 180 | + statusCode, |
| 181 | + "Start upload response did not contain upload session URL header", |
| 182 | + null)); |
| 183 | + } |
| 184 | + } else { |
| 185 | + future.setException( |
| 186 | + trailers.getException() != null |
| 187 | + ? trailers.getException() |
| 188 | + : new HttpJsonStatusRuntimeException(statusCode, "Failed to start upload", null)); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments