@@ -42,12 +42,56 @@ public class Api {
4242 private final ObjectMapper objectMapper = new ObjectMapper ();
4343 private Supplier <HttpClientBuilder > httpClientBuilderSupplier = HttpClientBuilder ::create ;
4444
45+ // Connect timeout = how long to wait for TCP/TLS to come up.
46+ // Response timeout = how long to wait once the request is sent.
47+ static final Timeout DEFAULT_CONNECT_TIMEOUT = Timeout .of (5 , TimeUnit .SECONDS );
48+ static final Timeout DEFAULT_RESPONSE_TIMEOUT = Timeout .of (30 , TimeUnit .SECONDS );
49+
50+ private Timeout connectTimeout = DEFAULT_CONNECT_TIMEOUT ;
51+ private Timeout responseTimeout = DEFAULT_RESPONSE_TIMEOUT ;
52+
4553 public Api (App app ) {
4654 this .app = app ;
4755 this .clientKey = app .getClientKey ();
4856 this .clientSecret = app .getClientSecret ();
4957 }
5058
59+ /** For testing: shrink the HTTP timeouts so tests don't have to wait 30s. */
60+ void setTimeoutsForTesting (Timeout connect , Timeout response ) {
61+ this .connectTimeout = connect ;
62+ this .responseTimeout = response ;
63+ }
64+
65+ private RequestConfig defaultRequestConfig () {
66+ return RequestConfig .custom ()
67+ .setConnectTimeout (connectTimeout )
68+ .setConnectionRequestTimeout (connectTimeout )
69+ .setResponseTimeout (responseTimeout )
70+ .build ();
71+ }
72+
73+ private HttpClientBuilder newBuilderWithProxy () {
74+ HttpClientBuilder builder = httpClientBuilderSupplier .get ();
75+ builder .setDefaultRequestConfig (defaultRequestConfig ());
76+ if (app .getProxy () != null ) {
77+ String [] splitted = app .getProxy ().split (":" , 2 );
78+ int port = splitted .length > 1 ? Integer .parseInt (splitted [1 ]) : 80 ;
79+ if (app .getProxyAuth () != null ) {
80+ String [] credentials = app .getProxyAuth ().split (":" , 2 );
81+ if (credentials .length == 2 ) {
82+ BasicCredentialsProvider credsProvider = new BasicCredentialsProvider ();
83+ credsProvider .setCredentials (
84+ new AuthScope (splitted [0 ], port ),
85+ new UsernamePasswordCredentials (credentials [0 ], credentials [1 ].toCharArray ())
86+ );
87+ builder .setDefaultCredentialsProvider (credsProvider );
88+ }
89+ }
90+ builder .setProxy (new HttpHost ("http" , splitted [0 ], port ));
91+ }
92+ return builder ;
93+ }
94+
5195 /**
5296 * For testing purposes only - allows overriding the API host
5397 */
@@ -104,30 +148,7 @@ public JsonNode pollTunnel(String tunnelID) throws Exception {
104148 }
105149
106150 public void destroyTunnel () throws Exception {
107- RequestConfig requestConfig = RequestConfig .custom ()
108- .setConnectTimeout (Timeout .of (1 , TimeUnit .SECONDS ))
109- .setConnectionRequestTimeout (Timeout .of (1 , TimeUnit .SECONDS ))
110- .build ();
111-
112- HttpClientBuilder builder = httpClientBuilderSupplier .get ();
113- builder .setDefaultRequestConfig (requestConfig );
114-
115- if (app .getProxy () != null ) {
116- String [] splitted = app .getProxy ().split (":" );
117- int port = splitted .length > 1 ? Integer .parseInt (splitted [1 ]) : 80 ;
118- if (app .getProxyAuth () != null ) {
119- String [] credentials = app .getProxyAuth ().split (":" );
120- BasicCredentialsProvider credsProvider = new BasicCredentialsProvider ();
121- credsProvider .setCredentials (
122- new AuthScope (splitted [0 ], port ),
123- new UsernamePasswordCredentials (credentials [0 ], credentials [1 ].toCharArray ())
124- );
125- builder .setDefaultCredentialsProvider (credsProvider );
126- }
127-
128- HttpHost proxy = new HttpHost ("http" , splitted [0 ], port );
129- builder .setProxy (proxy );
130- }
151+ HttpClientBuilder builder = newBuilderWithProxy ();
131152
132153 try (CloseableHttpClient httpClient = builder .build ()) {
133154 String auth = this .clientKey + ":" + this .clientSecret ;
@@ -143,24 +164,7 @@ public void destroyTunnel() throws Exception {
143164
144165 private JsonNode _post (String url , List <NameValuePair > postData ) throws Exception {
145166 try {
146- HttpClientBuilder builder = httpClientBuilderSupplier .get ();
147-
148- if (app .getProxy () != null ) {
149- String [] splitted = app .getProxy ().split (":" );
150- int port = splitted .length > 1 ? Integer .parseInt (splitted [1 ]) : 80 ;
151- if (app .getProxyAuth () != null ) {
152- String [] credentials = app .getProxyAuth ().split (":" );
153- BasicCredentialsProvider credsProvider = new BasicCredentialsProvider ();
154- credsProvider .setCredentials (
155- new AuthScope (splitted [0 ], port ),
156- new UsernamePasswordCredentials (credentials [0 ], credentials [1 ].toCharArray ())
157- );
158- builder .setDefaultCredentialsProvider (credsProvider );
159- }
160-
161- HttpHost proxy = new HttpHost ("http" , splitted [0 ], port );
162- builder .setProxy (proxy );
163- }
167+ HttpClientBuilder builder = newBuilderWithProxy ();
164168
165169 String responseBody ;
166170 try (CloseableHttpClient httpClient = builder .build ()) {
@@ -195,23 +199,7 @@ private JsonNode _post(String url, List<NameValuePair> postData) throws Exceptio
195199
196200 private JsonNode _get (String url ) throws Exception {
197201 try {
198- HttpClientBuilder builder = httpClientBuilderSupplier .get ();
199- if (app .getProxy () != null ) {
200- String [] splitted = app .getProxy ().split (":" );
201- int port = splitted .length > 1 ? Integer .parseInt (splitted [1 ]) : 80 ;
202- if (app .getProxyAuth () != null ) {
203- String [] credentials = app .getProxyAuth ().split (":" );
204- BasicCredentialsProvider credsProvider = new BasicCredentialsProvider ();
205- credsProvider .setCredentials (
206- new AuthScope (splitted [0 ], port ),
207- new UsernamePasswordCredentials (credentials [0 ], credentials [1 ].toCharArray ())
208- );
209- builder .setDefaultCredentialsProvider (credsProvider );
210- }
211-
212- HttpHost proxy = new HttpHost ("http" , splitted [0 ], port );
213- builder .setProxy (proxy );
214- }
202+ HttpClientBuilder builder = newBuilderWithProxy ();
215203
216204 String responseBody ;
217205 try (CloseableHttpClient httpClient = builder .build ()) {
0 commit comments