Skip to content

Commit 5871367

Browse files
committed
work in progress
1 parent f94246c commit 5871367

10 files changed

Lines changed: 216 additions & 80 deletions

File tree

src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.api.client.http.HttpRequestInitializer;import com.google.api.client.http.HttpTransport;import com.google.api.client.json.jackson.JacksonFactory;import com.google.api.services.storage.Storage;import com.google.api.services.storage.model.Bucket;import com.google.api.services.storage.model.Buckets;import com.google.api.services.storage.model.StorageObject;import com.google.gcloud.storage.StorageServiceOptions;import java.io.IOException;import java.util.List;public class DefaultStorageRpc implements StorageRpc { private final StorageServiceOptions options; private final Storage storage; public DefaultStorageRpc(StorageServiceOptions options) { HttpTransport transport = options.httpTransport(); HttpRequestInitializer initializer = transport.createRequestFactory() .getInitializer(); this.options = options; storage = new Storage.Builder(transport, new JacksonFactory(), initializer).build(); } @Override public List<Bucket> buckets() throws IOException { Buckets buckets = storage.buckets().list(options.project()).execute(); return buckets.getItems(); } @Override public List<StorageObject> objects(String bucket, String prefix, String delimiter) throws IOException { return null; } @Override public Bucket get(String bucket) throws IOException { return storage.buckets().get(bucket).execute(); } @Override public StorageObject get(String bucket, String object) throws IOException { return storage.objects().get(bucket, object).execute(); } @Override public Bucket patch(Bucket bucket) throws IOException { return storage.buckets().patch(bucket.getName(), bucket).execute(); } @Override public StorageObject patch(StorageObject storageObject) throws IOException { return storage.objects() .patch(storageObject.getBucket(), storageObject.getName(), storageObject).execute(); } @Override public void delete(String bucket, String object) throws IOException { storage.objects().delete(bucket, object).execute(); }}
1+
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.api.client.http.HttpRequestInitializer;import com.google.api.client.http.HttpTransport;import com.google.api.client.json.jackson.JacksonFactory;import com.google.api.services.storage.Storage;import com.google.api.services.storage.model.Bucket;import com.google.api.services.storage.model.Buckets;import com.google.api.services.storage.model.StorageObject;import com.google.gcloud.storage.StorageServiceOptions;import java.io.IOException;import java.util.List;public class DefaultStorageRpc implements StorageRpc { private final StorageServiceOptions options; private final Storage storage; public DefaultStorageRpc(StorageServiceOptions options) { HttpTransport transport = options.httpTransport(); HttpRequestInitializer initializer = transport.createRequestFactory() .getInitializer(); this.options = options; storage = new Storage.Builder(transport, new JacksonFactory(), initializer).build(); // Todo: set projection to full // Todo: make sure nulls are being used as Data.asNull() } @Override public List<Bucket> buckets() throws IOException { Buckets buckets = storage.buckets().list(options.project()).execute(); return buckets.getItems(); } @Override public List<StorageObject> objects(String bucket, String prefix, String delimiter) throws IOException { return null; } @Override public Bucket get(String bucket) throws IOException { return storage.buckets().get(bucket).execute(); } @Override public StorageObject get(String bucket, String object) throws IOException { return storage.objects().get(bucket, object).execute(); } @Override public Bucket patch(Bucket bucket) throws IOException { return storage.buckets().patch(bucket.getName(), bucket).execute(); } @Override public StorageObject patch(StorageObject storageObject) throws IOException { return storage.objects() .patch(storageObject.getBucket(), storageObject.getName(), storageObject).execute(); } @Override public void delete(String bucket, String object) throws IOException { storage.objects().delete(bucket, object).execute(); }}

src/main/java/com/google/gcloud/spi/StorageRpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.api.services.storage.model.Bucket;import com.google.api.services.storage.model.StorageObject;import java.io.IOException;import java.util.List;public interface StorageRpc { List<Bucket> buckets() throws IOException; List<StorageObject> objects(String bucket, String prefix, String delimiter) throws IOException; Bucket get(String bucket) throws IOException; StorageObject get(String bucket, String object) throws IOException; Bucket patch(Bucket bucket) throws IOException; StorageObject patch(StorageObject storageObject) throws IOException; //void delete(String bucket) throws IOException; void delete(String bucket, String object) throws IOException;}
1+
/* * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.gcloud.spi;import com.google.api.services.storage.model.Bucket;import com.google.api.services.storage.model.StorageObject;import com.google.gcloud.storage.BlobReadChannel;import com.google.gcloud.storage.ObjectWriteChannel;import java.io.IOException;import java.nio.ByteBuffer;import java.util.Iterator;public interface StorageRpc { Bucket create(Bucket bucket) throws IOException; StorageObject create(StorageObject object, ByteBuffer content) throws IOException; Iterator<Bucket> list() throws IOException; Iterator<StorageObject> list(String bucket, String prefix, String delimiter, String cursor, boolean versions, int limit) throws IOException; Bucket get(Bucket bucket) throws IOException; StorageObject get(StorageObject object) throws IOException; Bucket patch(Bucket bucket) throws IOException; StorageObject patch(StorageObject storageObject) throws IOException; void delete(Bucket bucket) throws IOException; void delete(StorageObject object) throws IOException; StorageObject compose(String bucket, Iterable<String> src, StorageObject dest) throws IOException; StorageObject copy(StorageObject from, StorageObject to) throws IOException; BlobReadChannel reader(StorageObject from) throws IOException; ObjectWriteChannel writer(StorageObject to) throws IOException;}

src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.gcloud.storage;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
1921
import com.google.api.client.util.DateTime;
2022
import com.google.api.services.storage.model.ObjectAccessControl;
2123
import com.google.api.services.storage.model.StorageObject;
@@ -34,6 +36,19 @@ public class Blob implements Serializable {
3436

3537
private static final long serialVersionUID = 2228487739943277159L;
3638

39+
static final Function<StorageObject, Blob> FROM_PB_FUNCTION =
40+
new Function<StorageObject, Blob>() {
41+
@Override public Blob apply(StorageObject pb) {
42+
return Blob.fromPb(pb);
43+
}
44+
};
45+
46+
static final Function<Blob, StorageObject> TO_PB_FUNCTION = new Function<Blob, StorageObject>() {
47+
@Override public StorageObject apply(Blob blob) {
48+
return blob.toPb();
49+
}
50+
};
51+
3752
private final String bucket;
3853
private final String id;
3954
private final String name;
@@ -86,7 +101,7 @@ private Builder() {
86101
}
87102

88103
public Builder bucket(String bucket) {
89-
this.bucket = bucket;
104+
this.bucket = checkNotNull(bucket);
90105
return this;
91106
}
92107

@@ -96,7 +111,7 @@ Builder id(String id) {
96111
}
97112

98113
public Builder name(String name) {
99-
this.name = name;
114+
this.name = checkNotNull(name);
100115
return this;
101116
}
102117

@@ -201,9 +216,9 @@ public Blob build() {
201216
}
202217

203218
private Blob(Builder builder) {
204-
bucket = builder.bucket;
219+
bucket = checkNotNull(builder.bucket);
220+
name = checkNotNull(builder.name);
205221
id = builder.id;
206-
name = builder.name;
207222
cacheControl = builder.cacheControl;
208223
contentEncoding = builder.contentEncoding;
209224
contentType = builder.contentType;
@@ -314,33 +329,37 @@ public long updateTime() {
314329
}
315330

316331
public Builder toBuilder() {
317-
return builder()
318-
.acl(acl)
332+
return new Builder()
319333
.bucket(bucket)
334+
.name(name)
335+
.id(id)
336+
.generation(generation)
320337
.cacheControl(cacheControl)
321338
.contentEncoding(contentEncoding)
322-
.crc32c(crc32c)
323339
.contentType(contentType)
324-
.deleteTime(deleteTime)
325-
.generation(generation)
340+
.contentDisposition(contentDisposition)
341+
.contentLanguage(contentLanguage)
342+
.componentCount(componentCount)
343+
.crc32c(crc32c)
326344
.md5(md5)
345+
.deleteTime(deleteTime)
346+
.updateTime(updateTime)
327347
.mediaLink(mediaLink)
328348
.metadata(metadata)
329349
.metageneration(metageneration)
330-
.name(name)
350+
.acl(acl)
331351
.owner(owner)
332-
.updateTime(updateTime)
333352
.size(size)
334-
.contentDisposition(contentDisposition)
335-
.componentCount(componentCount)
336-
.contentLanguage(contentLanguage)
337353
.etag(etag)
338-
.id(id)
339354
.selfLink(selfLink);
340355
}
341356

342-
public static Builder builder() {
343-
return new Builder();
357+
public static Builder builder(Bucket bucket, String name) {
358+
return builder(bucket.name(), name);
359+
}
360+
361+
public static Builder builder(String bucket, String name) {
362+
return new Builder().bucket(bucket).name(name);
344363
}
345364

346365
StorageObject toPb() {
@@ -376,7 +395,7 @@ StorageObject toPb() {
376395
}
377396

378397
static Blob fromPb(StorageObject storageObject) {
379-
return builder()
398+
return new Builder()
380399
.acl(Lists.transform(storageObject.getAcl(),
381400
new Function<ObjectAccessControl, Acl>() {
382401
@Override public Acl apply(ObjectAccessControl objectAccessControl) {

src/main/java/com/google/gcloud/storage/ObjectReadChannel.java renamed to src/main/java/com/google/gcloud/storage/BlobReadChannel.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
*
2828
* This class is @{link Serializable}, which allows incremental reads.
2929
*/
30-
public interface ObjectReadChannel extends ReadableByteChannel, Serializable, Closeable {
30+
public interface BlobReadChannel extends ReadableByteChannel, Serializable, Closeable {
3131

32-
String bucket();
33-
34-
String object();
35-
36-
int size();
32+
/**
33+
* Overridden to remove IOException.
34+
*
35+
* @see java.nio.channels.Channel#close()
36+
*/
37+
@Override
38+
void close();
3739

3840
void seek(int position);
3941
}

src/main/java/com/google/gcloud/storage/Bucket.java

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ public final class Bucket implements Serializable {
5858
private final StorageClass storageClass;
5959

6060

61+
static final Function<com.google.api.services.storage.model.Bucket, Bucket> FROM_PB_FUNCTION =
62+
new Function<com.google.api.services.storage.model.Bucket, Bucket>() {
63+
@Override public Bucket apply(com.google.api.services.storage.model.Bucket pb) {
64+
return Bucket.fromPb(pb);
65+
}
66+
};
67+
68+
static final Function<Bucket, com.google.api.services.storage.model.Bucket> TO_PB_FUNCTION =
69+
new Function<Bucket, com.google.api.services.storage.model.Bucket>() {
70+
@Override public com.google.api.services.storage.model.Bucket apply(Bucket bucket) {
71+
return bucket.toPb();
72+
}
73+
};
74+
6175
public static abstract class DeleteRule implements Serializable {
6276

6377
private static final long serialVersionUID = 3137971668395933033L;
@@ -87,7 +101,7 @@ Rule toPb() {
87101

88102
abstract void populateCondition(Rule.Condition condition);
89103

90-
static DeleteRule fromPb(Rule rule) {
104+
private static DeleteRule fromPb(Rule rule) {
91105
if (rule.getAction() != null && SUPPORTED_ACTION.endsWith(rule.getAction().getType())) {
92106
Rule.Condition condition = rule.getCondition();
93107
Integer age = condition.getAge();
@@ -314,8 +328,8 @@ public String value() {
314328

315329
public final static class Builder {
316330

317-
private final String id;
318-
private final String name;
331+
private String id;
332+
private String name;
319333
private Acl.Entity owner;
320334
private String selfLink;
321335
private boolean versioningEnabled;
@@ -331,9 +345,17 @@ public final static class Builder {
331345
private Iterable<Acl> acl = ImmutableList.of();
332346
private Iterable<Acl> defaultAcl = ImmutableList.of();
333347

334-
Builder(String id, String name) {
348+
private Builder() {
349+
}
350+
351+
public Builder name(String name) {
352+
this.name = checkNotNull(name);
353+
return this;
354+
}
355+
356+
Builder id(String id) {
335357
this.id = id;
336-
this.name = name;
358+
return this;
337359
}
338360

339361
Builder owner(Acl.Entity owner) {
@@ -413,7 +435,7 @@ public Bucket build() {
413435

414436
private Bucket(Builder builder) {
415437
id = builder.id;
416-
name = builder.name;
438+
name = checkNotNull(builder.name);
417439
etag = builder.etag;
418440
createTime = MoreObjects.firstNonNull(builder.createTime, 0L);
419441
metageneration = MoreObjects.firstNonNull(builder.metageneration, 0L);
@@ -495,7 +517,9 @@ public List<Acl> defaultAcl() {
495517
}
496518

497519
public Builder toBuilder() {
498-
return new Builder(id, name)
520+
return new Builder()
521+
.name(name)
522+
.id(id)
499523
.createTime(createTime)
500524
.etag(etag)
501525
.metageneration(metageneration)
@@ -512,6 +536,10 @@ public Builder toBuilder() {
512536
.deleteRules(deleteRules);
513537
}
514538

539+
public static Builder builder(String name) {
540+
return new Builder().name(name);
541+
}
542+
515543
com.google.api.services.storage.model.Bucket toPb() {
516544
com.google.api.services.storage.model.Bucket bucketPb =
517545
new com.google.api.services.storage.model.Bucket();
@@ -530,12 +558,7 @@ com.google.api.services.storage.model.Bucket toPb() {
530558
if (storageClass != null) {
531559
bucketPb.setStorageClass(storageClass.value());
532560
}
533-
bucketPb.setCors(
534-
transform(cors, new Function<Cors, com.google.api.services.storage.model.Bucket.Cors>() {
535-
@Override public com.google.api.services.storage.model.Bucket.Cors apply(Cors cors) {
536-
return cors.toPb();
537-
}
538-
}));
561+
bucketPb.setCors(transform(cors, Cors.TO_PB_FUNCTION));
539562
bucketPb.setAcl(transform(acl, new Function<Acl, BucketAccessControl>() {
540563
@Override public BucketAccessControl apply(Acl acl) {
541564
return acl.toBucketPb();
@@ -564,18 +587,15 @@ com.google.api.services.storage.model.Bucket toPb() {
564587
}
565588

566589
static Bucket fromPb(com.google.api.services.storage.model.Bucket bucketPb) {
567-
Builder builder = new Builder(bucketPb.getId(), bucketPb.getName())
590+
Builder builder = new Builder()
591+
.name(bucketPb.getName())
592+
.id(bucketPb.getId())
568593
.createTime(bucketPb.getTimeCreated().getValue())
569594
.etag(bucketPb.getEtag())
570595
.metageneration(bucketPb.getMetageneration())
571596
.location(Location.of(bucketPb.getLocation()))
572597
.storageClass(StorageClass.of(bucketPb.getStorageClass()))
573-
.cors(transform(bucketPb.getCors(),
574-
new Function<com.google.api.services.storage.model.Bucket.Cors, Cors>() {
575-
@Override public Cors apply(com.google.api.services.storage.model.Bucket.Cors cors) {
576-
return Cors.fromPb(cors);
577-
}
578-
}))
598+
.cors(transform(bucketPb.getCors(), Cors.FROM_PB_FUNCTION))
579599
.acl(transform(bucketPb.getAcl(),
580600
new Function<BucketAccessControl, Acl>() {
581601
@Override public Acl apply(BucketAccessControl bucketAccessControl) {

src/main/java/com/google/gcloud/storage/Cors.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ public final class Cors implements Serializable {
3434

3535
private static final long serialVersionUID = -8637770919343335655L;
3636

37+
static final Function<Bucket.Cors, Cors> FROM_PB_FUNCTION = new Function<Bucket.Cors, Cors>() {
38+
@Override public Cors apply(Bucket.Cors pb) {
39+
return Cors.fromPb(pb);
40+
}
41+
};
42+
43+
static final Function<Cors, Bucket.Cors> TO_PB_FUNCTION = new Function<Cors, Bucket.Cors>() {
44+
@Override public Bucket.Cors apply(Cors cors) {
45+
return cors.toPb();
46+
}
47+
};
48+
3749
private final Integer maxAgeSeconds;
3850
private final ImmutableList<Method> methods;
3951
private final ImmutableList<Origin> origins;

0 commit comments

Comments
 (0)