##### Coverage Report - com.ctrip.framework.apollo.common.auth.RestTemplateFactory

Classes in this File | Line Coverage | Branch Coverage | Complexity
RestTemplateFactory |
0% | 0/16
N/A | N/A
| 1.0;1

1 |  |
     package com.ctrip.framework.apollo.common.auth;
2 |  |
3 |  |
     import java.io.UnsupportedEncodingException;
4 |  |
     import java.util.ArrayList;
5 |  |
     import java.util.Collection;
6 |  |
7 |  |
     import org.apache.http.Header;
8 |  |
     import org.apache.http.auth.AuthScope;
9 |  |
     import org.apache.http.auth.UsernamePasswordCredentials;
10 |  |
     import org.apache.http.impl.client.BasicCredentialsProvider;
11 |  |
     import org.apache.http.impl.client.CloseableHttpClient;
12 |  |
     import org.apache.http.impl.client.HttpClientBuilder;
13 |  |
     import org.apache.http.message.BasicHeader;
14 |  |
     import org.springframework.beans.factory.FactoryBean;
15 |  |
     import org.springframework.beans.factory.InitializingBean;
16 |  |
     import org.springframework.beans.factory.annotation.Autowired;
17 |  |
     import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
18 |  |
     import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
19 |  |
     import org.springframework.stereotype.Component;
20 |  |
     import org.springframework.web.client.RestTemplate;
21 |  |
22 |  |
     import com.google.common.io.BaseEncoding;
23 |  |
24 | 0 |
     @Component
25 | 0 |
     public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
26 |  |
       @Autowired
27 |  |
       private HttpMessageConverters httpMessageConverters;
28 |  |
29 |  |
       private RestTemplate restTemplate;
30 |  |
31 |  |
       public RestTemplate getObject() {
32 | 0 |
         return restTemplate;
33 |  |
       }
34 |  |
35 |  |
       public Class<RestTemplate> getObjectType() {
36 | 0 |
         return RestTemplate.class;
37 |  |
       }
38 |  |
39 |  |
       public boolean isSingleton() {
40 | 0 |
         return true;
41 |  |
       }
42 |  |
43 |  |
       public void afterPropertiesSet() throws UnsupportedEncodingException {
44 | 0 |
         Collection<Header> defaultHeaders = new ArrayList<Header>();
45 | 0 |
         Header header = new BasicHeader("Authorization",
46 | 0 |
             "Basic " + BaseEncoding.base64().encode("apollo:".getBytes("UTF-8")));
47 | 0 |
         defaultHeaders.add(header);
48 |  |
49 | 0 |
         BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
50 | 0 |
         credentialsProvider.setCredentials(AuthScope.ANY,
51 |  |
             new UsernamePasswordCredentials("apollo", ""));
52 |  |
         CloseableHttpClient httpClient =
53 | 0 |
             HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider)
54 | 0 |
                 .setDefaultHeaders(defaultHeaders).build();
55 |  |
56 | 0 |
         restTemplate = new RestTemplate(httpMessageConverters.getConverters());
57 | 0 |
         restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
58 | 0 |
       }
59 |  |
60 |  |
     }