hpcloud-objectstorage: Issue 971 adding builders to domain objects and annotating with ConstructorProperties and Named

This commit is contained in:
Adam Lowe 2012-07-10 14:58:30 +01:00
parent 74f8b2d0c0
commit ed8a1d640f
5 changed files with 203 additions and 107 deletions

View File

@ -1,4 +1,4 @@
/** /*
* Licensed to jclouds, Inc. (jclouds) under one or more * Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file * contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
@ -18,57 +18,198 @@
*/ */
package org.jclouds.hpcloud.objectstorage.domain; package org.jclouds.hpcloud.objectstorage.domain;
import java.beans.ConstructorProperties;
import java.net.URI; import java.net.URI;
import com.google.gson.annotations.SerializedName; import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
/** /**
*
* @author James Murty * @author James Murty
*
*/ */
public class ContainerCDNMetadata implements Comparable<ContainerCDNMetadata> { public class ContainerCDNMetadata implements Comparable<ContainerCDNMetadata> {
protected ContainerCDNMetadata() { public static Builder<?> builder() {
// we want serializers like Gson to work w/o using sun.misc.Unsafe, return new ConcreteBuilder();
// prohibited in GAE. This also implies fields are not final.
// see http://code.google.com/p/jclouds/issues/detail?id=925
} }
private String name; public Builder<?> toBuilder() {
private boolean cdn_enabled; return new ConcreteBuilder().fromContainerCDNMetadata(this);
private long ttl; }
@SerializedName("x-cdn-uri")
private URI cdn_uri;
private String referrer_acl;
private String useragent_acl;
private boolean log_retention;
public ContainerCDNMetadata(String name, boolean cdnEnabled, long ttl, URI cdnUri) { public static abstract class Builder<T extends Builder<T>> {
protected abstract T self();
protected String name;
protected boolean cdnEnabled;
protected long ttl;
protected URI CDNUri;
protected String referrerAcl;
protected String useragentAcl;
protected boolean logRetention;
/**
* @see ContainerCDNMetadata#getName()
*/
public T name(String name) {
this.name = name; this.name = name;
this.cdn_enabled = cdnEnabled; return self();
this.ttl = ttl;
this.cdn_uri = cdnUri;
} }
/** /**
* Beware: The container name is not available from HEAD CDN responses and will be null. return * @see ContainerCDNMetadata#isCDNEnabled()
* the name of the container to which these CDN settings apply.
*/ */
public T CDNEnabled(boolean cdnEnabled) {
this.cdnEnabled = cdnEnabled;
return self();
}
/**
* @see ContainerCDNMetadata#getTTL
*/
public T ttl(long ttl) {
this.ttl = ttl;
return self();
}
/**
* @see ContainerCDNMetadata#getCDNUri()
*/
public T CDNUri(URI CDNUri) {
this.CDNUri = CDNUri;
return self();
}
/**
* @see ContainerCDNMetadata#getReferrerAcl()
*/
public T referrerAcl(String referrerAcl) {
this.referrerAcl = referrerAcl;
return self();
}
/**
* @see ContainerCDNMetadata#getUseragentAcl()
*/
public T useragent_acl(String useragentAcl) {
this.useragentAcl = useragentAcl;
return self();
}
/**
* @see ContainerCDNMetadata#isLogRetention()
*/
public T logRetention(boolean logRetention) {
this.logRetention = logRetention;
return self();
}
public ContainerCDNMetadata build() {
return new ContainerCDNMetadata(name, cdnEnabled, ttl, CDNUri, referrerAcl, useragentAcl, logRetention);
}
public T fromContainerCDNMetadata(ContainerCDNMetadata in) {
return this
.name(in.getName())
.CDNEnabled(in.isCDNEnabled())
.ttl(in.getTTL())
.CDNUri(in.getCDNUri())
.referrerAcl(in.getReferrerAcl())
.useragent_acl(in.getUseragentAcl())
.logRetention(in.isLogRetention());
}
}
private static class ConcreteBuilder extends Builder<ConcreteBuilder> {
@Override
protected ConcreteBuilder self() {
return this;
}
}
private final String name;
private final boolean cdnEnabled;
private final long ttl;
private final URI CDNUri;
private final String referrerAcl;
private final String useragentAcl;
private final boolean logRetention;
@ConstructorProperties({
"name", "cdn_enabled", "ttl", "x-cdn-uri", "referrer_acl", "useragent_acl", "log_retention"
})
protected ContainerCDNMetadata(@Nullable String name, boolean cdnEnabled, long ttl, @Nullable URI CDNUri,
@Nullable String referrerAcl, @Nullable String useragentAcl, boolean logRetention) {
this.name = name;
this.cdnEnabled = cdnEnabled;
this.ttl = ttl;
this.CDNUri = CDNUri;
this.referrerAcl = referrerAcl;
this.useragentAcl = useragentAcl;
this.logRetention = logRetention;
}
/**
* Beware: The container name is not available from HEAD CDN responses and will be null.
*
* @return the name of the container to which these CDN settings apply.
*/
@Nullable
public String getName() { public String getName() {
return name; return this.name;
}
public URI getCDNUri() {
return cdn_uri;
}
public long getTTL() {
return ttl;
} }
public boolean isCDNEnabled() { public boolean isCDNEnabled() {
return cdn_enabled; return this.cdnEnabled;
}
public long getTTL() {
return this.ttl;
}
@Nullable
public URI getCDNUri() {
return this.CDNUri;
}
@Nullable
public String getReferrerAcl() {
return this.referrerAcl;
}
@Nullable
public String getUseragentAcl() {
return this.useragentAcl;
}
public boolean isLogRetention() {
return this.logRetention;
}
@Override
public int hashCode() {
return Objects.hashCode(name, CDNUri);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
ContainerCDNMetadata that = ContainerCDNMetadata.class.cast(obj);
return Objects.equal(this.name, that.name) && Objects.equal(this.CDNUri, that.CDNUri);
}
protected ToStringHelper string() {
return Objects.toStringHelper(this)
.add("name", name).add("cdnEnabled", cdnEnabled).add("ttl", ttl).add("CDNUri", CDNUri)
.add("referrerAcl", referrerAcl).add("useragentAcl", useragentAcl).add("logRetention", logRetention);
}
@Override
public String toString() {
return string().toString();
} }
public int compareTo(ContainerCDNMetadata o) { public int compareTo(ContainerCDNMetadata o) {
@ -76,48 +217,4 @@ public class ContainerCDNMetadata implements Comparable<ContainerCDNMetadata> {
return -1; return -1;
return (this == o) ? 0 : getName().compareTo(o.getName()); return (this == o) ? 0 : getName().compareTo(o.getName());
} }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cdn_uri == null) ? 0 : cdn_uri.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ContainerCDNMetadata other = (ContainerCDNMetadata) obj;
if (cdn_uri == null) {
if (other.cdn_uri != null)
return false;
} else if (!cdn_uri.equals(other.cdn_uri))
return false;
return true;
}
public String getReferrerACL() {
return referrer_acl;
}
public String getUseragentACL() {
return useragent_acl;
}
public boolean isLogRetention() {
return log_retention;
}
@Override
public String toString() {
return String.format(
"[name=%s, cdn_uri=%s, cdn_enabled=%s, log_retention=%s, referrer_acl=%s, ttl=%s, useragent_acl=%s]",
name, cdn_uri, cdn_enabled, log_retention, referrer_acl, ttl, useragent_acl);
}
} }

