Skip to content

Commit 96ad5ef

Browse files
committed
Use withDefault
1 parent 10411a3 commit 96ad5ef

File tree

7 files changed

+22
-33
lines changed

7 files changed

+22
-33
lines changed

client/src/main/java/org/asynchttpclient/Response.java

Lines changed: 4 additions & 4 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -16,10 +16,6 @@
16
*/
16
*/
17
package org.asynchttpclient;
17
package org.asynchttpclient;
18

18

19-
import org.asynchttpclient.cookie.Cookie;
20-
import org.asynchttpclient.netty.NettyResponse;
21-
import org.asynchttpclient.uri.Uri;
22-
23
import io.netty.handler.codec.http.HttpHeaders;
19
import io.netty.handler.codec.http.HttpHeaders;
24

20

25
import java.io.InputStream;
21
import java.io.InputStream;
@@ -29,6 +25,10 @@
29
import java.util.ArrayList;
25
import java.util.ArrayList;
30
import java.util.List;
26
import java.util.List;
31

27

28+
import org.asynchttpclient.cookie.Cookie;
29+
import org.asynchttpclient.netty.NettyResponse;
30+
import org.asynchttpclient.uri.Uri;
31+
32
/**
32
/**
33
* Represents the asynchronous HTTP response callback for an {@link AsyncCompletionHandler}
33
* Represents the asynchronous HTTP response callback for an {@link AsyncCompletionHandler}
34
*/
34
*/

client/src/main/java/org/asynchttpclient/netty/handler/intercept/Unauthorized401Interceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -15,6 +15,7 @@
15

15

16
import static org.asynchttpclient.Dsl.realm;
16
import static org.asynchttpclient.Dsl.realm;
17
import static org.asynchttpclient.util.AuthenticatorUtils.*;
17
import static org.asynchttpclient.util.AuthenticatorUtils.*;
18+
import static org.asynchttpclient.util.MiscUtils.withDefault;
18
import io.netty.channel.Channel;
19
import io.netty.channel.Channel;
19
import io.netty.handler.codec.http.DefaultHttpHeaders;
20
import io.netty.handler.codec.http.DefaultHttpHeaders;
20
import io.netty.handler.codec.http.HttpHeaders;
21
import io.netty.handler.codec.http.HttpHeaders;
@@ -212,7 +213,7 @@ private void kerberosChallenge(Channel channel,//
212
NettyResponseFuture<?> future) throws SpnegoEngineException {
213
NettyResponseFuture<?> future) throws SpnegoEngineException {
213

214

214
Uri uri = request.getUri();
215
Uri uri = request.getUri();
215-
String host = request.getVirtualHost() == null ? uri.getHost() : request.getVirtualHost();
216+
String host = withDefault(request.getVirtualHost(), uri.getHost());
216
String challengeHeader = SpnegoEngine.instance().generateToken(host);
217
String challengeHeader = SpnegoEngine.instance().generateToken(host);
217
headers.set(HttpHeaders.Names.AUTHORIZATION, NEGOTIATE + " " + challengeHeader);
218
headers.set(HttpHeaders.Names.AUTHORIZATION, NEGOTIATE + " " + challengeHeader);
218
}
219
}

client/src/main/java/org/asynchttpclient/netty/request/NettyRequestFactory.java

Lines changed: 4 additions & 19 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -13,25 +13,10 @@
13
*/
13
*/
14
package org.asynchttpclient.netty.request;
14
package org.asynchttpclient.netty.request;
15

15

16-
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCEPT;
16+
import static io.netty.handler.codec.http.HttpHeaders.Names.*;
17-
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCEPT_ENCODING;
17+
import static org.asynchttpclient.util.AuthenticatorUtils.*;
18-
import static io.netty.handler.codec.http.HttpHeaders.Names.AUTHORIZATION;
19-
import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
20-
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
21-
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
22-
import static io.netty.handler.codec.http.HttpHeaders.Names.COOKIE;
23-
import static io.netty.handler.codec.http.HttpHeaders.Names.HOST;
24-
import static io.netty.handler.codec.http.HttpHeaders.Names.ORIGIN;
25-
import static io.netty.handler.codec.http.HttpHeaders.Names.PROXY_AUTHORIZATION;
26-
import static io.netty.handler.codec.http.HttpHeaders.Names.SEC_WEBSOCKET_KEY;
27-
import static io.netty.handler.codec.http.HttpHeaders.Names.SEC_WEBSOCKET_VERSION;
28-
import static io.netty.handler.codec.http.HttpHeaders.Names.TRANSFER_ENCODING;
29-
import static io.netty.handler.codec.http.HttpHeaders.Names.UPGRADE;
30-
import static io.netty.handler.codec.http.HttpHeaders.Names.USER_AGENT;
31
import static org.asynchttpclient.util.HttpUtils.*;
18
import static org.asynchttpclient.util.HttpUtils.*;
32-
import static org.asynchttpclient.util.AuthenticatorUtils.perRequestAuthorizationHeader;
19+
import static org.asynchttpclient.util.MiscUtils.*;
33-
import static org.asynchttpclient.util.AuthenticatorUtils.perRequestProxyAuthorizationHeader;
34-
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
35
import static org.asynchttpclient.ws.WebSocketUtils.getKey;
20
import static org.asynchttpclient.ws.WebSocketUtils.getKey;
36
import io.netty.buffer.ByteBuf;
21
import io.netty.buffer.ByteBuf;
37
import io.netty.handler.codec.http.DefaultFullHttpRequest;
22
import io.netty.handler.codec.http.DefaultFullHttpRequest;
@@ -78,7 +63,7 @@ private NettyBody body(Request request, boolean connect) {
78
NettyBody nettyBody = null;
63
NettyBody nettyBody = null;
79
if (!connect) {
64
if (!connect) {
80

65

81-
Charset bodyCharset = request.getCharset() == null ? DEFAULT_CHARSET : request.getCharset();
66+
Charset bodyCharset = withDefault(request.getCharset(), DEFAULT_CHARSET);
82

67

83
if (request.getByteData() != null)
68
if (request.getByteData() != null)
84
nettyBody = new NettyByteArrayBody(request.getByteData());
69
nettyBody = new NettyByteArrayBody(request.getByteData());

client/src/main/java/org/asynchttpclient/request/body/multipart/FileLikePart.java

Lines changed: 4 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -12,6 +12,8 @@
12
*/
12
*/
13
package org.asynchttpclient.request.body.multipart;
13
package org.asynchttpclient.request.body.multipart;
14

14

15+
import static org.asynchttpclient.util.MiscUtils.withDefault;
16+
15
import java.nio.charset.Charset;
17
import java.nio.charset.Charset;
16

18

17
/**
19
/**
@@ -42,10 +44,10 @@ public abstract class FileLikePart extends PartBase {
42
*/
44
*/
43
public FileLikePart(String name, String contentType, Charset charset, String contentId, String transfertEncoding) {
45
public FileLikePart(String name, String contentType, Charset charset, String contentId, String transfertEncoding) {
44
super(name,//
46
super(name,//
45-
contentType == null ? DEFAULT_CONTENT_TYPE : contentType,//
47+
withDefault(contentType, DEFAULT_CONTENT_TYPE),//
46
charset,//
48
charset,//
47
contentId,//
49
contentId,//
48-
transfertEncoding == null ? DEFAULT_TRANSFER_ENCODING : transfertEncoding);
50+
withDefault(transfertEncoding, DEFAULT_TRANSFER_ENCODING));
49
}
51
}
50

52

51
public final void setFileName(String fileName) {
53
public final void setFileName(String fileName) {

client/src/main/java/org/asynchttpclient/request/body/multipart/StringPart.java

Lines changed: 4 additions & 3 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -14,6 +14,7 @@
14

14

15
import static java.nio.charset.StandardCharsets.US_ASCII;
15
import static java.nio.charset.StandardCharsets.US_ASCII;
16
import static org.asynchttpclient.util.Assertions.assertNotNull;
16
import static org.asynchttpclient.util.Assertions.assertNotNull;
17+
import static org.asynchttpclient.util.MiscUtils.withDefault;
17

18

18
import java.nio.charset.Charset;
19
import java.nio.charset.Charset;
19

20

@@ -40,15 +41,15 @@ public class StringPart extends PartBase {
40
private final String value;
41
private final String value;
41

42

42
private static Charset charsetOrDefault(Charset charset) {
43
private static Charset charsetOrDefault(Charset charset) {
43-
return charset == null ? DEFAULT_CHARSET : charset;
44+
return withDefault(charset, DEFAULT_CHARSET);
44
}
45
}
45

46

46
private static String contentTypeOrDefault(String contentType) {
47
private static String contentTypeOrDefault(String contentType) {
47-
return contentType == null ? DEFAULT_CONTENT_TYPE : contentType;
48+
return withDefault(contentType, DEFAULT_CONTENT_TYPE);
48
}
49
}
49

50

50
private static String transferEncodingOrDefault(String transferEncoding) {
51
private static String transferEncodingOrDefault(String transferEncoding) {
51-
return transferEncoding == null ? DEFAULT_TRANSFER_ENCODING : transferEncoding;
52+
return withDefault(transferEncoding, DEFAULT_TRANSFER_ENCODING);
52
}
53
}
53

54

54
public StringPart(String name, String value) {
55
public StringPart(String name, String value) {

client/src/main/java/org/asynchttpclient/util/MiscUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -47,8 +47,8 @@ public static boolean getBoolean(String systemPropName, boolean defaultValue) {
47
return systemPropValue != null ? systemPropValue.equalsIgnoreCase("true") : defaultValue;
47
return systemPropValue != null ? systemPropValue.equalsIgnoreCase("true") : defaultValue;
48
}
48
}
49

49

50-
public static <T> T withDefault(T value, T defaults) {
50+
public static <T> T withDefault(T value, T def) {
51-
return value != null ? value : value;
51+
return value == null ? def : value;
52
}
52
}
53

53

54
public static void closeSilently(Closeable closeable) {
54
public static void closeSilently(Closeable closeable) {

extras/simple/src/main/java/org/asynchttpclient/extras/simple/SimpleAsyncHttpClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -13,7 +13,7 @@
13
package org.asynchttpclient.extras.simple;
13
package org.asynchttpclient.extras.simple;
14

14

15
import static org.asynchttpclient.Dsl.*;
15
import static org.asynchttpclient.Dsl.*;
16-
import static org.asynchttpclient.util.MiscUtils.closeSilently;
16+
import static org.asynchttpclient.util.MiscUtils.*;
17
import io.netty.handler.codec.http.HttpHeaders;
17
import io.netty.handler.codec.http.HttpHeaders;
18
import io.netty.handler.ssl.SslContext;
18
import io.netty.handler.ssl.SslContext;
19

19

@@ -660,7 +660,7 @@ public SimpleAsyncHttpClient build() {
660
if (proxyHost != null) {
660
if (proxyHost != null) {
661
Realm realm = null;
661
Realm realm = null;
662
if (proxyPrincipal != null) {
662
if (proxyPrincipal != null) {
663-
AuthScheme proxyAuthScheme = this.proxyAuthScheme == null ? AuthScheme.BASIC : this.proxyAuthScheme;
663+
AuthScheme proxyAuthScheme = withDefault(this.proxyAuthScheme, AuthScheme.BASIC);
664
realm = realm(proxyAuthScheme, proxyPrincipal, proxyPassword).build();
664
realm = realm(proxyAuthScheme, proxyPrincipal, proxyPassword).build();
665
}
665
}
666

666

0 commit comments

Comments
 (0)