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 numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
*/
1717
package org.asynchttpclient;
1818

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

2521
import java.io.InputStream;
@@ -29,6 +25,10 @@
2925
import java.util.ArrayList;
3026
import java.util.List;
3127

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

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

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

1616
import static org.asynchttpclient.Dsl.realm;
1717
import static org.asynchttpclient.util.AuthenticatorUtils.*;
18+
import static org.asynchttpclient.util.MiscUtils.withDefault;
1819
import io.netty.channel.Channel;
1920
import io.netty.handler.codec.http.DefaultHttpHeaders;
2021
import io.netty.handler.codec.http.HttpHeaders;
@@ -212,7 +213,7 @@ private void kerberosChallenge(Channel channel,//
212213
NettyResponseFuture<?> future) throws SpnegoEngineException {
213214

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

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

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

16-
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCEPT;
17-
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCEPT_ENCODING;
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;
16+
import static io.netty.handler.codec.http.HttpHeaders.Names.*;
17+
import static org.asynchttpclient.util.AuthenticatorUtils.*;
3118
import static org.asynchttpclient.util.HttpUtils.*;
32-
import static org.asynchttpclient.util.AuthenticatorUtils.perRequestAuthorizationHeader;
33-
import static org.asynchttpclient.util.AuthenticatorUtils.perRequestProxyAuthorizationHeader;
34-
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
19+
import static org.asynchttpclient.util.MiscUtils.*;
3520
import static org.asynchttpclient.ws.WebSocketUtils.getKey;
3621
import io.netty.buffer.ByteBuf;
3722
import io.netty.handler.codec.http.DefaultFullHttpRequest;
@@ -78,7 +63,7 @@ private NettyBody body(Request request, boolean connect) {
7863
NettyBody nettyBody = null;
7964
if (!connect) {
8065

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

8368
if (request.getByteData() != null)
8469
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 numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.asynchttpclient.request.body.multipart;
1414

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

1719
/**
@@ -42,10 +44,10 @@ public abstract class FileLikePart extends PartBase {
4244
*/
4345
public FileLikePart(String name, String contentType, Charset charset, String contentId, String transfertEncoding) {
4446
super(name,//
45-
contentType == null ? DEFAULT_CONTENT_TYPE : contentType,//
47+
withDefault(contentType, DEFAULT_CONTENT_TYPE),//
4648
charset,//
4749
contentId,//
48-
transfertEncoding == null ? DEFAULT_TRANSFER_ENCODING : transfertEncoding);
50+
withDefault(transfertEncoding, DEFAULT_TRANSFER_ENCODING));
4951
}
5052

5153
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 numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import static java.nio.charset.StandardCharsets.US_ASCII;
1616
import static org.asynchttpclient.util.Assertions.assertNotNull;
17+
import static org.asynchttpclient.util.MiscUtils.withDefault;
1718

1819
import java.nio.charset.Charset;
1920

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

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

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

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

5455
public StringPart(String name, String value) {

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

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

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

5454
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 numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package org.asynchttpclient.extras.simple;
1414

1515
import static org.asynchttpclient.Dsl.*;
16-
import static org.asynchttpclient.util.MiscUtils.closeSilently;
16+
import static org.asynchttpclient.util.MiscUtils.*;
1717
import io.netty.handler.codec.http.HttpHeaders;
1818
import io.netty.handler.ssl.SslContext;
1919

@@ -660,7 +660,7 @@ public SimpleAsyncHttpClient build() {
660660
if (proxyHost != null) {
661661
Realm realm = null;
662662
if (proxyPrincipal != null) {
663-
AuthScheme proxyAuthScheme = this.proxyAuthScheme == null ? AuthScheme.BASIC : this.proxyAuthScheme;
663+
AuthScheme proxyAuthScheme = withDefault(this.proxyAuthScheme, AuthScheme.BASIC);
664664
realm = realm(proxyAuthScheme, proxyPrincipal, proxyPassword).build();
665665
}
666666

0 commit comments

Comments
 (0)