77import org .apache .commons .io .IOUtils ;
88import org .apache .http .*;
99import org .apache .http .client .ResponseHandler ;
10+ import org .apache .http .client .config .RequestConfig ;
1011import org .apache .http .client .methods .*;
1112import org .apache .http .client .protocol .HttpClientContext ;
1213import org .apache .http .client .utils .HttpClientUtils ;
2223import java .io .InputStream ;
2324import java .nio .charset .Charset ;
2425import java .util .*;
25- import java .util .concurrent .*;
26+ import java .util .concurrent .Callable ;
27+ import java .util .concurrent .ExecutorService ;
28+ import java .util .concurrent .Executors ;
29+ import java .util .concurrent .Future ;
2630
2731
2832/**
@@ -34,12 +38,14 @@ public class BintrayImpl implements Bintray {
3438 private CloseableHttpClient client ;
3539 private ResponseHandler <HttpResponse > responseHandler = new BintrayResponseHandler ();
3640 private String baseUrl ;
41+ private int signRequestTimeoutPerFile ;
3742
3843
39- public BintrayImpl (CloseableHttpClient client , String baseUrl , int threadPoolSize ) {
44+ public BintrayImpl (CloseableHttpClient client , String baseUrl , int threadPoolSize , int signRequestTimeoutPerFile ) {
4045 this .client = client ;
4146 this .baseUrl = (baseUrl == null || baseUrl .isEmpty ()) ? BintrayClient .BINTRAY_API_URL : baseUrl ;
4247 this .executorService = Executors .newFixedThreadPool (threadPoolSize );
48+ this .signRequestTimeoutPerFile = signRequestTimeoutPerFile ;
4349 }
4450
4551 static public void addContentTypeJsonHeader (Map <String , String > headers ) {
@@ -96,27 +102,32 @@ public HttpResponse head(String uri, Map<String, String> headers) throws Bintray
96102 }
97103
98104 /**
99- * Executes a sign request using the ExecutorService to avoid timing out
105+ * Executes a sign request using the ExecutorService and uses the file count to set a timeout to avoid timing out
106+ * on long requests
100107 *
101108 * @throws BintrayCallException
102109 */
103- public HttpResponse sign (String uri , Map <String , String > headers ) throws BintrayCallException {
110+ public HttpResponse sign (String uri , Map <String , String > headers , int fileCount ) throws BintrayCallException {
104111 HttpPost signRequest = new HttpPost (createUrl (uri ));
105112 setHeaders (signRequest , headers );
113+ //Set signRequestTimeoutPerFile * fileCount timeout on request
114+ signRequest .setConfig (RequestConfig .custom ().setSocketTimeout (signRequestTimeoutPerFile * fileCount )
115+ .setConnectionRequestTimeout (signRequestTimeoutPerFile * fileCount )
116+ .setConnectTimeout (signRequestTimeoutPerFile * fileCount ).build ());
106117 RequestRunner runner = new RequestRunner (signRequest , client , responseHandler );
107118 Future <String > signResponse = executorService .submit (runner );
108-
109119 try {
110120 signResponse .get ();
111121 } catch (Exception e ) {
112122 BintrayCallException bce ;
113123 if (e .getCause () instanceof BintrayCallException ) {
114124 bce = (BintrayCallException ) e .getCause ();
115125 } else {
116- bce = new BintrayCallException (400 , e .getMessage (), (e .getCause () == null ) ? "" : e .getCause ().getMessage ());
126+ bce = new BintrayCallException (409 , e .getMessage (), (e .getCause () == null ) ? ""
127+ : ", " + e .getCause ().toString () + " : " + e .getCause ().getMessage ());
117128 }
118129 log .error (bce .toString ());
119- log .debug ("{}" , e );
130+ log .debug ("{}" , e . getMessage (), e );
120131 throw bce ;
121132 }
122133
@@ -203,11 +214,12 @@ private HttpResponse put(List<HttpPut> requests) throws MultipleBintrayCallExcep
203214 runners .add (runner );
204215 }
205216 try {
206- executions = executorService .invokeAll (runners , 10 , TimeUnit . MINUTES );
217+ executions = executorService .invokeAll (runners );
207218 } catch (InterruptedException e ) {
208- BintrayCallException bce = new BintrayCallException (400 , e .getMessage (), (e .getCause () == null ) ? "" : e .getCause ().getMessage ());
219+ BintrayCallException bce = new BintrayCallException (409 , e .getMessage (), (e .getCause () == null ) ? ""
220+ : e .getCause ().toString () + " : " + e .getCause ().getMessage ());
209221 log .error (bce .toString ());
210- log .debug ("{}" , e );
222+ log .debug ("{}" , e . getMessage (), e );
211223 errors .add (bce );
212224 }
213225
@@ -272,12 +284,12 @@ private HttpResponse execute(HttpUriRequest request, HttpClientContext context)
272284 return client .execute (request , responseHandler );
273285 }
274286 } catch (BintrayCallException bce ) {
275- log .debug ("{}" , bce );
287+ log .debug ("{}" , bce . toString (), bce );
276288 throw bce ;
277289 } catch (IOException ioe ) {
278290 //Underlying IOException form the client
279- String underlyingCause = (ioe .getCause () == null ) ? "" : ioe .getCause ().getMessage ();
280- log .debug ("{}" , ioe );
291+ String underlyingCause = (ioe .getCause () == null ) ? "" : ioe .toString () + " : " + ioe . getCause ().getMessage ();
292+ log .debug ("{}" , ioe . getMessage (), ioe );
281293 throw new BintrayCallException (400 , ioe .getMessage (), underlyingCause );
282294 }
283295 }
@@ -307,7 +319,7 @@ public String call() throws BintrayCallException {
307319 log .info ("Pushing " + pushPath );
308320 errorResultBuilder = new StringBuilder (" Pushing " + pushPath + " failed: " );
309321 } else {
310- errorResultBuilder = new StringBuilder (request .getMethod () + " " + request .getURI ().getPath () + " failed:" );
322+ errorResultBuilder = new StringBuilder (request .getMethod () + " " + request .getURI ().getPath () + " failed: " );
311323 }
312324 HttpResponse response ;
313325 try {
@@ -319,9 +331,9 @@ public String call() throws BintrayCallException {
319331 throw bce ;
320332 } catch (IOException ioe ) {
321333 log .debug ("IOException occured: '{}'" , ioe .getMessage (), ioe );
322- String cause = (ioe .getCause () == null ) ? (( ioe . getMessage () != null && ! ioe .getMessage ().equals ( "" )) ? ioe . getMessage () : ioe . toString ())
323- : " : " + (( ioe .getCause ().getMessage () != null && ! ioe . getCause (). getMessage (). equals ( "" )) ? ioe . getCause (). getMessage () : ioe . getCause (). toString ()) ;
324- errorResultBuilder .append (ioe .getMessage ()).append (cause );
334+ String cause = (ioe .getCause () != null ) ? (", caused by: " + ioe .getCause ().toString () + " : "
335+ + ioe .getCause ().getMessage ()) : "" ;
336+ errorResultBuilder .append (ioe .toString ()). append ( " : " ). append ( ioe . getMessage ()).append (cause );
325337 throw new BintrayCallException (HttpStatus .SC_BAD_REQUEST , ioe .getMessage (), errorResultBuilder .toString ());
326338 } finally {
327339 request .releaseConnection ();
@@ -366,7 +378,8 @@ public HttpResponse handleResponse(HttpResponse response) throws BintrayCallExce
366378 }
367379 }
368380
369- HttpResponse newResponse = DefaultHttpResponseFactory .INSTANCE .newHttpResponse (response .getStatusLine (), new HttpClientContext ());
381+ HttpResponse newResponse = DefaultHttpResponseFactory .INSTANCE .newHttpResponse (response .getStatusLine (),
382+ new HttpClientContext ());
370383 newResponse .setEntity (new StringEntity (entity , Charset .forName ("UTF-8" )));
371384 newResponse .setHeaders (response .getAllHeaders ());
372385 return newResponse ;
0 commit comments