96
96
import java .io .UnsupportedEncodingException ;
97
97
import java .net .InetSocketAddress ;
98
98
import java .net .URI ;
99
+ import java .net .URL ;
99
100
import java .net .URLEncoder ;
100
101
import java .nio .ByteBuffer ;
101
102
import java .security .NoSuchAlgorithmException ;
@@ -401,7 +402,7 @@ public void updated(WriteResult result) {
401
402
}
402
403
403
404
404
- private void setHttpTransactionContext (final AttributeStorage storage ,
405
+ void setHttpTransactionContext (final AttributeStorage storage ,
405
406
final HttpTransactionContext httpTransactionState ) {
406
407
407
408
if (httpTransactionState == null ) {
@@ -412,7 +413,7 @@ private void setHttpTransactionContext(final AttributeStorage storage,
412
413
413
414
}
414
415
415
- private HttpTransactionContext getHttpTransactionContext (final AttributeStorage storage ) {
416
+ HttpTransactionContext getHttpTransactionContext (final AttributeStorage storage ) {
416
417
417
418
return REQUEST_STATE_ATTR .get (storage );
418
419
@@ -497,26 +498,27 @@ boolean handleStatus(final HttpResponsePacket httpResponse,
497
498
} // END StatusHandler
498
499
499
500
500
- private final class HttpTransactionContext {
501
+ final class HttpTransactionContext {
501
502
502
- private final AtomicInteger redirectCount = new AtomicInteger (0 );
503
+ final AtomicInteger redirectCount = new AtomicInteger (0 );
503
504
504
- private final int maxRedirectCount ;
505
- private final boolean redirectsAllowed ;
506
- private final GrizzlyAsyncHttpProvider provider =
505
+ final int maxRedirectCount ;
506
+ final boolean redirectsAllowed ;
507
+ final GrizzlyAsyncHttpProvider provider =
507
508
GrizzlyAsyncHttpProvider .this ;
508
509
509
- private Request request ;
510
- private AsyncHandler handler ;
511
- private BodyHandler bodyHandler ;
512
- private StatusHandler statusHandler ;
513
- private StatusHandler .InvocationStatus invocationStatus =
510
+ Request request ;
511
+ String requestUrl ;
512
+ AsyncHandler handler ;
513
+ BodyHandler bodyHandler ;
514
+ StatusHandler statusHandler ;
515
+ StatusHandler .InvocationStatus invocationStatus =
514
516
StatusHandler .InvocationStatus .CONTINUE ;
515
- private GrizzlyResponseStatus responseStatus ;
516
- private GrizzlyResponseFuture future ;
517
- private String lastRedirectURI ;
518
- private AtomicLong totalBodyWritten = new AtomicLong ();
519
- private AsyncHandler .STATE currentState ;
517
+ GrizzlyResponseStatus responseStatus ;
518
+ GrizzlyResponseFuture future ;
519
+ String lastRedirectURI ;
520
+ AtomicLong totalBodyWritten = new AtomicLong ();
521
+ AsyncHandler .STATE currentState ;
520
522
521
523
522
524
// -------------------------------------------------------- Constructors
@@ -531,14 +533,15 @@ private final class HttpTransactionContext {
531
533
this .handler = handler ;
532
534
redirectsAllowed = provider .clientConfig .isRedirectEnabled ();
533
535
maxRedirectCount = provider .clientConfig .getMaxRedirects ();
536
+ this .requestUrl = request .getUrl ();
534
537
535
538
}
536
539
537
540
538
541
// ----------------------------------------------------- Private Methods
539
542
540
543
541
- private HttpTransactionContext copy () {
544
+ HttpTransactionContext copy () {
542
545
final HttpTransactionContext newContext =
543
546
new HttpTransactionContext (future ,
544
547
request ,
@@ -554,20 +557,20 @@ private HttpTransactionContext copy() {
554
557
}
555
558
556
559
557
- private void abort (final Throwable t ) {
560
+ void abort (final Throwable t ) {
558
561
if (future != null ) {
559
562
future .abort (t );
560
563
}
561
564
}
562
565
563
- private void done (final Callable c ) {
566
+ void done (final Callable c ) {
564
567
if (future != null ) {
565
568
future .done (c );
566
569
}
567
570
}
568
571
569
572
@ SuppressWarnings ({"unchecked" })
570
- private void result (Object result ) {
573
+ void result (Object result ) {
571
574
if (future != null ) {
572
575
future .delegate .result (result );
573
576
future .done (null );
@@ -699,7 +702,8 @@ private void sendAsGrizzlyRequest(final Request request,
699
702
final FilterChainContext ctx )
700
703
throws IOException {
701
704
702
- final URI uri = AsyncHttpProviderUtils .createUri (request .getUrl ());
705
+ final HttpTransactionContext httpCtx = getHttpTransactionContext (ctx .getConnection ());
706
+ final URI uri = AsyncHttpProviderUtils .createUri (httpCtx .requestUrl );
703
707
final HttpRequestPacket .Builder builder = HttpRequestPacket .builder ();
704
708
705
709
builder .method (request .getMethod ());
@@ -756,7 +760,7 @@ private void sendAsGrizzlyRequest(final Request request,
756
760
}
757
761
}
758
762
}
759
- final AsyncHandler h = getHttpTransactionContext ( ctx . getConnection ()) .handler ;
763
+ final AsyncHandler h = httpCtx .handler ;
760
764
if (TransferCompletionHandler .class .isAssignableFrom (h .getClass ())) {
761
765
final FluentCaseInsensitiveStringsMap map =
762
766
new FluentCaseInsensitiveStringsMap (request .getHeaders ());
@@ -1012,7 +1016,7 @@ protected void onInitialLineParsed(HttpHeader httpHeader,
1012
1016
}
1013
1017
final GrizzlyResponseStatus responseStatus =
1014
1018
new GrizzlyResponseStatus ((HttpResponsePacket ) httpHeader ,
1015
- getURI (context .request . getUrl () ),
1019
+ getURI (context .requestUrl ),
1016
1020
provider );
1017
1021
context .responseStatus = responseStatus ;
1018
1022
if (context .statusHandler != null ) {
@@ -1033,6 +1037,16 @@ protected void onInitialLineParsed(HttpHeader httpHeader,
1033
1037
1034
1038
}
1035
1039
1040
+ @ Override
1041
+ protected void onHttpError (final HttpHeader httpHeader ,
1042
+ final FilterChainContext ctx ,
1043
+ final Throwable t ) throws IOException {
1044
+ httpHeader .setSkipRemainder (true );
1045
+ final HttpTransactionContext context =
1046
+ provider .getHttpTransactionContext (ctx .getConnection ());
1047
+ context .abort (t );
1048
+ }
1049
+
1036
1050
@ SuppressWarnings ({"unchecked" })
1037
1051
@ Override
1038
1052
protected void onHttpHeadersParsed (HttpHeader httpHeader ,
@@ -1162,20 +1176,21 @@ private static boolean isRedirectAllowed(final HttpTransactionContext ctx) {
1162
1176
private static HttpTransactionContext cleanup (final FilterChainContext ctx ,
1163
1177
final GrizzlyAsyncHttpProvider provider ) {
1164
1178
1179
+ final Connection c = ctx .getConnection ();
1165
1180
final HttpTransactionContext context =
1166
- provider .getHttpTransactionContext (ctx . getConnection () );
1167
-
1168
- if (!context .provider .connectionManager .canReturnConnection (ctx . getConnection () )) {
1181
+ provider .getHttpTransactionContext (c );
1182
+ context . provider . setHttpTransactionContext ( c , null );
1183
+ if (!context .provider .connectionManager .canReturnConnection (c )) {
1169
1184
context .abort (new IOException ("Maximum pooled connections exceeded" ));
1170
1185
} else {
1171
- if (!context .provider .connectionManager .returnConnection (context .request . getUrl (), ctx . getConnection () )) {
1186
+ if (!context .provider .connectionManager .returnConnection (context .requestUrl , c )) {
1172
1187
try {
1173
1188
ctx .getConnection ().close ().markForRecycle (true );
1174
1189
} catch (IOException ignored ) {
1175
1190
}
1176
1191
}
1177
1192
}
1178
- context . provider . setHttpTransactionContext ( ctx . getConnection (), null );
1193
+
1179
1194
return context ;
1180
1195
1181
1196
}
@@ -1242,7 +1257,7 @@ public boolean handleStatus(final HttpResponsePacket responsePacket,
1242
1257
final Request req = httpTransactionContext .request ;
1243
1258
realm = new Realm .RealmBuilder ().clone (realm )
1244
1259
.setScheme (realm .getAuthScheme ())
1245
- .setUri (URI .create (req . getUrl () ).getPath ())
1260
+ .setUri (URI .create (httpTransactionContext . requestUrl ).getPath ())
1246
1261
.setMethodName (req .getMethod ())
1247
1262
.setUsePreemptiveAuth (true )
1248
1263
.parseWWWAuthenticateHeader (auth )
@@ -1319,9 +1334,9 @@ public boolean handleStatus(final HttpResponsePacket responsePacket,
1319
1334
1320
1335
URI orig ;
1321
1336
if (httpTransactionContext .lastRedirectURI == null ) {
1322
- orig = AsyncHttpProviderUtils .createUri (httpTransactionContext .request . getUrl () );
1337
+ orig = AsyncHttpProviderUtils .createUri (httpTransactionContext .requestUrl );
1323
1338
} else {
1324
- orig = AsyncHttpProviderUtils .getRedirectUri (AsyncHttpProviderUtils .createUri (httpTransactionContext .request . getUrl () ),
1339
+ orig = AsyncHttpProviderUtils .getRedirectUri (AsyncHttpProviderUtils .createUri (httpTransactionContext .requestUrl ),
1325
1340
httpTransactionContext .lastRedirectURI );
1326
1341
}
1327
1342
httpTransactionContext .lastRedirectURI = redirectURL ;
@@ -1359,6 +1374,7 @@ public boolean handleStatus(final HttpResponsePacket responsePacket,
1359
1374
httpTransactionContext .future = null ;
1360
1375
newContext .invocationStatus = InvocationStatus .CONTINUE ;
1361
1376
newContext .request = requestToSend ;
1377
+ newContext .requestUrl = requestToSend .getUrl ();
1362
1378
httpTransactionContext .provider .setHttpTransactionContext (c , newContext );
1363
1379
httpTransactionContext .provider .execute (c ,
1364
1380
requestToSend ,
@@ -1959,9 +1975,9 @@ static boolean isConnectionCacheable(final Connection c) {
1959
1975
return ((canCache != null ) ? canCache : false );
1960
1976
}
1961
1977
1962
- private void doAsyncTrackedConnection (final Request request ,
1963
- final GrizzlyResponseFuture requestFuture ,
1964
- final CompletionHandler <Connection > connectHandler )
1978
+ void doAsyncTrackedConnection (final Request request ,
1979
+ final GrizzlyResponseFuture requestFuture ,
1980
+ final CompletionHandler <Connection > connectHandler )
1965
1981
throws IOException , ExecutionException , InterruptedException {
1966
1982
final String url = request .getUrl ();
1967
1983
Connection c = pool .poll (AsyncHttpProviderUtils .getBaseUrl (url ));
@@ -2010,10 +2026,10 @@ Connection obtainConnection(final Request request,
2010
2026
2011
2027
}
2012
2028
2013
- private void doAsyncConnect (final String url ,
2014
- final Request request ,
2015
- final GrizzlyResponseFuture requestFuture ,
2016
- final CompletionHandler <Connection > connectHandler )
2029
+ void doAsyncConnect (final String url ,
2030
+ final Request request ,
2031
+ final GrizzlyResponseFuture requestFuture ,
2032
+ final CompletionHandler <Connection > connectHandler )
2017
2033
throws IOException , ExecutionException , InterruptedException {
2018
2034
2019
2035
final URI uri = AsyncHttpProviderUtils .createUri (url );
0 commit comments