Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 0572eed

Browse files
authored
feat: add REST interceptors infrastructure (#1607)
The added interceptors infrastructure mimics the one from gRPC.
1 parent 935b9bb commit 0572eed

13 files changed

+703
-86
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2022 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.core.BetaApi;
33+
import javax.annotation.Nullable;
34+
35+
/**
36+
* A {@link HttpJsonClientCall} which forwards all of its methods to another {@link
37+
* HttpJsonClientCall}.
38+
*/
39+
@BetaApi
40+
public abstract class ForwardingHttpJsonClientCall<RequestT, ResponseT>
41+
extends HttpJsonClientCall<RequestT, ResponseT> {
42+
43+
protected abstract HttpJsonClientCall<RequestT, ResponseT> delegate();
44+
45+
@Override
46+
public void start(Listener<ResponseT> responseListener, HttpJsonMetadata requestHeaders) {
47+
delegate().start(responseListener, requestHeaders);
48+
}
49+
50+
@Override
51+
public void request(int numMessages) {
52+
delegate().request(numMessages);
53+
}
54+
55+
@Override
56+
public void cancel(@Nullable String message, @Nullable Throwable cause) {
57+
delegate().cancel(message, cause);
58+
}
59+
60+
@Override
61+
public void halfClose() {
62+
delegate().halfClose();
63+
}
64+
65+
@Override
66+
public void sendMessage(RequestT message) {
67+
delegate().sendMessage(message);
68+
}
69+
70+
/**
71+
* A simplified version of {@link ForwardingHttpJsonClientCall} where subclasses can pass in a
72+
* {@link HttpJsonClientCall} as the delegate.
73+
*/
74+
public abstract static class SimpleForwardingHttpJsonClientCall<RequestT, ResponseT>
75+
extends ForwardingHttpJsonClientCall<RequestT, ResponseT> {
76+
77+
private final HttpJsonClientCall<RequestT, ResponseT> delegate;
78+
79+
protected SimpleForwardingHttpJsonClientCall(HttpJsonClientCall<RequestT, ResponseT> delegate) {
80+
this.delegate = delegate;
81+
}
82+
83+
@Override
84+
protected HttpJsonClientCall<RequestT, ResponseT> delegate() {
85+
return delegate;
86+
}
87+
}
88+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2022 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+
31+
package com.google.api.gax.httpjson;
32+
33+
import com.google.api.core.BetaApi;
34+
35+
/**
36+
* A {@link HttpJsonClientCall.Listener} which forwards all of its methods to another {@link
37+
* HttpJsonClientCall.Listener}.
38+
*/
39+
@BetaApi
40+
public abstract class ForwardingHttpJsonClientCallListener<ResponseT>
41+
extends HttpJsonClientCall.Listener<ResponseT> {
42+
43+
protected abstract HttpJsonClientCall.Listener<ResponseT> delegate();
44+
45+
@Override
46+
public void onHeaders(HttpJsonMetadata responseHeaders) {
47+
delegate().onHeaders(responseHeaders);
48+
}
49+
50+
@Override
51+
public void onMessage(ResponseT message) {
52+
delegate().onMessage(message);
53+
}
54+
55+
@Override
56+
public void onClose(int statusCode, HttpJsonMetadata trailers) {
57+
delegate().onClose(statusCode, trailers);
58+
}
59+
60+
/**
61+
* A simplified version of {@link ForwardingHttpJsonClientCallListener} where subclasses can pass
62+
* in a {@link HttpJsonClientCall.Listener} as the delegate.
63+
*/
64+
public abstract static class SimpleForwardingHttpJsonClientCallListener<ResponseT>
65+
extends ForwardingHttpJsonClientCallListener<ResponseT> {
66+
67+
private final HttpJsonClientCall.Listener<ResponseT> delegate;
68+
69+
protected SimpleForwardingHttpJsonClientCallListener(
70+
HttpJsonClientCall.Listener<ResponseT> delegate) {
71+
this.delegate = delegate;
72+
}
73+
74+
@Override
75+
protected HttpJsonClientCall.Listener<ResponseT> delegate() {
76+
return delegate;
77+
}
78+
}
79+
}

‎gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpJsonChannel.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,11 @@
2929
*/
3030
package com.google.api.gax.httpjson;
3131

32-
import com.google.api.core.ApiFuture;
3332
import com.google.api.core.BetaApi;
3433

3534
/** HttpJsonChannel contains the functionality to issue http-json calls. */
3635
@BetaApi
3736
public interface HttpJsonChannel {
3837
<RequestT, ResponseT> HttpJsonClientCall<RequestT, ResponseT> newCall(
3938
ApiMethodDescriptor<RequestT, ResponseT> methodDescriptor, HttpJsonCallOptions callOptions);
40-
41-
@Deprecated
42-
<ResponseT, RequestT> ApiFuture<ResponseT> issueFutureUnaryCall(
43-
HttpJsonCallOptions callOptions,
44-
RequestT request,
45-
ApiMethodDescriptor<RequestT, ResponseT> methodDescriptor);
4639
}

‎gax-httpjson/src/main/java/com/google/api/gax/httpjson/HttpJsonClientCallImpl.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,19 @@
3434
import com.google.api.gax.httpjson.HttpRequestRunnable.ResultListener;
3535
import com.google.api.gax.httpjson.HttpRequestRunnable.RunnableResult;
3636
import com.google.common.base.Preconditions;
37-
import com.google.common.collect.ImmutableMap;
3837
import java.io.IOException;
3938
import java.io.InputStreamReader;
4039
import java.io.Reader;
4140
import java.nio.charset.StandardCharsets;
4241
import java.util.ArrayDeque;
43-
import java.util.Map;
4442
import java.util.Queue;
4543
import java.util.concurrent.CancellationException;
4644
import java.util.concurrent.Executor;
4745
import javax.annotation.Nullable;
4846
import javax.annotation.concurrent.GuardedBy;
4947

5048
/**
51-
* This class servers as main implementation of {@link HttpJsonClientCall} for rest transport and is
49+
* This class serves as main implementation of {@link HttpJsonClientCall} for REST transport and is
5250
* expected to be used for every REST call. It currently supports unary and server-streaming
5351
* workflows. The overall behavior and surface of the class mimics as close as possible behavior of
5452
* the corresponding ClientCall implementation in gRPC transport.
@@ -90,7 +88,6 @@ final class HttpJsonClientCallImpl<RequestT, ResponseT>
9088
private final ApiMethodDescriptor<RequestT, ResponseT> methodDescriptor;
9189
private final HttpTransport httpTransport;
9290
private final Executor executor;
93-
private final HttpJsonMetadata defaultHeaders;
9491

9592
//
9693
// Request-specific data (provided by client code) before we get a response.
@@ -124,15 +121,13 @@ final class HttpJsonClientCallImpl<RequestT, ResponseT>
124121
String endpoint,
125122
HttpJsonCallOptions callOptions,
126123
HttpTransport httpTransport,
127-
Executor executor,
128-
HttpJsonMetadata defaultHeaders) {
124+
Executor executor) {
129125
this.methodDescriptor = methodDescriptor;
130126
this.endpoint = endpoint;
131127
this.callOptions = callOptions;
132128
this.httpTransport = httpTransport;
133129
this.executor = executor;
134130
this.closed = false;
135-
this.defaultHeaders = defaultHeaders;
136131
}
137132

138133
@Override
@@ -164,12 +159,7 @@ public void start(Listener<ResponseT> responseListener, HttpJsonMetadata request
164159
}
165160
Preconditions.checkState(this.listener == null, "The call is already started");
166161
this.listener = responseListener;
167-
Map<String, Object> mergedHeaders =
168-
ImmutableMap.<String, Object>builder()
169-
.putAll(defaultHeaders.getHeaders())
170-
.putAll(requestHeaders.getHeaders())
171-
.build();
172-
this.requestHeaders = requestHeaders.toBuilder().setHeaders(mergedHeaders).build();
162+
this.requestHeaders = requestHeaders;
173163
}
174164
}
175165

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 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.core.BetaApi;
33+
34+
/**
35+
* Interface for intercepting outgoing calls before they are dispatched by a {@link
36+
* HttpJsonChannel}.
37+
*
38+
* <p>The interceptor may be called for multiple {@link HttpJsonClientCall calls} by one or more
39+
* threads without completing the previous ones first. The implementations must be thread-safe.
40+
*/
41+
@BetaApi
42+
public interface HttpJsonClientInterceptor {
43+
/**
44+
* Intercept {@link HttpJsonClientCall} creation by the {@code next} {@link HttpJsonChannel}.
45+
*
46+
* @param method the remote method to be called
47+
* @param callOptions the runtime options to be applied to this call
48+
* @param next the channel which is being intercepted
49+
* @return the call object for the remote operation, never {@code null}
50+
*/
51+
<ReqT, RespT> HttpJsonClientCall<ReqT, RespT> interceptCall(
52+
ApiMethodDescriptor<ReqT, RespT> method,
53+
HttpJsonCallOptions callOptions,
54+
HttpJsonChannel next);
55+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2022 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.gax.httpjson.ForwardingHttpJsonClientCall.SimpleForwardingHttpJsonClientCall;
33+
import com.google.common.collect.ImmutableMap;
34+
import java.util.Map;
35+
36+
/**
37+
* An interceptor to handle custom headers.
38+
*
39+
* <p>Package-private for internal usage.
40+
*/
41+
class HttpJsonHeaderInterceptor implements HttpJsonClientInterceptor {
42+
43+
private final Map<String, String> staticHeaders;
44+
45+
public HttpJsonHeaderInterceptor(Map<String, String> staticHeaders) {
46+
this.staticHeaders = staticHeaders;
47+
}
48+
49+
@Override
50+
public <ReqT, RespT> HttpJsonClientCall<ReqT, RespT> interceptCall(
51+
ApiMethodDescriptor<ReqT, RespT> method,
52+
final HttpJsonCallOptions callOptions,
53+
HttpJsonChannel next) {
54+
HttpJsonClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
55+
return new SimpleForwardingHttpJsonClientCall<ReqT, RespT>(call) {
56+
@Override
57+
public void start(
58+
HttpJsonClientCall.Listener<RespT> responseListener, HttpJsonMetadata headers) {
59+
Map<String, Object> mergedHeaders =
60+
ImmutableMap.<String, Object>builder()
61+
.putAll(headers.getHeaders())
62+
.putAll(staticHeaders)
63+
.build();
64+
65+
super.start(responseListener, headers.toBuilder().setHeaders(mergedHeaders).build());
66+
}
67+
};
68+
}
69+
}

0 commit comments

Comments
 (0)