1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.transformation.linkchecker; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
import javax.inject.Singleton; |
24 |
|
|
25 |
|
import org.apache.http.client.config.CookieSpecs; |
26 |
|
import org.apache.http.client.config.RequestConfig; |
27 |
|
import org.apache.http.client.methods.CloseableHttpResponse; |
28 |
|
import org.apache.http.client.methods.HttpGet; |
29 |
|
import org.apache.http.impl.client.CloseableHttpClient; |
30 |
|
import org.apache.http.impl.client.HttpClientBuilder; |
31 |
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
32 |
|
import org.slf4j.Logger; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.component.phase.Initializable; |
35 |
|
import org.xwiki.component.phase.InitializationException; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
43 |
|
@Component |
44 |
|
@Singleton |
|
|
| 96.2% |
Uncovered Elements: 1 (26) |
Complexity: 5 |
Complexity Density: 0.23 |
|
45 |
|
public class DefaultHTTPChecker implements HTTPChecker, Initializable |
46 |
|
{ |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@Inject |
51 |
|
private Logger logger; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private CloseableHttpClient httpClient; |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
61 |
1 |
@Override... |
62 |
|
public void initialize() throws InitializationException |
63 |
|
{ |
64 |
1 |
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); |
65 |
|
|
66 |
|
|
67 |
1 |
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); |
68 |
1 |
httpClientBuilder.setConnectionManager(connectionManager); |
69 |
|
|
70 |
|
|
71 |
1 |
httpClientBuilder.useSystemProperties(); |
72 |
|
|
73 |
|
|
74 |
1 |
httpClientBuilder.setUserAgent("XWikiLinkChecker"); |
75 |
|
|
76 |
|
|
77 |
1 |
RequestConfig config = RequestConfig.custom() |
78 |
|
.setCookieSpec(CookieSpecs.IGNORE_COOKIES) |
79 |
|
.build(); |
80 |
1 |
httpClientBuilder.setDefaultRequestConfig(config); |
81 |
|
|
82 |
1 |
this.httpClient = httpClientBuilder.build(); |
83 |
|
} |
84 |
|
|
|
|
| 93.8% |
Uncovered Elements: 1 (16) |
Complexity: 4 |
Complexity Density: 0.29 |
|
85 |
2 |
@Override... |
86 |
|
public int check(String url) |
87 |
|
{ |
88 |
2 |
int responseCode; |
89 |
|
|
90 |
2 |
CloseableHttpResponse httpResponse = null; |
91 |
2 |
try { |
92 |
2 |
HttpGet httpGet = new HttpGet(url); |
93 |
2 |
httpResponse = this.httpClient.execute(httpGet); |
94 |
1 |
responseCode = httpResponse.getStatusLine().getStatusCode(); |
95 |
1 |
this.logger.debug("Result of pinging [{}]: code = [{}]", url, responseCode); |
96 |
|
} catch (Exception e) { |
97 |
|
|
98 |
|
|
99 |
1 |
responseCode = 0; |
100 |
1 |
this.logger.debug("Error while checking [{}]", url, e); |
101 |
|
} finally { |
102 |
2 |
if (httpResponse != null) { |
103 |
1 |
try { |
104 |
1 |
httpResponse.close(); |
105 |
|
} catch (Exception ee) { |
106 |
|
|
107 |
0 |
this.logger.error("Failed to close HTTP connection for [{}]", url, ee); |
108 |
|
} |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
2 |
return responseCode; |
113 |
|
} |
114 |
|
} |