Skip to content

Commit 120af4c

Browse files
feat/http client factory builders
1 parent 750c059 commit 120af4c

20 files changed

Lines changed: 528 additions & 104 deletions

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 37 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<maven.compiler.source>8</maven.compiler.source>
1919
<maven.compiler.target>8</maven.compiler.target>
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<apache.httpclient.version>4.5.13</apache.httpclient.version>
22+
<jackson.databind.version>2.13.0</jackson.databind.version>
2123
</properties>
2224

2325
</project>

traceo-sdk-core/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,35 @@
1717
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1818
</properties>
1919

20+
<dependencies>
21+
<dependency>
22+
<groupId>org.apache.httpcomponents</groupId>
23+
<artifactId>httpclient</artifactId>
24+
<version>${apache.httpclient.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.fasterxml.jackson.core</groupId>
28+
<artifactId>jackson-databind</artifactId>
29+
<version>${jackson.databind.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>junit</groupId>
33+
<artifactId>junit</artifactId>
34+
<version>4.13.1</version>
35+
<scope>test</scope>
36+
</dependency>
37+
</dependencies>
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
<configuration>
44+
<source>11</source>
45+
<target>11</target>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
2051
</project>

traceo-sdk-core/src/main/java/com/traceo/sdk/builder/ClientCoreConfigurationFactory.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.traceo.sdk.converters;
2+
3+
public class StringConverter {
4+
/**
5+
* Method to convert string to header-string:
6+
* eq. Some header Key -> some-header-key
7+
* @param key
8+
* @return
9+
*/
10+
public static String convertToHeaderKey(String key) {
11+
if (key == null) {
12+
return null;
13+
}
14+
15+
String lowercaseString = key.toLowerCase();
16+
String result = lowercaseString.replaceAll("\\s+", "-");
17+
18+
return result;
19+
}
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.traceo.sdk.exceptions;
2+
3+
public class SDKException extends Exception {
4+
public SDKException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.traceo.sdk.http;
2+
3+
import org.apache.http.client.ResponseHandler;
4+
import org.apache.http.impl.client.CloseableHttpClient;
5+
import org.apache.http.protocol.HttpContext;
6+
7+
import java.io.IOException;
8+
9+
public class HttpClient {
10+
11+
public static final HttpClientFactory httpClientFactory = new HttpClientFactory();
12+
private final HttpRequestFactory httpRequestFactory = new HttpRequestFactory();
13+
14+
private final CloseableHttpClient httpClient;
15+
private final HttpClientConfiguration configuration;
16+
17+
public HttpClient() {
18+
this(new HttpClientConfiguration());
19+
}
20+
21+
public HttpClient(HttpClientConfiguration config) {
22+
this.configuration = config;
23+
this.httpClient = httpClientFactory.create(config);
24+
}
25+
26+
public void execute(IRequest<?> IRequest) throws IOException {
27+
this.httpClient.execute(httpRequestFactory.create(IRequest, this.configuration));
28+
}
29+
30+
public void execute(IRequest<?> IRequest, ResponseHandler<Void> responseHandler) throws IOException {
31+
this.httpClient.execute(httpRequestFactory.create(IRequest, this.configuration), responseHandler);
32+
}
33+
34+
public void execute(IRequest<?> IRequest, HttpContext httpContext) throws IOException {
35+
this.httpClient.execute(httpRequestFactory.create(IRequest, this.configuration), null, httpContext);
36+
}
37+
38+
public void execute(IRequest<?> IRequest, ResponseHandler<Void> responseHandler, HttpContext context) throws IOException {
39+
this.httpClient.execute(httpRequestFactory.create(IRequest, this.configuration), responseHandler, context);
40+
}
41+
42+
public void shutdown() throws IOException {
43+
httpClient.close();
44+
}
45+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.traceo.sdk.http;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class HttpClientConfiguration {
7+
private Map<String, String> headers = new HashMap<String, String>();
8+
9+
private String host = null;
10+
11+
private String userAgent = null;
12+
13+
private boolean withCompression = true;
14+
15+
private int connectionRequestTimeout = 5000;
16+
17+
private int connectionTimeout = 5000;
18+
19+
private int socketTimeout = 5000;
20+
21+
private int maxRedirects = 5;
22+
23+
// Maximum connections per route (host:port)
24+
private int maxConnPerRoute = 5;
25+
26+
// Maximum total connections in the pool
27+
private int maxConnection = 100;
28+
29+
public HttpClientConfiguration() {}
30+
31+
public HttpClientConfiguration(HttpClientConfiguration other) {
32+
this.headers.putAll(other.getHeaders());
33+
this.host = other.getHost();
34+
this.connectionRequestTimeout = other.getConnectionTimeout();
35+
this.connectionTimeout = other.getConnectionTimeout();
36+
this.socketTimeout = other.getSocketTimeout();
37+
this.maxRedirects = other.getMaxRedirects();
38+
this.maxConnection = other.getMaxConnection();
39+
this.maxConnPerRoute = other.getMaxConnPerRoute();
40+
this.userAgent = other.getUserAgent();
41+
this.withCompression = other.isWithCompression();
42+
}
43+
44+
public int getMaxConnPerRoute() {
45+
return maxConnPerRoute;
46+
}
47+
48+
public void setMaxConnPerRoute(int maxConnPerRoute) {
49+
this.maxConnPerRoute = maxConnPerRoute;
50+
}
51+
52+
public int getMaxConnection() {
53+
return maxConnection;
54+
}
55+
56+
public void setMaxConnection(int maxConnection) {
57+
this.maxConnection = maxConnection;
58+
}
59+
60+
public void setWithCompression(boolean withCompression) {
61+
this.withCompression = withCompression;
62+
}
63+
64+
public int getSocketTimeout() {
65+
return socketTimeout;
66+
}
67+
68+
public void setSocketTimeout(int socketTimeout) {
69+
this.socketTimeout = socketTimeout;
70+
}
71+
72+
public boolean isWithCompression() {
73+
return withCompression;
74+
}
75+
76+
public String getUserAgent() {
77+
return userAgent;
78+
}
79+
80+
public void setUserAgent(String userAgent) {
81+
this.userAgent = userAgent;
82+
}
83+
84+
public int getMaxRedirects() {
85+
return maxRedirects;
86+
}
87+
88+
public void setMaxRedirects(int maxRedirects) {
89+
this.maxRedirects = maxRedirects;
90+
}
91+
92+
public int getConnectionRequestTimeout() {
93+
return connectionRequestTimeout;
94+
}
95+
96+
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
97+
this.connectionRequestTimeout = connectionRequestTimeout;
98+
}
99+
100+
public int getConnectionTimeout() {
101+
return connectionTimeout;
102+
}
103+
104+
public void setConnectionTimeout(int connectionTimeout) {
105+
this.connectionTimeout = connectionTimeout;
106+
}
107+
108+
public String getHost() {
109+
return host;
110+
}
111+
112+
public void setHost(String host) {
113+
this.host = host;
114+
}
115+
116+
public void addHeader(String name, String value) {
117+
headers.put(name, value);
118+
}
119+
120+
public Map<String, String> getHeaders() {
121+
return headers;
122+
}
123+
124+
public void setHeaders(Map<String, String> headers) {
125+
this.headers = headers;
126+
}
127+
}

0 commit comments

Comments
 (0)