|
26 | 26 | import java.io.StringWriter; |
27 | 27 | import java.io.UnsupportedEncodingException; |
28 | 28 | import java.net.HttpURLConnection; |
| 29 | +import java.net.InetSocketAddress; |
| 30 | +import java.net.Proxy; |
29 | 31 | import java.net.URL; |
30 | 32 | import java.net.URLDecoder; |
31 | 33 | import java.net.UnknownHostException; |
@@ -72,6 +74,8 @@ enum RequestMethod { |
72 | 74 |
|
73 | 75 | private int connectTimeout; |
74 | 76 | private int readTimeout; |
| 77 | + private String proxyHost; |
| 78 | + private int proxyPort; |
75 | 79 |
|
76 | 80 | public Http(Configuration configuration) { |
77 | 81 | this.configuration = configuration; |
@@ -126,14 +130,22 @@ public NodeWrapper put(String url, Request request) { |
126 | 130 | return httpRequestXML(RequestMethod.PUT, url, request.toXML()); |
127 | 131 | } |
128 | 132 |
|
129 | | - public void setConnectTimeout(int timeout){ |
| 133 | + public void setConnectTimeout(int timeout) { |
130 | 134 | connectTimeout = timeout; |
131 | 135 | } |
132 | 136 |
|
133 | 137 | public void setReadTimeout(int timeout) { |
134 | 138 | readTimeout = timeout; |
135 | 139 | } |
136 | 140 |
|
| 141 | + public void setProxyHost(String proxyHost) { |
| 142 | + this.proxyHost = proxyHost; |
| 143 | + } |
| 144 | + |
| 145 | + public void setProxyPort(int proxyPort) { |
| 146 | + this.proxyPort = proxyPort; |
| 147 | + } |
| 148 | + |
137 | 149 | private NodeWrapper httpRequestXML(RequestMethod requestMethod, String url) { |
138 | 150 | return httpRequestXML(requestMethod, url, null); |
139 | 151 | } |
@@ -182,28 +194,36 @@ private NodeWrapper httpRequestXML(RequestMethod requestMethod, String url, Stri |
182 | 194 | responseStream = connection.getResponseCode() == 422 ? connection.getErrorStream() |
183 | 195 | : connection.getInputStream(); |
184 | 196 |
|
185 | | - String xml = StringUtils.inputStreamToString(responseStream); |
| 197 | + String responseXml = StringUtils.inputStreamToString(responseStream); |
186 | 198 |
|
187 | 199 | configuration.getLogger().log(Level.INFO, "[Genesis] [{0}]] {1} {2}", |
188 | 200 | new Object[]{getCurrentTime(), requestMethod.toString(), url}); |
189 | 201 | configuration.getLogger().log(Level.FINE, "[Genesis] [{0}] {1} {2} {3}", |
190 | 202 | new Object[]{getCurrentTime(), requestMethod.toString(), url, connection.getResponseCode()}); |
191 | 203 |
|
192 | | - if (xml != null && configuration.isDebugModeEnabled() == true) { |
| 204 | + if (configuration.isDebugModeEnabled()) { |
193 | 205 | configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(postBody)); |
194 | | - configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(xml)); |
| 206 | + configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(responseXml)); |
195 | 207 | } |
196 | 208 |
|
197 | | - nodeWrapper = NodeWrapperFactory.instance.create(xml); |
| 209 | + nodeWrapper = NodeWrapperFactory.instance.create(responseXml); |
198 | 210 | } finally { |
199 | 211 | if (responseStream != null) { |
200 | 212 | responseStream.close(); |
201 | 213 | } |
202 | 214 | } |
203 | 215 | } catch (UnknownHostException e) { |
204 | | - throw new NetworkException("No route to host " + e.getMessage() + ":" + port, e); |
| 216 | + String errorStr = "No route to host " + host + ":" + port; |
| 217 | + if (proxyHost != null && proxyPort > 0) { |
| 218 | + errorStr += " , used proxy_host: " + proxyHost + ", proxy_port: " + proxyPort; |
| 219 | + } |
| 220 | + throw new NetworkException(errorStr, e); |
205 | 221 | } catch (IOException e) { |
206 | | - throw new UnexpectedException(e.getMessage() + ", host: " + host + ", port: " + port, e); |
| 222 | + String errorStr = ", host: " + host + ", port: " + port; |
| 223 | + if (proxyHost != null && proxyPort > 0) { |
| 224 | + errorStr += ", used proxy_host: " + proxyHost + ", proxy_port: " + proxyPort; |
| 225 | + } |
| 226 | + throw new UnexpectedException(e.getMessage() + errorStr, e); |
207 | 227 | } finally { |
208 | 228 | if (connection != null) { |
209 | 229 | connection.disconnect(); |
@@ -252,7 +272,7 @@ private JsonNode httpRequestJson(RequestMethod requestMethod, String url, String |
252 | 272 | configuration.getLogger().log(Level.FINE, "[Genesis] [{0}] {1} {2} {3}", |
253 | 273 | new Object[]{getCurrentTime(), requestMethod.toString(), url, connection.getResponseCode()}); |
254 | 274 |
|
255 | | - if (json != null && configuration.isDebugModeEnabled() == true) { |
| 275 | + if (configuration.isDebugModeEnabled()) { |
256 | 276 | configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(postBody)); |
257 | 277 | configuration.getLogger().log(Level.INFO, formatSanitizeBodyForLog(json)); |
258 | 278 | } |
@@ -338,11 +358,15 @@ private HttpURLConnection buildConnection(RequestMethod requestMethod, String ur |
338 | 358 | String user_pass = configuration.getUsername() + ":" + configuration.getPassword(); |
339 | 359 | String encoded = Base64.getEncoder().encodeToString(user_pass.getBytes()); |
340 | 360 |
|
341 | | - |
342 | | - connection = (HttpURLConnection) url.openConnection(); |
| 361 | + if (proxyHost != null && !proxyHost.isEmpty() && proxyPort > 0) { |
| 362 | + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); |
| 363 | + connection = (HttpURLConnection) url.openConnection(proxy); |
| 364 | + } else { |
| 365 | + connection = (HttpURLConnection) url.openConnection(); |
| 366 | + } |
343 | 367 | connection.setRequestMethod(requestMethod.toString()); |
344 | 368 | connection.addRequestProperty("Accept", acceptHeader); |
345 | | - connection.setRequestProperty("Content-Type",contentType); |
| 369 | + connection.setRequestProperty("Content-Type", contentType); |
346 | 370 | connection.addRequestProperty("Authorization", "Basic " + encoded); |
347 | 371 | connection.setDoOutput(true); |
348 | 372 | connection.setConnectTimeout(connectTimeout); |
|
0 commit comments