1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.repository.http.internal; |
21 |
|
|
22 |
|
import java.util.Map; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Singleton; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.math.NumberUtils; |
28 |
|
import org.apache.http.auth.AuthScope; |
29 |
|
import org.apache.http.auth.UsernamePasswordCredentials; |
30 |
|
import org.apache.http.client.CredentialsProvider; |
31 |
|
import org.apache.http.client.config.RequestConfig; |
32 |
|
import org.apache.http.config.SocketConfig; |
33 |
|
import org.apache.http.impl.client.BasicCredentialsProvider; |
34 |
|
import org.apache.http.impl.client.CloseableHttpClient; |
35 |
|
import org.apache.http.impl.client.HttpClientBuilder; |
36 |
|
import org.apache.http.impl.conn.BasicHttpClientConnectionManager; |
37 |
|
import org.xwiki.component.annotation.Component; |
38 |
|
import org.xwiki.extension.ExtensionManagerConfiguration; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
46 |
|
@Component |
47 |
|
@Singleton |
|
|
| 34.6% |
Uncovered Elements: 17 (26) |
Complexity: 5 |
Complexity Density: 0.25 |
|
48 |
|
public class DefaultHttpClientFactory implements HttpClientFactory |
49 |
|
{ |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@Inject |
54 |
|
private ExtensionManagerConfiguration configuration; |
55 |
|
|
|
|
| 60% |
Uncovered Elements: 4 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
56 |
109 |
@Override... |
57 |
|
public HttpClientBuilder createHttpClientBuilder(String user, String password) |
58 |
|
{ |
59 |
109 |
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); |
60 |
|
|
61 |
|
|
62 |
109 |
httpClientBuilder.useSystemProperties(); |
63 |
|
|
64 |
|
|
65 |
109 |
httpClientBuilder.setUserAgent(this.configuration.getUserAgent()); |
66 |
|
|
67 |
|
|
68 |
109 |
if (user != null) { |
69 |
0 |
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
70 |
|
|
71 |
0 |
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); |
72 |
0 |
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); |
73 |
|
} |
74 |
|
|
75 |
109 |
return httpClientBuilder; |
76 |
|
} |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
109 |
@Override... |
79 |
|
public CloseableHttpClient createClient(String user, String password) |
80 |
|
{ |
81 |
109 |
return createHttpClientBuilder(user, password).build(); |
82 |
|
} |
83 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
84 |
0 |
@Override... |
85 |
|
public HttpClientBuilder createHttpClientBuilder(Map<String, String> properties) |
86 |
|
{ |
87 |
0 |
HttpClientBuilder httpClientBuilder = |
88 |
|
createHttpClientBuilder(properties.get("auth.user"), properties.get("auth.password")); |
89 |
|
|
90 |
|
|
91 |
0 |
BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); |
92 |
0 |
SocketConfig.Builder socketConfigBuilder = SocketConfig.custom(); |
93 |
0 |
socketConfigBuilder.setSoTimeout(getIntProperty(properties, SOCKET_TIMEOUT, 30000)); |
94 |
0 |
connectionManager.setSocketConfig(socketConfigBuilder.build()); |
95 |
0 |
httpClientBuilder.setConnectionManager(connectionManager); |
96 |
|
|
97 |
|
|
98 |
0 |
RequestConfig.Builder requestBuilder = RequestConfig.custom(); |
99 |
0 |
requestBuilder = requestBuilder.setConnectTimeout(getIntProperty(properties, CONNECTION_TIMEOUT, 30000)); |
100 |
0 |
httpClientBuilder.setDefaultRequestConfig(requestBuilder.build()); |
101 |
|
|
102 |
0 |
return httpClientBuilder; |
103 |
|
} |
104 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
0 |
private int getIntProperty(Map<String, String> properties, String key, int def)... |
106 |
|
{ |
107 |
0 |
return NumberUtils.toInt(properties.get(key), def); |
108 |
|
} |
109 |
|
} |