nl.topicus.jdbc.shaded.io.grpc.ServerInterceptors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spanner-jdbc Show documentation
Show all versions of spanner-jdbc Show documentation
JDBC Driver for Google Cloud Spanner
/*
* Copyright 2014, Google Inc. All rights reserved.
*
* 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 Inc. 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 nl.topicus.jdbc.shaded.io.grpc;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Preconditions;
import java.nl.topicus.jdbc.shaded.io.BufferedInputStream;
import java.nl.topicus.jdbc.shaded.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 nl.topicus.jdbc.shaded.com.ined 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.nl.topicus.jdbc.shaded.com.grpc/grpc-java/issues/1712")
public static ServerServiceDefinition useInputStreamMessages(
final ServerServiceDefinition serviceDef) {
final MethodDescriptor.Marshaller marshaller =
new MethodDescriptor.Marshaller() {
@Override
public InputStream stream(final InputStream value) {
return value;
}
@Override
public InputStream parse(final InputStream stream) {
if (stream.markSupported()) {
return stream;
} else {
return new BufferedInputStream(stream);
}
}
};
return useMarshalledMessages(serviceDef, marshaller);
}
/**
* 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 nl.topicus.jdbc.shaded.com.ined 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.nl.topicus.jdbc.shaded.com.grpc/grpc-java/issues/1712")
public static ServerServiceDefinition useMarshalledMessages(
final ServerServiceDefinition serviceDef,
final MethodDescriptor.Marshaller marshaller) {
List> wrappedMethods =
new ArrayList>();
List> wrappedDescriptors =
new ArrayList>();
// Wrap the descriptors
for (final ServerMethodDefinition, ?> definition : serviceDef.getMethods()) {
final MethodDescriptor, ?> originalMethodDescriptor = definition.getMethodDescriptor();
final MethodDescriptor 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 void wrapAndAddMethod(
ServerServiceDefinition.Builder serviceDefBuilder, ServerMethodDefinition method,
List extends ServerInterceptor> interceptors) {
ServerCallHandler callHandler = method.getServerCallHandler();
for (ServerInterceptor interceptor : interceptors) {
callHandler = InterceptCallHandler.create(interceptor, callHandler);
}
serviceDefBuilder.addMethod(method.withServerCallHandler(callHandler));
}
private static class InterceptCallHandler implements ServerCallHandler {
public static InterceptCallHandler create(
ServerInterceptor interceptor, ServerCallHandler callHandler) {
return new InterceptCallHandler(interceptor, callHandler);
}
private final ServerInterceptor interceptor;
private final ServerCallHandler callHandler;
private InterceptCallHandler(ServerInterceptor interceptor,
ServerCallHandler callHandler) {
this.interceptor = Preconditions.checkNotNull(interceptor, "interceptor");
this.callHandler = callHandler;
}
@Override
public ServerCall.Listener startCall(
ServerCall call,
Metadata headers) {
return interceptor.interceptCall(call, headers, callHandler);
}
}
private static ServerMethodDefinition wrapMethod(
final ServerMethodDefinition definition,
final MethodDescriptor wrappedMethod) {
final ServerCallHandler wrappedHandler = wrapHandler(
definition.getServerCallHandler(),
definition.getMethodDescriptor(),
wrappedMethod);
return ServerMethodDefinition.create(wrappedMethod, wrappedHandler);
}
private static ServerCallHandler wrapHandler(
final ServerCallHandler originalHandler,
final MethodDescriptor originalMethod,
final MethodDescriptor wrappedMethod) {
return new ServerCallHandler() {
@Override
public ServerCall.Listener startCall(
final ServerCall call,
final Metadata headers) {
final ServerCall unwrappedCall =
new PartialForwardingServerCall() {
@Override
protected ServerCall 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 getMethodDescriptor() {
return originalMethod;
}
};
final ServerCall.Listener originalListener = originalHandler
.startCall(unwrappedCall, headers);
return new PartialForwardingServerCallListener() {
@Override
protected ServerCall.Listener delegate() {
return originalListener;
}
@Override
public void onMessage(WReqT message) {
final InputStream is = wrappedMethod.streamRequest(message);
final OReqT originalMessage = originalMethod.parseRequest(is);
delegate().onMessage(originalMessage);
}
};
}
};
}
}