##### Coverage Report - com.ctrip.apollo.util.http.HttpUtil

Classes in this File | Line Coverage | Branch Coverage | Complexity
HttpUtil |
90% | 36/40
70% | 7/10
| 3.0;3
HttpUtil$1 |
100% | 2/2
N/A | N/A
| 3.0;3
HttpUtil$2 |
100% | 2/2
N/A | N/A
| 3.0;3

1 |  |
     package com.ctrip.apollo.util.http;
2 |  |
3 |  |
     import com.google.common.base.Charsets;
4 |  |
     import com.google.common.base.Function;
5 |  |
     import com.google.common.io.BaseEncoding;
6 |  |
     import com.google.gson.Gson;
7 |  |
8 |  |
     import com.ctrip.apollo.util.ConfigUtil;
9 |  |
10 |  |
     import org.unidal.helper.Files;
11 |  |
     import org.unidal.lookup.annotation.Inject;
12 |  |
     import org.unidal.lookup.annotation.Named;
13 |  |
14 |  |
     import java.io.IOException;
15 |  |
     import java.io.InputStream;
16 |  |
     import java.io.UnsupportedEncodingException;
17 |  |
     import java.lang.reflect.Type;
18 |  |
     import java.net.HttpURLConnection;
19 |  |
     import java.net.URL;
20 |  |
21 |  |
     /**
22 |  |
      * @author Jason Song(song_s@ctrip.com)
23 |  |
      */
24 |  |
     @Named(type = HttpUtil.class)
25 | 48 |
     public class HttpUtil {
26 |  |
       @Inject
27 |  |
       private ConfigUtil m_configUtil;
28 |  |
       private Gson gson;
29 |  |
       private String basicAuth;
30 |  |
31 |  |
       /**
32 |  |
        * Constructor.
33 |  |
        */
34 | 12 |
       public HttpUtil() {
35 | 12 |
         gson = new Gson();
36 |  |
         try {
37 | 12 |
           basicAuth = "Basic " + BaseEncoding.base64().encode("user:".getBytes("UTF-8"));
38 | 0 |
         } catch (UnsupportedEncodingException ex) {
39 | 0 |
           ex.printStackTrace();
40 | 12 |
         }
41 | 12 |
       }
42 |  |
43 |  |
       /**
44 |  |
        * Do get operation for the http request.
45 |  |
        *
46 |  |
        * @param httpRequest  the request
47 |  |
        * @param responseType the response type
48 |  |
        * @return the response
49 |  |
        * @throws RuntimeException if any error happened or response code is neither 200 nor 304
50 |  |
        */
51 |  |
       public <T> HttpResponse<T> doGet(HttpRequest httpRequest, final Class<T> responseType) {
52 | 69 |
         Function<String, T> convertResponse = new Function<String, T>() {
53 |  |
           @Override
54 |  |
           public T apply(String input) {
55 | 17 |
             return gson.fromJson(input, responseType);
56 |  |
           }
57 |  |
         };
58 |  |
59 | 69 |
         return doGetWithSerializeFunction(httpRequest, convertResponse);
60 |  |
       }
61 |  |
62 |  |
       /**
63 |  |
        * Do get operation for the http request.
64 |  |
        *
65 |  |
        * @param httpRequest  the request
66 |  |
        * @param responseType the response type
67 |  |
        * @return the response
68 |  |
        * @throws RuntimeException if any error happened or response code is neither 200 nor 304
69 |  |
        */
70 |  |
       public <T> HttpResponse<T> doGet(HttpRequest httpRequest, final Type responseType) {
71 | 65 |
         Function<String, T> convertResponse = new Function<String, T>() {
72 |  |
           @Override
73 |  |
           public T apply(String input) {
74 | 31 |
             return gson.fromJson(input, responseType);
75 |  |
           }
76 |  |
         };
77 |  |
78 | 34 |
         return doGetWithSerializeFunction(httpRequest, convertResponse);
79 |  |
       }
80 |  |
81 |  |
       private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest,
82 |  |
                                                              Function<String, T> serializeFunction) {
83 | 102 |
         InputStream is = null;
84 |  |
         int statusCode;
85 |  |
         try {
86 | 102 |
           HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection();
87 |  |
88 | 103 |
           conn.setRequestMethod("GET");
89 | 103 |
           conn.setRequestProperty("Authorization", basicAuth);
90 |  |
91 | 103 |
           int connectTimeout = httpRequest.getConnectTimeout();
92 | 103 |
           if (connectTimeout < 0) {
93 | 103 |
             connectTimeout = m_configUtil.getConnectTimeout();
94 |  |
           }
95 |  |
96 | 102 |
           int readTimeout = httpRequest.getReadTimeout();
97 | 102 |
           if (readTimeout < 0) {
98 | 73 |
             readTimeout = m_configUtil.getReadTimeout();
99 |  |
           }
100 |  |
101 | 103 |
           conn.setConnectTimeout(connectTimeout);
102 | 103 |
           conn.setReadTimeout(readTimeout);
103 |  |
104 | 103 |
           conn.connect();
105 |  |
106 | 96 |
           statusCode = conn.getResponseCode();
107 |  |
108 | 92 |
           if (statusCode == 200) {
109 | 48 |
             is = conn.getInputStream();
110 | 48 |
             String content = Files.IO.INSTANCE.readFrom(is, Charsets.UTF_8.name());
111 | 48 |
             return new HttpResponse<>(statusCode, serializeFunction.apply(content));
112 |  |
           }
113 |  |
114 | 44 |
           if (statusCode == 304) {
115 | 0 |
             return new HttpResponse<>(statusCode, null);
116 |  |
           }
117 |  |
118 | 11 |
         } catch (Throwable ex) {
119 | 11 |
           throw new RuntimeException("Could not complete get operation", ex);
120 |  |
         } finally {
121 | 103 |
           if (is != null) {
122 |  |
             try {
123 | 48 |
               is.close();
124 | 0 |
             } catch (IOException ex) {
125 |  |
               // ignore
126 | 107 |
             }
127 |  |
           }
128 |  |
         }
129 | 88 |
         throw new RuntimeException(String.format("Get operation failed for %s, status code - %d",
130 | 44 |
             httpRequest.getUrl(), statusCode));
131 |  |
       }
132 |  |
133 |  |
     }