1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.repository.http.internal

File DefaultHttpClientFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

2
20
4
1
109
60
5
0.25
5
4
1.25

Classes

Class Line # Actions
DefaultHttpClientFactory 48 20 0% 5 17
0.3461538634.6%
 

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.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    * Configures user agent, timeouts, proxy and authentication.
42    *
43    * @version $Id: 5a35028b874a1bd250417452c5624114d3528346 $
44    * @since 5.2M1
45    */
46    @Component
47    @Singleton
 
48    public class DefaultHttpClientFactory implements HttpClientFactory
49    {
50    /**
51    * Used to get the user agent to use when performing HTTP calls to the remote Extension Repository.
52    */
53    @Inject
54    private ExtensionManagerConfiguration configuration;
55   
 
56  109 toggle @Override
57    public HttpClientBuilder createHttpClientBuilder(String user, String password)
58    {
59  109 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
60   
61    // Pre-configure with everything configured at JVM level
62  109 httpClientBuilder.useSystemProperties();
63   
64    // Setup user agent
65  109 httpClientBuilder.setUserAgent(this.configuration.getUserAgent());
66   
67    // Setup authentication
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   
 
78  109 toggle @Override
79    public CloseableHttpClient createClient(String user, String password)
80    {
81  109 return createHttpClientBuilder(user, password).build();
82    }
83   
 
84  0 toggle @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    // Set socket timeouts
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    // Set request timeouts
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   
 
105  0 toggle private int getIntProperty(Map<String, String> properties, String key, int def)
106    {
107  0 return NumberUtils.toInt(properties.get(key), def);
108    }
109    }