Prefer Guava.nullToEmpty and HttpUtils.nullToEmpty

Make HttpUtils.valueOrEmpty static and change method name to
nullToEmpty to mimic Guava.
This commit is contained in:
Andrew Gaul 2012-03-19 20:29:37 -07:00
parent 73749de4e3
commit e2417605a2
4 changed files with 15 additions and 20 deletions

View File

@ -49,6 +49,7 @@ import org.jclouds.logging.Logger;
import org.jclouds.util.Strings2; import org.jclouds.util.Strings2;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Multimaps; import com.google.common.collect.Multimaps;
@ -167,7 +168,7 @@ public class SignRequest implements HttpRequestFilter {
private void appendPayloadMetadata(HttpRequest request, StringBuilder buffer) { private void appendPayloadMetadata(HttpRequest request, StringBuilder buffer) {
buffer.append( buffer.append(
utils.valueOrEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() Strings.nullToEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata()
.getContentType())).append("\n"); .getContentType())).append("\n");
} }
@ -176,7 +177,7 @@ public class SignRequest implements HttpRequestFilter {
// Only the value is used, not the header // Only the value is used, not the header
// name. If a request does not include the header, this is an empty string. // name. If a request does not include the header, this is an empty string.
for (String header : new String[] { "Range" }) 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. // Standard HTTP header, in UTC format. Only the date value is used, not the header name.
toSign.append(request.getFirstHeaderOrNull(HttpHeaders.DATE)).append("\n"); toSign.append(request.getFirstHeaderOrNull(HttpHeaders.DATE)).append("\n");
} }

View File

@ -62,6 +62,7 @@ import org.jclouds.s3.Bucket;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap; 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 // note that we fall back to headers, and some requests such as ?uploads do not have a
// payload, yet specify payload related parameters // payload, yet specify payload related parameters
buffer.append( buffer.append(
request.getPayload() == null ? utils.valueOrEmpty(request.getFirstHeaderOrNull("Content-MD5")) : utils request.getPayload() == null ? Strings.nullToEmpty(request.getFirstHeaderOrNull("Content-MD5")) :
.valueOrEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() HttpUtils.nullToEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata()
.getContentMD5())).append("\n"); .getContentMD5())).append("\n");
buffer.append( 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"); : request.getPayload().getContentMetadata().getContentType())).append("\n");
for (String header : FIRST_HEADERS_TO_SIGN) 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 @VisibleForTesting
@ -270,8 +271,4 @@ public class RequestAuthorizeSignature implements HttpRequestFilter, RequestSign
} }
} }
} }
private String valueOrEmpty(Collection<String> collection) {
return (collection != null && collection.size() >= 1) ? collection.iterator().next() : "";
}
} }

View File

@ -46,6 +46,7 @@ import org.jclouds.logging.Logger;
import org.jclouds.util.Strings2; import org.jclouds.util.Strings2;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
@ -122,10 +123,10 @@ public class SharedKeyLiteAuthentication implements HttpRequestFilter {
private void appendPayloadMetadata(HttpRequest request, StringBuilder buffer) { private void appendPayloadMetadata(HttpRequest request, StringBuilder buffer) {
buffer.append( buffer.append(
utils.valueOrEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() HttpUtils.nullToEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata()
.getContentMD5())).append("\n"); .getContentMD5())).append("\n");
buffer.append( buffer.append(
utils.valueOrEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata() Strings.nullToEmpty(request.getPayload() == null ? null : request.getPayload().getContentMetadata()
.getContentType())).append("\n"); .getContentType())).append("\n");
} }
@ -166,7 +167,7 @@ public class SharedKeyLiteAuthentication implements HttpRequestFilter {
private void appendHttpHeaders(HttpRequest request, StringBuilder toSign) { private void appendHttpHeaders(HttpRequest request, StringBuilder toSign) {
for (String header : FIRST_HEADERS_TO_SIGN) 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 @VisibleForTesting

View File

@ -399,16 +399,12 @@ public class HttpUtils {
from.getPayload().release(); from.getPayload().release();
} }
public String valueOrEmpty(String in) { public static String nullToEmpty(byte[] md5) {
return in != null ? in : "";
}
public String valueOrEmpty(byte[] md5) {
return md5 != null ? CryptoStreams.base64(md5) : ""; return md5 != null ? CryptoStreams.base64(md5) : "";
} }
public String valueOrEmpty(Collection<String> collection) { public static String nullToEmpty(Collection<String> collection) {
return (collection != null && collection.size() >= 1) ? collection.iterator().next() : ""; return (collection == null || collection.isEmpty()) ? "" : collection.iterator().next();
} }
public static Long attemptToParseSizeAndRangeFromHeaders(HttpMessage from) throws HttpException { public static Long attemptToParseSizeAndRangeFromHeaders(HttpMessage from) throws HttpException {