| 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 | |
|
| 24 | |
|
| 25 | |
@Named(type = HttpUtil.class) |
| 26 | 48 | public class HttpUtil { |
| 27 | |
@Inject |
| 28 | |
private ConfigUtil m_configUtil; |
| 29 | |
private Gson gson; |
| 30 | |
private String basicAuth; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 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 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public <T> HttpResponse<T> doGet(HttpRequest httpRequest, final Class<T> responseType) { |
| 53 | 68 | 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 | 68 | return doGetWithSerializeFunction(httpRequest, convertResponse); |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public <T> HttpResponse<T> doGet(HttpRequest httpRequest, final Type responseType) { |
| 72 | 64 | Function<String, T> convertResponse = new Function<String, T>() { |
| 73 | |
@Override |
| 74 | |
public T apply(String input) { |
| 75 | 31 | return gson.fromJson(input, responseType); |
| 76 | |
} |
| 77 | |
}; |
| 78 | |
|
| 79 | 33 | return doGetWithSerializeFunction(httpRequest, convertResponse); |
| 80 | |
} |
| 81 | |
|
| 82 | |
private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest, |
| 83 | |
Function<String, T> serializeFunction) { |
| 84 | 101 | InputStream is = null; |
| 85 | |
int statusCode; |
| 86 | |
try { |
| 87 | 101 | HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection(); |
| 88 | |
|
| 89 | 101 | conn.setRequestMethod("GET"); |
| 90 | 101 | conn.setRequestProperty("Authorization", basicAuth); |
| 91 | |
|
| 92 | 101 | int connectTimeout = httpRequest.getConnectTimeout(); |
| 93 | 101 | if (connectTimeout < 0) { |
| 94 | 101 | connectTimeout = m_configUtil.getConnectTimeout(); |
| 95 | |
} |
| 96 | |
|
| 97 | 101 | int readTimeout = httpRequest.getReadTimeout(); |
| 98 | 101 | if (readTimeout < 0) { |
| 99 | 71 | readTimeout = m_configUtil.getReadTimeout(); |
| 100 | |
} |
| 101 | |
|
| 102 | 101 | conn.setConnectTimeout(connectTimeout); |
| 103 | 101 | conn.setReadTimeout(readTimeout); |
| 104 | |
|
| 105 | 101 | conn.connect(); |
| 106 | |
|
| 107 | 97 | statusCode = conn.getResponseCode(); |
| 108 | |
|
| 109 | 93 | if (statusCode == 200) { |
| 110 | 48 | is = conn.getInputStream(); |
| 111 | 48 | String content = Files.IO.INSTANCE.readFrom(is, Charsets.UTF_8.name()); |
| 112 | 48 | return new HttpResponse<>(statusCode, serializeFunction.apply(content)); |
| 113 | |
} |
| 114 | |
|
| 115 | 45 | 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 | 101 | if (is != null) { |
| 123 | |
try { |
| 124 | 48 | is.close(); |
| 125 | 0 | } catch (IOException ex) { |
| 126 | |
|
| 127 | 104 | } |
| 128 | |
} |
| 129 | |
} |
| 130 | 90 | throw new ApolloConfigException(String.format("Get operation failed for %s, status code - %d", |
| 131 | 45 | httpRequest.getUrl(), statusCode)); |
| 132 | |
} |
| 133 | |
|
| 134 | |
} |