Add retrieving the self uri and make getURI more flexible so as to work with Openstack Diablo

This commit is contained in:
Matt Stephenson 2011-11-08 11:31:34 -06:00
parent e9cff5e17c
commit dd5f343954
2 changed files with 63 additions and 17 deletions

View File

@ -22,31 +22,81 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import sun.awt.image.ImageWatched;
import javax.annotation.Nullable;
/**
* @author Dmitri Babaev
* @author Matt Stephenson
*/
public class Resource {
private List<Map<String, String>> links = Lists.newArrayList();
public URI getURI() {
//This is the preference order for returning a URI in getURI
private enum LinkType {
BOOKMARK_JSON(new Predicate<Map<String, String>>() {
@Override
public boolean apply(@Nullable Map<String, String> linkMap) {
return Functions.forMap(linkMap, "").apply("rel").equals("bookmark") &&
Functions.forMap(linkMap, "").apply("type").contains("json");
}
}),
BOOKMARK_ANY(new Predicate<Map<String, String>>() {
@Override
public boolean apply(@Nullable Map<String, String> linkMap) {
return Functions.forMap(linkMap, "").apply("rel").equals("bookmark");
}
}),
SELF(new Predicate<Map<String, String>>() {
@Override
public boolean apply(@Nullable Map<String, String> linkMap) {
return Functions.forMap(linkMap, "").apply("rel").equals("self");
}
});
Predicate<Map<String,String>> linkPredicate;
LinkType(Predicate<Map<String,String>> linkPredicate) {
this.linkPredicate = linkPredicate;
};
}
private final ConcurrentSkipListMap<LinkType,URI> orderedSelfReferences = new ConcurrentSkipListMap<LinkType,URI>();
private void populateOrderedSelfReferences() {
for (Map<String, String> linkProperties : links) {
try {
if (!Functions.forMap(linkProperties, "").apply("rel").equals("bookmark"))
continue;
if (!Functions.forMap(linkProperties, "").apply("type").contains("json"))
continue;
return new URI(linkProperties.get("href"));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
for (LinkType type : LinkType.values()) {
if(type.linkPredicate.apply(linkProperties)) {
try {
orderedSelfReferences.put(type, new URI(linkProperties.get("href")));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
}
if(orderedSelfReferences.isEmpty())
throw new IllegalStateException("URI is not available");
}
throw new IllegalStateException("URI is not available");
public URI getURI() {
if(orderedSelfReferences.isEmpty())
populateOrderedSelfReferences();
return orderedSelfReferences.firstEntry().getValue();
}
public URI getSelfURI() {
if(orderedSelfReferences.isEmpty())
populateOrderedSelfReferences();
return orderedSelfReferences.get(LinkType.SELF);
}
}

View File

@ -9,6 +9,7 @@
"created" : "2010-08-10T12:00:00Z",
"hostId" : "e4d909c290d0fb1ca068ffaddf22cbd0",
"affinityId" : "fc88bcf8394db9c8d0564e08ca6a9724188a84d1",
"uuid" : "d84e2086-fc0d-11e0-8e08-2837371c69ae",
"status" : "BUILD",
"progress" : 60,
"addresses" : {
@ -53,6 +54,7 @@
"created" : "2010-08-10T12:00:00Z",
"hostId" : "9e107d9d372bb6826bd81d3542a419d6",
"affinityId" : "b414fa41cb37b97dcb58d6c76112af1258e9eae2",
"uuid" : "e66d54d4-fc0d-11e0-b93f-2837371c69ae",
"status" : "ACTIVE",
"addresses" : {
"public" : [
@ -74,12 +76,6 @@
},
{
"rel" : "bookmark",
"type" : "application/vnd.openstack.compute-v1.1+xml",
"href" : "http://servers.api.openstack.org/1234/servers/5678"
},
{
"rel" : "bookmark",
"type" : "application/vnd.openstack.compute-v1.1+json",
"href" : "http://servers.api.openstack.org/1234/servers/56789"
}
]