-
Notifications
You must be signed in to change notification settings - Fork 7
/
EnableContextSupportConfiguration.java
77 lines (65 loc) · 3.48 KB
/
EnableContextSupportConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.cumulocity.microservice.context.annotation;
import com.cumulocity.microservice.context.ContextService;
import com.cumulocity.microservice.context.ContextServiceImpl;
import com.cumulocity.microservice.context.credentials.Credentials;
import com.cumulocity.microservice.context.credentials.MicroserviceCredentials;
import com.cumulocity.microservice.context.credentials.UserCredentials;
import com.cumulocity.microservice.context.scope.BaseScope;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import static com.cumulocity.microservice.context.scope.BaseScope.DEFAULT_CACHE_EXPIRATION_TIMEOUT;
@Configuration
public class EnableContextSupportConfiguration {
public static final String USER_SCOPE = "user";
public static final String TENANT_SCOPE = "tenant";
@Bean
public ContextService<Credentials> credentialsContextService() {
return new ContextServiceImpl<>(Credentials.class);
}
@Bean
public ContextService<MicroserviceCredentials> microserviceContextService() {
return new ContextServiceImpl<>(MicroserviceCredentials.class);
}
@Bean
public ContextService<UserCredentials> userContextService() {
return new ContextServiceImpl<>(UserCredentials.class);
}
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public CustomScopeConfigurer contextScopeConfigurer(
final ContextService<MicroserviceCredentials> microserviceContextService,
final ContextService<UserCredentials> userContextService,
@Value("${tenantCacheExpirationTimeout:" + DEFAULT_CACHE_EXPIRATION_TIMEOUT + "}") Long cacheExpirationTimeout) {
final CustomScopeConfigurer configurer = new CustomScopeConfigurer();
// todo implement scope clearing after SubscriptionRemovedEvent
configurer.setScopes(ImmutableMap.<String, Object>builder()
.put(USER_SCOPE, new BaseScope(true) {
protected String getContextId() {
final UserCredentials context = userContextService.getContext();
return context.getTenant() + "/" + context.getUsername() + ":" + context.getPassword() + "," +
context.getAppKey() + "," +
context.getOAuthAccessToken() + ":" + context.getXsrfToken() + "," +
context.getTfaToken();
}
})
.put(TENANT_SCOPE, new BaseScope(true, cacheExpirationTimeout) {
protected String getContextId() {
final MicroserviceCredentials context = microserviceContextService.getContext();
return context.getTenant() + "/" + context.getUsername() + ":" + context.getPassword() + "," +
context.getAppKey() + "," +
context.getOAuthAccessToken() + ":" + context.getXsrfToken() + "," +
context.getTfaToken();
}
})
.build()
);
return configurer;
}
}