Skip to content

Commit 88f5536

Browse files
authored
Merge pull request #835 from commercetools/example_regions
[DEVX-495] make serviceregion configurable in examples
2 parents 3754a18 + e9ad7ac commit 88f5536

File tree

21 files changed

+144
-82
lines changed

21 files changed

+144
-82
lines changed

examples/maven-okhttp3/src/test/java/com/commercetools/sdk/examples/GettingStarted.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.junit.jupiter.api.Assertions;
99
import org.junit.jupiter.api.Test;
1010

11+
import java.util.Optional;
12+
1113
public class GettingStarted {
1214

1315
@Test
@@ -16,7 +18,7 @@ public void project() {
1618
ClientCredentials.of().withClientId(System.getenv("CTP_CLIENT_ID"))
1719
.withClientSecret(System.getenv("CTP_CLIENT_SECRET"))
1820
.build(),
19-
ServiceRegion.GCP_EUROPE_WEST1).build(System.getenv("CTP_PROJECT_KEY"));
21+
ServiceRegion.valueOf(Optional.ofNullable(System.getenv("CTP_SERVICE_REGION")).orElse("GCP_EUROPE_WEST1"))).build(System.getenv("CTP_PROJECT_KEY"));
2022

2123
Project response = apiRoot
2224
.get()

examples/maven-okhttp4/src/test/java/com/commercetools/sdk/examples/GettingStarted.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.junit.jupiter.api.Assertions;
99
import org.junit.jupiter.api.Test;
1010

11+
import java.util.Optional;
12+
1113
public class GettingStarted {
1214

1315
@Test
@@ -16,7 +18,7 @@ public void project() {
1618
ClientCredentials.of().withClientId(System.getenv("CTP_CLIENT_ID"))
1719
.withClientSecret(System.getenv("CTP_CLIENT_SECRET"))
1820
.build(),
19-
ServiceRegion.GCP_EUROPE_WEST1).build(System.getenv("CTP_PROJECT_KEY"));
21+
ServiceRegion.valueOf(Optional.ofNullable(System.getenv("CTP_SERVICE_REGION")).orElse("GCP_EUROPE_WEST1"))).build(System.getenv("CTP_PROJECT_KEY"));
2022

2123
Project response = apiRoot
2224
.get()

examples/spring-datadog-statsd/src/main/java/com/commercetools/sdk/examples/springmvc/config/CtpSecurityConfig.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
77
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
88
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9-
import org.springframework.security.config.web.server.ServerHttpSecurity;
109
import org.springframework.security.web.SecurityFilterChain;
11-
import org.springframework.security.web.server.SecurityWebFilterChain;
12-
import org.springframework.security.web.server.context.ServerSecurityContextRepository;
13-
import org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository;
1410

