Commit 06e48843 authored by Dmitriy Novikov's avatar Dmitriy Novikov
Browse files

Added primary TenantInfoFactory

parent 4403865f
/*
* Copyright 2022 Google LLC
* Copyright 2022 EPAM Systems, Inc
*
* 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
*
* https://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 org.opengroup.osdu.indexer.di;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* Prototype spring bean overriding @RequestScope TenantInfoFactory
*/
@Component("gcpTenantInfoFactory")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Primary
public class TenantInfoFactory implements FactoryBean<TenantInfo> {
@Autowired
private ITenantFactory tenantFactory;
@Autowired
private DpsHeaders headers;
public TenantInfoFactory() {
}
public TenantInfo getObject() throws Exception {
String id = this.headers.getPartitionIdWithFallbackToAccountId();
TenantInfo tenantInfo = this.tenantFactory.getTenantInfo(id);
if (tenantInfo == null) {
throw AppException.createUnauthorized(
String.format("could not retrieve tenant info for data partition id: %s", id));
} else {
return tenantInfo;
}
}
public Class<?> getObjectType() {
return TenantInfo.class;
}
public boolean isSingleton() {
return false;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment