1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.transformation.linkchecker

File DefaultHTTPChecker.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
22
2
1
114
60
5
0.23
11
2
2.5

Classes

Class Line # Actions
DefaultHTTPChecker 45 22 0% 5 1
0.9615384396.2%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Default implementation using Apache Http Client.
39    *
40    * @version $Id: e0ab42331ea6915b5035a0f808a0db0a7e4ae3d3 $
41    * @since 3.3M1
42    */
43    @Component
44    @Singleton
 
45    public class DefaultHTTPChecker implements HTTPChecker, Initializable
46    {
47    /**
48    * The logger to log.
49    */
50    @Inject
51    private Logger logger;
52   
53    /**
54    * The client to connect to the remote site using HTTP.
55    * <p>
56    * Note: If one day we wish to configure timeouts, here's some good documentation about it:
57    * http://brian.olore.net/wp/2009/08/apache-httpclient-timeout/
58    */
59    private CloseableHttpClient httpClient;
60   
 
61  1 toggle @Override
62    public void initialize() throws InitializationException
63    {
64  1 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
65   
66    // Make the Http Client reusable by several threads
67  1 PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
68  1 httpClientBuilder.setConnectionManager(connectionManager);
69   
70    // Pre-configure with everything configured at JVM level (e.g. proxy setup).
71  1 httpClientBuilder.useSystemProperties();
72   
73    // Set our user agent to be a good citizen.
74  1 httpClientBuilder.setUserAgent("XWikiLinkChecker");
75   
76    // Ignore cookies since this can cause errors in logs and we don't need cookies when checking sites.
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   
 
85  2 toggle @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    // Some error in the transport or in the passed URL, use a special response code (0) which isn't in the
98    // list of allowed response codes, see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
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    // Failed to close, ignore but log the error.
107  0 this.logger.error("Failed to close HTTP connection for [{}]", url, ee);
108    }
109    }
110    }
111   
112  2 return responseCode;
113    }
114    }