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