Skip to content

Commit 2eeb0e2

Browse files
Merge pull request #1 from AsyncHttpClient/master
Merging from original repo on 3/28
2 parents 90b561e + c9b5391 commit 2eeb0e2

27 files changed

+336
-124
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Async Http Client library purpose is to allow Java applications to easily execut
1010
<dependency>
1111
<groupId>com.ning</groupId>
1212
<artifactId>async-http-client</artifactId>
13-
<version>1.8.3</version>
13+
<version>1.8.4</version>
1414
</dependency>
1515
```
1616

@@ -24,7 +24,7 @@ Then in your code you can simply do ([Javadoc](http://sonatype.github.com/async-
2424
import com.ning.http.client.*;
2525
import java.util.concurrent.Future;
2626

27-
AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory.getAsyncHttpClient();
27+
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
2828
Future<Response> f = asyncHttpClient.prepareGet("http://www.ning.com/").execute();
2929
Response r = f.get();
3030
```
@@ -37,7 +37,7 @@ You can also accomplish asynchronous (non-blocking) operation without using a Fu
3737
import com.ning.http.client.*;
3838
import java.util.concurrent.Future;
3939

40-
AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory.getAsyncHttpClient();
40+
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
4141
asyncHttpClient.prepareGet("http://www.ning.com/").execute(new AsyncCompletionHandler<Response>(){
4242

4343
@Override
@@ -62,7 +62,7 @@ You can also mix Future with AsyncHandler to only retrieve part of the asynchron
6262
import com.ning.http.client.*;
6363
import java.util.concurrent.Future;
6464

65-
AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory.getAsyncHttpClient();
65+
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
6666
Future<Integer> f = asyncHttpClient.prepareGet("http://www.ning.com/").execute(
6767
new AsyncCompletionHandler<Integer>(){
6868

@@ -89,7 +89,7 @@ which is something you want to do for large responses: this way you can process
8989
import com.ning.http.client.*;
9090
import java.util.concurrent.Future;
9191

92-
AsyncHttpClient c = AsyncHttpClientFactory.getAsyncHttpClient();
92+
AsyncHttpClient c = new AsyncHttpClient();
9393
Future<String> f = c.prepareGet("http://www.ning.com/").execute(new AsyncHandler<String>() {
9494
private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
9595

@@ -140,7 +140,7 @@ Finally, you can also configure the AsyncHttpClient via its AsyncHttpClientConfi
140140
```java
141141
AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder()
142142
S.setProxyServer(new ProxyServer("127.0.0.1", 38080)).build();
143-
AsyncHttpClient c = AsyncHttpClientFactory.getAsyncHttpClient(cf);
143+
AsyncHttpClient c = new AsyncHttpClient(cf);
144144
```
145145

146146
## WebSocket
@@ -176,7 +176,7 @@ The library uses Java non blocking I/O for supporting asynchronous operations. T
176176

177177
```java
178178
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().build();
179-
AsyncHttpClient client = AsyncHttpClientFactory.getAsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config);
179+
AsyncHttpClient client = new AsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config);
180180
```
181181

182182
## User Group

api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<dependency>
3535
<groupId>org.slf4j</groupId>
3636
<artifactId>slf4j-api</artifactId>
37-
<version>1.7.5</version>
37+
<version>1.7.6</version>
3838
</dependency>
3939
</dependencies>
4040

api/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.asynchttpclient;
1717

18+
import static org.asynchttpclient.util.MiscUtil.getBoolean;
19+
1820
import org.asynchttpclient.date.TimeConverter;
1921
import org.asynchttpclient.filter.IOExceptionFilter;
2022
import org.asynchttpclient.filter.RequestFilter;
@@ -578,7 +580,7 @@ public static class Builder {
578580
private boolean useProxyProperties = Boolean.getBoolean(ASYNC_CLIENT + "useProxyProperties");
579581
private boolean useProxySelector = Boolean.getBoolean(ASYNC_CLIENT + "useProxySelector");
580582
private boolean allowPoolingConnection = true;
581-
private boolean useRelativeURIsWithSSLProxies = Boolean.getBoolean(ASYNC_CLIENT + "useRelativeURIsWithSSLProxies");
583+
private boolean useRelativeURIsWithSSLProxies = getBoolean(ASYNC_CLIENT + "useRelativeURIsWithSSLProxies", true);
582584
private ScheduledExecutorService reaper;
583585
private ExecutorService applicationThreadPool;
584586
private ProxyServerSelector proxyServerSelector = null;

api/src/main/java/org/asynchttpclient/AsyncHttpClientFactory.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/*
2+
* Copyright (c) 2010-2014 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
113
package org.asynchttpclient;
214

315
import org.asynchttpclient.util.AsyncImplHelper;
@@ -38,25 +50,22 @@ public static AsyncHttpClient getAsyncHttpClient() {
3850
return (AsyncHttpClient) asyncHttpClientImplClass.newInstance();
3951
} catch (InstantiationException e) {
4052
throw new AsyncHttpClientImplException("Unable to create the class specified by system property : "
41-
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY,e);
53+
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, e);
4254
} catch (IllegalAccessException e) {
4355
throw new AsyncHttpClientImplException("Unable to find the class specified by system property : "
44-
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY,e);
56+
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, e);
4557
}
4658
return new DefaultAsyncHttpClient();
4759
}
4860

4961
public static AsyncHttpClient getAsyncHttpClient(AsyncHttpProvider provider) {
5062
if (attemptInstantiation()) {
5163
try {
52-
Constructor<AsyncHttpClient> constructor = asyncHttpClientImplClass
53-
.getConstructor(AsyncHttpProvider.class);
64+
Constructor<AsyncHttpClient> constructor = asyncHttpClientImplClass.getConstructor(AsyncHttpProvider.class);
5465
return constructor.newInstance(provider);
55-
}catch (Exception e) {
56-
throw new AsyncHttpClientImplException(
57-
"Unable to find the instantiate the class specified by system property : "
58-
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY
59-
+ "(AsyncHttpProvider) due to : " + e.getMessage(), e);
66+
} catch (Exception e) {
67+
throw new AsyncHttpClientImplException("Unable to find the instantiate the class specified by system property : "
68+
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY + "(AsyncHttpProvider) due to : " + e.getMessage(), e);
6069
}
6170
}
6271
return new DefaultAsyncHttpClient(provider);
@@ -65,14 +74,11 @@ public static AsyncHttpClient getAsyncHttpClient(AsyncHttpProvider provider) {
6574
public static AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
6675
if (attemptInstantiation()) {
6776
try {
68-
Constructor<AsyncHttpClient> constructor = asyncHttpClientImplClass
69-
.getConstructor(AsyncHttpClientConfig.class);
77+
Constructor<AsyncHttpClient> constructor = asyncHttpClientImplClass.getConstructor(AsyncHttpClientConfig.class);
7078
return constructor.newInstance(config);
7179
} catch (Exception e) {
72-
throw new AsyncHttpClientImplException(
73-
"Unable to find the instantiate the class specified by system property : "
74-
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY
75-
+ "(AsyncHttpProvider) due to : " + e.getMessage(), e);
80+
throw new AsyncHttpClientImplException("Unable to find the instantiate the class specified by system property : "
81+
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY + "(AsyncHttpProvider) due to : " + e.getMessage(), e);
7682
}
7783
}
7884
return new DefaultAsyncHttpClient(config);
@@ -81,14 +87,12 @@ public static AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
8187
public static AsyncHttpClient getAsyncHttpClient(AsyncHttpProvider provider, AsyncHttpClientConfig config) {
8288
if (attemptInstantiation()) {
8389
try {
84-
Constructor<AsyncHttpClient> constructor = asyncHttpClientImplClass.getConstructor(
85-
AsyncHttpProvider.class, AsyncHttpClientConfig.class);
90+
Constructor<AsyncHttpClient> constructor = asyncHttpClientImplClass.getConstructor(AsyncHttpProvider.class,
91+
AsyncHttpClientConfig.class);
8692
return constructor.newInstance(provider, config);
8793
} catch (Exception e) {
88-
throw new AsyncHttpClientImplException(
89-
"Unable to find the instantiate the class specified by system property : "
90-
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY
91-
+ "(AsyncHttpProvider) due to : " + e.getMessage(), e);
94+
throw new AsyncHttpClientImplException("Unable to find the instantiate the class specified by system property : "
95+
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY + "(AsyncHttpProvider) due to : " + e.getMessage(), e);
9296
}
9397
}
9498
return new DefaultAsyncHttpClient(provider, config);
@@ -101,10 +105,8 @@ public static AsyncHttpClient getAsyncHttpClient(String providerClass, AsyncHttp
101105
AsyncHttpClientConfig.class);
102106
return constructor.newInstance(providerClass, config);
103107
} catch (Exception e) {
104-
throw new AsyncHttpClientImplException(
105-
"Unable to find the instantiate the class specified by system property : "
106-
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY
107-
+ "(AsyncHttpProvider) due to : " + e.getMessage(), e);
108+
throw new AsyncHttpClientImplException("Unable to find the instantiate the class specified by system property : "
109+
+ AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY + "(AsyncHttpProvider) due to : " + e.getMessage(), e);
108110
}
109111
}
110112
return new DefaultAsyncHttpClient(providerClass, config);
@@ -115,8 +117,7 @@ private static boolean attemptInstantiation() {
115117
lock.lock();
116118
try {
117119
if (!instantiated) {
118-
asyncHttpClientImplClass = AsyncImplHelper
119-
.getAsyncImplClass(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY);
120+
asyncHttpClientImplClass = AsyncImplHelper.getAsyncImplClass(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY);
120121
instantiated = true;
121122
}
122123
} finally {

api/src/main/java/org/asynchttpclient/AsyncHttpClientImplException.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
/*
2+
* Copyright (c) 2010-2014 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
113
package org.asynchttpclient;
214

15+
@SuppressWarnings("serial")
316
public class AsyncHttpClientImplException extends RuntimeException {
417

518
public AsyncHttpClientImplException(String msg) {
@@ -9,5 +22,4 @@ public AsyncHttpClientImplException(String msg) {
922
public AsyncHttpClientImplException(String msg, Exception e) {
1023
super(msg, e);
1124
}
12-
1325
}

api/src/main/java/org/asynchttpclient/AsyncHttpClientRegistry.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/*
2+
* Copyright (c) 2010-2014 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
113
package org.asynchttpclient;
214

315
import java.util.Set;
@@ -58,13 +70,12 @@ public interface AsyncHttpClientRegistry {
5870
*
5971
* @return
6072
*/
61-
73+
6274
Set<String> getAllRegisteredNames();
6375

6476
/**
6577
* Removes all instances from this registry.
6678
*/
67-
68-
void clearAllInstances();
6979

70-
}
80+
void clearAllInstances();
81+
}

api/src/main/java/org/asynchttpclient/AsyncHttpClientRegistryImpl.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/*
2+
* Copyright (c) 2010-2014 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
113
package org.asynchttpclient;
214

315
import org.asynchttpclient.util.AsyncImplHelper;
@@ -23,19 +35,17 @@ public static AsyncHttpClientRegistry getInstance() {
2335
lock.lock();
2436
try {
2537
if (_instance == null) {
26-
Class asyncHttpClientRegistryImplClass = AsyncImplHelper
38+
Class<?> asyncHttpClientRegistryImplClass = AsyncImplHelper
2739
.getAsyncImplClass(AsyncImplHelper.ASYNC_HTTP_CLIENT_REGISTRY_SYSTEM_PROPERTY);
2840
if (asyncHttpClientRegistryImplClass != null)
2941
_instance = (AsyncHttpClientRegistry) asyncHttpClientRegistryImplClass.newInstance();
3042
else
3143
_instance = new AsyncHttpClientRegistryImpl();
3244
}
3345
} catch (InstantiationException e) {
34-
throw new AsyncHttpClientImplException("Couldn't instantiate AsyncHttpClientRegistry : "
35-
+ e.getMessage(), e);
46+
throw new AsyncHttpClientImplException("Couldn't instantiate AsyncHttpClientRegistry : " + e.getMessage(), e);
3647
} catch (IllegalAccessException e) {
37-
throw new AsyncHttpClientImplException("Couldn't instantiate AsyncHttpClientRegistry : "
38-
+ e.getMessage(), e);
48+
throw new AsyncHttpClientImplException("Couldn't instantiate AsyncHttpClientRegistry : " + e.getMessage(), e);
3949
} finally {
4050
lock.unlock();
4151
}
@@ -74,7 +84,7 @@ public AsyncHttpClient addOrReplace(String name, AsyncHttpClient ahc) {
7484
*/
7585
@Override
7686
public boolean registerIfNew(String name, AsyncHttpClient ahc) {
77-
return asyncHttpClientMap.putIfAbsent(name, ahc)==null;
87+
return asyncHttpClientMap.putIfAbsent(name, ahc) == null;
7888
}
7989

8090
/*
@@ -107,5 +117,4 @@ public Set<String> getAllRegisteredNames() {
107117
public void clearAllInstances() {
108118
asyncHttpClientMap.clear();
109119
}
110-
111120
}

api/src/main/java/org/asynchttpclient/RequestBuilderBase.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,18 +466,26 @@ public T addCookie(Cookie cookie) {
466466
return derived.cast(this);
467467
}
468468

469-
private void resetParameters() {
469+
public void resetQueryParameters() {
470+
request.queryParams = null;
471+
}
472+
473+
public void resetCookies() {
474+
request.cookies.clear();
475+
}
476+
477+
public void resetParameters() {
470478
request.params = null;
471479
}
472480

473-
private void resetNonMultipartData() {
481+
public void resetNonMultipartData() {
474482
request.byteData = null;
475483
request.stringData = null;
476484
request.streamData = null;
477485
request.length = -1;
478486
}
479487

480-
private void resetMultipartData() {
488+
public void resetMultipartData() {
481489
request.parts = null;
482490
}
483491

@@ -618,7 +626,7 @@ public Request build() {
618626
try {
619627
request.length = Long.parseLong(contentLength);
620628
} catch (NumberFormatException e) {
621-
// NoOp -- we wdn't specify length so it will be chunked?
629+
// NoOp -- we won't specify length so it will be chunked?
622630
}
623631
}
624632
}

api/src/main/java/org/asynchttpclient/date/RFC2616DateParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private Tokens tokenize() {
8383
}
8484

8585
// finish lastToken
86-
if (inToken = true)
86+
if (inToken == true)
8787
ends[tokenCount++] = end;
8888

8989
return new Tokens(starts, ends, tokenCount);

api/src/main/java/org/asynchttpclient/extra/ThrottleRequestFilter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ public ThrottleRequestFilter(int maxConnections) {
3535
}
3636

3737
public ThrottleRequestFilter(int maxConnections, int maxWait) {
38-
this.maxWait = maxWait;
39-
available = new Semaphore(maxConnections, true);
38+
this(maxConnections, maxWait, false);
39+
}
40+
41+
public ThrottleRequestFilter(int maxConnections, int maxWait, boolean fair) {
42+
this.maxWait = maxWait;
43+
available = new Semaphore(maxConnections, fair);
4044
}
4145

4246
/**

api/src/main/java/org/asynchttpclient/oauth/OAuthSignatureCalculator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ public String calculateSignature(String method, String baseURL, long oauthTimest
126126
allParameters.add(KEY_OAUTH_NONCE, nonce);
127127
allParameters.add(KEY_OAUTH_SIGNATURE_METHOD, OAUTH_SIGNATURE_METHOD);
128128
allParameters.add(KEY_OAUTH_TIMESTAMP, String.valueOf(oauthTimestamp));
129-
allParameters.add(KEY_OAUTH_TOKEN, userAuth.getKey());
129+
if (userAuth.getKey() != null) {
130+
allParameters.add(KEY_OAUTH_TOKEN, userAuth.getKey());
131+
}
130132
allParameters.add(KEY_OAUTH_VERSION, OAUTH_VERSION_1_0);
131133

132134
if (formParams != null) {
@@ -164,7 +166,9 @@ public String constructAuthHeader(String signature, String nonce, long oauthTime
164166
StringBuilder sb = new StringBuilder(200);
165167
sb.append("OAuth ");
166168
sb.append(KEY_OAUTH_CONSUMER_KEY).append("=\"").append(consumerAuth.getKey()).append("\", ");
167-
sb.append(KEY_OAUTH_TOKEN).append("=\"").append(userAuth.getKey()).append("\", ");
169+
if (userAuth.getKey() != null) {
170+
sb.append(KEY_OAUTH_TOKEN).append("=\"").append(userAuth.getKey()).append("\", ");
171+
}
168172
sb.append(KEY_OAUTH_SIGNATURE_METHOD).append("=\"").append(OAUTH_SIGNATURE_METHOD).append("\", ");
169173

170174
// careful: base64 has chars that need URL encoding:

0 commit comments

Comments
 (0)