Skip to content
Snippets Groups Projects
Commit ecd368cb authored by Jörg Bolay's avatar Jörg Bolay
Browse files

KOP-2964 umstellen von WebCLient auf RestClient

parent 5d79aae5
No related branches found
No related tags found
1 merge request!10KOP-2964 umstellen von WebClient auf RestClient und entfernen von...
Pipeline #1519 failed
......@@ -52,6 +52,17 @@
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>3.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
......
package de.ozgcloud.nachrichten.postfach.osiv2.config;
import jakarta.annotation.PostConstruct;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
......@@ -11,15 +12,21 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.client.*;
import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequestEntityConverter;
import org.springframework.security.oauth2.client.endpoint.*;
import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.ClientRegistrations;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.client.OAuth2ClientHttpRequestInterceptor;
import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClient;
import de.ozgcloud.nachrichten.postfach.osiv2.gen.ApiClient;
......@@ -34,6 +41,7 @@ public class ApiClientConfiguration {
private final OsiPostfachProperties.ApiConfiguration apiConfiguration;
private final OsiPostfachProperties.ProxyConfiguration proxyConfiguration;
private static final String RESOURCE_HEADER = "resource";
@Bean
MessageExchangeApi messageExchangeApi(ApiClient apiClient) {
......@@ -42,15 +50,117 @@ public class ApiClientConfiguration {
@Bean
ApiClient apiClient(OAuth2AuthorizedClientManager authorizedClientManager){
var apiClient = new ApiClient(restClient(authorizedClientManager));
apiClient.setBasePath(apiConfiguration.getUrl());
return apiClient;
}
@Bean
public RestClient restClient(OAuth2AuthorizedClientManager authorizedClientManager) {
OAuth2ClientHttpRequestInterceptor requestInterceptor =
new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
return RestClient.builder()
.requestInterceptor(requestInterceptor)
.
.build();
}
private RestClient restClient;
@PostConstruct
void initialize() {
this.restClient = RestClient.builder()
.messageConverters((messageConverters) -> {
messageConverters.clear();
messageConverters.add(new FormHttpMessageConverter());
// messageConverters.add(new JsonbHttpMessageConverter());
messageConverters.add(new OAuth2AccessTokenResponseHttpMessageConverter());
})
.defaultStatusHandler(new OAuth2ErrorResponseErrorHandler())
// TODO: Customize the instance of RestClient as needed...
.build();
}
@Bean
public OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authorizationCodeAccessTokenResponseClient() {
RestClientAuthorizationCodeTokenResponseClient accessTokenResponseClient =
new RestClientAuthorizationCodeTokenResponseClient();
accessTokenResponseClient.setRestClient(this.restClient);
return accessTokenResponseClient;
}
RestClient restClient = RestClient.builder()
.requestFactory(createProxyRequestFactory())
.requestInterceptor(createOAuth2Interceptor(authorizedClientManager))
.baseUrl(apiConfiguration.getUrl()).build();
@Bean
public OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenAccessTokenResponseClient() {
RestClientRefreshTokenTokenResponseClient accessTokenResponseClient =
new RestClientRefreshTokenTokenResponseClient();
accessTokenResponseClient.setRestClient(this.restClient);
return accessTokenResponseClient;
}
@Bean
public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsAccessTokenResponseClient() {
RestClientClientCredentialsTokenResponseClient accessTokenResponseClient =
new RestClientClientCredentialsTokenResponseClient();
accessTokenResponseClient.setRestClient(this.restClient);
return new ApiClient(restClient);
return accessTokenResponseClient;
}
@Bean
public OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> passwordAccessTokenResponseClient() {
return (grantRequest) -> {
throw new UnsupportedOperationException("The `password` grant type is not supported.");
};
}
@Bean
public OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerAccessTokenResponseClient() {
RestClientJwtBearerTokenResponseClient accessTokenResponseClient =
new RestClientJwtBearerTokenResponseClient();
accessTokenResponseClient.setRestClient(this.restClient);
return accessTokenResponseClient;
}
@Bean
public OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> tokenExchangeAccessTokenResponseClient() {
RestClientTokenExchangeTokenResponseClient accessTokenResponseClient =
new RestClientTokenExchangeTokenResponseClient();
accessTokenResponseClient.setRestClient(this.restClient);
return accessTokenResponseClient;
}
// @Bean
// RestClient restClient(OAuth2AuthorizedClientManager authorizedClientManager) {
//
// RestClient restClient = RestClient.builder()
// .requestFactory(createProxyRequestFactory())
// .requestInterceptor(createOAuth2Interceptor(authorizedClientManager))
// .baseUrl(apiConfiguration.getUrl())
//// .defaultHeader(RESOURCE_HEADER, apiConfiguration.getResource())
// .build();
// return restClient;
// }
// @Bean
// public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsAccessTokenResponseClient(RestClient restClient) {
// RestClientClientCredentialsTokenResponseClient accessTokenResponseClient =
// new RestClientClientCredentialsTokenResponseClient();
// accessTokenResponseClient.setRestClient(restClient);
//
// return accessTokenResponseClient;
// }
private ClientHttpRequestFactory createProxyRequestFactory(){
var requestFactory = new HttpComponentsClientHttpRequestFactory();
if(proxyConfiguration.isEnabled()){
......@@ -59,7 +169,7 @@ public class ApiClientConfiguration {
new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort()),
//TODO: hier brauchen wir noch eine Ordentliche Lösung
// new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword().toCharArray())
new UsernamePasswordCredentials("bla", "blub".toCharArray())
new UsernamePasswordCredentials("", "".toCharArray())
);
var httpClient = HttpClientBuilder.create()
.setProxy(new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort()))
......
......@@ -17,6 +17,7 @@ import java.util.Objects;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
......@@ -75,6 +76,7 @@ public class OsiPostfachRemoteServiceITCase {
postfachFacadeMockClient = OSI_MOCK_SERVER_EXTENSION.getPostfachFacadeMockClient();
}
// @Disabled
@DisplayName("should send dummy request with jwt")
@Test
@SneakyThrows
......@@ -87,6 +89,7 @@ public class OsiPostfachRemoteServiceITCase {
))
);
osiPostfachRemoteService.sendMessage(postfachNachricht);
var requests = postfachFacadeMockClient.retrieveRecordedRequests(request());
......@@ -100,6 +103,7 @@ public class OsiPostfachRemoteServiceITCase {
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
@Disabled
@DisplayName("should receive one messages")
@Test
@SneakyThrows
......@@ -116,6 +120,7 @@ public class OsiPostfachRemoteServiceITCase {
assertThat(messageList).size().isEqualTo(1);
}
@Disabled
@DisplayName("should receive two messages")
@Test
@SneakyThrows
......@@ -165,6 +170,7 @@ public class OsiPostfachRemoteServiceITCase {
createMessagesCall("getMessage", messageJson);
}
@Disabled
@DisplayName("should delete message")
@Test
@SneakyThrows
......
......@@ -23,7 +23,7 @@ import de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachAddressTestFactory
import de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory;
@SpringBootTest(classes = TestApplication.class)
@ActiveProfiles("itcase")
@ActiveProfiles("local")
@EnabledIfEnvironmentVariable(named = "SH_STAGE_CLIENT_SECRET", matches = ".+")
public class OsiPostfachRemoteServiceRemoteITCase {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment