Skip to content

Commit 2cc4cf1

Browse files
author
Stephane Landelle
committed
Port MiscUtil.isNonEmpty to master
1 parent dcb575d commit 2cc4cf1

File tree

17 files changed

+105
-38
lines changed

17 files changed

+105
-38
lines changed

api/src/main/java/com/ning/http/client/FluentCaseInsensitiveStringsMap.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 java.util.ArrayList;
2022
import java.util.Arrays;
2123
import java.util.Collection;
@@ -66,7 +68,7 @@ public FluentCaseInsensitiveStringsMap(Map<String, Collection<String>> src) {
6668
* @return This object
6769
*/
6870
public FluentCaseInsensitiveStringsMap add(String key, String... values) {
69-
if ((values != null) && (values.length > 0)) {
71+
if (isNonEmpty(values)) {
7072
add(key, Arrays.asList(values));
7173
}
7274
return this;

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

Lines changed: 5 additions & 3 deletions
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 java.io.UnsupportedEncodingException;
2022
import java.security.MessageDigest;
2123
import java.security.NoSuchAlgorithmException;
@@ -431,7 +433,7 @@ public RealmBuilder parseWWWAuthenticateHeader(String headerLine) {
431433
setAlgorithm(match(headerLine, "algorithm"));
432434
setOpaque(match(headerLine, "opaque"));
433435
setQop(match(headerLine, "qop"));
434-
if (getNonce() != null && !getNonce().equalsIgnoreCase("")) {
436+
if (isNonEmpty(getNonce())) {
435437
setScheme(AuthScheme.DIGEST);
436438
} else {
437439
setScheme(AuthScheme.BASIC);
@@ -444,7 +446,7 @@ public RealmBuilder parseProxyAuthenticateHeader(String headerLine) {
444446
setNonce(match(headerLine, "nonce"));
445447
setOpaque(match(headerLine, "opaque"));
446448
setQop(match(headerLine, "qop"));
447-
if (getNonce() != null && !getNonce().equalsIgnoreCase("")) {
449+
if (isNonEmpty(getNonce())) {
448450
setScheme(AuthScheme.DIGEST);
449451
} else {
450452
setScheme(AuthScheme.BASIC);
@@ -599,7 +601,7 @@ private static String toBase16(byte[] bytes) {
599601
public Realm build() {
600602

601603
// Avoid generating
602-
if (nonce != null && nonce.length() > 0) {
604+
if (isNonEmpty(nonce)) {
603605
newCnonce();
604606
try {
605607
newResponse();

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

Lines changed: 4 additions & 2 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
@@ -384,7 +386,7 @@ private String buildUrl(String url) {
384386
}
385387
}
386388

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

api/src/main/java/com/ning/http/client/listener/TransferCompletionHandler.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.listener;
1414

15+
import static com.ning.http.util.MiscUtil.isNonEmpty;
16+
1517
import com.ning.http.client.AsyncCompletionHandlerBase;
1618
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
1719
import com.ning.http.client.HttpResponseBodyPart;
@@ -144,7 +146,7 @@ public Response onCompleted(Response response) throws Exception {
144146
*/
145147
public STATE onHeaderWriteCompleted() {
146148
List<String> list = transferAdapter.getHeaders().get("Content-Length");
147-
if (list != null && list.size() > 0 && list.get(0) != "") {
149+
if (isNonEmpty(list) && list.get(0).length() != 0) {
148150
totalBytesToTransfer.set(Long.valueOf(list.get(0)));
149151
}
150152

api/src/main/java/com/ning/http/client/providers/ResponseBase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.ning.http.client.providers;
22

3+
import static com.ning.http.util.MiscUtil.isNonEmpty;
4+
35
import java.io.IOException;
46
import java.io.InputStream;
57
import java.net.URI;
@@ -126,10 +128,10 @@ public boolean hasResponseStatus() {
126128
}
127129

128130
public boolean hasResponseHeaders() {
129-
return headers != null && !headers.getHeaders().isEmpty();
131+
return headers != null && isNonEmpty(headers.getHeaders());
130132
}
131133

132134
public boolean hasResponseBody() {
133-
return bodyParts != null && !bodyParts.isEmpty();
135+
return isNonEmpty(bodyParts);
134136
}
135137
}

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

Lines changed: 5 additions & 3 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;
@@ -373,7 +375,7 @@ public T call() throws Exception {
373375
} catch (Throwable t) {
374376
logger.debug(t.getMessage(), t);
375377

376-
if (IOException.class.isAssignableFrom(t.getClass()) && config.getIOExceptionFilters().size() > 0) {
378+
if (IOException.class.isAssignableFrom(t.getClass()) && !config.getIOExceptionFilters().isEmpty()) {
377379
FilterContext fc = new FilterContext.FilterContextBuilder().asyncHandler(asyncHandler)
378380
.request(request).ioException(IOException.class.cast(t)).build();
379381

@@ -516,7 +518,7 @@ private void configure(URI uri, HttpURLConnection urlConnection, Request request
516518
AuthenticatorUtils.computeBasicAuthentication(realm));
517519
break;
518520
case DIGEST:
519-
if (realm.getNonce() != null && realm.getNonce().length() > 0) {
521+
if (isNonEmpty(realm.getNonce())) {
520522
try {
521523
urlConnection.setRequestProperty("Authorization",
522524
AuthenticatorUtils.computeDigestAuthentication(realm));
@@ -552,7 +554,7 @@ private void configure(URI uri, HttpURLConnection urlConnection, Request request
552554
config));
553555
}
554556

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

api/src/main/java/com/ning/http/util/AuthenticatorUtils.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.util;
1414

15+
import static com.ning.http.util.MiscUtil.isNonEmpty;
16+
1517
import com.ning.http.client.ProxyServer;
1618
import com.ning.http.client.Realm;
1719

@@ -40,7 +42,7 @@ public static String computeDigestAuthentication(Realm realm) throws NoSuchAlgor
4042
builder.append("algorithm").append('=').append(realm.getAlgorithm()).append(", ");
4143

4244
construct(builder, "response", realm.getResponse());
43-
if (realm.getOpaque() != null && realm.getOpaque().length() > 0)
45+
if (isNonEmpty(realm.getOpaque()))
4446
construct(builder, "opaque", realm.getOpaque());
4547
builder.append("qop").append('=').append(realm.getQop()).append(", ");
4648
builder.append("nc").append('=').append(realm.getNc()).append(", ");
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2010-2012 Sonatype, Inc. 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 http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package com.ning.http.util;
14+
15+
import java.util.Collection;
16+
import java.util.Map;
17+
18+
public class MiscUtil {
19+
20+
private MiscUtil() {
21+
}
22+
23+
public static boolean isNonEmpty(String string) {
24+
return string != null && string.length() != 0;
25+
}
26+
27+
public static boolean isNonEmpty(Object[] array) {
28+
return array != null && array.length != 0;
29+
}
30+
31+
public static boolean isNonEmpty(byte[] array) {
32+
return array != null && array.length != 0;
33+
}
34+
35+
public static boolean isNonEmpty(Collection<?> collection) {
36+
return collection != null && !collection.isEmpty();
37+
}
38+
39+
public static boolean isNonEmpty(Map<?, ?> map) {
40+
return map != null && !map.isEmpty();
41+
}
42+
}

api/src/main/java/com/ning/http/util/ProxyUtils.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.util;
1414

15+
import static com.ning.http.util.MiscUtil.isNonEmpty;
16+
1517
import com.ning.http.client.ProxyServer;
1618
import com.ning.http.client.ProxyServer.Protocol;
1719
import com.ning.http.client.Request;
@@ -89,7 +91,7 @@ public static boolean avoidProxy(final ProxyServer proxyServer, final String tar
8991

9092
List<String> nonProxyHosts = proxyServer.getNonProxyHosts();
9193

92-
if (nonProxyHosts != null && nonProxyHosts.size() > 0) {
94+
if (isNonEmpty(nonProxyHosts)) {
9395
for (String nonProxyHost : nonProxyHosts) {
9496
if (nonProxyHost.startsWith("*") && nonProxyHost.length() > 1
9597
&& targetHost.endsWith(nonProxyHost.substring(1).toLowerCase())) {

api/src/test/java/com/ning/http/client/async/ParamEncodingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.concurrent.TimeUnit;
3131
import java.util.concurrent.TimeoutException;
3232

33+
import static com.ning.http.util.MiscUtil.isNonEmpty;
3334
import static org.testng.Assert.assertEquals;
3435
import static org.testng.Assert.assertNotNull;
3536

@@ -42,7 +43,7 @@ public void handle(String s,
4243
HttpServletResponse response) throws IOException, ServletException {
4344
if ("POST".equalsIgnoreCase(request.getMethod())) {
4445
String p = request.getParameter("test");
45-
if (p != null && !p.equals("")) {
46+
if (isNonEmpty(p)) {
4647
response.setStatus(HttpServletResponse.SC_OK);
4748
response.addHeader("X-Param", p);
4849
} else {

api/src/test/java/com/ning/http/client/async/PostWithQSTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.concurrent.TimeUnit;
3535
import java.util.concurrent.TimeoutException;
3636

37+
import static com.ning.http.util.MiscUtil.isNonEmpty;
3738
import static org.testng.Assert.assertEquals;
3839
import static org.testng.Assert.assertNotNull;
3940

@@ -54,7 +55,7 @@ public void handle(String s,
5455
HttpServletResponse response) throws IOException, ServletException {
5556
if ("POST".equalsIgnoreCase(request.getMethod())) {
5657
String qs = request.getQueryString();
57-
if (qs != null && !qs.equals("") && request.getContentLength() == 3) {
58+
if (isNonEmpty(qs) && request.getContentLength() == 3) {
5859
ServletInputStream is = request.getInputStream();
5960
response.setStatus(HttpServletResponse.SC_OK);
6061
byte buf[] = new byte[is.available()];

api/src/test/java/com/ning/http/client/async/QueryParametersTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.concurrent.TimeUnit;
3434
import java.util.concurrent.TimeoutException;
3535

36+
import static com.ning.http.util.MiscUtil.isNonEmpty;
3637
import static org.testng.Assert.assertEquals;
3738
import static org.testng.Assert.assertNotNull;
3839

@@ -49,7 +50,7 @@ public void handle(String s,
4950
HttpServletResponse response) throws IOException, ServletException {
5051
if ("GET".equalsIgnoreCase(request.getMethod())) {
5152
String qs = request.getQueryString();
52-
if (qs != null && !qs.equals("")) {
53+
if (isNonEmpty(qs)) {
5354
for (String qnv : qs.split("&")) {
5455
String nv[] = qnv.split("=");
5556
response.addHeader(nv[0], nv[1]);

providers/apache/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;
@@ -357,7 +359,7 @@ private HttpMethodBase createMethod(HttpClient client, Request request) throws I
357359

358360
method.setFollowRedirects(false);
359361
Collection<Cookie> cookies = request.getCookies();
360-
if ((cookies != null) && !cookies.isEmpty()) {
362+
if (isNonEmpty(cookies)) {
361363
method.setRequestHeader("Cookie", AsyncHttpProviderUtils.encodeCookies(request.getCookies()));
362364
}
363365

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
import java.util.concurrent.atomic.AtomicInteger;
140140
import java.util.concurrent.atomic.AtomicLong;
141141

142+
import static com.ning.http.util.MiscUtil.isNonEmpty;
142143
import static com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProviderConfig.Property.BUFFER_WEBSOCKET_FRAGMENTS;
143144
import static com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProviderConfig.Property.MAX_HTTP_PACKET_HEADER_SIZE;
144145
import static com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProviderConfig.Property.TRANSPORT_CUSTOMIZER;
@@ -974,11 +975,11 @@ private void addHeaders(final Request request,
974975
final HttpRequestPacket requestPacket) {
975976

976977
final FluentCaseInsensitiveStringsMap map = request.getHeaders();
977-
if (map != null && !map.isEmpty()) {
978+
if (isNonEmpty(map)) {
978979
for (final Map.Entry<String, List<String>> entry : map.entrySet()) {
979980
final String headerName = entry.getKey();
980981
final List<String> headerValues = entry.getValue();
981-
if (headerValues != null && !headerValues.isEmpty()) {
982+
if (isNonEmpty(headerValues)) {
982983
for (final String headerValue : headerValues) {
983984
requestPacket.addHeader(headerName, headerValue);
984985
}
@@ -1007,7 +1008,7 @@ private void addCookies(final Request request,
10071008
final HttpRequestPacket requestPacket) {
10081009

10091010
final Collection<Cookie> cookies = request.getCookies();
1010-
if (cookies != null && !cookies.isEmpty()) {
1011+
if (isNonEmpty(cookies)) {
10111012
StringBuilder sb = new StringBuilder(128);
10121013
org.glassfish.grizzly.http.Cookie[] gCookies =
10131014
new org.glassfish.grizzly.http.Cookie[cookies.size()];
@@ -1041,16 +1042,16 @@ private void addQueryString(final Request request,
10411042
final HttpRequestPacket requestPacket) {
10421043

10431044
final FluentStringsMap map = request.getQueryParams();
1044-
if (map != null && !map.isEmpty()) {
1045+
if (isNonEmpty(map)) {
10451046
StringBuilder sb = new StringBuilder(128);
10461047
for (final Map.Entry<String, List<String>> entry : map.entrySet()) {
10471048
final String name = entry.getKey();
10481049
final List<String> values = entry.getValue();
1049-
if (values != null && !values.isEmpty()) {
1050+
if (isNonEmpty(values)) {
10501051
try {
10511052
for (int i = 0, len = values.size(); i < len; i++) {
10521053
final String value = values.get(i);
1053-
if (value != null && value.length() > 0) {
1054+
if (isNonEmpty(value)) {
10541055
sb.append(URLEncoder.encode(name, "UTF-8")).append('=')
10551056
.append(URLEncoder.encode(values.get(i), "UTF-8")).append('&');
10561057
} else {
@@ -2054,7 +2055,7 @@ private final class ParamsBodyHandler implements BodyHandler {
20542055

20552056
public boolean handlesBodyType(final Request request) {
20562057
final FluentStringsMap params = request.getParams();
2057-
return (params != null && !params.isEmpty());
2058+
return isNonEmpty(params);
20582059
}
20592060

20602061
@SuppressWarnings({"unchecked"})
@@ -2076,7 +2077,7 @@ public boolean doHandle(final FilterChainContext ctx,
20762077
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
20772078
String name = entry.getKey();
20782079
List<String> values = entry.getValue();
2079-
if (values != null && !values.isEmpty()) {
2080+
if (isNonEmpty(values)) {
20802081
if (sb == null) {
20812082
sb = new StringBuilder(128);
20822083
}
@@ -2201,7 +2202,7 @@ private static final class PartsBodyHandler implements BodyHandler {
22012202

22022203
public boolean handlesBodyType(final Request request) {
22032204
final List<Part> parts = request.getParts();
2204-
return (parts != null && !parts.isEmpty());
2205+
return isNonEmpty(parts);
22052206
}
22062207

22072208
@SuppressWarnings({"unchecked"})
@@ -2768,15 +2769,15 @@ public WebSocket sendMessage(byte[] message) {
27682769

27692770
@Override
27702771
public WebSocket stream(byte[] fragment, boolean last) {
2771-
if (fragment != null && fragment.length > 0) {
2772+
if (isNonEmpty(fragment)) {
27722773
gWebSocket.stream(last, fragment, 0, fragment.length);
27732774
}
27742775
return this;
27752776
}
27762777

27772778
@Override
27782779
public WebSocket stream(byte[] fragment, int offset, int len, boolean last) {
2779-
if (fragment != null && fragment.length > 0) {
2780+
if (isNonEmpty(fragment)) {
27802781
gWebSocket.stream(last, fragment, offset, len);
27812782
}
27822783
return this;

0 commit comments

Comments
 (0)