From b5b43d9bd0cbf68e6ae38b282e32dc679fe8cc36 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 28 Sep 2015 09:16:53 +0200 Subject: [PATCH] Fix NPE when listing buckets in empty project and blobs in empty bucket --- .../google/gcloud/storage/StorageImpl.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java index 57f697f3e585..4c80c6741559 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java @@ -34,6 +34,7 @@ import com.google.api.services.storage.model.StorageObject; import com.google.common.base.Function; import com.google.common.base.Functions; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -234,14 +235,16 @@ public Tuple> cal } }, serviceOptions.retryParams(), EXCEPTION_HANDLER); String cursor = result.x(); - return new BaseListResult<>(new BucketPageFetcher(serviceOptions, cursor, optionsMap), cursor, - Iterables.transform(result.y(), + Iterable buckets = + result.y() == null ? ImmutableList.of() : Iterables.transform(result.y(), new Function() { @Override public BucketInfo apply(com.google.api.services.storage.model.Bucket bucketPb) { return BucketInfo.fromPb(bucketPb); } - })); + }); + return new BaseListResult<>(new BucketPageFetcher(serviceOptions, cursor, optionsMap), cursor, + buckets); } @Override @@ -259,14 +262,17 @@ public Tuple> call() { } }, serviceOptions.retryParams(), EXCEPTION_HANDLER); String cursor = result.x(); - return new BaseListResult<>(new BlobPageFetcher(bucket, serviceOptions, cursor, optionsMap), cursor, - Iterables.transform(result.y(), + Iterable blobs = + result.y() == null ? ImmutableList.of() : Iterables.transform(result.y(), new Function() { @Override public BlobInfo apply(StorageObject storageObject) { return BlobInfo.fromPb(storageObject); } - })); + }); + return new BaseListResult<>(new BlobPageFetcher(bucket, serviceOptions, cursor, optionsMap), + cursor, + blobs); } @Override @@ -434,7 +440,7 @@ public BlobReadChannel reader(String bucket, String blob, BlobSourceOption... op return new BlobReadChannelImpl(options(), BlobInfo.of(bucket, blob), optionsMap); } - @Override + @Override public BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options) { final Map optionsMap = optionMap(blobInfo, options); return new BlobWriterChannelImpl(options(), blobInfo, optionsMap); @@ -461,12 +467,12 @@ public URL signUrl(BlobInfo blobInfo, long expiration, SignUrlOption... options) stBuilder.append(HttpMethod.GET); } stBuilder.append('\n'); - if (firstNonNull((Boolean) optionMap.get(SignUrlOption.Option.MD5) , false)) { + if (firstNonNull((Boolean) optionMap.get(SignUrlOption.Option.MD5), false)) { checkArgument(blobInfo.md5() != null, "Blob is missing a value for md5"); stBuilder.append(blobInfo.md5()); } stBuilder.append('\n'); - if (firstNonNull((Boolean) optionMap.get(SignUrlOption.Option.CONTENT_TYPE) , false)) { + if (firstNonNull((Boolean) optionMap.get(SignUrlOption.Option.CONTENT_TYPE), false)) { checkArgument(blobInfo.contentType() != null, "Blob is missing a value for content-type"); stBuilder.append(blobInfo.contentType()); }