Skip to content
Draft
Show file tree
Hide file tree
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
@@ -0,0 +1,192 @@
/*
* Copyright 2026 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.httpjson;

import com.google.api.client.http.HttpMethods;
import com.google.api.core.ApiFuture;
import com.google.api.core.InternalApi;
import com.google.api.core.SettableApiFuture;
import com.google.api.gax.resumable.ResumableUploadClient;
import com.google.api.gax.resumable.ResumableUploadSession;
import com.google.api.gax.resumable.StartUploadRequest;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* Implementation of {@link ResumableUploadClient} using HTTP/JSON transport.
*
* <p>Executes the low-level HTTP wire calls for managing resumable upload sessions.
*/
@NullMarked
@InternalApi
public final class HttpJsonResumableUploadClient implements ResumableUploadClient {

private static final String UPLOAD_PROTOCOL_HEADER = "X-Goog-Upload-Protocol";
private static final String UPLOAD_COMMAND_HEADER = "X-Goog-Upload-Command";
private static final String UPLOAD_URL_HEADER = "X-Goog-Upload-URL";
private static final String UPLOAD_GRANULARITY_HEADER = "X-Goog-Upload-Chunk-Granularity";

private static final Map<String, List<String>> START_UPLOAD_HEADERS =
ImmutableMap.of(
UPLOAD_PROTOCOL_HEADER, ImmutableList.of("resumable"),
UPLOAD_COMMAND_HEADER, ImmutableList.of("start"));

private static final ApiMethodDescriptor<StartUploadRequest, String> START_UPLOAD_DESCRIPTOR =
ApiMethodDescriptor.<StartUploadRequest, String>newBuilder()
.setFullMethodName("ResumableUpload/StartUpload")
.setHttpMethod(HttpMethods.POST)
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
new HttpRequestFormatter<StartUploadRequest>() {
@Override
public Map<String, List<String>> getQueryParamNames(StartUploadRequest request) {
return request.getQueryParams();
}

@Override
public String getRequestBody(StartUploadRequest request) {
return request.getJsonPayload();

Check warning on line 85 in sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpJsonResumableUploadClient.java

View check run for this annotation

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

This method's return value is marked "@NullMarked at class level" but null is returned.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AaAW8govxOhvm0Uj4YS9&open=AaAW8govxOhvm0Uj4YS9&pullRequest=14091
}

@Override
public String getPath(StartUploadRequest request) {
return request.getPath();
}

@Override
public PathTemplate getPathTemplate() {
return PathTemplate.create("{+path}");
}
})
.setResponseParser(StringHttpResponseParser.create())
.build();

private final ClientContext clientContext;

public static HttpJsonResumableUploadClient create(ClientContext clientContext) {
return new HttpJsonResumableUploadClient(clientContext);
}

private HttpJsonResumableUploadClient(ClientContext clientContext) {
this.clientContext = Preconditions.checkNotNull(clientContext);
}

@Override
public UnaryCallable<StartUploadRequest, ResumableUploadSession> startUploadCallable() {
return new UnaryCallable<StartUploadRequest, ResumableUploadSession>() {
@Override
public ApiFuture<ResumableUploadSession> futureCall(
StartUploadRequest request, @Nullable ApiCallContext inputContext) {
Preconditions.checkNotNull(request);
HttpJsonCallContext context =
(HttpJsonCallContext)
HttpJsonCallContext.createDefault()
.nullToSelf(clientContext.getDefaultCallContext())
.merge(inputContext)

Check warning on line 122 in sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpJsonResumableUploadClient.java

View check run for this annotation

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

Annotate the parameter with @javax.annotation.Nullable in method 'merge' 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=AaAW8govxOhvm0Uj4YS-&open=AaAW8govxOhvm0Uj4YS-&pullRequest=14091
.withExtraHeaders(START_UPLOAD_HEADERS);

HttpJsonClientCall<StartUploadRequest, String> clientCall =
HttpJsonClientCalls.newCall(START_UPLOAD_DESCRIPTOR, context);

SettableApiFuture<ResumableUploadSession> future = SettableApiFuture.create();
HttpJsonClientCalls.startUnaryCall(
clientCall, request, context, new StartUploadResponseListener(future));

return future;
}
};
}

