forked from spring-projects/spring-aot-smoke-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
250 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 33 additions & 18 deletions
51
framework/rest-template/src/main/java/com/example/resttemplate/CLR.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
33 changes: 0 additions & 33 deletions
33
framework/rest-template/src/main/java/com/example/resttemplate/DataDto.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
...ework/rest-template/src/main/java/com/example/resttemplate/RestTemplateConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...emplate/src/main/java/com/example/resttemplate/SkipSslVerificationHttpRequestFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.