/
ServerInterceptors.java
301 lines (273 loc) · 13.1 KB
/
ServerInterceptors.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright 2014 The gRPC Authors
*
* 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
*
* http://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 io.grpc;
import com.google.common.base.Preconditions;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Utility methods for working with {@link ServerInterceptor}s.
*/
public final class ServerInterceptors {
// Prevent instantiation
private ServerInterceptors() {}
/**
* Create a new {@code ServerServiceDefinition} whose {@link ServerCallHandler}s will call
* {@code interceptors} before calling the pre-existing {@code ServerCallHandler}. The first
* interceptor will have its {@link ServerInterceptor#interceptCall} called first.
*
* @param serviceDef the service definition for which to intercept all its methods.
* @param interceptors array of interceptors to apply to the service.
* @return a wrapped version of {@code serviceDef} with the interceptors applied.
*/
public static ServerServiceDefinition interceptForward(ServerServiceDefinition serviceDef,
ServerInterceptor... interceptors) {
return interceptForward(serviceDef, Arrays.asList(interceptors));
}
public static ServerServiceDefinition interceptForward(BindableService bindableService,
ServerInterceptor... interceptors) {
return interceptForward(bindableService.bindService(), Arrays.asList(interceptors));
}
/**
* Create a new {@code ServerServiceDefinition} whose {@link ServerCallHandler}s will call
* {@code interceptors} before calling the pre-existing {@code ServerCallHandler}. The first
* interceptor will have its {@link ServerInterceptor#interceptCall} called first.
*
* @param serviceDef the service definition for which to intercept all its methods.
* @param interceptors list of interceptors to apply to the service.
* @return a wrapped version of {@code serviceDef} with the interceptors applied.
*/
public static ServerServiceDefinition interceptForward(
ServerServiceDefinition serviceDef,
List<? extends ServerInterceptor> interceptors) {
List<? extends ServerInterceptor> copy = new ArrayList<>(interceptors);
Collections.reverse(copy);
return intercept(serviceDef, copy);
}
public static ServerServiceDefinition interceptForward(
BindableService bindableService,
List<? extends ServerInterceptor> interceptors) {
return interceptForward(bindableService.bindService(), interceptors);
}
/**
* Create a new {@code ServerServiceDefinition} whose {@link ServerCallHandler}s will call
* {@code interceptors} before calling the pre-existing {@code ServerCallHandler}. The last
* interceptor will have its {@link ServerInterceptor#interceptCall} called first.
*
* @param serviceDef the service definition for which to intercept all its methods.
* @param interceptors array of interceptors to apply to the service.
* @return a wrapped version of {@code serviceDef} with the interceptors applied.
*/
public static ServerServiceDefinition intercept(ServerServiceDefinition serviceDef,
ServerInterceptor... interceptors) {
return intercept(serviceDef, Arrays.asList(interceptors));
}
public static ServerServiceDefinition intercept(BindableService bindableService,
ServerInterceptor... interceptors) {
Preconditions.checkNotNull(bindableService, "bindableService");
return intercept(bindableService.bindService(), Arrays.asList(interceptors));
}
/**
* Create a new {@code ServerServiceDefinition} whose {@link ServerCallHandler}s will call
* {@code interceptors} before calling the pre-existing {@code ServerCallHandler}. The last
* interceptor will have its {@link ServerInterceptor#interceptCall} called first.
*
* @param serviceDef the service definition for which to intercept all its methods.
* @param interceptors list of interceptors to apply to the service.
* @return a wrapped version of {@code serviceDef} with the interceptors applied.
*/
public static ServerServiceDefinition intercept(ServerServiceDefinition serviceDef,
List<? extends ServerInterceptor> interceptors) {
Preconditions.checkNotNull(serviceDef, "serviceDef");
if (interceptors.isEmpty()) {
return serviceDef;
}
ServerServiceDefinition.Builder serviceDefBuilder
= ServerServiceDefinition.builder(serviceDef.getServiceDescriptor());
for (ServerMethodDefinition<?, ?> method : serviceDef.getMethods()) {
wrapAndAddMethod(serviceDefBuilder, method, interceptors);
}
return serviceDefBuilder.build();
}
public static ServerServiceDefinition intercept(BindableService bindableService,
List<? extends ServerInterceptor> interceptors) {
Preconditions.checkNotNull(bindableService, "bindableService");
return intercept(bindableService.bindService(), interceptors);
}
/**
* Create a new {@code ServerServiceDefinition} whose {@link MethodDescriptor} serializes to
* and from InputStream for all methods. The InputStream is guaranteed return true for
* markSupported(). The {@code ServerCallHandler} created will automatically
* convert back to the original types for request and response before calling the existing
* {@code ServerCallHandler}. Calling this method combined with the intercept methods will
* allow the developer to choose whether to intercept messages of InputStream, or the modeled
* types of their application.
*
* @param serviceDef the service definition to convert messages to InputStream
* @return a wrapped version of {@code serviceDef} with the InputStream conversion applied.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1712")
public static ServerServiceDefinition useInputStreamMessages(
final ServerServiceDefinition serviceDef) {
final MethodDescriptor.Marshaller<InputStream> marshaller =
new MethodDescriptor.Marshaller<InputStream>() {
@Override
public InputStream stream(final InputStream value) {
return value;
}
@Override
public InputStream parse(final InputStream stream) {
if (stream.markSupported()) {
return stream;
} else if (stream instanceof KnownLength) {
return new KnownLengthBufferedInputStream(stream);
} else {
return new BufferedInputStream(stream);
}
}
};
return useMarshalledMessages(serviceDef, marshaller);
}
/** {@link BufferedInputStream} that also implements {@link KnownLength}. */
private static final class KnownLengthBufferedInputStream extends BufferedInputStream
implements KnownLength {
KnownLengthBufferedInputStream(InputStream in) {
super(in);
}
}
/**
* Create a new {@code ServerServiceDefinition} whose {@link MethodDescriptor} serializes to
* and from T for all methods. The {@code ServerCallHandler} created will automatically
* convert back to the original types for request and response before calling the existing
* {@code ServerCallHandler}. Calling this method combined with the intercept methods will
* allow the developer to choose whether to intercept messages of T, or the modeled types
* of their application. This can also be chained to allow for interceptors to handle messages
* as multiple different T types within the chain if the added cost of serialization is not
* a concern.
*
* @param serviceDef the service definition to convert messages to T
* @return a wrapped version of {@code serviceDef} with the T conversion applied.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1712")
public static <T> ServerServiceDefinition useMarshalledMessages(
final ServerServiceDefinition serviceDef,
final MethodDescriptor.Marshaller<T> marshaller) {
List<ServerMethodDefinition<?, ?>> wrappedMethods =
new ArrayList<>();
List<MethodDescriptor<?, ?>> wrappedDescriptors =
new ArrayList<>();
// Wrap the descriptors
for (final ServerMethodDefinition<?, ?> definition : serviceDef.getMethods()) {
final MethodDescriptor<?, ?> originalMethodDescriptor = definition.getMethodDescriptor();
final MethodDescriptor<T, T> wrappedMethodDescriptor =
originalMethodDescriptor.toBuilder(marshaller, marshaller).build();
wrappedDescriptors.add(wrappedMethodDescriptor);
wrappedMethods.add(wrapMethod(definition, wrappedMethodDescriptor));
}
// Build the new service descriptor
final ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition
.builder(new ServiceDescriptor(serviceDef.getServiceDescriptor().getName(),
wrappedDescriptors));
// Create the new service definiton.
for (ServerMethodDefinition<?, ?> definition : wrappedMethods) {
serviceBuilder.addMethod(definition);
}
return serviceBuilder.build();
}
private static <ReqT, RespT> void wrapAndAddMethod(
ServerServiceDefinition.Builder serviceDefBuilder, ServerMethodDefinition<ReqT, RespT> method,
List<? extends ServerInterceptor> interceptors) {
ServerCallHandler<ReqT, RespT> callHandler = method.getServerCallHandler();
for (ServerInterceptor interceptor : interceptors) {
callHandler = InterceptCallHandler.create(interceptor, callHandler);
}
serviceDefBuilder.addMethod(method.withServerCallHandler(callHandler));
}
static final class InterceptCallHandler<ReqT, RespT> implements ServerCallHandler<ReqT, RespT> {
public static <ReqT, RespT> InterceptCallHandler<ReqT, RespT> create(
ServerInterceptor interceptor, ServerCallHandler<ReqT, RespT> callHandler) {
return new InterceptCallHandler<>(interceptor, callHandler);
}
private final ServerInterceptor interceptor;
private final ServerCallHandler<ReqT, RespT> callHandler;
private InterceptCallHandler(
ServerInterceptor interceptor, ServerCallHandler<ReqT, RespT> callHandler) {
this.interceptor = Preconditions.checkNotNull(interceptor, "interceptor");
this.callHandler = callHandler;
}
@Override
public ServerCall.Listener<ReqT> startCall(
ServerCall<ReqT, RespT> call,
Metadata headers) {
return interceptor.interceptCall(call, headers, callHandler);
}
}
static <OReqT, ORespT, WReqT, WRespT> ServerMethodDefinition<WReqT, WRespT> wrapMethod(
final ServerMethodDefinition<OReqT, ORespT> definition,
final MethodDescriptor<WReqT, WRespT> wrappedMethod) {
final ServerCallHandler<WReqT, WRespT> wrappedHandler = wrapHandler(
definition.getServerCallHandler(),
definition.getMethodDescriptor(),
wrappedMethod);
return ServerMethodDefinition.create(wrappedMethod, wrappedHandler);
}
private static <OReqT, ORespT, WReqT, WRespT> ServerCallHandler<WReqT, WRespT> wrapHandler(
final ServerCallHandler<OReqT, ORespT> originalHandler,
final MethodDescriptor<OReqT, ORespT> originalMethod,
final MethodDescriptor<WReqT, WRespT> wrappedMethod) {
return new ServerCallHandler<WReqT, WRespT>() {
@Override
public ServerCall.Listener<WReqT> startCall(
final ServerCall<WReqT, WRespT> call,
final Metadata headers) {
final ServerCall<OReqT, ORespT> unwrappedCall =
new PartialForwardingServerCall<OReqT, ORespT>() {
@Override
protected ServerCall<WReqT, WRespT> delegate() {
return call;
}
@Override
public void sendMessage(ORespT message) {
final InputStream is = originalMethod.streamResponse(message);
final WRespT wrappedMessage = wrappedMethod.parseResponse(is);
delegate().sendMessage(wrappedMessage);
}
@Override
public MethodDescriptor<OReqT, ORespT> getMethodDescriptor() {
return originalMethod;
}
};
final ServerCall.Listener<OReqT> originalListener = originalHandler
.startCall(unwrappedCall, headers);
return new PartialForwardingServerCallListener<WReqT>() {
@Override
protected ServerCall.Listener<OReqT> delegate() {
return originalListener;
}
@Override
public void onMessage(WReqT message) {
final InputStream is = wrappedMethod.streamRequest(message);
final OReqT originalMessage = originalMethod.parseRequest(is);
delegate().onMessage(originalMessage);
}
};
}
};
}
}