Skip to content

Commit

Permalink
Merge branch '3.0.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Jun 26, 2023
2 parents 4f19e60 + 3857057 commit 6a09605
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 163 deletions.
7 changes: 7 additions & 0 deletions framework/rest-template/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3'
services:
httpecho:
image: 'mhalbritter/http-echo:2'
ports:
- '80'
- '443'
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ class RestTemplateApplicationAotTests {
@Test
@DisabledIfEnvironmentVariable(named = "CI", matches = "true", disabledReason = "HTTP is blocked on CI")
void httpWorks(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(output)
.hasSingleLineContaining("http: DataDto{url='http://httpbin.org/anything', method='GET'}");
});
Awaitility.await()
.atMost(Duration.ofSeconds(30))
.untilAsserted(() -> assertThat(output).hasSingleLineContaining("http worked:"));
}

@Test
void httpsWorks(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(output)
.hasSingleLineContaining("https: DataDto{url='https://httpbin.org/anything', method='GET'}");
});
Awaitility.await()
.atMost(Duration.ofSeconds(30))
.untilAsserted(() -> assertThat(output).hasSingleLineContaining("https worked:"));
}

}
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
package com.example.resttemplate;

import java.net.URI;
import java.time.Duration;

import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

@Component
@RegisterReflectionForBinding(DataDto.class)
class CLR implements CommandLineRunner {

private final RestTemplateBuilder restTemplateBuilder;
private final RestTemplate restTemplate;

CLR(RestTemplateBuilder restTemplateBuilder) {
this.restTemplateBuilder = restTemplateBuilder;
private final String host;

private final int port;

private final int tlsPort;

CLR(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
this.host = env("HTTPECHO_HOST", "localhost");
this.port = env("HTTPECHO_PORT_80", 8080);
this.tlsPort = env("HTTPECHO_PORT_443", 8443);
}

@Override
public void run(String... args) {
RestTemplate restTemplate = this.restTemplateBuilder.setConnectTimeout(Duration.ofSeconds(5))
.setReadTimeout(Duration.ofSeconds(5))
.build();
http(restTemplate);
https(restTemplate);
http();
https();
}

private void http(RestTemplate restTemplate) {
private void http() {
try {
DataDto dto = restTemplate.getForObject(URI.create("http://httpbin.org/anything"), DataDto.class);
System.out.printf("http: %s%n", dto);
String response = this.restTemplate
.getForObject(URI.create("http://%s:%d/".formatted(this.host, this.port)), String.class);
System.out.printf("http worked: %s%n", response);
}
catch (Exception ex) {
System.out.println("http failed:");
ex.printStackTrace(System.out);
}
}

private void https(RestTemplate restTemplate) {
private void https() {
try {
DataDto dto = restTemplate.getForObject(URI.create("https://httpbin.org/anything"), DataDto.class);
System.out.printf("https: %s%n", dto);
String response = this.restTemplate
.getForObject(URI.create("https://%s:%d/".formatted(this.host, this.tlsPort)), String.class);
System.out.printf("https worked: %s%n", response);
}
catch (Exception ex) {
System.out.println("https failed:");
ex.printStackTrace(System.out);
}
}

private static String env(String name, String def) {
String value = System.getenv(name);
return StringUtils.hasLength(value) ? value : def;
}

private static int env(String name, int def) {
String value = System.getenv(name);
return StringUtils.hasLength(value) ? Integer.parseInt(value) : def;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.resttemplate;

import java.time.Duration;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration(proxyBeanMethods = false)
class RestTemplateConfiguration {

@Bean
RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.requestFactory(SkipSslVerificationHttpRequestFactory::new)
.defaultHeader("User-Agent", "RestTemplateApplication")
.setConnectTimeout(Duration.ofSeconds(5))
.setReadTimeout(Duration.ofSeconds(5))
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.example.resttemplate;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.springframework.http.client.SimpleClientHttpRequestFactory;

/**
* A {@link SimpleClientHttpRequestFactory} which skips TLS checks.
*/
class SkipSslVerificationHttpRequestFactory extends SimpleClientHttpRequestFactory {

@Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
if (connection instanceof HttpsURLConnection httpsURLConnection) {
prepareHttpsConnection(httpsURLConnection);
}
super.prepareConnection(connection, httpMethod);
}

private void prepareHttpsConnection(HttpsURLConnection connection) {
connection.setHostnameVerifier(new SkipHostnameVerifier());
try {
connection.setSSLSocketFactory(createSslSocketFactory());
}
catch (Exception ex) {
// Ignore
}
}

private SSLSocketFactory createSslSocketFactory() throws Exception {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new TrustManager[] { new SkipX509TrustManager() }, new SecureRandom());
return context.getSocketFactory();
}

private static class SkipHostnameVerifier implements HostnameVerifier {

@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}

}

private static class SkipX509TrustManager implements X509TrustManager {

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

}

}
7 changes: 7 additions & 0 deletions framework/webclient/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3'
services:
httpecho:
image: 'mhalbritter/http-echo:2'
ports:
- '80'
- '443'
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,21 @@ class WebClientApplicationAotTests {
@DisabledIfEnvironmentVariable(named = "CI", matches = "true", disabledReason = "HTTP is blocked on CI")
void httpWorks(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(output)
.hasSingleLineContaining("http: DataDto{url='http://httpbin.org/anything', method='GET'}");
assertThat(output).hasSingleLineContaining("https worked:");
});
}

@Test
void httpsWorks(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(output)
.hasSingleLineContaining("https: DataDto{url='https://httpbin.org/anything', method='GET'}");
assertThat(output).hasSingleLineContaining("https worked:");
});
}

