All Downloads are FREE. Search and download functionalities are using the official Maven repository.

orbit.client.net.ClientAuthInterceptor.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0a19
Show newest version
/*
 Copyright (C) 2015 - 2019 Electronic Arts Inc.  All rights reserved.
 This file is part of the Orbit Project .
 See license in LICENSE.
 */

package orbit.client.net

import io.grpc.CallOptions
import io.grpc.Channel
import io.grpc.ClientCall
import io.grpc.ClientInterceptor
import io.grpc.ForwardingClientCall
import io.grpc.Metadata
import io.grpc.MethodDescriptor
import orbit.shared.proto.Headers

internal class ClientAuthInterceptor(private val localNode: LocalNode) : ClientInterceptor {
    override fun  interceptCall(
        method: MethodDescriptor,
        callOptions: CallOptions,
        next: Channel
    ): ClientCall {
        return object :
            ForwardingClientCall.SimpleForwardingClientCall(next.newCall(method, callOptions)) {
            override fun start(responseListener: Listener?, headers: Metadata) {

                val nodeId = localNode.status.nodeInfo?.id

                if (nodeId == null) {
                    headers.put(NAMESPACE, localNode.status.namespace)

                } else {
                    headers.put(NAMESPACE, nodeId.namespace)
                    headers.put(NODE_KEY, nodeId.key)
                }
                super.start(responseListener, headers)
            }
        }
    }

    companion object {
        private val NAMESPACE = Metadata.Key.of(Headers.NAMESPACE_NAME, Metadata.ASCII_STRING_MARSHALLER)
        private val NODE_KEY = Metadata.Key.of(Headers.NODE_KEY_NAME, Metadata.ASCII_STRING_MARSHALLER)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy