Skip to content

Commit 604ea93

Browse files
committed
Clean up Exception handling
1 parent 34a3066 commit 604ea93

File tree

23 files changed

+151
-295
lines changed

23 files changed

+151
-295
lines changed

api/src/main/java/org/asynchttpclient/netty/request/NettyRequestFactoryBase.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121
import static org.asynchttpclient.util.HttpUtils.useProxyConnect;
2222
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
2323

24-
import java.io.IOException;
2524
import java.util.List;
2625

2726
import org.asynchttpclient.AsyncHttpClientConfig;
2827
import org.asynchttpclient.Realm;
29-
import org.asynchttpclient.Request;
3028
import org.asynchttpclient.Realm.AuthScheme;
29+
import org.asynchttpclient.Request;
3130
import org.asynchttpclient.ntlm.NtlmEngine;
3231
import org.asynchttpclient.proxy.ProxyServer;
3332
import org.asynchttpclient.spnego.SpnegoEngine;
@@ -43,7 +42,7 @@ public NettyRequestFactoryBase(AsyncHttpClientConfig config) {
4342

4443
protected abstract List<String> getProxyAuthorizationHeader(Request request);
4544

46-
protected String firstRequestOnlyProxyAuthorizationHeader(Request request, ProxyServer proxyServer, boolean connect) throws IOException {
45+
protected String firstRequestOnlyProxyAuthorizationHeader(Request request, ProxyServer proxyServer, boolean connect) {
4746
String proxyAuthorization = null;
4847

4948
if (connect) {
@@ -111,7 +110,7 @@ protected String systematicProxyAuthorizationHeader(Request request, ProxyServer
111110
return proxyAuthorization;
112111
}
113112

114-
protected String firstRequestOnlyAuthorizationHeader(Request request, ProxyServer proxyServer, Realm realm) throws IOException {
113+
protected String firstRequestOnlyAuthorizationHeader(Request request, ProxyServer proxyServer, Realm realm) {
115114
String authorizationHeader = null;
116115

117116
if (realm != null && realm.getUsePreemptiveAuth()) {
@@ -130,11 +129,7 @@ else if (request.getVirtualHost() != null)
130129
else
131130
host = request.getUri().getHost();
132131

133-
try {
134-
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
135-
} catch (Throwable e) {
136-
throw new IOException(e);
137-
}
132+
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
138133
break;
139134
default:
140135
break;

api/src/main/java/org/asynchttpclient/ntlm/NtlmEngineException.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,14 @@
2626

2727
package org.asynchttpclient.ntlm;
2828

29+
2930
/**
3031
* Signals NTLM protocol failure.
31-
*
32-
* @since 4.0
3332
*/
34-
public class NtlmEngineException extends Exception {
33+
public class NtlmEngineException extends RuntimeException {
3534

3635
private static final long serialVersionUID = 6027981323731768824L;
3736

38-
public NtlmEngineException() {
39-
super();
40-
}
41-
4237
/**
4338
* Creates a new NTLMEngineException with the specified message.
4439
*

api/src/main/java/org/asynchttpclient/request/body/generator/BodyGenerator.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
package org.asynchttpclient.request.body.generator;
1515

16-
import java.io.IOException;
17-
1816
import org.asynchttpclient.request.body.Body;
1917

2018
/**
@@ -28,7 +26,6 @@ public interface BodyGenerator {
2826
* needs to be resend after an authentication challenge of a redirect.
2927
*
3028
* @return The request body, never {@code null}.
31-
* @throws IOException If the body could not be created.
3229
*/
33-
Body createBody() throws IOException;
30+
Body createBody();
3431
}

api/src/main/java/org/asynchttpclient/request/body/generator/ByteArrayBodyGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void close() throws IOException {
6464
* {@inheritDoc}
6565
*/
6666
@Override
67-
public Body createBody() throws IOException {
67+
public Body createBody() {
6868
return new ByteBody();
6969
}
7070
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1313
*/
14-
package org.asynchttpclient.netty.request.body;
14+
package org.asynchttpclient.request.body.generator;
1515

1616
import static java.nio.charset.StandardCharsets.US_ASCII;
1717

@@ -22,7 +22,6 @@
2222
import java.util.concurrent.atomic.AtomicInteger;
2323

2424
import org.asynchttpclient.request.body.Body;
25-
import org.asynchttpclient.request.body.generator.BodyGenerator;
2625

2726
/**
2827
* {@link BodyGenerator} which may return just part of the payload at the time handler is requesting it.
@@ -36,7 +35,7 @@ public final class FeedableBodyGenerator implements BodyGenerator {
3635
private FeedListener listener;
3736

3837
@Override
39-
public Body createBody() throws IOException {
38+
public Body createBody() {
4039
return new PushBody();
4140
}
4241

api/src/main/java/org/asynchttpclient/request/body/generator/FileBodyGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package org.asynchttpclient.request.body.generator;
1414

1515
import java.io.File;
16-
import java.io.IOException;
1716

1817
import org.asynchttpclient.request.body.RandomAccessBody;
1918

@@ -55,7 +54,7 @@ public long getRegionSeek() {
5554
* {@inheritDoc}
5655
*/
5756
@Override
58-
public RandomAccessBody createBody() throws IOException {
57+
public RandomAccessBody createBody() {
5958
throw new UnsupportedOperationException("FileBodyGenerator.createBody isn't used, Netty direclt sends the file");
6059
}
6160
}

api/src/main/java/org/asynchttpclient/request/body/generator/InputStreamBodyGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public InputStream getInputStream() {
4747
* {@inheritDoc}
4848
*/
4949
@Override
50-
public Body createBody() throws IOException {
50+
public Body createBody() {
5151
return new InputStreamBody(inputStream);
5252
}
5353

api/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static SpnegoEngine instance() {
7878
return instance;
7979
}
8080

81-
public String generateToken(String server) throws Throwable {
81+
public String generateToken(String server) throws SpnegoEngineException {
8282
GSSContext gssContext = null;
8383
byte[] token = null; // base64 decoded challenge
8484
Oid negotiationOid = null;
@@ -140,7 +140,7 @@ public String generateToken(String server) throws Throwable {
140140

141141
token = gssContext.initSecContext(token, 0, token.length);
142142
if (token == null) {
143-
throw new Exception("GSS security context initialization failed");
143+
throw new SpnegoEngineException("GSS security context initialization failed");
144144
}
145145

146146
/*
@@ -159,16 +159,16 @@ public String generateToken(String server) throws Throwable {
159159
} catch (GSSException gsse) {
160160
log.error("generateToken", gsse);
161161
if (gsse.getMajor() == GSSException.DEFECTIVE_CREDENTIAL || gsse.getMajor() == GSSException.CREDENTIALS_EXPIRED)
162-
throw new Exception(gsse.getMessage(), gsse);
162+
throw new SpnegoEngineException(gsse.getMessage(), gsse);
163163
if (gsse.getMajor() == GSSException.NO_CRED)
164-
throw new Exception(gsse.getMessage(), gsse);
164+
throw new SpnegoEngineException(gsse.getMessage(), gsse);
165165
if (gsse.getMajor() == GSSException.DEFECTIVE_TOKEN || gsse.getMajor() == GSSException.DUPLICATE_TOKEN
166166
|| gsse.getMajor() == GSSException.OLD_TOKEN)
167-
throw new Exception(gsse.getMessage(), gsse);
167+
throw new SpnegoEngineException(gsse.getMessage(), gsse);
168168
// other error
169-
throw new Exception(gsse.getMessage());
169+
throw new SpnegoEngineException(gsse.getMessage());
170170
} catch (IOException ex) {
171-
throw new Exception(ex.getMessage());
171+
throw new SpnegoEngineException(ex.getMessage());
172172
}
173173
}
174174
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2015 AsyncHttpClient Project. 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
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package org.asynchttpclient.spnego;
15+
16+
/**
17+
* Signals SPNEGO protocol failure.
18+
*/
19+
public class SpnegoEngineException extends RuntimeException {
20+
21+
private static final long serialVersionUID = -3123799505052881438L;
22+
23+
public SpnegoEngineException(String message) {
24+
super(message);
25+
}
26+
27+
public SpnegoEngineException(String message, Throwable cause) {
28+
super(message, cause);
29+
}
30+
}

api/src/test/java/org/asynchttpclient/simple/SimpleAsyncHttpClientTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.ByteArrayInputStream;
2323
import java.io.ByteArrayOutputStream;
2424
import java.io.File;
25-
import java.io.IOException;
2625
import java.util.ArrayList;
2726
import java.util.Collections;
2827
import java.util.List;
@@ -287,8 +286,8 @@ public void testCloseDerivedValidMaster() throws Exception {
287286
}
288287
}
289288

290-
@Test(groups = { "standalone", "default_provider" })
291-
public void testCloseMasterInvalidDerived() throws Exception {
289+
@Test(groups = { "standalone", "default_provider" }, expectedExceptions = { IllegalStateException.class })
290+
public void testCloseMasterInvalidDerived() throws Throwable {
292291
SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setUrl(getTargetUrl()).build();
293292
SimpleAsyncHttpClient derived = client.derive().build();
294293

@@ -298,7 +297,7 @@ public void testCloseMasterInvalidDerived() throws Exception {
298297
derived.get().get();
299298
fail("Expected closed AHC");
300299
} catch (ExecutionException e) {
301-
assertTrue(e.getCause() instanceof IOException);
300+
throw e.getCause();
302301
}
303302
}
304303

providers/netty3/src/main/java/org/asynchttpclient/netty/handler/HttpProtocol.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
import java.util.List;
2626

2727
import org.asynchttpclient.AsyncHandler;
28+
import org.asynchttpclient.AsyncHandler.State;
2829
import org.asynchttpclient.AsyncHttpClientConfig;
2930
import org.asynchttpclient.FluentCaseInsensitiveStringsMap;
3031
import org.asynchttpclient.Realm;
32+
import org.asynchttpclient.Realm.AuthScheme;
3133
import org.asynchttpclient.Request;
3234
import org.asynchttpclient.RequestBuilder;
33-
import org.asynchttpclient.AsyncHandler.State;
34-
import org.asynchttpclient.Realm.AuthScheme;
3535
import org.asynchttpclient.channel.pool.ConnectionStrategy;
3636
import org.asynchttpclient.netty.NettyAsyncHttpProviderConfig;
3737
import org.asynchttpclient.netty.NettyResponseBodyPart;
@@ -41,7 +41,6 @@
4141
import org.asynchttpclient.netty.channel.ChannelManager;
4242
import org.asynchttpclient.netty.request.NettyRequestSender;
4343
import org.asynchttpclient.ntlm.NtlmEngine;
44-
import org.asynchttpclient.ntlm.NtlmEngineException;
4544
import org.asynchttpclient.proxy.ProxyServer;
4645
import org.asynchttpclient.spnego.SpnegoEngine;
4746
import org.asynchttpclient.uri.Uri;
@@ -69,7 +68,7 @@ private Realm kerberosChallenge(Channel channel,//
6968
Request request,//
7069
FluentCaseInsensitiveStringsMap headers,//
7170
Realm realm,//
72-
NettyResponseFuture<?> future) throws NtlmEngineException {
71+
NettyResponseFuture<?> future) {
7372

7473
Uri uri = request.getUri();
7574
String host = request.getVirtualHost() == null ? uri.getHost() : request.getVirtualHost();
@@ -100,7 +99,7 @@ private Realm kerberosProxyChallenge(Channel channel,//
10099
Request request,//
101100
ProxyServer proxyServer,//
102101
FluentCaseInsensitiveStringsMap headers,//
103-
NettyResponseFuture<?> future) throws NtlmEngineException {
102+
NettyResponseFuture<?> future) {
104103

105104
try {
106105
String challengeHeader = SpnegoEngine.instance().generateToken(proxyServer.getHost());
@@ -135,7 +134,7 @@ private Realm ntlmChallenge(String authenticateHeader,//
135134
Request request,//
136135
FluentCaseInsensitiveStringsMap headers,//
137136
Realm realm,//
138-
NettyResponseFuture<?> future) throws NtlmEngineException {
137+
NettyResponseFuture<?> future) {
139138

140139
if (authenticateHeader.equals("NTLM")) {
141140
// server replied bare NTLM => we didn't preemptively sent Type1Msg
@@ -159,7 +158,7 @@ private Realm ntlmProxyChallenge(String authenticateHeader,//
159158
Request request,//
160159
ProxyServer proxyServer,//
161160
FluentCaseInsensitiveStringsMap headers,//
162-
NettyResponseFuture<?> future) throws NtlmEngineException {
161+
NettyResponseFuture<?> future) {
163162

164163
future.getAndSetAuth(false);
165164
headers.remove(HttpHeaders.Names.PROXY_AUTHORIZATION);
@@ -174,7 +173,7 @@ private Realm ntlmProxyChallenge(String authenticateHeader,//
174173
return realm;
175174
}
176175

177-
private void addType3NTLMAuthorizationHeader(String auth, FluentCaseInsensitiveStringsMap headers, Realm realm, boolean proxyInd) throws NtlmEngineException {
176+
private void addType3NTLMAuthorizationHeader(String auth, FluentCaseInsensitiveStringsMap headers, Realm realm, boolean proxyInd) {
178177
headers.remove(authorizationHeaderName(proxyInd));
179178

180179
if (isNonEmpty(auth) && auth.startsWith("NTLM ")) {
@@ -184,7 +183,7 @@ private void addType3NTLMAuthorizationHeader(String auth, FluentCaseInsensitiveS
184183
}
185184
}
186185

187-
private void finishUpdate(final NettyResponseFuture<?> future, Channel channel, boolean expectOtherChunks) throws IOException {
186+
private void finishUpdate(final NettyResponseFuture<?> future, Channel channel, boolean expectOtherChunks) {
188187

189188
future.cancelTimeouts();
190189

@@ -217,7 +216,7 @@ private boolean exitAfterHandling401(//
217216
final Request request,//
218217
int statusCode,//
219218
Realm realm,//
220-
ProxyServer proxyServer) throws Exception {
219+
ProxyServer proxyServer) {
221220

222221
if (statusCode == UNAUTHORIZED.getCode() && realm != null && !future.getAndSetAuth(true)) {
223222

@@ -284,7 +283,7 @@ private boolean exitAfterHandling407(//
284283
Request request,//
285284
int statusCode,//
286285
Realm realm,//
287-
ProxyServer proxyServer) throws Exception {
286+
ProxyServer proxyServer) {
288287

289288
if (statusCode == PROXY_AUTHENTICATION_REQUIRED.getCode() && realm != null && !future.getAndSetAuth(true)) {
290289

@@ -341,7 +340,7 @@ private boolean exitAfterHandlingConnect(//
341340
final Request request,//
342341
ProxyServer proxyServer,//
343342
int statusCode,//
344-
HttpRequest httpRequest) throws IOException {
343+
HttpRequest httpRequest) {
345344

346345
if (statusCode == OK.getCode() && httpRequest.getMethod() == HttpMethod.CONNECT) {
347346

@@ -357,21 +356,22 @@ private boolean exitAfterHandlingConnect(//
357356
logger.debug("Connecting to proxy {} for scheme {}", proxyServer, scheme);
358357
channelManager.upgradeProtocol(channel.getPipeline(), scheme, host, port);
359358

360-
} catch (Throwable ex) {
359+
future.setReuseChannel(true);
360+
future.setConnectAllowed(false);
361+
requestSender.sendNextRequest(new RequestBuilder(future.getRequest()).build(), future);
362+
363+
} catch (Exception ex) {
361364
requestSender.abort(channel, future, ex);
362365
}
363366

364-
future.setReuseChannel(true);
365-
future.setConnectAllowed(false);
366-
requestSender.sendNextRequest(new RequestBuilder(future.getRequest()).build(), future);
367367
return true;
368368
}
369369

370370
return false;
371371
}
372372

373373
private boolean exitAfterHandlingStatus(Channel channel, NettyResponseFuture<?> future, HttpResponse response,
374-
AsyncHandler<?> handler, NettyResponseStatus status) throws IOException, Exception {
374+
AsyncHandler<?> handler, NettyResponseStatus status) throws Exception {
375375
if (!future.getAndSetStatusReceived(true) && handler.onStatusReceived(status) != State.CONTINUE) {
376376
finishUpdate(future, channel, HttpHeaders.isTransferEncodingChunked(response));
377377
return true;
@@ -380,7 +380,7 @@ private boolean exitAfterHandlingStatus(Channel channel, NettyResponseFuture<?>
380380
}
381381

382382
private boolean exitAfterHandlingHeaders(Channel channel, NettyResponseFuture<?> future, HttpResponse response,
383-
AsyncHandler<?> handler, NettyResponseHeaders responseHeaders) throws IOException, Exception {
383+
AsyncHandler<?> handler, NettyResponseHeaders responseHeaders) throws Exception {
384384
if (!response.headers().isEmpty() && handler.onHeadersReceived(responseHeaders) != State.CONTINUE) {
385385
finishUpdate(future, channel, HttpHeaders.isTransferEncodingChunked(response));
386386
return true;
@@ -437,7 +437,7 @@ private boolean handleHttpResponse(final HttpResponse response,//
437437
private void handleChunk(HttpChunk chunk,//
438438
final Channel channel,//
439439
final NettyResponseFuture<?> future,//
440-
AsyncHandler<?> handler) throws IOException, Exception {
440+
AsyncHandler<?> handler) throws Exception {
441441

442442
boolean last = chunk.isLast();
443443
// we don't notify updateBodyAndInterrupt with the last chunk as it's empty

0 commit comments

Comments
 (0)