Skip to content

Commit 0cf8266

Browse files
author
oleksiys
committed
[1.9.x] + grizzly refactoring and NTLM support
1 parent 66544eb commit 0cf8266

17 files changed

+2170
-1793
lines changed

src/main/java/com/ning/http/client/providers/grizzly/AhcEventFilter.java

Lines changed: 764 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/com/ning/http/client/providers/grizzly/ConnectionManager.java

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,42 @@ static boolean isConnectionCacheable(final Connection c) {
8686
return (canCache != null) ? canCache : false;
8787
}
8888

89-
void doAsyncTrackedConnection(final Request request,
89+
void getConnectionAsync(final Request request,
9090
final GrizzlyResponseFuture requestFuture,
9191
final CompletionHandler<Connection> connectHandler)
9292
throws IOException, ExecutionException, InterruptedException {
9393

94-
Connection c = pool.poll(getPartitionId(request, requestFuture.getProxy()));
94+
final ProxyServer proxy = ProxyUtils.getProxyServer(config, request);
95+
96+
Connection c = pool.poll(getPartitionId(request, proxy));
9597
if (c == null) {
9698
if (!connectionMonitor.acquire()) {
9799
throw new IOException("Max connections exceeded");
98100
}
99-
doAsyncConnect(request, requestFuture, connectHandler);
101+
openConnectionAsync(request, proxy, requestFuture, connectHandler);
100102
} else {
101103
provider.touchConnection(c, request);
102104
connectHandler.completed(c);
103105
}
104106
}
105107

106-
Connection obtainConnection(final Request request,
108+
Connection openConnectionSync(final Request request,
107109
final GrizzlyResponseFuture requestFuture)
108110
throws IOException, ExecutionException,
109111
InterruptedException, TimeoutException {
110112

111-
final Connection c = obtainConnection0(request, requestFuture);
113+
final Connection c = openConnectionSync0(request,
114+
ProxyUtils.getProxyServer(config, request), requestFuture);
112115
DO_NOT_CACHE.set(c, Boolean.TRUE);
113116
return c;
114117
}
115118

116-
void doAsyncConnect(final Request request,
119+
private void openConnectionAsync(final Request request,
120+
final ProxyServer proxy,
117121
final GrizzlyResponseFuture requestFuture,
118122
final CompletionHandler<Connection> connectHandler)
119123
throws IOException, ExecutionException, InterruptedException {
120124

121-
ProxyServer proxy = requestFuture.getProxy();
122125
final Uri uri = request.getUri();
123126
String host = (proxy != null) ? proxy.getHost() : uri.getHost();
124127
int port = (proxy != null) ? proxy.getPort() : uri.getPort();
@@ -132,40 +135,33 @@ void doAsyncConnect(final Request request,
132135
HostnameVerifierListener.wrapWithHostnameVerifierHandler(
133136
completionHandler, verifier, uri.getHost());
134137
}
135-
if (request.getLocalAddress() != null) {
136-
connectionHandler.connect(
137-
new InetSocketAddress(host, getPort(uri, port)),
138-
new InetSocketAddress(request.getLocalAddress(), 0),
139-
completionHandler);
140-
} else {
141-
connectionHandler.connect(
142-
new InetSocketAddress(host, getPort(uri, port)),
143-
completionHandler);
144-
}
138+
139+
connectionHandler.connect(
140+
new InetSocketAddress(host, getPort(uri, port)),
141+
new InetSocketAddress(request.getLocalAddress(), 0),
142+
completionHandler);
145143
}
146144

147-
private Connection obtainConnection0(final Request request,
145+
private Connection openConnectionSync0(final Request request,
146+
final ProxyServer proxy,
148147
final GrizzlyResponseFuture requestFuture)
149148
throws IOException, ExecutionException,
150149
InterruptedException, TimeoutException {
151150

152151
final Uri uri = request.getUri();
153-
final ProxyServer proxy = requestFuture.getProxy();
154152
String host = (proxy != null) ? proxy.getHost() : uri.getHost();
155153
int port = (proxy != null) ? proxy.getPort() : uri.getPort();
156154
int cTimeout = config.getConnectTimeout();
157155
FutureImpl<Connection> future = Futures.createSafeFuture();
158156
CompletionHandler<Connection> ch = Futures.toCompletionHandler(future,
159157
createConnectionCompletionHandler(request, requestFuture, null));
160-
if (cTimeout > 0) {
161-
connectionHandler.connect(
162-
new InetSocketAddress(host, getPort(uri, port)), ch);
163-
return future.get(cTimeout, TimeUnit.MILLISECONDS);
164-
} else {
165-
connectionHandler.connect(
166-
new InetSocketAddress(host, getPort(uri, port)), ch);
167-
return future.get();
168-
}
158+
159+
connectionHandler.connect(
160+
new InetSocketAddress(host, getPort(uri, port)), ch);
161+
162+
return cTimeout > 0
163+
? future.get(cTimeout, TimeUnit.MILLISECONDS)
164+
: future.get();
169165
}
170166

171167
boolean returnConnection(final Request request, final Connection c) {
@@ -188,7 +184,7 @@ void destroy() {
188184
pool.destroy();
189185
}
190186

191-
CompletionHandler<Connection> createConnectionCompletionHandler(
187+
private CompletionHandler<Connection> createConnectionCompletionHandler(
192188
final Request request, final GrizzlyResponseFuture future,
193189
final CompletionHandler<Connection> wrappedHandler) {
194190

@@ -201,16 +197,15 @@ public void cancelled() {
201197
}
202198
}
203199

204-
public void failed(Throwable throwable) {
200+
public void failed(final Throwable throwable) {
205201
if (wrappedHandler != null) {
206202
wrappedHandler.failed(throwable);
207203
} else {
208204
future.abort(throwable);
209205
}
210206
}
211207

212-
public void completed(Connection connection) {
213-
future.setConnection(connection);
208+
public void completed(final Connection connection) {
214209
provider.touchConnection(connection, request);
215210
if (wrappedHandler != null) {
216211
connection.addCloseListener(connectionMonitor);

src/main/java/com/ning/http/client/providers/grizzly/FeedableBodyGenerator.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static java.lang.Boolean.TRUE;
3838
import static java.util.concurrent.TimeUnit.MILLISECONDS;
3939
import static org.glassfish.grizzly.ssl.SSLUtils.getSSLEngine;
40+
import org.glassfish.grizzly.utils.Exceptions;
4041
import static org.glassfish.grizzly.utils.Exceptions.*;
4142

4243
/**
@@ -177,7 +178,7 @@ public void run() {
177178
feeder.flush();
178179
}
179180
} catch (IOException ioe) {
180-
HttpTransactionContext.currentTransaction(c).abort(ioe);
181+
HttpTransactionContext.currentTransaction(requestPacket).abort(ioe);
181182
}
182183
}
183184
};
@@ -212,8 +213,8 @@ public void onStart(Connection connection) {
212213
}
213214

214215
@Override
215-
public void onFailure(Connection connection, Throwable t) {
216-
HttpTransactionContext.currentTransaction(c).abort(t);
216+
public void onFailure(final Connection connection, final Throwable t) {
217+
connection.closeWithReason(Exceptions.makeIOException(t));
217218
}
218219

219220
public void onComplete(Connection connection) {
@@ -222,7 +223,7 @@ public void onComplete(Connection connection) {
222223
try {
223224
feeder.flush();
224225
} catch (IOException ioe) {
225-
HttpTransactionContext.currentTransaction(c).abort(ioe);
226+
connection.closeWithReason(ioe);
226227
}
227228
}
228229
}
@@ -379,9 +380,9 @@ private static void block(final Connection c,
379380
future.get();
380381
}
381382
} catch (ExecutionException e) {
382-
HttpTransactionContext.currentTransaction(c).abort(e.getCause());
383+
c.closeWithReason(Exceptions.makeIOException(e.getCause()));
383384
} catch (Exception e) {
384-
HttpTransactionContext.currentTransaction(c).abort(e);
385+
c.closeWithReason(Exceptions.makeIOException(e));
385386
}
386387
}
387388

@@ -587,7 +588,7 @@ public void onWritePossible() throws Exception {
587588
@Override
588589
public void onError(Throwable t) {
589590
c.setMaxAsyncWriteQueueSize(feedableBodyGenerator.origMaxPendingBytes);
590-
HttpTransactionContext.currentTransaction(c).abort(t);
591+
c.closeWithReason(Exceptions.makeIOException(t));
591592
}
592593

593594
} // END WriteHandlerImpl
@@ -607,7 +608,7 @@ public void ready() {
607608
} catch (IOException e) {
608609
final Connection c = feedableBodyGenerator.context.getConnection();
609610
c.setMaxAsyncWriteQueueSize(feedableBodyGenerator.origMaxPendingBytes);
610-
HttpTransactionContext.currentTransaction(c).abort(e);
611+
c.closeWithReason(Exceptions.makeIOException(e));
611612
}
612613
}
613614

0 commit comments

Comments
 (0)