Skip to content

Commit dda58cd

Browse files
committed
[BAEL-13597] - Added new module : httpclient-simple, updated httpclient with new version and code
1 parent e41d832 commit dda58cd

File tree

13 files changed

+249
-31
lines changed

13 files changed

+249
-31
lines changed

httpclient-simple/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

httpclient-simple/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=========
2+
## HttpClient 4.x Cookbooks and Examples
3+
4+
###The Course
5+
The "REST With Spring" Classes: http://bit.ly/restwithspring
6+
7+
8+
### Relevant Articles:
9+
10+
- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code)

httpclient-simple/pom.xml

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>httpclient-simple</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
<name>httpclient-simple</name>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-java</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<relativePath>../parent-java</relativePath>
14+
</parent>
15+
16+
<dependencies>
17+
<!-- utils -->
18+
<dependency>
19+
<groupId>org.apache.commons</groupId>
20+
<artifactId>commons-lang3</artifactId>
21+
<version>${commons-lang3.version}</version>
22+
</dependency>
23+
<!-- http client -->
24+
<dependency>
25+
<groupId>org.apache.httpcomponents</groupId>
26+
<artifactId>httpclient</artifactId>
27+
<version>${httpclient.version}</version>
28+
<exclusions>
29+
<exclusion>
30+
<artifactId>commons-logging</artifactId>
31+
<groupId>commons-logging</groupId>
32+
</exclusion>
33+
</exclusions>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.httpcomponents</groupId>
37+
<artifactId>fluent-hc</artifactId>
38+
<version>${httpclient.version}</version>
39+
<exclusions>
40+
<exclusion>
41+
<artifactId>commons-logging</artifactId>
42+
<groupId>commons-logging</groupId>
43+
</exclusion>
44+
</exclusions>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.apache.httpcomponents</groupId>
48+
<artifactId>httpmime</artifactId>
49+
<version>${httpclient.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>commons-codec</groupId>
53+
<artifactId>commons-codec</artifactId>
54+
<version>${commons-codec.version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.apache.httpcomponents</groupId>
58+
<artifactId>httpasyncclient</artifactId>
59+
<version>${httpasyncclient.version}</version> <!-- 4.0.2 --> <!-- 4.1-beta1 -->
60+
<exclusions>
61+
<exclusion>
62+
<artifactId>commons-logging</artifactId>
63+
<groupId>commons-logging</groupId>
64+
</exclusion>
65+
</exclusions>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.github.tomakehurst</groupId>
69+
<artifactId>wiremock</artifactId>
70+
<version>${wiremock.version}</version>
71+
<scope>test</scope>
72+
</dependency>
73+
</dependencies>
74+
75+
<build>
76+
<finalName>httpclient</finalName>
77+
<resources>
78+
<resource>
79+
<directory>src/main/resources</directory>
80+
<filtering>true</filtering>
81+
</resource>
82+
</resources>
83+
</build>
84+
85+
<profiles>
86+
<profile>
87+
<id>live</id>
88+
<build>
89+
<plugins>
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-surefire-plugin</artifactId>
93+
<executions>
94+
<execution>
95+
<phase>integration-test</phase>
96+
<goals>
97+
<goal>test</goal>
98+
</goals>
99+
<configuration>
100+
<excludes>
101+
<exclude>**/*ManualTest.java</exclude>
102+
</excludes>
103+
<includes>
104+
<include>**/*LiveTest.java</include>
105+
</includes>
106+
</configuration>
107+
</execution>
108+
</executions>
109+
<configuration>
110+
<systemPropertyVariables>
111+
<test.mime>json</test.mime>
112+
</systemPropertyVariables>
113+
</configuration>
114+
</plugin>
115+
</plugins>
116+
</build>
117+
</profile>
118+
</profiles>
119+
120+
<properties>
121+
<!-- util -->
122+
<guava.version>19.0</guava.version>
123+
<commons-lang3.version>3.5</commons-lang3.version>
124+
<commons-codec.version>1.10</commons-codec.version>
125+
<httpasyncclient.version>4.1.4</httpasyncclient.version>
126+
<!-- testing -->
127+
<wiremock.version>2.5.1</wiremock.version>
128+
<httpclient.version>4.5.8</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
129+
<!-- maven plugins -->
130+
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
131+
</properties>
132+
133+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<logger name="org.springframework" level="WARN" />
11+
<logger name="org.springframework.transaction" level="WARN" />
12+
13+
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
14+
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
15+
16+
<root level="INFO">
17+
<appender-ref ref="STDOUT" />
18+
</root>
19+
</configuration>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.httpclient;
2+
3+
import org.apache.http.HttpEntity;
4+
import org.apache.http.client.methods.CloseableHttpResponse;
5+
6+
import java.io.IOException;
7+
8+
public final class ResponseUtil {
9+
private ResponseUtil() {
10+
}
11+
12+
public static void closeResponse(CloseableHttpResponse response) throws IOException {
13+
if (response == null) {
14+
return;
15+
}
16+
17+
try {
18+
final HttpEntity entity = response.getEntity();
19+
if (entity != null) {
20+
entity.getContent().close();
21+
}
22+
} finally {
23+
response.close();
24+
}
25+
}
26+
}

httpclient/src/test/java/org/baeldung/httpclient/base/HttpClientBasicLiveTest.java renamed to httpclient-simple/src/test/java/org/baeldung/httpclient/base/HttpClientBasicLiveTest.java

File renamed without changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

httpclient/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
88
### Relevant Articles:
99

1010
- [HttpClient 4 – Send Custom Cookie](http://www.baeldung.com/httpclient-4-cookies)
11-
- [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code)
1211
- [HttpClient 4 – Cancel Request](http://www.baeldung.com/httpclient-cancel-request)
1312
- [HttpClient 4 Cookbook](http://www.baeldung.com/httpclient4)
1413
- [Unshorten URLs with HttpClient](http://www.baeldung.com/unshorten-url-httpclient)

httpclient/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,10 @@
122122
<guava.version>19.0</guava.version>
123123
<commons-lang3.version>3.5</commons-lang3.version>
124124
<commons-codec.version>1.10</commons-codec.version>
125-
<httpasyncclient.version>4.1.2</httpasyncclient.version>
125+
<httpasyncclient.version>4.1.4</httpasyncclient.version>
126126
<!-- testing -->
127127
<wiremock.version>2.5.1</wiremock.version>
128-
<httpcore.version>4.4.5</httpcore.version>
129-
<httpclient.version>4.5.3</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
128+
<httpclient.version>4.5.8</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
130129
<!-- maven plugins -->
131130
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
132131
</properties>

httpclient/src/test/java/org/baeldung/httpclient/HttpAsyncClientLiveTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.junit.Assert.assertThat;
55

66
import java.io.IOException;
7-
import java.security.cert.X509Certificate;
87
import java.util.concurrent.ExecutionException;
98
import java.util.concurrent.Future;
109

@@ -18,8 +17,7 @@
1817
import org.apache.http.client.config.RequestConfig;
1918
import org.apache.http.client.methods.HttpGet;
2019
import org.apache.http.client.protocol.HttpClientContext;
21-
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
22-
import org.apache.http.conn.ssl.SSLContexts;
20+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
2321
import org.apache.http.conn.ssl.TrustStrategy;
2422
import org.apache.http.impl.client.BasicCookieStore;
2523
import org.apache.http.impl.client.BasicCredentialsProvider;
@@ -31,6 +29,7 @@
3129
import org.apache.http.nio.reactor.ConnectingIOReactor;
3230
import org.apache.http.protocol.BasicHttpContext;
3331
import org.apache.http.protocol.HttpContext;
32+
import org.apache.http.ssl.SSLContexts;
3433
import org.junit.Test;
3534

3635
public class HttpAsyncClientLiveTest {
@@ -104,7 +103,7 @@ public void whenUseSSLWithHttpAsyncClient_thenCorrect() throws Exception {
104103
final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
105104
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
106105

107-
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setSSLHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLContext(sslContext).build();
106+
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSSLContext(sslContext).build();
108107

109108
client.start();
110109
final HttpGet request = new HttpGet(HOST_WITH_SSL);

0 commit comments

Comments
 (0)