@@ -41,13 +41,12 @@ final class GatewayProxy {
4141 private Map <String , String > defaultHeaders ;
4242 private String masterKey ;
4343 private Map <String , String > resourceTokens ;
44- private int requestTimeout ;
44+ private ConnectionPolicy connectionPolicy ;
4545 private HttpClient httpClient ;
46- private int maxPoolSize ;
47- private int idleConnectionTimeout ;
46+ private HttpClient mediaHttpClient ;
4847
4948 public GatewayProxy (URI serviceEndpoint ,
50- ConnectionPolicy policy ,
49+ ConnectionPolicy connectionPolicy ,
5150 ConsistencyLevel consistencyLevel ,
5251 String masterKey ,
5352 Map <String , String > resourceTokens ) {
@@ -65,12 +64,9 @@ public GatewayProxy(URI serviceEndpoint,
6564 consistencyLevel .toString ());
6665 }
6766
67+ this .connectionPolicy = connectionPolicy ;
6868 this .masterKey = masterKey ;
6969 this .resourceTokens = resourceTokens ;
70- this .requestTimeout = policy .getRequestTimeout ();
71- this .maxPoolSize = policy .getMaxPoolSize ();
72- this .idleConnectionTimeout = policy .getIdleConnectionTimeout ();
73- this .httpClient = this .createHttpClient ();
7470 }
7571
7672 public DocumentServiceResponse doCreate (DocumentServiceRequest request )
@@ -111,30 +107,45 @@ public DocumentServiceResponse doSQLQuery(DocumentServiceRequest request)
111107 return this .performPostRequest (request );
112108 }
113109
110+ private HttpClient getHttpClient (boolean isForMedia ) {
111+ if (isForMedia ) {
112+ if (this .mediaHttpClient == null ) this .mediaHttpClient = this .createHttpClient (isForMedia );
113+ return this .mediaHttpClient ;
114+ } else {
115+ if (this .httpClient == null ) this .httpClient = this .createHttpClient (isForMedia );
116+ return this .httpClient ;
117+ }
118+ }
119+
114120 /**
115121 * Only one instance is created for the httpClient for optimization.
116122 * A PoolingClientConnectionManager is used with the Http client
117123 * to be able to reuse connections and execute requests concurrently.
118124 * A timeout for closing each connection is set so that connections don't leak.
119125 * A timeout is set for requests to avoid deadlocks.
120- * @return
126+ * @return the created HttpClient
121127 */
122- private HttpClient createHttpClient () {
123- if (this .httpClient == null ) {
124- PoolingClientConnectionManager conMan = new PoolingClientConnectionManager ( SchemeRegistryFactory .createDefault () );
125- conMan .setMaxTotal (maxPoolSize );
126- conMan .setDefaultMaxPerRoute (maxPoolSize );
127- conMan .closeIdleConnections (this .idleConnectionTimeout , TimeUnit .SECONDS );
128- this .httpClient = new DefaultHttpClient (conMan );
129- final HttpParams httpParams = httpClient .getParams ();
130- HttpConnectionParams .setConnectionTimeout (httpParams , requestTimeout * 1000 );
131- HttpConnectionParams .setSoTimeout (httpParams , requestTimeout * 1000 );
128+ private HttpClient createHttpClient (boolean isForMedia ) {
129+ PoolingClientConnectionManager conMan = new PoolingClientConnectionManager (
130+ SchemeRegistryFactory .createDefault ());
131+ conMan .setMaxTotal (this .connectionPolicy .getMaxPoolSize ());
132+ conMan .setDefaultMaxPerRoute (this .connectionPolicy .getMaxPoolSize ());
133+ conMan .closeIdleConnections (this .connectionPolicy .getIdleConnectionTimeout (), TimeUnit .SECONDS );
134+ HttpClient httpClient = new DefaultHttpClient (conMan );
135+ HttpParams httpParams = httpClient .getParams ();
136+
137+ if (isForMedia ) {
138+ HttpConnectionParams .setConnectionTimeout (httpParams ,
139+ this .connectionPolicy .getMediaRequestTimeout () * 1000 );
140+ HttpConnectionParams .setSoTimeout (httpParams , this .connectionPolicy .getMediaRequestTimeout () * 1000 );
141+ } else {
142+ HttpConnectionParams .setConnectionTimeout (httpParams , this .connectionPolicy .getRequestTimeout () * 1000 );
143+ HttpConnectionParams .setSoTimeout (httpParams , this .connectionPolicy .getRequestTimeout () * 1000 );
132144 }
133-
134- return this . httpClient ;
145+
146+ return httpClient ;
135147 }
136148
137-
138149 private void putMoreContentIntoDocumentServiceRequest (
139150 DocumentServiceRequest request ,
140151 String httpMethod ) {
@@ -181,8 +192,7 @@ private void putMoreContentIntoDocumentServiceRequest(
181192 }
182193 }
183194
184- private void fillHttpRequestBaseWithHeaders (Map <String , String > headers ,
185- HttpRequestBase httpBase ) {
195+ private void fillHttpRequestBaseWithHeaders (Map <String , String > headers , HttpRequestBase httpBase ) {
186196 // Add default headers.
187197 for (Map .Entry <String , String > entry : this .defaultHeaders .entrySet ()) {
188198 httpBase .setHeader (entry .getKey (), entry .getValue ());
@@ -195,8 +205,7 @@ private void fillHttpRequestBaseWithHeaders(Map<String, String> headers,
195205 }
196206 }
197207
198- private void maybeThrowException (HttpResponse response )
199- throws DocumentClientException {
208+ private void maybeThrowException (HttpResponse response ) throws DocumentClientException {
200209 int statusCode = response .getStatusLine ().getStatusCode ();
201210
202211 if (statusCode >= HttpConstants .StatusCodes .MINIMUM_STATUSCODE_AS_ERROR_GATEWAY ) {
@@ -218,14 +227,12 @@ private void maybeThrowException(HttpResponse response)
218227 responseHeaders .put (header .getName (), header .getValue ());
219228 }
220229
221- throw new DocumentClientException (statusCode ,
222- new Error (body ),
223- responseHeaders );
230+ throw new DocumentClientException (statusCode , new Error (body ), responseHeaders );
224231 }
225232 }
226233
227234 private DocumentServiceResponse performDeleteRequest (
228- DocumentServiceRequest request ) throws DocumentClientException {
235+ DocumentServiceRequest request ) throws DocumentClientException {
229236 putMoreContentIntoDocumentServiceRequest (
230237 request ,
231238 HttpConstants .HttpMethods .DELETE );
@@ -246,9 +253,8 @@ private DocumentServiceResponse performDeleteRequest(
246253 HttpDelete httpDelete = new HttpDelete (uri );
247254 this .fillHttpRequestBaseWithHeaders (request .getHeaders (), httpDelete );
248255 HttpResponse response = null ;
249- try
250- {
251- response = this .httpClient .execute (httpDelete );
256+ try {
257+ response = this .getHttpClient (request .getIsMedia ()).execute (httpDelete );
252258 } catch (IOException e ) {
253259 httpDelete .releaseConnection ();
254260 throw new IllegalStateException ("Http client execution failed." , e );
@@ -261,8 +267,7 @@ private DocumentServiceResponse performDeleteRequest(
261267 return new DocumentServiceResponse (response );
262268 }
263269
264- private DocumentServiceResponse performGetRequest (
265- DocumentServiceRequest request ) throws DocumentClientException {
270+ private DocumentServiceResponse performGetRequest (DocumentServiceRequest request ) throws DocumentClientException {
266271 putMoreContentIntoDocumentServiceRequest (request ,
267272 HttpConstants .HttpMethods .GET );
268273 URI uri ;
@@ -283,7 +288,7 @@ private DocumentServiceResponse performGetRequest(
283288 this .fillHttpRequestBaseWithHeaders (request .getHeaders (), httpGet );
284289 HttpResponse response = null ;
285290 try {
286- response = this .httpClient .execute (httpGet );
291+ response = this .getHttpClient ( request . getIsMedia ()) .execute (httpGet );
287292 } catch (IOException e ) {
288293 httpGet .releaseConnection ();
289294 throw new IllegalStateException ("Http client execution failed." , e );
@@ -293,8 +298,7 @@ private DocumentServiceResponse performGetRequest(
293298 return new DocumentServiceResponse (response );
294299 }
295300
296- private DocumentServiceResponse performPostRequest (
297- DocumentServiceRequest request ) throws DocumentClientException {
301+ private DocumentServiceResponse performPostRequest (DocumentServiceRequest request ) throws DocumentClientException {
298302 putMoreContentIntoDocumentServiceRequest (
299303 request ,
300304 HttpConstants .HttpMethods .POST );
@@ -317,7 +321,7 @@ private DocumentServiceResponse performPostRequest(
317321 httpPost .setEntity (request .getBody ());
318322 HttpResponse response = null ;
319323 try {
320- response = httpClient .execute (httpPost );
324+ response = this . getHttpClient ( request . getIsMedia ()) .execute (httpPost );
321325 } catch (IOException e ) {
322326 httpPost .releaseConnection ();
323327 throw new IllegalStateException ("Http client execution failed." , e );
@@ -327,8 +331,7 @@ private DocumentServiceResponse performPostRequest(
327331 return new DocumentServiceResponse (response );
328332 }
329333
330- private DocumentServiceResponse performPutRequest (
331- DocumentServiceRequest request ) throws DocumentClientException {
334+ private DocumentServiceResponse performPutRequest (DocumentServiceRequest request ) throws DocumentClientException {
332335 putMoreContentIntoDocumentServiceRequest (request ,
333336 HttpConstants .HttpMethods .PUT );
334337 URI uri ;
@@ -351,7 +354,7 @@ private DocumentServiceResponse performPutRequest(
351354 HttpResponse response = null ;
352355 try {
353356 httpPut .releaseConnection ();
354- response = this .httpClient .execute (httpPut );
357+ response = this .getHttpClient ( request . getIsMedia ()) .execute (httpPut );
355358 } catch (IOException e ) {
356359 throw new IllegalStateException ("Http client execution failed." , e );
357360 }
0 commit comments