1511
@Configuration
1612
@EnableWebSecurity
@@ -19,8 +15,7 @@ public class CtpSecurityConfig {
1915
@Bean
2016
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
2117
http
22-
.anonymous()
23-
.and()
18+
.anonymous(anonymous -> anonymous.authorities("ROLE_ANON"))
2419
.authorizeHttpRequests((requests) -> requests
2520
.requestMatchers("**").permitAll()
2621
.requestMatchers("/resources/**").permitAll()

examples/spring-datadog-statsd/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.commercetools.api.client.ProjectScopedApiRoot;
55
import com.commercetools.api.defaultconfig.ApiRootBuilder;
6+
import com.commercetools.api.defaultconfig.ServiceRegion;
67
import com.commercetools.monitoring.datadog.statsd.DatadogMiddleware;
78
import com.commercetools.monitoring.datadog.statsd.DatadogResponseSerializer;
89
import com.timgroup.statsd.NonBlockingStatsDClientBuilder;
@@ -15,6 +16,8 @@
1516
import org.springframework.context.annotation.Bean;
1617
import org.springframework.context.annotation.Configuration;
1718

19+
import java.util.Optional;
20+
1821
@Configuration
1922
@EnableAutoConfiguration
2023
public class CtpClientBeanService {
@@ -28,18 +31,26 @@ public class CtpClientBeanService {
2831
@Value(value = "${ctp.project.key}")
2932
private String projectKey;
3033

34+
@Value(value = "${ctp.service.region:#{null}}")
35+
private String serviceRegion;
36+
3137
private ClientCredentials credentials() {
3238
return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build();
3339
}
3440

41+
@Bean
42+
public ServiceRegion serviceRegion() {
43+
return ServiceRegion.valueOf(Optional.ofNullable(this.serviceRegion).orElse("GCP_EUROPE_WEST1"));
44+
}
45+
3546
@Bean
3647
public ProjectScopedApiRoot apiRoot() {
3748
StatsDClient statsd = new NonBlockingStatsDClientBuilder()
3849
.hostname("localhost")
3950
.build();
4051

4152
ApiRootBuilder builder = ApiRootBuilder.of()
42-
.defaultClient(credentials())
53+
.defaultClient(credentials(), serviceRegion())
4354
.withSerializer(new DatadogResponseSerializer(ResponseSerializer.of(), statsd))
4455
.withTelemetryMiddleware(new DatadogMiddleware(statsd));
4556
return builder.build(projectKey);

examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/CtpSecurityConfig.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public static class CtpReactiveAuthenticationManagerResolver
9494
implements ReactiveAuthenticationManagerResolver<ServerWebExchange> {
9595
private final ApiHttpClient apiHttpClient;
9696

97+
private final ServiceRegion serviceRegion;
98+
9799
@Value(value = "${ctp.client.id}")
98100
private String clientId;
99101

@@ -108,23 +110,24 @@ private ClientCredentials credentials() {
108110
}
109111

110112
@Autowired
111-
public CtpReactiveAuthenticationManagerResolver(final ApiHttpClient apiHttpClient) {
113+
public CtpReactiveAuthenticationManagerResolver(final ApiHttpClient apiHttpClient, final ServiceRegion serviceRegion) {
112114
this.apiHttpClient = apiHttpClient;
115+
this.serviceRegion = serviceRegion;
113116
}
114117

115118
@Override
116119
public Mono<ReactiveAuthenticationManager> resolve(final ServerWebExchange context) {
117120
return Mono.just(new CtpReactiveAuthenticationManager(meClient(apiHttpClient, context.getSession()),
118-
credentials(), projectKey));
121+
credentials(), projectKey, serviceRegion));
119122
}
120123

121124
private ProjectApiRoot meClient(final ApiHttpClient client, final Mono<WebSession> session) {
122125
TokenStorage storage = new SessionTokenStorage(session);
123126

124127
ApiRootBuilder builder = ApiRootBuilder.of(client)
125-
.withApiBaseUrl(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
128+
.withApiBaseUrl(serviceRegion.getApiUrl())
126129
.withProjectKey(projectKey)
127-
.withAnonymousRefreshFlow(credentials(), ServiceRegion.GCP_EUROPE_WEST1, storage);
130+
.withAnonymousRefreshFlow(credentials(), serviceRegion, storage);
128131

129132
return builder.build(projectKey);
130133
}

examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/MeClientFilter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ public class MeClientFilter implements WebFilter {
3535
@Value(value = "${ctp.project.key}")
3636
private String projectKey;
3737

38+
private final ServiceRegion serviceRegion;
39+
3840
private ClientCredentials credentials() {
3941
return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build();
4042
}
4143

4244
@Autowired
43-
public MeClientFilter(ApiHttpClient client) {
45+
public MeClientFilter(final ApiHttpClient client, final ServiceRegion serviceRegion) {
4446
this.client = client;
47+
this.serviceRegion = serviceRegion;
4548
}
4649

4750
@Override
@@ -59,9 +62,9 @@ private ProjectApiRoot meClient(ApiHttpClient client, Mono<WebSession> session)
5962
TokenStorage storage = new SessionTokenStorage(session);
6063

6164
ApiRootBuilder builder = ApiRootBuilder.of(client)
62-
.withApiBaseUrl(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
65+
.withApiBaseUrl(serviceRegion.getApiUrl())
6366
.withProjectKey(projectKey)
64-
.withAnonymousRefreshFlow(credentials(), ServiceRegion.GCP_EUROPE_WEST1, storage)
67+
.withAnonymousRefreshFlow(credentials(), serviceRegion, storage)
6568
.withTelemetryMiddleware(new DatadogMiddleware(ApiClient.getDefaultApiClient()))
6669
.withSerializer(new DatadogResponseSerializer(ResponseSerializer.of(), ApiClient.getDefaultApiClient()));
6770

examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.commercetools.api.client.ProjectApiRoot;
44
import com.commercetools.api.defaultconfig.ApiRootBuilder;
5+
import com.commercetools.api.defaultconfig.ServiceRegion;
56
import com.commercetools.monitoring.datadog.DatadogMiddleware;
67
import com.commercetools.monitoring.datadog.DatadogResponseSerializer;
78
import com.datadog.api.client.ApiClient;
@@ -15,6 +16,8 @@
1516
import org.springframework.context.annotation.Bean;
1617
import org.springframework.context.annotation.Configuration;
1718

19+
import java.util.Optional;
20+
1821
@Configuration
1922
@EnableAutoConfiguration
2023
public class CtpClientBeanService {
@@ -28,14 +31,22 @@ public class CtpClientBeanService {
2831
@Value(value = "${ctp.project.key}")
2932
private String projectKey;
3033

34+
@Value(value = "${ctp.service.region:#{null}}")
35+
private String serviceRegion;
36+
37+
@Bean
38+
public ServiceRegion serviceRegion() {
39+
return ServiceRegion.valueOf(Optional.ofNullable(this.serviceRegion).orElse("GCP_EUROPE_WEST1"));
40+
}
41+
3142
private ClientCredentials credentials() {
3243
return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build();
3344
}
3445

3546
@Bean
3647
public ApiHttpClient client() {
3748
ApiRootBuilder builder = ApiRootBuilder.of()
38-
.defaultClient(credentials())
49+
.defaultClient(credentials(), serviceRegion())
3950
.withTelemetryMiddleware(new DatadogMiddleware(ApiClient.getDefaultApiClient()))
4051
.withSerializer(new DatadogResponseSerializer(ResponseSerializer.of(), ApiClient.getDefaultApiClient()));
4152
return builder.buildClient();

examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpReactiveAuthenticationManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import reactor.core.publisher.Mono;
2525

2626
public class CtpReactiveAuthenticationManager implements ReactiveAuthenticationManager {
27+
private final ServiceRegion serviceRegion;
2728
VrapHttpClient client;
2829
ProjectApiRoot apiRoot;
2930

@@ -32,11 +33,12 @@ public class CtpReactiveAuthenticationManager implements ReactiveAuthenticationM
3233
private final String projectKey;
3334

3435
public CtpReactiveAuthenticationManager(final ProjectApiRoot apiRoot, final ClientCredentials credentials,
35-
final String projectKey) {
36+
final String projectKey, final ServiceRegion serviceRegion) {
3637
this.apiRoot = apiRoot;
3738
this.client = HttpClientSupplier.of().get();
3839
this.credentials = credentials;
3940
this.projectKey = projectKey;
41+
this.serviceRegion = serviceRegion;
4042
}
4143

4244
@Override
@@ -56,7 +58,7 @@ public Mono<Authentication> authenticate(Authentication authentication) {
5658
GlobalCustomerPasswordTokenSupplier supplier = new GlobalCustomerPasswordTokenSupplier(
5759
credentials.getClientId(), credentials.getClientSecret(), authentication.getName(),
5860
authentication.getCredentials().toString(), null,
59-
ServiceRegion.GCP_EUROPE_WEST1.getPasswordFlowTokenURL(projectKey), client);
61+
serviceRegion.getPasswordFlowTokenURL(projectKey), client);
6062

6163
return Mono.zip(Mono.fromFuture(supplier.getToken().exceptionally(throwable -> null)),
6264
Mono.just(customerSignInResultApiHttpResponse.getBody()));

examples/spring-dynatrace-oneagent/src/main/java/com/commercetools/sdk/examples/springmvc/config/CtpSecurityConfig.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
77
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
88
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9-
import org.springframework.security.config.web.server.ServerHttpSecurity;
109
import org.springframework.security.web.SecurityFilterChain;
11-
import org.springframework.security.web.server.SecurityWebFilterChain;
12-
import org.springframework.security.web.server.context.ServerSecurityContextRepository;
13-
import org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository;
1410

1511
@Configuration
1612
@EnableWebSecurity
@@ -19,8 +15,7 @@ public class CtpSecurityConfig {
1915
@Bean
2016
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
2117
http
22-
.anonymous()
23-
.and()
18+
.anonymous(anonymous -> anonymous.authorities("ROLE_ANON"))
2419
.authorizeHttpRequests((requests) -> requests
2520
.requestMatchers("**").permitAll()
2621
.requestMatchers("/resources/**").permitAll()

examples/spring-dynatrace-oneagent/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
import com.commercetools.api.client.ProjectScopedApiRoot;
55
import com.commercetools.api.defaultconfig.ApiRootBuilder;
6+
import com.commercetools.api.defaultconfig.ServiceRegion;
67
import io.vrap.rmf.base.client.oauth2.ClientCredentials;
78

89
import org.springframework.beans.factory.annotation.Value;
910
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
1011
import org.springframework.context.annotation.Bean;
1112
import org.springframework.context.annotation.Configuration;
1213

14+
import java.util.Optional;
15+
1316
@Configuration
1417
@EnableAutoConfiguration
1518
public class CtpClientBeanService {
@@ -23,14 +26,22 @@ public class CtpClientBeanService {
2326
@Value(value = "${ctp.project.key}")
2427
private String projectKey;
2528

29+
@Value(value = "${ctp.service.region:#{null}}")
30+
private String serviceRegion;
31+
2632
private ClientCredentials credentials() {
2733
return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build();
2834
}
2935

36+
@Bean
37+
public ServiceRegion serviceRegion() {
38+
return ServiceRegion.valueOf(Optional.ofNullable(this.serviceRegion).orElse("GCP_EUROPE_WEST1"));
39+
}
40+
3041
@Bean
3142
public ProjectScopedApiRoot apiRoot() {
3243
ApiRootBuilder builder = ApiRootBuilder.of()
33-
.defaultClient(credentials());
44+
.defaultClient(credentials(), serviceRegion());
3445
return builder.build(projectKey);
3546
}
3647
}

0 commit comments

Comments
 (0)