@Test
void serviceWorks(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(output)
.hasSingleLineContaining("service: ExchangeDataDto{url='https://httpbin.org/anything', method='GET'}");
assertThat(output).hasSingleLineContaining("service worked:");
});
}

Expand Down
36 changes: 18 additions & 18 deletions framework/webclient/src/main/java/com/example/webclient/CLR.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

import java.time.Duration;

import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.Builder;

@Component
@RegisterReflectionForBinding(DataDto.class)
class CLR implements CommandLineRunner {

private final Builder webClientBuilder;
private final WebClient httpWebClient;

private final WebClient httpsWebClient;

private final DataService dataService;

CLR(WebClient.Builder webClientBuilder, DataService dataService) {
this.webClientBuilder = webClientBuilder;
CLR(@Qualifier("http") WebClient httpWebClient, @Qualifier("https") WebClient httpsWebClient,
DataService dataService) {
this.httpWebClient = httpWebClient;
this.httpsWebClient = httpsWebClient;
this.dataService = dataService;
}

Expand All @@ -30,14 +32,13 @@ public void run(String... args) {

private void http() {
try {
WebClient webClient = this.webClientBuilder.baseUrl("http://httpbin.org/").build();
DataDto dto = webClient.get()
.uri("/anything")
String response = this.httpWebClient.get()
.uri("/")
.retrieve()
.bodyToMono(DataDto.class)
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(10))
.block();
System.out.printf("http: %s%n", dto);
System.out.printf("http worked: %s%n", response);
}
catch (Exception ex) {
System.out.println("http failed:");
Expand All @@ -47,14 +48,13 @@ private void http() {

private void https() {
try {
WebClient webClient = this.webClientBuilder.baseUrl("https://httpbin.org/").build();
DataDto dto = webClient.get()
.uri("/anything")
String response = this.httpsWebClient.get()
.uri("/")
.retrieve()
.bodyToMono(DataDto.class)
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(10))
.block();
System.out.printf("https: %s%n", dto);
System.out.printf("https worked: %s%n", response);
}
catch (Exception ex) {
System.out.println("https failed:");
Expand All @@ -64,8 +64,8 @@ private void https() {

private void service() {
try {
ExchangeDataDto dto = this.dataService.getData();
System.out.printf("service: %s%n", dto);
String dto = this.dataService.getData();
System.out.printf("service worked: %s%n", dto);
}
catch (Exception ex) {
System.out.println("service failed:");
Expand Down
Loading

0 comments on commit 6a09605

Please sign in to comment.