Skip to content

Commit 1d77e08

Browse files
committed
wip
1 parent 83b9ce0 commit 1d77e08

3 files changed

Lines changed: 143 additions & 41 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package com.google.gcloud.storage;
1818

19+
import com.google.api.services.storage.model.BucketAccessControl;
20+
import com.google.api.services.storage.model.ObjectAccessControl;
21+
import com.google.gcloud.storage.Acl.Project.ProjectRole;
22+
1923
import java.io.Serializable;
2024

2125
public abstract class Acl implements Serializable {
@@ -148,4 +152,36 @@ public Type type() {
148152
public Role role() {
149153
return role;
150154
}
155+
156+
public static Acl fromPb(ObjectAccessControl objectAccessControl) {
157+
Role role = Role.valueOf(objectAccessControl.getRole());
158+
return forEntity(objectAccessControl.getEntity(), role);
159+
}
160+
161+
private static Acl forEntity(String entity, Role role) {
162+
if (entity.startsWith("user-")) {
163+
return User.of(entity.substring(5), role);
164+
}
165+
if (entity.equals(User.ALL_USERS)) {
166+
return User.ofAllUsers(role);
167+
}
168+
if (entity.equals(User.ALL_AUTHENTICATED_USERS)) {
169+
return User.ofAllAuthenticatedUsers(role);
170+
}
171+
if (entity.startsWith("group-")) {
172+
return Group.of(entity.substring(6), role);
173+
}
174+
if (entity.startsWith("project-")) {
175+
int idx = entity.indexOf('-', 9);
176+
String team = entity.substring(9, idx);
177+
String projectId = entity.substring(idx + 1);
178+
return Project.of(ProjectRole.valueOf(team.toUpperCase()), projectId, role);
179+
}
180+
return null;
181+
}
182+
183+
public static Acl fromPb(BucketAccessControl bucketAccessControl) {
184+
Role role = Role.valueOf(bucketAccessControl.getRole());
185+
return forEntity(bucketAccessControl.getEntity(), role);
186+
}
151187
}

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

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@
1717
package com.google.gcloud.storage;
1818

1919
import static com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.common.collect.Iterables.transform;
21+
import static com.google.common.collect.Lists.newArrayList;
2022

23+
import com.google.api.client.util.DateTime;
2124
import com.google.api.services.storage.model.Bucket;
25+
import com.google.api.services.storage.model.BucketAccessControl;
26+
import com.google.api.services.storage.model.ObjectAccessControl;
27+
import com.google.common.base.Function;
2228
import com.google.common.base.MoreObjects;
2329
import com.google.common.collect.ImmutableList;
2430
import com.google.common.collect.ImmutableMap;
31+
import com.google.common.collect.Iterables;
2532

2633
import java.io.Serializable;
2734
import java.util.List;
@@ -35,7 +42,7 @@ public final class BucketInfo implements Serializable {
3542
private final String etag;
3643
private final long createTime;
3744
private final long metageneration;
38-
private final Cors cors;
45+
private final ImmutableList<Cors> cors;
3946
private final ImmutableList<Acl> acl;
4047
private final ImmutableList<Acl> defaultAcl;
4148
private final Location location;
@@ -84,6 +91,11 @@ public static StorageClass of(String value) {
8491
return option == null ? new StorageClass(value) : option.storageClass;
8592
}
8693

94+
@Override
95+
public String toString() {
96+
return value();
97+
}
98+
8799
public String value() {
88100
return value;
89101
}
@@ -134,6 +146,11 @@ public static Location of(String value) {
134146
return option == null ? new Location(value) : option.location;
135147
}
136148

149+
@Override
150+
public String toString() {
151+
return value();
152+
}
153+
137154
public String value() {
138155
return value;
139156
}
@@ -148,9 +165,9 @@ public final static class Builder {
148165
private String etag;
149166
private Long createTime;
150167
private Long metageneration;
151-
private Cors cors;
152-
private Iterable<Acl> acl;
153-
private Iterable<Acl> defaultAcl;
168+
private Iterable<Cors> cors = ImmutableList.of();
169+
private Iterable<Acl> acl = ImmutableList.of();
170+
private Iterable<Acl> defaultAcl = ImmutableList.of();
154171

155172
Builder(String id, String name) {
156173
this.id = id;
@@ -182,7 +199,7 @@ Builder metageneration(Long metageneration) {
182199
return this;
183200
}
184201

185-
Builder cors(Cors cors) {
202+
Builder cors(Iterable<Cors> cors) {
186203
this.cors = cors;
187204
return this;
188205
}
@@ -210,7 +227,7 @@ private BucketInfo(Builder builder) {
210227
metageneration = MoreObjects.firstNonNull(builder.metageneration, 0L);
211228
location = builder.location;
212229
storageClass = builder.storageClass;
213-
cors = builder.cors;
230+
cors = ImmutableList.copyOf(builder.cors);
214231
acl = ImmutableList.copyOf(builder.acl);
215232
defaultAcl = ImmutableList.copyOf(builder.defaultAcl);
216233
}
@@ -243,15 +260,15 @@ public StorageClass storageClass() {
243260
return storageClass;
244261
}
245262

246-
public Cors cors() {
263+
public List<Cors> cors() {
247264
return cors;
248265
}
249266

250267
public List<Acl> acl() {
251268
return acl;
252269
}
253270

254-
public List<Acl> defaultObjectAcl() {
271+
public List<Acl> defaultAcl() {
255272
return defaultAcl;
256273
}
257274

@@ -267,33 +284,64 @@ public Builder toBuilder() {
267284
.storageClass(storageClass);
268285
}
269286

270-
void fromPb(Bucket bucket) {
287+
BucketInfo fromPb(Bucket bucket) {
271288
Builder builder = new Builder(bucket.getId(), bucket.getName())
272289
.createTime(bucket.getTimeCreated().getValue())
273290
.etag(bucket.getEtag())
274291
.metageneration(bucket.getMetageneration())
275292
.location(Location.of(bucket.getLocation()))
276-
.storageClass(StorageClass.of(bucket.getStorageClass()));
277-
278-
/*
279-
cors = builder.cors;
280-
acl = ImmutableList.copyOf(builder.acl);
281-
defaultAcl = ImmutableList.copyOf(builder.defaultAcl);
282-
*/
283-
293+
.storageClass(StorageClass.of(bucket.getStorageClass()))
294+
.cors(transform(bucket.getCors(), new Function<Bucket.Cors, Cors>() {
295+
@Override public Cors apply(Bucket.Cors cors) {
296+
return Cors.fromPb(cors);
297+
}
298+
}))
299+
.acl(transform(bucket.getAcl(), new Function<BucketAccessControl, Acl>() {
300+
@Override public Acl apply(BucketAccessControl bucketAccessControl) {
301+
return Acl.fromPb(bucketAccessControl);
302+
}
303+
}))
304+
.defaultAcl(transform(bucket.getDefaultObjectAcl(), new Function<ObjectAccessControl, Acl>() {
305+
@Override public Acl apply(ObjectAccessControl objectAccessControl) {
306+
return Acl.fromPb(objectAccessControl);
307+
}
308+
}));
309+
return builder.build();
284310
}
285311

286-
/*
287312
Bucket toPb() {
288-
id = builder.id;
289-
name = builder.name;
290-
etag = builder.etag;
291-
createTime = MoreObjects.firstNonNull(builder.createTime, 0L);
292-
metageneration = MoreObjects.firstNonNull(builder.metageneration, 0L);
293-
location = builder.location;
294-
storageClass = builder.storageClass;
295-
cors = builder.cors;
296-
acl = ImmutableList.copyOf(builder.acl);
297-
defaultAcl = ImmutableList.copyOf(builder.defaultAcl);
298-
}*/
313+
Bucket bucket = new Bucket();
314+
bucket.setId(id);
315+
bucket.setName(name);
316+
bucket.setEtag(etag);
317+
if (createTime > 0) {
318+
bucket.setTimeCreated(new DateTime(createTime));
319+
}
320+
if (metageneration > 0) {
321+
bucket.setMetageneration(metageneration);
322+
}
323+
if (location != null) {
324+
bucket.setLocation(location.value());
325+
}
326+
if (storageClass != null) {
327+
bucket.setStorageClass(storageClass.value());
328+
}
329+
bucket.setCors(newArrayList(Iterables.transform(cors, new Function<Cors, Bucket.Cors>() {
330+
@Override public Bucket.Cors apply(Cors cors) {
331+
return cors.toPb();
332+
}
333+
})));
334+
bucket.setAcl(newArrayList(Iterables.transform(acl, new Function<Acl, BucketAccessControl>() {
335+
@Override public BucketAccessControl apply(Acl acl) {
336+
return new BucketAccessControl().setEntity(acl.toEntity());
337+
}
338+
})));
339+
bucket.setDefaultObjectAcl(
340+
newArrayList(Iterables.transform(defaultAcl, new Function<Acl, ObjectAccessControl>() {
341+
@Override public ObjectAccessControl apply(Acl acl) {
342+
return new ObjectAccessControl().setEntity(acl.toEntity());
343+
}
344+
})));
345+
return bucket;
346+
}
299347
}

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,19 @@
1717
package com.google.gcloud.storage;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.common.collect.Iterables.transform;
21+
import static com.google.common.collect.Lists.newArrayList;
2022

2123
import com.google.api.services.storage.model.Bucket;
2224
import com.google.common.base.Function;
2325
import com.google.common.base.Functions;
24-
import com.google.common.base.Preconditions;
2526
import com.google.common.collect.ImmutableList;
26-
import com.google.common.collect.Iterables;
2727

2828
import java.io.Serializable;
2929
import java.net.URI;
3030
import java.net.URISyntaxException;
3131
import java.util.List;
3232

33-
import javax.annotation.Nullable;
34-
3533
public final class Cors implements Serializable {
3634

3735
private static final long serialVersionUID = -8637770919343335655L;
@@ -63,7 +61,7 @@ public static Origin any() {
6361

6462
public static Origin of(String scheme, String host, int port) {
6563
try {
66-
of(new URI(scheme, null, host, port, null, null, null).toString());
64+
return of(new URI(scheme, null, host, port, null, null, null).toString());
6765
} catch (URISyntaxException e) {
6866
throw new IllegalArgumentException(e);
6967
}
@@ -76,6 +74,11 @@ public static Origin of(String value) {
7674
return new Origin(value);
7775
}
7876

77+
@Override
78+
public String toString() {
79+
return value();
80+
}
81+
7982
public String value() {
8083
return value;
8184
}
@@ -151,13 +154,28 @@ public static Builder builder() {
151154
return new Builder();
152155
}
153156

154-
Cors fromPb(Bucket.Cors cors) {
157+
Bucket.Cors toPb() {
158+
Bucket.Cors pb = new Bucket.Cors();
159+
pb.setMaxAgeSeconds(maxAgeSeconds);
160+
pb.setResponseHeader(responseHeaders);
161+
pb.setMethod(newArrayList(transform(methods(), Functions.toStringFunction())));
162+
pb.setOrigin(newArrayList(transform(origins(), Functions.toStringFunction())));
163+
return pb;
164+
}
165+
166+
static Cors fromPb(Bucket.Cors cors) {
155167
Builder builder = builder().maxAgeSeconds(cors.getMaxAgeSeconds());
156-
builder.methods(Iterables.transform(cors.getMethod(), new Function<String, Method>() {
157-
@Override public Method apply(String name) {
158-
return Method.valueOf(name);
159-
}
160-
}));
161-
builder
168+
builder.methods(transform(cors.getMethod(), new Function<String, Method>() {
169+
@Override public Method apply(String name) {
170+
return Method.valueOf(name.toUpperCase());
171+
}
172+
}));
173+
builder.origins(transform(cors.getOrigin(), new Function<String, Origin>() {
174+
@Override public Origin apply(String value) {
175+
return Origin.of(value);
176+
}
177+
}));
178+
builder.responseHeaders(cors.getResponseHeader());
179+
return builder.build();
162180
}
163181
}

0 commit comments

Comments
 (0)