View File

@ -61,8 +61,9 @@ public class ParseContainerCDNMetadataFromHeaders implements
// just need the name from the path // just need the name from the path
List<String> parts = newArrayList(Splitter.on('/').split(request.getEndpoint().getPath())); List<String> parts = newArrayList(Splitter.on('/').split(request.getEndpoint().getPath()));
return new ContainerCDNMetadata(parts.get(parts.size()-1), Boolean return ContainerCDNMetadata.builder().name(parts.get(parts.size() - 1))
.parseBoolean(cdnEnabled), Long.parseLong(cdnTTL), URI.create(cdnUri)); .CDNEnabled(Boolean.parseBoolean(cdnEnabled)).ttl(Long.parseLong(cdnTTL)).CDNUri(URI.create(cdnUri))
.build();
} }
} }

View File

@ -20,6 +20,7 @@ package org.jclouds.hpcloud.objectstorage.functions;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
@ -31,9 +32,10 @@ import org.jclouds.rest.internal.GeneratedHttpRequest;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.Maps;
/** /**
* This parses @{link {@link org.jclouds.hpcloud.objectstorage.domain.ContainerMetadata} from * This parses @{link {@link org.jclouds.openstack.swift.domain.ContainerMetadata} from
* HTTP headers. * HTTP headers.
* *
* @author Jeremy Daggett * @author Jeremy Daggett
@ -43,28 +45,23 @@ public class ParseContainerMetadataFromHeaders implements Function<HttpResponse,
private GeneratedHttpRequest request; private GeneratedHttpRequest request;
public ContainerMetadata apply(HttpResponse from) { public ContainerMetadata apply(HttpResponse from) {
ContainerMetadata to = new ContainerMetadata(); return ContainerMetadata.builder().name(request.getArgs().get(0).toString())
.readACL(from.getFirstHeaderOrNull(SwiftHeaders.CONTAINER_READ))
to.setName(request.getArgs().get(0).toString()); .bytes(Long.valueOf(from.getFirstHeaderOrNull(SwiftHeaders.CONTAINER_BYTES_USED)))
.count(Long.valueOf(from.getFirstHeaderOrNull(SwiftHeaders.CONTAINER_OBJECT_COUNT)))
to.setReadACL(from.getFirstHeaderOrNull(SwiftHeaders.CONTAINER_READ)); .metadata(extractUserMetadata(from)).build();
to.setBytes(Long.valueOf(from.getFirstHeaderOrNull(SwiftHeaders.CONTAINER_BYTES_USED)));
to.setCount(Long.valueOf(from.getFirstHeaderOrNull(SwiftHeaders.CONTAINER_OBJECT_COUNT)));
addUserMetadataTo(from, to);
return to;
} }
@VisibleForTesting @VisibleForTesting
void addUserMetadataTo(HttpResponse from, ContainerMetadata metadata) { Map<String, String> extractUserMetadata(HttpResponse from) {
Map<String, String> metadata = Maps.newHashMap();
for (Entry<String, String> header : from.getHeaders().entries()) { for (Entry<String, String> header : from.getHeaders().entries()) {
if (header.getKey() != null && header.getKey().startsWith(SwiftHeaders.CONTAINER_METADATA_PREFIX)) if (header.getKey() != null && header.getKey().startsWith(SwiftHeaders.CONTAINER_METADATA_PREFIX))
metadata.getMetadata().put((header.getKey().substring(SwiftHeaders.CONTAINER_METADATA_PREFIX.length())).toLowerCase(), metadata.put((header.getKey().substring(SwiftHeaders.CONTAINER_METADATA_PREFIX.length())).toLowerCase(),
header.getValue()); header.getValue());
} }
return metadata;
} }
@Override @Override

View File

@ -92,7 +92,8 @@ public class HPCloudObjectStorageClientLiveTest extends CommonSwiftClientLiveTes
assertTrue(cdnMetadataList.size() >= 1); assertTrue(cdnMetadataList.size() >= 1);
final long initialTTL = cdnMetadata.getTTL(); final long initialTTL = cdnMetadata.getTTL();
assertTrue(cdnMetadataList.contains(new ContainerCDNMetadata(containerNameWithCDN, true, initialTTL, cdnUri))); assertTrue(cdnMetadataList.contains(ContainerCDNMetadata.builder().name(containerNameWithCDN)
.CDNEnabled(true).ttl(initialTTL).CDNUri(cdnUri).build()));
/* /*
* Test listing with options FIXFIX cdnMetadataList = * Test listing with options FIXFIX cdnMetadataList =

View File

@ -31,7 +31,7 @@ import org.jclouds.http.functions.ParseJson;
import org.jclouds.json.config.GsonModule; import org.jclouds.json.config.GsonModule;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Key; import com.google.inject.Key;
@ -51,16 +51,16 @@ public class ParseContainerCDNMetadataListFromJsonResponseTest {
InputStream is = getClass().getResourceAsStream("/test_list_cdn.json"); InputStream is = getClass().getResourceAsStream("/test_list_cdn.json");
Set<ContainerCDNMetadata> expects = ImmutableSortedSet.of( Set<ContainerCDNMetadata> expects = ImmutableSet.of(
new ContainerCDNMetadata("hpcloud-blobstore.testCDNOperationsContainerWithCDN", false, 3600, ContainerCDNMetadata.builder().name("hpcloud-blobstore.testCDNOperationsContainerWithCDN").CDNEnabled(false).ttl(3600)
URI.create("https://cdnmgmt.hpcloud.net:8080/v1/AUTH_test/")), .CDNUri(URI.create("https://cdnmgmt.hpcloud.net:8080/v1/AUTH_test/")).build(),
new ContainerCDNMetadata("hpcloud-blobstore5", true, 28800, ContainerCDNMetadata.builder().name("hpcloud-blobstore5").CDNEnabled(true).ttl(28800)
URI.create("https://cdnmgmt.hpcloud.net:8080/v1/AUTH_test/")), .CDNUri(URI.create("https://cdnmgmt.hpcloud.net:8080/v1/AUTH_test/")).build(),
new ContainerCDNMetadata("hpcloud-cfcdnint.testCDNOperationsContainerWithCDN", false, 3600, ContainerCDNMetadata.builder().name("hpcloud-cfcdnint.testCDNOperationsContainerWithCDN").CDNEnabled(false).ttl(3600)
URI.create("https://cdnmgmt.hpcloud.net:8080/v1/AUTH_test/"))); .CDNUri(URI.create("https://cdnmgmt.hpcloud.net:8080/v1/AUTH_test/")).build());
ParseJson<SortedSet<ContainerCDNMetadata>> parser = i.getInstance(Key ParseJson<SortedSet<ContainerCDNMetadata>> parser = i.getInstance(
.get(new TypeLiteral<ParseJson<SortedSet<ContainerCDNMetadata>>>() { Key.get(new TypeLiteral<ParseJson<SortedSet<ContainerCDNMetadata>>>() {
})); }));
assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload(is).build()), expects); assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok").payload(is).build()), expects);