package io.grpc; import com.google.common.base.Preconditions; import io.grpc.MethodDescriptor; import io.grpc.ServerCall; import io.grpc.ServerServiceDefinition; import io.grpc.ServiceDescriptor; import java.io.BufferedInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; /* loaded from: classes6.dex */ public final class ServerInterceptors { private ServerInterceptors() { } public static ServerServiceDefinition interceptForward(ServerServiceDefinition serverServiceDefinition, ServerInterceptor... serverInterceptorArr) { return interceptForward(serverServiceDefinition, (List) Arrays.asList(serverInterceptorArr)); } public static ServerServiceDefinition interceptForward(BindableService bindableService, ServerInterceptor... serverInterceptorArr) { return interceptForward(bindableService.bindService(), (List) Arrays.asList(serverInterceptorArr)); } public static ServerServiceDefinition interceptForward(ServerServiceDefinition serverServiceDefinition, List list) { ArrayList arrayList = new ArrayList(list); Collections.reverse(arrayList); return intercept(serverServiceDefinition, arrayList); } public static ServerServiceDefinition interceptForward(BindableService bindableService, List list) { return interceptForward(bindableService.bindService(), list); } public static ServerServiceDefinition intercept(ServerServiceDefinition serverServiceDefinition, ServerInterceptor... serverInterceptorArr) { return intercept(serverServiceDefinition, (List) Arrays.asList(serverInterceptorArr)); } public static ServerServiceDefinition intercept(BindableService bindableService, ServerInterceptor... serverInterceptorArr) { Preconditions.checkNotNull(bindableService, "bindableService"); return intercept(bindableService.bindService(), (List) Arrays.asList(serverInterceptorArr)); } public static ServerServiceDefinition intercept(ServerServiceDefinition serverServiceDefinition, List list) { Preconditions.checkNotNull(serverServiceDefinition, "serviceDef"); if (list.isEmpty()) { return serverServiceDefinition; } ServerServiceDefinition.Builder builder = ServerServiceDefinition.builder(serverServiceDefinition.getServiceDescriptor()); Iterator> it = serverServiceDefinition.getMethods().iterator(); while (it.hasNext()) { wrapAndAddMethod(builder, it.next(), list); } return builder.build(); } public static ServerServiceDefinition intercept(BindableService bindableService, List list) { Preconditions.checkNotNull(bindableService, "bindableService"); return intercept(bindableService.bindService(), list); } public static ServerServiceDefinition useInputStreamMessages(ServerServiceDefinition serverServiceDefinition) { return useMarshalledMessages(serverServiceDefinition, new MethodDescriptor.Marshaller() { // from class: io.grpc.ServerInterceptors.1 @Override // io.grpc.MethodDescriptor.Marshaller public InputStream stream(InputStream inputStream) { return inputStream; } /* JADX WARN: Can't rename method to resolve collision */ @Override // io.grpc.MethodDescriptor.Marshaller public InputStream parse(InputStream inputStream) { if (inputStream.markSupported()) { return inputStream; } if (inputStream instanceof KnownLength) { return new KnownLengthBufferedInputStream(inputStream); } return new BufferedInputStream(inputStream); } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class KnownLengthBufferedInputStream extends BufferedInputStream implements KnownLength { public KnownLengthBufferedInputStream(InputStream inputStream) { super(inputStream); } } /* JADX WARN: Multi-variable type inference failed */ public static ServerServiceDefinition useMarshalledMessages(ServerServiceDefinition serverServiceDefinition, MethodDescriptor.Marshaller marshaller) { ArrayList arrayList = new ArrayList(); ArrayList arrayList2 = new ArrayList(); for (ServerMethodDefinition serverMethodDefinition : serverServiceDefinition.getMethods()) { MethodDescriptor build = serverMethodDefinition.getMethodDescriptor().toBuilder(marshaller, marshaller).build(); arrayList2.add(build); arrayList.add(wrapMethod(serverMethodDefinition, build)); } ServiceDescriptor.Builder schemaDescriptor = ServiceDescriptor.newBuilder(serverServiceDefinition.getServiceDescriptor().getName()).setSchemaDescriptor(serverServiceDefinition.getServiceDescriptor().getSchemaDescriptor()); Iterator it = arrayList2.iterator(); while (it.hasNext()) { schemaDescriptor.addMethod((MethodDescriptor) it.next()); } ServerServiceDefinition.Builder builder = ServerServiceDefinition.builder(schemaDescriptor.build()); Iterator it2 = arrayList.iterator(); while (it2.hasNext()) { builder.addMethod((ServerMethodDefinition) it2.next()); } return builder.build(); } private static void wrapAndAddMethod(ServerServiceDefinition.Builder builder, ServerMethodDefinition serverMethodDefinition, List list) { ServerCallHandler serverCallHandler = serverMethodDefinition.getServerCallHandler(); Iterator it = list.iterator(); while (it.hasNext()) { serverCallHandler = InterceptCallHandler.create(it.next(), serverCallHandler); } builder.addMethod(serverMethodDefinition.withServerCallHandler(serverCallHandler)); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class InterceptCallHandler implements ServerCallHandler { private final ServerCallHandler callHandler; private final ServerInterceptor interceptor; public static InterceptCallHandler create(ServerInterceptor serverInterceptor, ServerCallHandler serverCallHandler) { return new InterceptCallHandler<>(serverInterceptor, serverCallHandler); } private InterceptCallHandler(ServerInterceptor serverInterceptor, ServerCallHandler serverCallHandler) { this.interceptor = (ServerInterceptor) Preconditions.checkNotNull(serverInterceptor, "interceptor"); this.callHandler = serverCallHandler; } @Override // io.grpc.ServerCallHandler public final ServerCall.Listener startCall(ServerCall serverCall, Metadata metadata) { return this.interceptor.interceptCall(serverCall, metadata, this.callHandler); } } /* JADX INFO: Access modifiers changed from: package-private */ public static ServerMethodDefinition wrapMethod(ServerMethodDefinition serverMethodDefinition, MethodDescriptor methodDescriptor) { return ServerMethodDefinition.create(methodDescriptor, wrapHandler(serverMethodDefinition.getServerCallHandler(), serverMethodDefinition.getMethodDescriptor(), methodDescriptor)); } /* JADX INFO: Access modifiers changed from: package-private */ /* JADX INFO: Add missing generic type declarations: [WRespT, WReqT] */ /* renamed from: io.grpc.ServerInterceptors$2, reason: invalid class name */ /* loaded from: classes6.dex */ public class AnonymousClass2 implements ServerCallHandler { final ServerCallHandler val$originalHandler; final MethodDescriptor val$originalMethod; final MethodDescriptor val$wrappedMethod; AnonymousClass2(MethodDescriptor methodDescriptor, MethodDescriptor methodDescriptor2, ServerCallHandler serverCallHandler) { this.val$originalMethod = methodDescriptor; this.val$wrappedMethod = methodDescriptor2; this.val$originalHandler = serverCallHandler; } @Override // io.grpc.ServerCallHandler public ServerCall.Listener startCall(ServerCall serverCall, Metadata metadata) { return new PartialForwardingServerCallListener(this, this.val$originalHandler.startCall(new PartialForwardingServerCall(this, serverCall) { // from class: io.grpc.ServerInterceptors.2.1 final AnonymousClass2 this$0; final ServerCall val$call; { this.this$0 = this; this.val$call = serverCall; } @Override // io.grpc.ServerCall public void sendMessage(ORespT orespt) { delegate().sendMessage(this.this$0.val$wrappedMethod.parseResponse(this.this$0.val$originalMethod.streamResponse(orespt))); } @Override // io.grpc.ServerCall public MethodDescriptor getMethodDescriptor() { return this.this$0.val$originalMethod; } @Override // io.grpc.PartialForwardingServerCall protected ServerCall delegate() { return this.val$call; } }, metadata)) { // from class: io.grpc.ServerInterceptors.2.2 final AnonymousClass2 this$0; final ServerCall.Listener val$originalListener; { this.this$0 = this; this.val$originalListener = r2; } @Override // io.grpc.ServerCall.Listener public void onMessage(WReqT wreqt) { delegate().onMessage(this.this$0.val$originalMethod.parseRequest(this.this$0.val$wrappedMethod.streamRequest(wreqt))); } @Override // io.grpc.PartialForwardingServerCallListener protected ServerCall.Listener delegate() { return this.val$originalListener; } }; } } private static ServerCallHandler wrapHandler(ServerCallHandler serverCallHandler, MethodDescriptor methodDescriptor, MethodDescriptor methodDescriptor2) { return new AnonymousClass2(methodDescriptor, methodDescriptor2, serverCallHandler); } }