Skip to content

Commit b1a6280

Browse files
author
Stephane Landelle
committed
Sick with those (not null and not empty) tests everywhere
1 parent 5faa1cd commit b1a6280

20 files changed

+114
-53
lines changed

src/main/java/com/ning/http/client/Realm.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package com.ning.http.client;
1818

19+
import static com.ning.http.util.MiscUtil.isNonEmpty;
20+
1921
import org.slf4j.Logger;
2022
import org.slf4j.LoggerFactory;
2123

@@ -579,7 +581,7 @@ private static String toBase16(byte[] bytes) {
579581
public Realm build() {
580582

581583
// Avoid generating
582-
if (nonce != null && !nonce.equals("")) {
584+
if (isNonEmpty(nonce)) {
583585
newCnonce();
584586
try {
585587
newResponse();

src/main/java/com/ning/http/client/RequestBuilderBase.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.ning.http.client;
1717

18+
import static com.ning.http.util.MiscUtil.isNonEmpty;
19+
1820
import com.ning.http.client.Request.EntityWriter;
1921
import com.ning.http.util.UTF8UrlEncoder;
2022
import org.slf4j.Logger;
@@ -145,7 +147,7 @@ private String toUrl(boolean encode) {
145147
}
146148
}
147149

148-
if (queryParams != null && !queryParams.isEmpty()) {
150+
if (isNonEmpty(queryParams)) {
149151

150152
StringBuilder builder = new StringBuilder();
151153
if (!url.substring(8).contains("/")) { // no other "/" than http[s]:// -> http://localhost:1234
@@ -300,15 +302,15 @@ public String toString() {
300302
sb.append("\t");
301303
sb.append(method);
302304
sb.append("\theaders:");
303-
if (headers != null && !headers.isEmpty()) {
305+
if (isNonEmpty(headers)) {
304306
for (String name : headers.keySet()) {
305307
sb.append("\t");
306308
sb.append(name);
307309
sb.append(":");
308310
sb.append(headers.getJoinedValue(name, ", "));
309311
}
310312
}
311-
if (params != null && !params.isEmpty()) {
313+
if (isNonEmpty(params)) {
312314
sb.append("\tparams:");
313315
for (String name : params.keySet()) {
314316
sb.append("\t");
@@ -384,7 +386,7 @@ private String buildUrl(String url) {
384386
}
385387
}
386388

387-
if (uri.getRawQuery() != null && !uri.getRawQuery().equals("")) {
389+
if (isNonEmpty(uri.getRawQuery())) {
388390
String[] queries = uri.getRawQuery().split("&");
389391
int pos;
390392
for (String query : queries) {

src/main/java/com/ning/http/client/ntlm/NTLMEngine.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@
3838

3939
package com.ning.http.client.ntlm;
4040

41-
import com.ning.http.util.Base64;
41+
import static com.ning.http.util.MiscUtil.isNonEmpty;
4242

43-
import javax.crypto.Cipher;
44-
import javax.crypto.spec.SecretKeySpec;
4543
import java.io.UnsupportedEncodingException;
4644
import java.security.Key;
4745
import java.security.MessageDigest;
4846
import java.util.Arrays;
4947
import java.util.Locale;
5048

49+
import javax.crypto.Cipher;
50+
import javax.crypto.spec.SecretKeySpec;
51+
52+
import com.ning.http.util.Base64;
53+
5154
/**
5255
* Provides an implementation for NTLMv1, NTLMv2, and NTLM2 Session forms of the NTLM
5356
* authentication protocol.
@@ -123,12 +126,12 @@ final String getResponseFor(String message, String username, String password,
123126
String host, String domain) throws NTLMEngineException {
124127

125128
final String response;
126-
if (message == null || message.trim().equals("")) {
127-
response = getType1Message(host, domain);
128-
} else {
129+
if (isNonEmpty(message)) {
129130
Type2Message t2m = new Type2Message(message);
130131
response = getType3Message(username, password, host, domain, t2m.getChallenge(), t2m
131132
.getFlags(), t2m.getTarget(), t2m.getTargetInfo());
133+
} else {
134+
response = getType1Message(host, domain);
132135
}
133136
return response;
134137
}

src/main/java/com/ning/http/client/providers/apache/ApacheAsyncHttpProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.ning.http.client.providers.apache;
1414

15+
import static com.ning.http.util.MiscUtil.isNonEmpty;
16+
1517
import com.ning.http.client.AsyncHandler;
1618
import com.ning.http.client.AsyncHttpClientConfig;
1719
import com.ning.http.client.AsyncHttpProvider;
@@ -355,7 +357,7 @@ private HttpMethodBase createMethod(HttpClient client, Request request) throws I
355357
}
356358

357359
method.setFollowRedirects(false);
358-
if ((request.getCookies() != null) && !request.getCookies().isEmpty()) {
360+
if (isNonEmpty(request.getCookies())) {
359361
for (Cookie cookie : request.getCookies()) {
360362
method.setRequestHeader("Cookie", AsyncHttpProviderUtils.encodeCookies(request.getCookies()));
361363
}

src/main/java/com/ning/http/client/providers/apache/ApacheResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.ning.http.client.providers.apache;
1414

15+
import static com.ning.http.util.MiscUtil.isNonEmpty;
16+
1517
import com.ning.http.client.Cookie;
1618
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
1719
import com.ning.http.client.HttpResponseBodyPart;
@@ -190,6 +192,6 @@ public boolean hasResponseHeaders() {
190192
*/
191193
/* @Override */
192194
public boolean hasResponseBody() {
193-
return bodyParts != null && !bodyParts.isEmpty();
195+
return isNonEmpty(bodyParts);
194196
}
195197
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package com.ning.http.client.providers.grizzly;
1515

16+
import static com.ning.http.util.MiscUtil.isNonEmpty;
17+
1618
import com.ning.http.client.AsyncHandler;
1719
import com.ning.http.client.AsyncHttpClientConfig;
1820
import com.ning.http.client.AsyncHttpProvider;
@@ -970,11 +972,11 @@ private void addHeaders(final Request request,
970972
final HttpRequestPacket requestPacket) {
971973

972974
final FluentCaseInsensitiveStringsMap map = request.getHeaders();
973-
if (map != null && !map.isEmpty()) {
975+
if (isNonEmpty(map)) {
974976
for (final Map.Entry<String, List<String>> entry : map.entrySet()) {
975977
final String headerName = entry.getKey();
976978
final List<String> headerValues = entry.getValue();
977-
if (headerValues != null && !headerValues.isEmpty()) {
979+
if (isNonEmpty(headerValues)) {
978980
for (final String headerValue : headerValues) {
979981
requestPacket.addHeader(headerName, headerValue);
980982
}
@@ -1003,7 +1005,7 @@ private void addCookies(final Request request,
10031005
final HttpRequestPacket requestPacket) {
10041006

10051007
final Collection<Cookie> cookies = request.getCookies();
1006-
if (cookies != null && !cookies.isEmpty()) {
1008+
if (isNonEmpty(cookies)) {
10071009
StringBuilder sb = new StringBuilder(128);
10081010
org.glassfish.grizzly.http.Cookie[] gCookies =
10091011
new org.glassfish.grizzly.http.Cookie[cookies.size()];
@@ -1037,12 +1039,12 @@ private void addQueryString(final Request request,
10371039
final HttpRequestPacket requestPacket) {
10381040

10391041
final FluentStringsMap map = request.getQueryParams();
1040-
if (map != null && !map.isEmpty()) {
1042+
if (isNonEmpty(map)) {
10411043
StringBuilder sb = new StringBuilder(128);
10421044
for (final Map.Entry<String, List<String>> entry : map.entrySet()) {
10431045
final String name = entry.getKey();
10441046
final List<String> values = entry.getValue();
1045-
if (values != null && !values.isEmpty()) {
1047+
if (isNonEmpty(values)) {
10461048
try {
10471049
for (int i = 0, len = values.size(); i < len; i++) {
10481050
final String value = values.get(i);
@@ -1948,8 +1950,7 @@ private final class ParamsBodyHandler implements BodyHandler {
19481950

19491951

19501952
public boolean handlesBodyType(final Request request) {
1951-
final FluentStringsMap params = request.getParams();
1952-
return (params != null && !params.isEmpty());
1953+
return isNonEmpty(request.getParams());
19531954
}
19541955

19551956
@SuppressWarnings({"unchecked"})
@@ -1971,7 +1972,7 @@ public boolean doHandle(final FilterChainContext ctx,
19711972
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
19721973
String name = entry.getKey();
19731974
List<String> values = entry.getValue();
1974-
if (values != null && !values.isEmpty()) {
1975+
if (isNonEmpty(values)) {
19751976
if (sb == null) {
19761977
sb = new StringBuilder(128);
19771978
}
@@ -2095,8 +2096,7 @@ private static final class PartsBodyHandler implements BodyHandler {
20952096

20962097

20972098
public boolean handlesBodyType(final Request request) {
2098-
final List<Part> parts = request.getParts();
2099-
return (parts != null && !parts.isEmpty());
2099+
return isNonEmpty(request.getParts());
21002100
}
21012101

21022102
@SuppressWarnings({"unchecked"})

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

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

1414
package com.ning.http.client.providers.grizzly;
1515

16+
import static com.ning.http.util.MiscUtil.isNonEmpty;
17+
1618
import com.ning.http.client.Cookie;
1719
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
1820
import com.ning.http.client.HttpResponseBodyPart;
@@ -68,7 +70,7 @@ public GrizzlyResponse(final HttpResponseStatus status,
6870
this.headers = headers;
6971
this.bodyParts = bodyParts;
7072

71-
if (bodyParts != null && !bodyParts.isEmpty()) {
73+
if (isNonEmpty(bodyParts)) {
7274
if (bodyParts.size() == 1) {
7375
responseBody = ((GrizzlyResponseBodyPart) bodyParts.get(0)).getBodyBuffer();
7476
} else {
@@ -261,7 +263,7 @@ public List<Cookie> getCookies() {
261263

262264
if (cookies == null) {
263265
List<String> values = headers.getHeaders().get("set-cookie");
264-
if (values != null && !values.isEmpty()) {
266+
if (isNonEmpty(values)) {
265267
CookiesBuilder.ServerCookiesBuilder builder =
266268
new CookiesBuilder.ServerCookiesBuilder(false);
267269
for (String header : values) {
@@ -290,15 +292,15 @@ public boolean hasResponseStatus() {
290292
* {@inheritDoc}
291293
*/
292294
public boolean hasResponseHeaders() {
293-
return (headers != null && !headers.getHeaders().isEmpty());
295+
return headers != null && !headers.getHeaders().isEmpty();
294296
}
295297

296298

297299
/**
298300
* {@inheritDoc}
299301
*/
300302
public boolean hasResponseBody() {
301-
return (bodyParts != null && !bodyParts.isEmpty());
303+
return isNonEmpty(bodyParts);
302304
}
303305

304306

src/main/java/com/ning/http/client/providers/jdk/JDKAsyncHttpProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.ning.http.client.providers.jdk;
1414

15+
import static com.ning.http.util.MiscUtil.isNonEmpty;
16+
1517
import com.ning.http.client.AsyncHandler;
1618
import com.ning.http.client.AsyncHttpClientConfig;
1719
import com.ning.http.client.AsyncHttpProvider;
@@ -517,7 +519,7 @@ private void configure(URI uri, HttpURLConnection urlConnection, Request request
517519
AuthenticatorUtils.computeBasicAuthentication(realm));
518520
break;
519521
case DIGEST:
520-
if (realm.getNonce() != null && !realm.getNonce().equals("")) {
522+
if (isNonEmpty(realm.getNonce())) {
521523
try {
522524
urlConnection.setRequestProperty("Authorization",
523525
AuthenticatorUtils.computeDigestAuthentication(realm));
@@ -551,7 +553,7 @@ private void configure(URI uri, HttpURLConnection urlConnection, Request request
551553
urlConnection.setRequestProperty("User-Agent", AsyncHttpProviderUtils.constructUserAgent(JDKAsyncHttpProvider.class));
552554
}
553555

554-
if (request.getCookies() != null && !request.getCookies().isEmpty()) {
556+
if (isNonEmpty(request.getCookies())) {
555557
urlConnection.setRequestProperty("Cookie", AsyncHttpProviderUtils.encodeCookies(request.getCookies()));
556558
}
557559

src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.ning.http.client.providers.jdk;
1414

15+
import static com.ning.http.util.MiscUtil.isNonEmpty;
16+
1517
import com.ning.http.client.Cookie;
1618
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
1719
import com.ning.http.client.HttpResponseBodyPart;
@@ -204,6 +206,6 @@ public boolean hasResponseHeaders() {
204206
*/
205207
/* @Override */
206208
public boolean hasResponseBody() {
207-
return bodyParts != null && !bodyParts.isEmpty();
209+
return isNonEmpty(bodyParts);
208210
}
209211
}

src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.ning.http.client.providers.netty;
1717

18+
import static com.ning.http.util.MiscUtil.isNonEmpty;
19+
1820
import com.ning.http.client.AsyncHandler;
1921
import com.ning.http.client.AsyncHandler.STATE;
2022
import com.ning.http.client.AsyncHttpClientConfig;
@@ -617,7 +619,6 @@ private static SpnegoEngine getSpnegoEngine() {
617619
return spnegoEngine;
618620
}
619621

620-
@SuppressWarnings("deprecation")
621622
private static HttpRequest construct(AsyncHttpClientConfig config,
622623
Request request,
623624
HttpMethod m,
@@ -634,15 +635,13 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
634635
if (m.equals(HttpMethod.CONNECT)) {
635636
nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_0, m, AsyncHttpProviderUtils.getAuthority(uri));
636637
} else {
637-
StringBuilder path = null;
638+
String path = null;
638639
if (isProxyServer(config, request))
639-
path = new StringBuilder(uri.toString());
640-
else {
641-
path = new StringBuilder(uri.getRawPath());
642-
if (uri.getQuery() != null) {
643-
path.append("?").append(uri.getRawQuery());
644-
}
645-
}
640+
path = uri.toString();
641+
else if (uri.getRawQuery() != null)
642+
path = uri.getRawPath() + "?" + uri.getRawQuery();
643+
else
644+
path = uri.getRawPath();
646645
nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, m, path.toString());
647646
}
648647
boolean webSocket = isWebSocket(uri);
@@ -708,7 +707,7 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
708707
AuthenticatorUtils.computeBasicAuthentication(realm));
709708
break;
710709
case DIGEST:
711-
if (realm.getNonce() != null && !realm.getNonce().equals("")) {
710+
if (isNonEmpty(realm.getNonce())) {
712711
try {
713712
nettyRequest.setHeader(HttpHeaders.Names.AUTHORIZATION,
714713
AuthenticatorUtils.computeDigestAuthentication(realm));
@@ -793,7 +792,7 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
793792
}
794793

795794
if (!m.equals(HttpMethod.CONNECT)) {
796-
if (request.getCookies() != null && !request.getCookies().isEmpty()) {
795+
if (isNonEmpty(request.getCookies())) {
797796
CookieEncoder httpCookieEncoder = new CookieEncoder(false);
798797
Iterator<Cookie> ic = request.getCookies().iterator();
799798
Cookie c;
@@ -830,7 +829,7 @@ private static HttpRequest construct(AsyncHttpClientConfig config,
830829
int length = lengthWrapper[0];
831830
nettyRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(length));
832831
nettyRequest.setContent(ChannelBuffers.wrappedBuffer(bytes, 0, length));
833-
} else if (request.getParams() != null && !request.getParams().isEmpty()) {
832+
} else if (isNonEmpty(request.getParams())) {
834833
StringBuilder sb = new StringBuilder();
835834
for (final Entry<String, List<String>> paramEntry : request.getParams()) {
836835
final String key = paramEntry.getKey();

src/main/java/com/ning/http/client/providers/netty/NettyResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.ning.http.client.providers.netty;
1717

18+
import static com.ning.http.util.MiscUtil.isNonEmpty;
19+
1820
import com.ning.http.client.Cookie;
1921
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
2022
import com.ning.http.client.HttpResponseBodyPart;
@@ -215,7 +217,7 @@ public boolean hasResponseHeaders() {
215217
*/
216218
/* @Override */
217219
public boolean hasResponseBody() {
218-
return bodyParts != null && !bodyParts.isEmpty();
220+
return isNonEmpty(bodyParts);
219221
}
220222

221223
}

0 commit comments

Comments
 (0)