|
| 1 | +package com.ctrip.framework.apollo.util.http; |
| 2 | + |
| 3 | +import com.ctrip.framework.apollo.build.ApolloInjector; |
| 4 | +import com.ctrip.framework.apollo.exceptions.ApolloConfigException; |
| 5 | +import com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException; |
| 6 | +import com.ctrip.framework.apollo.util.ConfigUtil; |
| 7 | +import com.google.common.base.Function; |
| 8 | +import com.google.common.io.CharStreams; |
| 9 | +import com.google.gson.Gson; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.InputStreamReader; |
| 13 | +import java.lang.reflect.Type; |
| 14 | +import java.net.HttpURLConnection; |
| 15 | +import java.net.URL; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Jason Song([email protected]) |
| 21 | + * @deprecated in favor of the interface {@link HttpClient} and it's default implementation {@link DefaultHttpClient} |
| 22 | + */ |
| 23 | +@Deprecated |
| 24 | +public class HttpUtil implements HttpClient { |
| 25 | + private ConfigUtil m_configUtil; |
| 26 | + private static final Gson GSON = new Gson(); |
| 27 | + |
| 28 | + /** |
| 29 | + * Constructor. |
| 30 | + */ |
| 31 | + public HttpUtil() { |
| 32 | + m_configUtil = ApolloInjector.getInstance(ConfigUtil.class); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Do get operation for the http request. |
| 37 | + * |
| 38 | + * @param httpRequest the request |
| 39 | + * @param responseType the response type |
| 40 | + * @return the response |
| 41 | + * @throws ApolloConfigException if any error happened or response code is neither 200 nor 304 |
| 42 | + */ |
| 43 | + @Override |
| 44 | + public <T> HttpResponse<T> doGet(HttpRequest httpRequest, final Class<T> responseType) { |
| 45 | + Function<String, T> convertResponse = new Function<String, T>() { |
| 46 | + @Override |
| 47 | + public T apply(String input) { |
| 48 | + return GSON.fromJson(input, responseType); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + return doGetWithSerializeFunction(httpRequest, convertResponse); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Do get operation for the http request. |
| 57 | + * |
| 58 | + * @param httpRequest the request |
| 59 | + * @param responseType the response type |
| 60 | + * @return the response |
| 61 | + * @throws ApolloConfigException if any error happened or response code is neither 200 nor 304 |
| 62 | + */ |
| 63 | + @Override |
| 64 | + public <T> HttpResponse<T> doGet(HttpRequest httpRequest, final Type responseType) { |
| 65 | + Function<String, T> convertResponse = new Function<String, T>() { |
| 66 | + @Override |
| 67 | + public T apply(String input) { |
| 68 | + return GSON.fromJson(input, responseType); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + return doGetWithSerializeFunction(httpRequest, convertResponse); |
| 73 | + } |
| 74 | + |
| 75 | + private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest, |
| 76 | + Function<String, T> serializeFunction) { |
| 77 | + InputStreamReader isr = null; |
| 78 | + InputStreamReader esr = null; |
| 79 | + int statusCode; |
| 80 | + try { |
| 81 | + HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection(); |
| 82 | + |
| 83 | + conn.setRequestMethod("GET"); |
| 84 | + |
| 85 | + Map<String, String> headers = httpRequest.getHeaders(); |
| 86 | + if (headers != null && headers.size() > 0) { |
| 87 | + for (Map.Entry<String, String> entry : headers.entrySet()) { |
| 88 | + conn.setRequestProperty(entry.getKey(), entry.getValue()); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + int connectTimeout = httpRequest.getConnectTimeout(); |
| 93 | + if (connectTimeout < 0) { |
| 94 | + connectTimeout = m_configUtil.getConnectTimeout(); |
| 95 | + } |
| 96 | + |
| 97 | + int readTimeout = httpRequest.getReadTimeout(); |
| 98 | + if (readTimeout < 0) { |
| 99 | + readTimeout = m_configUtil.getReadTimeout(); |
| 100 | + } |
| 101 | + |
| 102 | + conn.setConnectTimeout(connectTimeout); |
| 103 | + conn.setReadTimeout(readTimeout); |
| 104 | + |
| 105 | + conn.connect(); |
| 106 | + |
| 107 | + statusCode = conn.getResponseCode(); |
| 108 | + String response; |
| 109 | + |
| 110 | + try { |
| 111 | + isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8); |
| 112 | + response = CharStreams.toString(isr); |
| 113 | + } catch (IOException ex) { |
| 114 | + /** |
| 115 | + * according to https://docs.oracle.com/javase/7/docs/technotes/guides/net/http-keepalive.html, |
| 116 | + * we should clean up the connection by reading the response body so that the connection |
| 117 | + * could be reused. |
| 118 | + */ |
| 119 | + InputStream errorStream = conn.getErrorStream(); |
| 120 | + |
| 121 | + if (errorStream != null) { |
| 122 | + esr = new InputStreamReader(errorStream, StandardCharsets.UTF_8); |
| 123 | + try { |
| 124 | + CharStreams.toString(esr); |
| 125 | + } catch (IOException ioe) { |
| 126 | + //ignore |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // 200 and 304 should not trigger IOException, thus we must throw the original exception out |
| 131 | + if (statusCode == 200 || statusCode == 304) { |
| 132 | + throw ex; |
| 133 | + } |
| 134 | + // for status codes like 404, IOException is expected when calling conn.getInputStream() |
| 135 | + throw new ApolloConfigStatusCodeException(statusCode, ex); |
| 136 | + } |
| 137 | + |
| 138 | + if (statusCode == 200) { |
| 139 | + return new HttpResponse<>(statusCode, serializeFunction.apply(response)); |
| 140 | + } |
| 141 | + |
| 142 | + if (statusCode == 304) { |
| 143 | + return new HttpResponse<>(statusCode, null); |
| 144 | + } |
| 145 | + } catch (ApolloConfigStatusCodeException ex) { |
| 146 | + throw ex; |
| 147 | + } catch (Throwable ex) { |
| 148 | + throw new ApolloConfigException("Could not complete get operation", ex); |
| 149 | + } finally { |
| 150 | + if (isr != null) { |
| 151 | + try { |
| 152 | + isr.close(); |
| 153 | + } catch (IOException ex) { |
| 154 | + // ignore |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if (esr != null) { |
| 159 | + try { |
| 160 | + esr.close(); |
| 161 | + } catch (IOException ex) { |
| 162 | + // ignore |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + throw new ApolloConfigStatusCodeException(statusCode, |
| 168 | + String.format("Get operation failed for %s", httpRequest.getUrl())); |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments