Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit e25bd7f

Browse files
committed
Merge pull request #5 from davidgev/master
Updated async-http-client and netty versions to fix Connection leak issue.
2 parents 3137dd4 + f47b729 commit e25bd7f

File tree

7 files changed

+137
-81
lines changed

7 files changed

+137
-81
lines changed

.gitignore

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
1+
######################
2+
# Logs
3+
######################
4+
*.log
5+
/logs
6+
7+
######################
8+
# Build output directies
9+
######################
10+
/target
11+
*/target
12+
/build
13+
*/build
14+
test-output
115
*.class
216
*~
317
.*.swp
418
.*.swo
519
.loadpath
620
.buildpath
21+
22+
######################
23+
# Eclipse specific files/directories
24+
######################
725
.classpath
826
.project
927
.settings
28+
.metadata
29+
30+
######################
31+
# IntelliJ specific files/directories
32+
######################
33+
out
1034
.idea
1135
*.iml
1236
*.ipr
1337
*.iws
38+
*.iml
39+
atlassian-ide-plugin.xml
1440
nbproject
15-
.DS_Store
16-
target
17-
test-output
1841
/META-INF/MANIFEST.MF
42+
43+
######################
44+
# OS generated files
45+
######################
46+
.DS_Store*
47+
ehthumbs.db
48+
Icon?
49+
Thumbs.db

pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
</licenses>
6363
<properties>
6464
<jersey.version>1.14</jersey.version>
65+
<async-http-client-version>1.8.5</async-http-client-version>
66+
<netty-version>3.9.0.Final</netty-version>
6567
</properties>
6668
<dependencies>
6769
<dependency>
@@ -83,7 +85,7 @@
8385
<dependency>
8486
<groupId>com.ning</groupId>
8587
<artifactId>async-http-client</artifactId>
86-
<version>1.7.6</version>
88+
<version>${async-http-client-version}</version>
8789
</dependency>
8890
<dependency>
8991
<groupId>com.sun.xml.bind</groupId>
@@ -121,6 +123,11 @@
121123
<version>${jersey.version}</version>
122124
<scope>test</scope>
123125
</dependency>
126+
<dependency>
127+
<groupId>io.netty</groupId>
128+
<artifactId>netty</artifactId>
129+
<version>${netty-version}</version>
130+
</dependency>
124131
</dependencies>
125132
<build>
126133
<defaultGoal>install</defaultGoal>
@@ -149,6 +156,7 @@
149156
<groupId>org.apache.felix</groupId>
150157
<artifactId>maven-bundle-plugin</artifactId>
151158
<extensions>true</extensions>
159+
<version>2.4.0</version>
152160
<configuration>
153161
<manifestLocation>META-INF</manifestLocation>
154162
<instructions>
@@ -197,7 +205,11 @@
197205
<plugin>
198206
<groupId>org.apache.maven.plugins</groupId>
199207
<artifactId>maven-compiler-plugin</artifactId>
208+
<version>2.0.2</version>
200209
<configuration>
210+
<source>1.6</source>
211+
<target>1.6</target>
212+
<compilerVersion>1.6</compilerVersion>
201213
<maxmem>1024m</maxmem>
202214
</configuration>
203215
</plugin>

src/main/java/org/sonatype/spice/jersey/client/ahc/AhcClientHandler.java

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,27 @@
1111
*******************************************************************************/
1212
package org.sonatype.spice.jersey.client.ahc;
1313

14-
import com.ning.http.client.*;
14+
import java.util.ArrayList;
15+
import java.util.Iterator;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
import javax.ws.rs.core.Context;
20+
21+
import org.sonatype.spice.jersey.client.ahc.config.AhcConfig;
22+
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig;
23+
24+
import com.ning.http.client.AsyncHttpClient;
25+
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
26+
import com.ning.http.client.RequestBuilder;
27+
import com.ning.http.client.Response;
28+
import com.ning.http.client.cookie.Cookie;
1529
import com.sun.jersey.api.client.ClientHandler;
1630
import com.sun.jersey.api.client.ClientHandlerException;
1731
import com.sun.jersey.api.client.ClientRequest;
1832
import com.sun.jersey.api.client.ClientResponse;
1933
import com.sun.jersey.core.header.InBoundHeaders;
2034
import com.sun.jersey.spi.MessageBodyWorkers;
21-
import org.sonatype.spice.jersey.client.ahc.config.AhcConfig;
22-
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig;
23-
24-
import javax.ws.rs.core.Context;
25-
import java.util.ArrayList;
26-
import java.util.Iterator;
27-
import java.util.List;
28-
import java.util.Map;
2935

3036
/**
3137
* A root handler with Sonatype AsyncHttpClient acting as a backend.
@@ -55,7 +61,7 @@ public final class AhcClientHandler implements ClientHandler {
5561

5662
private final AhcRequestWriter requestWriter = new AhcRequestWriter();
5763

58-
private List<Cookie> cookies = new ArrayList<Cookie>();
64+
private final List<Cookie> cookies = new ArrayList<Cookie>();
5965

6066
@Context
6167
private MessageBodyWorkers workers;
@@ -65,7 +71,7 @@ public final class AhcClientHandler implements ClientHandler {
6571
*
6672
* @param client the {@link AsyncHttpClient}.
6773
*/
68-
public AhcClientHandler(AsyncHttpClient client) {
74+
public AhcClientHandler(final AsyncHttpClient client) {
6975
this(client, new DefaultAhcConfig());
7076
}
7177

@@ -75,7 +81,7 @@ public AhcClientHandler(AsyncHttpClient client) {
7581
* @param client the {@link AsyncHttpClient}.
7682
* @param config the client configuration.
7783
*/
78-
public AhcClientHandler(AsyncHttpClient client, AhcConfig config) {
84+
public AhcClientHandler(final AsyncHttpClient client, final AhcConfig config) {
7985
this.client = client;
8086
this.config = config;
8187
}
@@ -105,6 +111,7 @@ public AsyncHttpClient getHttpClient() {
105111
* @return the {@link ClientResponse}
106112
* @throws ClientHandlerException
107113
*/
114+
@Override
108115
public ClientResponse handle(final ClientRequest cr)
109116
throws ClientHandlerException {
110117

@@ -117,7 +124,7 @@ public ClientResponse handle(final ClientRequest cr)
117124

118125
applyResponseCookies(response.getCookies());
119126

120-
ClientResponse r = new ClientResponse(response.getStatusCode(),
127+
final ClientResponse r = new ClientResponse(response.getStatusCode(),
121128
getInBoundHeaders(response),
122129
response.getResponseBodyAsStream(),
123130
workers);
@@ -126,7 +133,7 @@ public ClientResponse handle(final ClientRequest cr)
126133
r.close();
127134
}
128135
return r;
129-
} catch (Exception e) {
136+
} catch (final Exception e) {
130137
throw new ClientHandlerException(e);
131138
}
132139
}
@@ -136,13 +143,13 @@ public ClientResponse handle(final ClientRequest cr)
136143
*
137144
* @param responseCookies list of cookies from response
138145
*/
139-
private void applyResponseCookies(List<Cookie> responseCookies) {
146+
private void applyResponseCookies(final List<Cookie> responseCookies) {
140147
if (responseCookies != null) {
141-
for (Cookie rc : responseCookies) {
148+
for (final Cookie rc : responseCookies) {
142149
// remove existing cookie
143-
Iterator<Cookie> it = cookies.iterator();
150+
final Iterator<Cookie> it = cookies.iterator();
144151
while (it.hasNext()) {
145-
Cookie c = it.next();
152+
final Cookie c = it.next();
146153
if (isSame(rc, c)) {
147154
it.remove();
148155
break;
@@ -154,13 +161,13 @@ private void applyResponseCookies(List<Cookie> responseCookies) {
154161
}
155162
}
156163

157-
private boolean isSame(Cookie c, Cookie o) {
164+
private boolean isSame(final Cookie c, final Cookie o) {
158165
return isEquals(c.getDomain(), o.getDomain()) &&
159166
isEquals(c.getPath(), o.getPath()) &&
160167
isEquals(c.getName(), o.getName());
161168
}
162169

163-
private boolean isEquals(Object o, Object o2) {
170+
private boolean isEquals(final Object o, final Object o2) {
164171
return (o == null && o2 == null) || o != null && o.equals(o2);
165172
}
166173

@@ -170,7 +177,7 @@ private boolean isEquals(Object o, Object o2) {
170177
* @param method An HTTP method
171178
* @return true if s body can be allowed.
172179
*/
173-
private boolean allowBody(String method) {
180+
private boolean allowBody(final String method) {
174181
if (method.equalsIgnoreCase("GET") || method.equalsIgnoreCase("OPTIONS")
175182
&& method.equalsIgnoreCase("TRACE")
176183
&& method.equalsIgnoreCase("HEAD")) {
@@ -186,7 +193,7 @@ private boolean allowBody(String method) {
186193
* @param cr the HTTP request.
187194
* @return {@link RequestBuilder}
188195
*/
189-
private RequestBuilder getRequestBuilder(ClientRequest cr) {
196+
private RequestBuilder getRequestBuilder(final ClientRequest cr) {
190197
final String strMethod = cr.getMethod();
191198
final String uri = cr.getURI().toString();
192199

@@ -207,10 +214,10 @@ private RequestBuilder getRequestBuilder(ClientRequest cr) {
207214
}
208215
}
209216

210-
private InBoundHeaders getInBoundHeaders(Response response) {
211-
InBoundHeaders headers = new InBoundHeaders();
212-
FluentCaseInsensitiveStringsMap respHeaders = response.getHeaders();
213-
for (Map.Entry<String, List<String>> header : respHeaders) {
217+
private InBoundHeaders getInBoundHeaders(final Response response) {
218+
final InBoundHeaders headers = new InBoundHeaders();
219+
final FluentCaseInsensitiveStringsMap respHeaders = response.getHeaders();
220+
for (final Map.Entry<String, List<String>> header : respHeaders) {
214221
headers.put(header.getKey(), header.getValue());
215222
}
216223
return headers;
@@ -226,8 +233,8 @@ public AhcRequestWriter getAhcRequestWriter() {
226233
return requestWriter;
227234
}
228235

229-
private void handleCookie(RequestBuilder requestBuilder) {
230-
for (Cookie c : cookies) {
236+
private void handleCookie(final RequestBuilder requestBuilder) {
237+
for (final Cookie c : cookies) {
231238
requestBuilder.addCookie(c);
232239
}
233240
}

src/main/java/org/sonatype/spice/jersey/client/ahc/AhcHttpClient.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
*******************************************************************************/
1212
package org.sonatype.spice.jersey.client.ahc;
1313

14+
import org.sonatype.spice.jersey.client.ahc.config.AhcConfig;
15+
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig;
16+
1417
import com.ning.http.client.AsyncHttpClient;
1518
import com.sun.jersey.api.client.Client;
1619
import com.sun.jersey.api.client.config.ClientConfig;
17-
import org.sonatype.spice.jersey.client.ahc.config.AhcConfig;
18-
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig;
1920
import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory;
2021

2122
/**
@@ -41,8 +42,8 @@
4142
*/
4243
public class AhcHttpClient extends Client {
4344

44-
private AhcClientHandler clientHandler;
45-
45+
private final AhcClientHandler clientHandler;
46+
4647
/**
4748
* Create a new client instance.
4849
*
@@ -57,7 +58,7 @@ public AhcHttpClient() {
5758
* @param root the root client handler for dispatching a request and
5859
* returning a response.
5960
*/
60-
public AhcHttpClient(AhcClientHandler root) {
61+
public AhcHttpClient(final AhcClientHandler root) {
6162
this(root, null);
6263
}
6364

@@ -74,8 +75,8 @@ public AhcHttpClient(AhcClientHandler root) {
7475
* is utilized instead.
7576
*/
7677
@Deprecated
77-
public AhcHttpClient(AhcClientHandler root, ClientConfig config,
78-
IoCComponentProviderFactory provider) {
78+
public AhcHttpClient(final AhcClientHandler root, final ClientConfig config,
79+
final IoCComponentProviderFactory provider) {
7980
this(root, provider);
8081
}
8182

@@ -87,8 +88,8 @@ public AhcHttpClient(AhcClientHandler root, ClientConfig config,
8788
* returning a response.
8889
* @param provider the IoC component provider factory.
8990
*/
90-
public AhcHttpClient(AhcClientHandler root,
91-
IoCComponentProviderFactory provider) {
91+
public AhcHttpClient(final AhcClientHandler root,
92+
final IoCComponentProviderFactory provider) {
9293
super(root, root.getConfig(), provider);
9394

9495
this.clientHandler = root;
@@ -119,7 +120,7 @@ public static AhcHttpClient create() {
119120
* @param cc the client configuration.
120121
* @return a default client.
121122
*/
122-
public static AhcHttpClient create(ClientConfig cc) {
123+
public static AhcHttpClient create(final ClientConfig cc) {
123124
return create(cc, null);
124125
}
125126

@@ -130,7 +131,7 @@ public static AhcHttpClient create(ClientConfig cc) {
130131
* @param provider the IoC component provider factory.
131132
* @return a default client.
132133
*/
133-
public static AhcHttpClient create(ClientConfig cc, IoCComponentProviderFactory provider) {
134+
public static AhcHttpClient create(final ClientConfig cc, final IoCComponentProviderFactory provider) {
134135
return new AhcHttpClient(createDefaultClientHander(cc), provider);
135136
}
136137

@@ -151,7 +152,7 @@ protected void finalize(){
151152
} finally {
152153
try {
153154
super.finalize();
154-
} catch (Throwable e) {
155+
} catch (final Throwable e) {
155156
// TODO swallow?
156157
}
157158
}
@@ -162,28 +163,28 @@ protected void finalize(){
162163
*
163164
* @return a default AsyncHttpClient client handler.
164165
*/
165-
private static AhcClientHandler createDefaultClientHander(ClientConfig cc) {
166+
private static AhcClientHandler createDefaultClientHander(final ClientConfig cc) {
166167

167168
if (AhcConfig.class.isAssignableFrom(cc.getClass()) || DefaultAhcConfig.class.isAssignableFrom(cc.getClass())) {
168-
AhcConfig c = AhcConfig.class.cast(cc);
169+
final AhcConfig c = AhcConfig.class.cast(cc);
169170
return new AhcClientHandler(new AsyncHttpClient(c.getAsyncHttpClientConfigBuilder().build()), c);
170171
} else {
171172
throw new IllegalStateException("Client Config Type not supported");
172173
}
173174
}
174175

175176
@Override
176-
public void setFollowRedirects(Boolean redirect) {
177+
public void setFollowRedirects(final Boolean redirect) {
177178
clientHandler.getConfig().getAsyncHttpClientConfigBuilder().setFollowRedirects(redirect);
178179
}
179180

180181
@Override
181-
public void setReadTimeout(Integer interval) {
182+
public void setReadTimeout(final Integer interval) {
182183
clientHandler.getConfig().getAsyncHttpClientConfigBuilder().setRequestTimeoutInMs(interval);
183184
}
184185

185186
@Override
186-
public void setConnectTimeout(Integer interval) {
187+
public void setConnectTimeout(final Integer interval) {
187188
clientHandler.getConfig().getAsyncHttpClientConfigBuilder().setConnectionTimeoutInMs(interval);
188189
}
189190
}

0 commit comments

Comments
 (0)