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 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");
}

View File

@ -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<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 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

View File

@ -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<String> collection) {
return (collection != null && collection.size() >= 1) ? collection.iterator().next() : "";
public static String nullToEmpty(Collection<String> collection) {
return (collection == null || collection.isEmpty()) ? "" : collection.iterator().next();
}
public static Long attemptToParseSizeAndRangeFromHeaders(HttpMessage from) throws HttpException {