private static class StartUploadResponseListener extends HttpJsonClientCall.Listener<String> {

private final SettableApiFuture<ResumableUploadSession> future;
@Nullable private String uploadUrl;
private long chunkGranularity = 1L;

StartUploadResponseListener(SettableApiFuture<ResumableUploadSession> future) {
this.future = future;
}

@Override
public void onHeaders(HttpJsonMetadata responseHeaders) {
Map<String, Object> headers = responseHeaders.getHeaders();

String url = HttpHeadersUtils.getFirstHeader(headers, UPLOAD_URL_HEADER);
if (Strings.isNullOrEmpty(url)) {
url = HttpHeadersUtils.getFirstHeader(headers, "Location");
}
if (!Strings.isNullOrEmpty(url)) {
this.uploadUrl = url;
}

String granularityStr = HttpHeadersUtils.getFirstHeader(headers, UPLOAD_GRANULARITY_HEADER);
if (!Strings.isNullOrEmpty(granularityStr)) {
try {
this.chunkGranularity = Long.parseLong(granularityStr);
} catch (NumberFormatException ignored) {
this.chunkGranularity = 1L;
}
}
}
Comment on lines +147 to +167

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using com.google.api.client.http.HttpHeaders and calling putAll with all response headers is highly inefficient. HttpHeaders is a heavy class that uses reflection to map header keys to class fields. Since we only need to extract a couple of specific headers case-insensitively, we can perform a direct case-insensitive lookup on the raw headers map. This avoids unnecessary object allocation and reflection overhead.

    @Override
    public void onHeaders(HttpJsonMetadata responseHeaders) {
      if (responseHeaders != null && responseHeaders.getHeaders() != null) {
        Map<String, List<String>> headers = responseHeaders.getHeaders();

        String url = getFirstHeader(headers, UPLOAD_URL_HEADER);
        if (Strings.isNullOrEmpty(url)) {
          url = getFirstHeader(headers, "Location");
        }
        if (!Strings.isNullOrEmpty(url)) {
          this.uploadUrl = url;
        }

        String granularityStr = getFirstHeader(headers, UPLOAD_GRANULARITY_HEADER);
        if (!Strings.isNullOrEmpty(granularityStr)) {
          try {
            this.chunkGranularity = Long.parseLong(granularityStr);
          } catch (NumberFormatException ignored) {
            this.chunkGranularity = 1L;
          }
        }
      }
    }

    @Nullable
    private static String getFirstHeader(Map<String, List<String>> headers, String name) {
      for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        if (entry.getKey().equalsIgnoreCase(name)) {
          List<String> values = entry.getValue();
          return values != null && !values.isEmpty() ? values.get(0) : null;
        }
      }
      return null;
    }
References
  1. When annotating a method with @Nullable, verify if any callers pass the returned value directly to APIs that do not accept nulls (such as Guava's ImmutableMap.Builder). If null checks are missing, they should be added or tracked for follow-up work.


@Override
public void onMessage(@Nullable String message) {}

Check failure on line 170 in sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpJsonResumableUploadClient.java

View check run for this annotation

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

Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AaAR6jTSEM5UgkcrX7wF&open=AaAR6jTSEM5UgkcrX7wF&pullRequest=14091

@Override
public void onClose(int statusCode, HttpJsonMetadata trailers) {
if (statusCode >= 200 && statusCode < 300) {
if (!Strings.isNullOrEmpty(uploadUrl)) {
future.set(ResumableUploadSession.create(uploadUrl, chunkGranularity));
} else {
future.setException(
new HttpJsonStatusRuntimeException(

Check warning on line 179 in sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpJsonResumableUploadClient.java

View check run for this annotation

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

Annotate the parameter with @javax.annotation.Nullable in constructor 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=AaAR6jTSEM5UgkcrX7wA&open=AaAR6jTSEM5UgkcrX7wA&pullRequest=14091
statusCode,
"Start upload response did not contain upload session URL header",
null));
}
} else {
future.setException(
trailers.getException() != null
? trailers.getException()
: new HttpJsonStatusRuntimeException(statusCode, "Failed to start upload", null));

Check warning on line 188 in sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpJsonResumableUploadClient.java

View check run for this annotation

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

Annotate the parameter with @javax.annotation.Nullable in constructor 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=AaAR6jTSEM5UgkcrX7wB&open=AaAR6jTSEM5UgkcrX7wB&pullRequest=14091
}
}
}
}
Loading
Loading