From e2417605a2d0d8f5dae5310f9fc2808c76aeb288 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 19 Mar 2012 20:29:37 -0700 Subject: [PATCH] Prefer Guava.nullToEmpty and HttpUtils.nullToEmpty Make HttpUtils.valueOrEmpty static and change method name to nullToEmpty to mimic Guava. --- .../java/org/jclouds/atmos/filters/SignRequest.java | 5 +++-- .../s3/filters/RequestAuthorizeSignature.java | 13 +++++-------- .../filters/SharedKeyLiteAuthentication.java | 7 ++++--- core/src/main/java/org/jclouds/http/HttpUtils.java | 10 +++------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/apis/atmos/src/main/java/org/jclouds/atmos/filters/SignRequest.java b/apis/atmos/src/main/java/org/jclouds/atmos/filters/SignRequest.java index af9bcc2798..568a1e50fd 100644 --- a/apis/atmos/src/main/java/org/jclouds/atmos/filters/SignRequest.java +++ b/apis/atmos/src/main/java/org/jclouds/atmos/filters/SignRequest.java @@ -49,6 +49,7 @@ import org.jclouds.logging.Logger; import org.jclouds.util.Strings2; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.Multimaps; @@ -167,7 +168,7 @@ public class SignRequest implements HttpRequestFilter { private void appendPayloadMetadata(HttpRequest request, StringBuilder buffer) { buffer.append( - utils.valueOrEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() + Strings.nullToEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() .getContentType())).append("\n"); } @@ -176,7 +177,7 @@ public class SignRequest implements HttpRequestFilter { // Only the value is used, not the header // name. If a request does not include the header, this is an empty string. for (String header : new String[] { "Range" }) - toSign.append(utils.valueOrEmpty(request.getHeaders().get(header)).toLowerCase()).append("\n"); + toSign.append(HttpUtils.nullToEmpty(request.getHeaders().get(header)).toLowerCase()).append("\n"); // Standard HTTP header, in UTC format. Only the date value is used, not the header name. toSign.append(request.getFirstHeaderOrNull(HttpHeaders.DATE)).append("\n"); } diff --git a/apis/s3/src/main/java/org/jclouds/s3/filters/RequestAuthorizeSignature.java b/apis/s3/src/main/java/org/jclouds/s3/filters/RequestAuthorizeSignature.java index 5255c1e7a9..4c9978b691 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/filters/RequestAuthorizeSignature.java +++ b/apis/s3/src/main/java/org/jclouds/s3/filters/RequestAuthorizeSignature.java @@ -62,6 +62,7 @@ import org.jclouds.s3.Bucket; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Predicate; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; @@ -204,14 +205,14 @@ public class RequestAuthorizeSignature implements HttpRequestFilter, RequestSign // note that we fall back to headers, and some requests such as ?uploads do not have a // payload, yet specify payload related parameters buffer.append( - request.getPayload() == null ? utils.valueOrEmpty(request.getFirstHeaderOrNull("Content-MD5")) : utils - .valueOrEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() + request.getPayload() == null ? Strings.nullToEmpty(request.getFirstHeaderOrNull("Content-MD5")) : + HttpUtils.nullToEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() .getContentMD5())).append("\n"); buffer.append( - utils.valueOrEmpty(request.getPayload() == null ? request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE) + Strings.nullToEmpty(request.getPayload() == null ? request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE) : request.getPayload().getContentMetadata().getContentType())).append("\n"); for (String header : FIRST_HEADERS_TO_SIGN) - buffer.append(valueOrEmpty(request.getHeaders().get(header))).append("\n"); + buffer.append(HttpUtils.nullToEmpty(request.getHeaders().get(header))).append("\n"); } @VisibleForTesting @@ -270,8 +271,4 @@ public class RequestAuthorizeSignature implements HttpRequestFilter, RequestSign } } } - - private String valueOrEmpty(Collection collection) { - return (collection != null && collection.size() >= 1) ? collection.iterator().next() : ""; - } } diff --git a/common/azure/src/main/java/org/jclouds/azure/storage/filters/SharedKeyLiteAuthentication.java b/common/azure/src/main/java/org/jclouds/azure/storage/filters/SharedKeyLiteAuthentication.java index aa967571a0..9bdb901e71 100644 --- a/common/azure/src/main/java/org/jclouds/azure/storage/filters/SharedKeyLiteAuthentication.java +++ b/common/azure/src/main/java/org/jclouds/azure/storage/filters/SharedKeyLiteAuthentication.java @@ -46,6 +46,7 @@ import org.jclouds.logging.Logger; import org.jclouds.util.Strings2; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; @@ -122,10 +123,10 @@ public class SharedKeyLiteAuthentication implements HttpRequestFilter { private void appendPayloadMetadata(HttpRequest request, StringBuilder buffer) { buffer.append( - utils.valueOrEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() + HttpUtils.nullToEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() .getContentMD5())).append("\n"); buffer.append( - utils.valueOrEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() + Strings.nullToEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() .getContentType())).append("\n"); } @@ -166,7 +167,7 @@ public class SharedKeyLiteAuthentication implements HttpRequestFilter { private void appendHttpHeaders(HttpRequest request, StringBuilder toSign) { for (String header : FIRST_HEADERS_TO_SIGN) - toSign.append(utils.valueOrEmpty(request.getHeaders().get(header))).append("\n"); + toSign.append(HttpUtils.nullToEmpty(request.getHeaders().get(header))).append("\n"); } @VisibleForTesting diff --git a/core/src/main/java/org/jclouds/http/HttpUtils.java b/core/src/main/java/org/jclouds/http/HttpUtils.java index e6b4eb8ed5..0ee0dd24cb 100644 --- a/core/src/main/java/org/jclouds/http/HttpUtils.java +++ b/core/src/main/java/org/jclouds/http/HttpUtils.java @@ -399,16 +399,12 @@ public class HttpUtils { from.getPayload().release(); } - public String valueOrEmpty(String in) { - return in != null ? in : ""; - } - - public String valueOrEmpty(byte[] md5) { + public static String nullToEmpty(byte[] md5) { return md5 != null ? CryptoStreams.base64(md5) : ""; } - public String valueOrEmpty(Collection collection) { - return (collection != null && collection.size() >= 1) ? collection.iterator().next() : ""; + public static String nullToEmpty(Collection collection) { + return (collection == null || collection.isEmpty()) ? "" : collection.iterator().next(); } public static Long attemptToParseSizeAndRangeFromHeaders(HttpMessage from) throws HttpException {