Commit 6740e23f authored by Dmitriy Novikov's avatar Dmitriy Novikov
Browse files

Fixed headers

parent e48fc5c8
Pipeline #101175 failed with stages
in 7 seconds
package org.opengroup.osdu.indexer.di;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.inject.Inject;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.indexer.IElasticSettingService;
import org.opengroup.osdu.core.common.model.search.ClusterSettings;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.multitenancy.ITenantInfoService;
import org.opengroup.osdu.core.common.provider.interfaces.IElasticCredentialsCache;
import org.opengroup.osdu.core.common.provider.interfaces.IElasticRepository;
import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* Spring bean overriding @RequestScope AuditLogger
*/
@Component("gcpElasticSettingService")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Primary
@RequiredArgsConstructor
public class ElasticSettingServiceImpl implements IElasticSettingService {
@Inject
private TenantInfo tenantInfo;
@Inject
private javax.inject.Provider<ITenantInfoService> tenantInfoServiceProvider;
@Inject
private IElasticRepository elasticRepository;
@Inject
private IElasticCredentialsCache elasticCredentialCache;
@Inject
private JaxRsDpsLog log;
@Inject
private IndexerConfigurationProperties configurationProperties;
@Override
public ClusterSettings getElasticClusterInformation() {
return getClusterSettingsByTenantInfo(tenantInfo);
}
@Override
public Map<String, ClusterSettings> getAllClustersSettings() {
List<TenantInfo> tenantInfos = tenantInfoServiceProvider.get().getAllTenantInfos();
return tenantInfos.stream()
.collect(Collectors.toMap(TenantInfo::getDataPartitionId,
this::getClusterSettingsByTenantInfo));
}
private ClusterSettings getClusterSettingsByTenantInfo(TenantInfo tenantInfo) {
String cacheKey = String.format("%s-%s", configurationProperties.getGaeService(),
tenantInfo.getName());
ClusterSettings clusterInfo = (ClusterSettings) this.elasticCredentialCache.get(cacheKey);
if (clusterInfo != null) {
return clusterInfo;
}
log.warning(String.format("elastic-credential cache missed for tenant: %s",
tenantInfo.getName()));
clusterInfo = this.elasticRepository.getElasticClusterSettings(tenantInfo);
if (clusterInfo == null) {
throw new AppException(HttpStatus.SC_NOT_FOUND, "Tenant not found",
"No information about the given tenant was found");
}
this.elasticCredentialCache.put(cacheKey, clusterInfo);
return clusterInfo;
}
}
......@@ -20,8 +20,8 @@ package org.opengroup.osdu.indexer.di;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.logging.LogUtils;
import org.opengroup.osdu.core.common.logging.audit.AuditPayload;
......@@ -29,6 +29,7 @@ import org.opengroup.osdu.core.common.model.AppEngineHeaders;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.http.Request;
import org.opengroup.osdu.indexer.config.IndexerConfigProperties;
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;
......@@ -44,10 +45,10 @@ public class JaxRsDpsLog implements AutoCloseable {
private final IndexerConfigProperties properties;
private final ILogger log;
private final DpsHeaders headers;
private final ThreadLocalHeaders headers;
@Inject
public JaxRsDpsLog(IndexerConfigProperties properties, ILogger log, DpsHeaders headers) {
@Autowired
public JaxRsDpsLog(IndexerConfigProperties properties, ILogger log, ThreadLocalHeaders headers) {
this.properties = properties;
this.log = log;
this.headers = headers;
......@@ -158,7 +159,7 @@ public class JaxRsDpsLog implements AutoCloseable {
private Map<String, String> getLabels() {
Map<String, String> out;
if (headers != null) {
out = LogUtils.createStandardLabelsFromMap(headers.getHeaders());
out = LogUtils.createStandardLabelsFromMap(convertAtomicToDpsHeaders().getHeaders());
if (out.containsKey(AppEngineHeaders.TASK_RETRY_COUNT)) {
out.put(AppEngineHeaders.TASK_RETRY_COUNT,
StringUtils.join(out.get(AppEngineHeaders.TASK_RETRY_COUNT), ','));
......@@ -167,4 +168,15 @@ public class JaxRsDpsLog implements AutoCloseable {
}
return Collections.emptyMap();
}
@NotNull
private DpsHeaders convertAtomicToDpsHeaders() {
DpsHeaders dpsHeaders = new DpsHeaders();
dpsHeaders.put("data-partition-id", headers.getDataPartitionIdAtomic().get());
dpsHeaders.put("authorization", headers.getAuthorizationAtomic().get());
dpsHeaders.put("user", headers.getUserEmailAtomic().get());
dpsHeaders.put("account-id", headers.getAccountIdAtomic().get());
dpsHeaders.put("correlation-id", headers.getCorrelationIdAtomic().get());
return dpsHeaders;
}
}
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