Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.orange.cepheus.cep.model.Configuration;
import com.orange.cepheus.cep.model.EventTypeIn;
import com.orange.cepheus.cep.model.Provider;
import com.orange.cepheus.cep.tenant.TenantFilter;
import com.orange.ngsi.client.NgsiClient;
import com.orange.ngsi.model.EntityId;
import com.orange.ngsi.model.SubscribeContext;
Expand Down Expand Up @@ -110,6 +111,9 @@ private void removeSubscription(String subscriptionId) {
private ScheduledFuture scheduledFuture;

private URI hostURI;

@Autowired(required=false)
TenantFilter tenantFilter;

/**
* Update subscription to new provider of the incoming events defined in the Configuration
Expand All @@ -126,6 +130,14 @@ public void setConfiguration(Configuration configuration) {

// Keep a reference to configuration for next migration
eventTypeIns = configuration.getEventTypeIns();

if(tenantFilter!=null){
for (EventTypeIn eventType : configuration.getEventTypeIns()) {
for (Provider provider : eventType.getProviders()) {
tenantFilter.getClientProviderMap().put(provider.getServiceName()+provider.getServicePath(), configuration.getService()+configuration.getServicePath());
}
}
}

// TODO : send unsubscribeContext with removedEventTypesIn

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public BadHeaderException(String message) {
* Map of all the context for each tenant, key: tenantId, a concatenation of service and servicePath
*/
private final ConcurrentMap<String, TenantScope.Context> tenantContexts = new ConcurrentHashMap<>();
private final ConcurrentMap<String, String> clientProviderMap = new ConcurrentHashMap<>();

/**
* @return the clientProviderMap
*/
public ConcurrentMap<String, String> getClientProviderMap() {
return clientProviderMap;
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
Expand All @@ -76,7 +84,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
String servicePath = getServicePath(httpServletRequest);

// Associate the tenant context to the current thread
TenantScope.storeTenantContext(getTenantContext(service, servicePath));
TenantScope.storeTenantContext(getTenantContext(service, servicePath, httpServletRequest));

// Continue request
filterChain.doFilter(servletRequest, servletResponse);
Expand Down Expand Up @@ -149,6 +157,42 @@ private TenantScope.Context getTenantContext(String service, String servicePath)
}
return tenantMap;
}

/**
* Return a tenant context for the given service / servicePath
* @param service
* @param servicePath
* @param httpServletRequest
* @return a tenant context
* @throws BadHeaderException
*/
private TenantScope.Context getTenantContext(String service, String servicePath, HttpServletRequest httpServletRequest) throws BadHeaderException {
String tenantId = tenantIdFromService(service, servicePath);

TenantScope.Context tenantMap = null;
if (!httpServletRequest.getRequestURI().startsWith("/v1/admin")) {
tenantMap = tenantContexts.get(clientProviderMap.get(tenantId));
}else{
tenantMap = tenantContexts.get(tenantId);
if (tenantMap == null) {
synchronized (this) {
tenantMap = tenantContexts.get(tenantId);
if (tenantMap == null) {
tenantMap = new TenantScope.Context();
tenantMap.put(TENANT_ID, tenantId);
if (!DEFAULT_SERVICE.equals(service)) {
tenantMap.put(FIWARE_SERVICE, service);
}
if (!DEFAULT_SERVICE_PATH.equals(servicePath)) {
tenantMap.put(FIWARE_SERVICE_PATH, servicePath);
}
tenantContexts.put(tenantId, tenantMap);
}
}
}
}
return tenantMap;
}

/**
* Extract the Fiware-Service from request
Expand Down