Skip to content

Commit fcdcec9

Browse files
committed
Merge remote branch 'origin/master'
2 parents a7b0fcd + f715493 commit fcdcec9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+861
-259
lines changed

src/main/java/com/ning/http/client/AsyncHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public void close() {
365365
protected void finalize() throws Throwable {
366366
try {
367367
if (!isClosed.get()) {
368-
logger.warn("AsyncHttpClient.close() hasn't been invoked, which may produce file descriptor leaks");
368+
logger.debug("AsyncHttpClient.close() hasn't been invoked, which may produce file descriptor leaks");
369369
}
370370
} finally {
371371
super.finalize();

src/main/java/com/ning/http/client/AsyncHttpClientConfig.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.ning.http.client.filter.IOExceptionFilter;
1919
import com.ning.http.client.filter.RequestFilter;
2020
import com.ning.http.client.filter.ResponseFilter;
21+
import com.ning.http.util.ProxyUtils;
2122

2223
import javax.net.ssl.SSLContext;
2324
import javax.net.ssl.SSLEngine;
@@ -419,6 +420,7 @@ public static class Builder {
419420
private int maxDefaultRedirects = Integer.getInteger(ASYNC_CLIENT + "defaultMaxRedirects", 5);
420421
private boolean compressionEnabled = Boolean.getBoolean(ASYNC_CLIENT + "compressionEnabled");
421422
private String userAgent = System.getProperty(ASYNC_CLIENT + "userAgent", "NING/1.0");
423+
private boolean useProxyProperties = Boolean.getBoolean(ASYNC_CLIENT + "useProxyProperties");
422424
private boolean allowPoolingConnection = true;
423425
private ScheduledExecutorService reaper = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
424426
public Thread newThread(Runnable r) {
@@ -822,7 +824,21 @@ public Builder setRemoveQueryParamsOnRedirect(boolean removeQueryParamOnRedirect
822824
this.removeQueryParamOnRedirect = removeQueryParamOnRedirect;
823825
return this;
824826
}
825-
827+
828+
/**
829+
* Sets whether AHC should use the default http.proxy* system properties
830+
* to obtain proxy information.
831+
* <p/>
832+
* If useProxyProperties is set to <code>true</code> but {@link #setProxyServer(ProxyServer)} was used
833+
* to explicitly set a proxy server, the latter is preferred.
834+
* <p/>
835+
* See http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
836+
*/
837+
public Builder setUseProxyProperties(boolean useProxyProperties) {
838+
this.useProxyProperties = useProxyProperties;
839+
return this;
840+
}
841+
826842
/**
827843
* Create a config builder with values taken from the given prototype configuration.
828844
*
@@ -859,6 +875,10 @@ public Builder(AsyncHttpClientConfig prototype) {
859875
* @return an {@link AsyncHttpClientConfig}
860876
*/
861877
public AsyncHttpClientConfig build() {
878+
if (proxyServer == null && useProxyProperties) {
879+
proxyServer = ProxyUtils.createProxy(System.getProperties());
880+
}
881+
862882
return new AsyncHttpClientConfig(defaultMaxTotalConnections,
863883
defaultMaxConnectionPerHost,
864884
defaultConnectionTimeOutInMs,

src/main/java/com/ning/http/client/RequestBuilderBase.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,20 @@ private void resetMultipartData() {
392392
request.parts = null;
393393
}
394394

395+
private void checkIfBodyAllowed() {
396+
if ("GET".equals(request.method) || "HEAD".equals(request.method)) {
397+
throw new IllegalArgumentException("Can NOT set Body on HTTP Request Method GET nor HEAD.");
398+
}
399+
}
400+
395401
public T setBody(File file) {
402+
checkIfBodyAllowed();
396403
request.file = file;
397404
return derived.cast(this);
398405
}
399406

400407
public T setBody(byte[] data) throws IllegalArgumentException {
408+
checkIfBodyAllowed();
401409
resetParameters();
402410
resetNonMultipartData();
403411
resetMultipartData();
@@ -406,6 +414,7 @@ public T setBody(byte[] data) throws IllegalArgumentException {
406414
}
407415

408416
public T setBody(String data) throws IllegalArgumentException {
417+
checkIfBodyAllowed();
409418
resetParameters();
410419
resetNonMultipartData();
411420
resetMultipartData();
@@ -414,6 +423,7 @@ public T setBody(String data) throws IllegalArgumentException {
414423
}
415424

416425
public T setBody(InputStream stream) throws IllegalArgumentException {
426+
checkIfBodyAllowed();
417427
resetParameters();
418428
resetNonMultipartData();
419429
resetMultipartData();
@@ -426,6 +436,7 @@ public T setBody(EntityWriter dataWriter) {
426436
}
427437

428438
public T setBody(EntityWriter dataWriter, long length) throws IllegalArgumentException {
439+
checkIfBodyAllowed();
429440
resetParameters();
430441
resetNonMultipartData();
431442
resetMultipartData();
@@ -435,6 +446,7 @@ public T setBody(EntityWriter dataWriter, long length) throws IllegalArgumentExc
435446
}
436447

437448
public T setBody(BodyGenerator bodyGenerator) {
449+
checkIfBodyAllowed();
438450
request.bodyGenerator = bodyGenerator;
439451
return derived.cast(this);
440452
}

src/main/java/com/ning/http/client/oauth/OAuthSignatureCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void calculateAndAddSignature(String baseURL, Request request, RequestBui
8989
long timestamp = System.currentTimeMillis() / 1000L;
9090
String signature = calculateSignature(method, baseURL, timestamp, nonce, request.getParams(), request.getQueryParams());
9191
String headerValue = constructAuthHeader(signature, nonce, timestamp);
92-
requestBuilder = requestBuilder.addHeader(HEADER_AUTHORIZATION, headerValue);
92+
requestBuilder = requestBuilder.setHeader(HEADER_AUTHORIZATION, headerValue);
9393
}
9494

9595
/**

src/main/java/com/ning/http/client/providers/apache/ApacheAsyncHttpProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.ning.http.client.filter.FilterException;
3939
import com.ning.http.client.filter.IOExceptionFilter;
4040
import com.ning.http.client.filter.ResponseFilter;
41+
import com.ning.http.client.listener.TransferCompletionHandler;
4142
import com.ning.http.client.resumable.ResumableAsyncHandler;
4243
import com.ning.http.util.AsyncHttpProviderUtils;
4344
import com.ning.http.util.ProxyUtils;
@@ -448,6 +449,10 @@ public T call() {
448449
future.setReaperFuture(reaperFuture);
449450
}
450451

452+
if (TransferCompletionHandler.class.isAssignableFrom(asyncHandler.getClass())) {
453+
throw new IllegalStateException(TransferCompletionHandler.class.getName() + "not supported by this provider");
454+
}
455+
451456
int statusCode = 200;
452457
try {
453458
statusCode = httpClient.executeMethod(method);
@@ -530,7 +535,7 @@ public T call() {
530535
if (byteToRead > 0) {
531536
int minBytes = Math.min(8192, byteToRead);
532537
byte[] bytes = new byte[minBytes];
533-
int leftBytes = minBytes < 8192 ? 0 : byteToRead;
538+
int leftBytes = minBytes < 8192 ? minBytes : byteToRead;
534539
int read = 0;
535540
while (leftBytes > -1) {
536541

src/main/java/com/ning/http/client/providers/apache/ApacheResponseFuture.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,11 @@ public V get(long timeout, TimeUnit unit) throws InterruptedException, Execution
178178
if (!contentProcessed.get() && timeout != -1 && ((System.currentTimeMillis() - touch.get()) <= responseTimeoutInMs)) {
179179
return get(timeout, unit);
180180
}
181-
timedOut.set(true);
182-
throw new TimeoutException(String.format("No response received after %s", responseTimeoutInMs));
181+
182+
if (exception.get() == null) {
183+
timedOut.set(true);
184+
throw new ExecutionException(new TimeoutException(String.format("No response received after %s", responseTimeoutInMs)));
185+
}
183186
} catch (CancellationException ce) {
184187
}
185188

src/main/java/com/ning/http/client/providers/jdk/JDKAsyncHttpProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.ning.http.client.filter.FilterException;
3535
import com.ning.http.client.filter.IOExceptionFilter;
3636
import com.ning.http.client.filter.ResponseFilter;
37+
import com.ning.http.client.listener.TransferCompletionHandler;
3738
import com.ning.http.multipart.MultipartRequestEntity;
3839
import com.ning.http.util.AsyncHttpProviderUtils;
3940
import com.ning.http.util.AuthenticatorUtils;
@@ -248,6 +249,10 @@ public T call() throws Exception {
248249
configure(uri, urlConnection, request);
249250
urlConnection.connect();
250251

252+
if (TransferCompletionHandler.class.isAssignableFrom(asyncHandler.getClass())) {
253+
throw new IllegalStateException(TransferCompletionHandler.class.getName() + "not supported by this provider");
254+
}
255+
251256
int statusCode = urlConnection.getResponseCode();
252257

253258
logger.debug("\n\nRequest {}\n\nResponse {}\n", request, statusCode);
@@ -336,7 +341,7 @@ public T call() throws Exception {
336341
if (byteToRead > 0) {
337342
int minBytes = Math.min(8192, byteToRead);
338343
byte[] bytes = new byte[minBytes];
339-
int leftBytes = minBytes < 8192 ? 0 : byteToRead;
344+
int leftBytes = minBytes < 8192 ? minBytes : byteToRead;
340345
int read = 0;
341346
while (leftBytes > -1) {
342347

src/main/java/com/ning/http/client/providers/jdk/JDKFuture.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public boolean cancel(boolean mayInterruptIfRunning) {
9393
super.done();
9494
return innerFuture.cancel(mayInterruptIfRunning);
9595
} else {
96-
super.done();
96+
super.done();
9797
return false;
9898
}
9999
}
@@ -127,10 +127,13 @@ public V get(long timeout, TimeUnit unit) throws InterruptedException, Execution
127127
}
128128
} catch (TimeoutException t) {
129129
if (!contentProcessed.get() && timeout != -1 && ((System.currentTimeMillis() - touch.get()) <= responseTimeoutInMs)) {
130-
return get(timeout,unit);
130+
return get(timeout, unit);
131+
}
132+
133+
if (exception.get() == null) {
134+
timedOut.set(true);
135+
throw new ExecutionException(new TimeoutException(String.format("No response received after %s", responseTimeoutInMs)));
131136
}
132-
timedOut.set(true);
133-
throw new TimeoutException(String.format("No response received after %s", responseTimeoutInMs));
134137
} catch (CancellationException ce) {
135138
}
136139

@@ -145,7 +148,7 @@ public V get(long timeout, TimeUnit unit) throws InterruptedException, Execution
145148
*
146149
* @return <code>true</code> if response has expired and should be terminated.
147150
*/
148-
public boolean hasExpired(){
151+
public boolean hasExpired() {
149152
return responseTimeoutInMs != -1 && ((System.currentTimeMillis() - touch.get()) > responseTimeoutInMs);
150153
}
151154

0 commit comments

Comments
 (0)