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

com.sitewhere.microservice.grpc.TenantTokenServerInterceptor Maven / Gradle / Ivy

The newest version!
/**
 * Copyright © 2014-2021 The SiteWhere 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 com.sitewhere.microservice.grpc;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sitewhere.spi.microservice.IFunctionIdentifier;
import com.sitewhere.spi.microservice.IMicroservice;
import com.sitewhere.spi.microservice.IMicroserviceConfiguration;

import io.grpc.Context;
import io.grpc.Contexts;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCall.Listener;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.Status;

/**
 * Pulls tenant token from call metadata and adds it to context for use in
 * implementation class.
 */
public class TenantTokenServerInterceptor implements ServerInterceptor {

    /** Static logger instance */
    private static Log LOGGER = LogFactory.getLog(TenantTokenServerInterceptor.class);

    /** Parent microservice */
    private IMicroservice microservice;

    public TenantTokenServerInterceptor(
	    IMicroservice microservice) {
	this.microservice = microservice;
    }

    /*
     * (non-Javadoc)
     * 
     * @see io.grpc.ServerInterceptor#interceptCall(io.grpc.ServerCall,
     * io.grpc.Metadata, io.grpc.ServerCallHandler)
     */
    @Override
    public  Listener interceptCall(ServerCall call, Metadata headers,
	    ServerCallHandler next) {
	if (headers.containsKey(GrpcKeys.TENANT_KEY)) {
	    String tenantToken = headers.get(GrpcKeys.TENANT_KEY);
	    LOGGER.trace("Server received tenant token key: " + tenantToken);
	    Context ctx = Context.current().withValue(GrpcKeys.TENANT_CONTEXT_KEY, tenantToken);
	    return Contexts.interceptCall(ctx, call, headers, next);
	} else {
	    call.close(Status.UNAUTHENTICATED.withDescription("Tenant token not passed in metadata."), headers);
	    return new ServerCall.Listener() {
	    };
	}
    }

    protected IMicroservice getMicroservice() {
	return microservice;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy