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
Expand Up @@ -25,6 +25,7 @@
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.ClientStreamingCallable;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.ResumableUploadCallable;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.cloud.location.GetLocationRequest;
Expand Down Expand Up @@ -161,6 +162,11 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {
throw new UnsupportedOperationException("Not implemented: testIamPermissionsCallable()");
}

public ResumableUploadCallable<EchoRequest, EchoResponse> resumableUploadCallable() {
throw new UnsupportedOperationException("Not implemented: resumableUploadCallable()");
}

@Override
public abstract void close();
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import com.google.api.gax.rpc.ClientStreamingCallable;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.RequestParamsBuilder;
import com.google.api.gax.rpc.ResumableUploadCallable;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.pathtemplate.PathTemplate;
import com.google.cloud.location.GetLocationRequest;
Expand Down Expand Up @@ -272,6 +274,7 @@ public class GrpcEchoStub extends EchoStub {
private final BackgroundResource backgroundResources;
private final GrpcOperationsStub operationsStub;
private final GrpcStubCallableFactory callableFactory;
private final HttpJsonEchoResumableUploadStub uploadStub;

private static final PathTemplate ECHO_0_PATH_TEMPLATE = PathTemplate.create("{header=**}");
private static final PathTemplate ECHO_1_PATH_TEMPLATE = PathTemplate.create("{routing_id=**}");
Expand Down Expand Up @@ -321,6 +324,16 @@ protected GrpcEchoStub(
this.callableFactory = callableFactory;
this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory);

if (clientContext.getCredentials() != null) {
TransportChannelProvider httpJsonProvider =
EchoStubSettings.defaultHttpJsonTransportProviderBuilder().build();
ClientContext backgroundHttpContext =
clientContext.withTransportChannelProvider(httpJsonProvider);
this.uploadStub = HttpJsonEchoResumableUploadStub.create(settings, backgroundHttpContext);
} else {
this.uploadStub = null;
}
Comment on lines +327 to +335

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

GrpcEchoStub only initializes uploadStub if clientContext.getCredentials() != null, whereas HttpJsonEchoStub initializes it unconditionally. This prevents using resumable uploads in local testing or emulator environments where credentials are null. We should initialize uploadStub unconditionally.

    TransportChannelProvider httpJsonProvider =
        EchoStubSettings.defaultHttpJsonTransportProviderBuilder().build();
    ClientContext backgroundHttpContext =
        clientContext.withTransportChannelProvider(httpJsonProvider);
    this.uploadStub = HttpJsonEchoResumableUploadStub.create(settings, backgroundHttpContext);


GrpcCallSettings<EchoRequest, EchoResponse> echoTransportSettings =
GrpcCallSettings.<EchoRequest, EchoResponse>newBuilder()
.setMethodDescriptor(echoMethodDescriptor)
Expand Down Expand Up @@ -633,10 +646,23 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {
return testIamPermissionsCallable;
}

@Override
public ResumableUploadCallable<EchoRequest, EchoResponse> resumableUploadCallable() {
if (uploadStub == null) {
throw new IllegalStateException(
"Resumable uploads require HTTP/JSON transport. Credentials are not available "
+ "on the provided gRPC channel to initialize the background HTTP client.");
}
return uploadStub.resumableUploadCallable();
}

@Override
public final void close() {
try {
backgroundResources.close();
if (uploadStub != null) {
uploadStub.close();
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
Expand All @@ -647,6 +673,9 @@ public final void close() {
@Override
public void shutdown() {
backgroundResources.shutdown();
if (uploadStub != null) {
uploadStub.shutdown();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.showcase.v1beta1.stub;

import com.google.api.core.BetaApi;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.core.BackgroundResourceAggregation;
import com.google.api.gax.httpjson.ApiMethodDescriptor;
import com.google.api.gax.httpjson.HttpJsonCallSettings;
import com.google.api.gax.httpjson.HttpJsonCallableFactory;
import com.google.api.gax.httpjson.ProtoMessageRequestFormatter;
import com.google.api.gax.httpjson.ProtoMessageResponseParser;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.ResumableUploadCallable;
import com.google.showcase.v1beta1.EchoRequest;
import com.google.showcase.v1beta1.EchoResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import javax.annotation.Generated;

// AUTO-GENERATED DOCUMENTATION AND CLASS.
/**
* REST stub implementation for resumable uploads in the Echo service API.
*
* <p>This stub contains only the Scotty resumable upload RPCs and is meant to be instantiated
* inside both the standard gRPC and HTTP/JSON stubs without incurring the overhead of
* loading all standard RPC callables.
*/
@BetaApi
@Generated("by gapic-generator-java")
public class HttpJsonEchoResumableUploadStub extends EchoStub {
private static final ApiMethodDescriptor<EchoRequest, EchoResponse>
echoResumableUploadMethodDescriptor =
ApiMethodDescriptor.<EchoRequest, EchoResponse>newBuilder()
.setFullMethodName("google.showcase.v1beta1.Echo/EchoResumableUpload")
.setHttpMethod("POST")
.setType(ApiMethodDescriptor.MethodType.UNARY)
.setRequestFormatter(
ProtoMessageRequestFormatter.<EchoRequest>newBuilder()
.setPath("/v1beta1/echo:resumable", request -> new HashMap<>())
.build())
.setResponseParser(
ProtoMessageResponseParser.<EchoResponse>newBuilder()
.setDefaultInstance(EchoResponse.getDefaultInstance())
.build())
.build();

private final ResumableUploadCallable<EchoRequest, EchoResponse> resumableUploadCallable;
private final BackgroundResource backgroundResources;

public static final HttpJsonEchoResumableUploadStub create(
EchoStubSettings settings, ClientContext clientContext) throws IOException {
return new HttpJsonEchoResumableUploadStub(settings, clientContext);
}

protected HttpJsonEchoResumableUploadStub(EchoStubSettings settings, ClientContext clientContext)
throws IOException {
this.backgroundResources =
new BackgroundResourceAggregation(
Collections.singletonList(clientContext.getTransportChannel()));

HttpJsonCallSettings<EchoRequest, EchoResponse> resumableUploadTransportSettings =
HttpJsonCallSettings.<EchoRequest, EchoResponse>newBuilder()
.setMethodDescriptor(echoResumableUploadMethodDescriptor)
.build();
this.resumableUploadCallable =
HttpJsonCallableFactory.createResumableUploadCallable(
resumableUploadTransportSettings, clientContext);
}

@Override
public ResumableUploadCallable<EchoRequest, EchoResponse> resumableUploadCallable() {
return resumableUploadCallable;
}

@Override
public final void close() {
try {
backgroundResources.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException("Failed to close resource", e);
}
}

@Override
public void shutdown() {
backgroundResources.shutdown();
}

@Override
public boolean isShutdown() {
return backgroundResources.isShutdown();
}

@Override
public boolean isTerminated() {
return backgroundResources.isTerminated();
}

@Override
public void shutdownNow() {
backgroundResources.shutdownNow();
}

@Override
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
return backgroundResources.awaitTermination(duration, unit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.api.gax.rpc.ClientStreamingCallable;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.RequestParamsBuilder;
import com.google.api.gax.rpc.ResumableUploadCallable;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.pathtemplate.PathTemplate;
Expand Down Expand Up @@ -611,6 +612,7 @@ public class HttpJsonEchoStub extends EchoStub {
private final UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable;
private final UnaryCallable<TestIamPermissionsRequest, TestIamPermissionsResponse>
testIamPermissionsCallable;
private final HttpJsonEchoResumableUploadStub uploadStub;

private final BackgroundResource backgroundResources;
private final HttpJsonOperationsStub httpJsonOperationsStub;
Expand Down Expand Up @@ -886,6 +888,8 @@ protected HttpJsonEchoStub(
settings.testIamPermissionsSettings(),
clientContext);

this.uploadStub = HttpJsonEchoResumableUploadStub.create(settings, clientContext);

this.backgroundResources =
new BackgroundResourceAggregation(clientContext.getBackgroundResources());
}
Expand Down Expand Up @@ -1010,6 +1014,11 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {
return testIamPermissionsCallable;
}

@Override
public ResumableUploadCallable<EchoRequest, EchoResponse> resumableUploadCallable() {
return uploadStub.resumableUploadCallable();
}

@Override
public ClientStreamingCallable<EchoRequest, EchoResponse> collectCallable() {
throw new UnsupportedOperationException(
Expand All @@ -1027,6 +1036,9 @@ public BidiStreamingCallable<EchoRequest, EchoResponse> chatCallable() {
public final void close() {
try {
backgroundResources.close();
if (uploadStub != null) {
uploadStub.close();
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
Comment on lines 1036 to 1044

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

HttpJsonEchoStub shares its primary transport channel with uploadStub. When HttpJsonEchoStub.close() is called, it closes the primary channel via backgroundResources.close(), and then calls uploadStub.close(), which closes the same primary channel again. This double-close of the primary transport channel is redundant and can be avoided by not calling uploadStub.close() in HttpJsonEchoStub.close().

  public final void close() {
    try {
      backgroundResources.close();
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new IllegalStateException("Failed to close resource", e);
    }

Expand All @@ -1037,6 +1049,9 @@ public final void close() {
@Override
public void shutdown() {
backgroundResources.shutdown();
if (uploadStub != null) {
uploadStub.shutdown();
}
}

@Override
Expand Down
Loading
Loading