Issue 695: Add host to URI if missing via EndpointParam annotation

This commit is contained in:
Jason King 2011-11-15 16:24:29 +00:00
parent b86598cf49
commit a72ba8405d
2 changed files with 42 additions and 2 deletions

View File

@ -19,6 +19,7 @@
package org.jclouds.rest.internal; package org.jclouds.rest.internal;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Collections2.filter; import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Iterables.concat; import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.get; import static com.google.common.collect.Iterables.get;
@ -755,9 +756,18 @@ public class RestAnnotationProcessor<T> {
} else { } else {
throw new IllegalStateException("no annotations on class or method: " + method); throw new IllegalStateException("no annotations on class or method: " + method);
} }
return injector.getInstance(Key.get(URI.class, annotation.value())); endpoint = injector.getInstance(Key.get(URI.class, annotation.value()));
} }
return endpoint; return addHostIfMissing(endpoint, injector.getInstance(Key.get(URI.class, org.jclouds.location.Provider.class)));
}
public static URI addHostIfMissing(URI original, URI withHost) {
checkNotNull(withHost,"URI witHost cannot be null");
checkArgument(withHost.getHost()!=null, "URI withHost must have host:"+withHost);
if(original == null) return null;
if (original.getHost() != null) return original;
return withHost.resolve(original);
} }
public static final TypeLiteral<ListenableFuture<Boolean>> futureBooleanLiteral = new TypeLiteral<ListenableFuture<Boolean>>() { public static final TypeLiteral<ListenableFuture<Boolean>> futureBooleanLiteral = new TypeLiteral<ListenableFuture<Boolean>>() {

View File

@ -32,6 +32,7 @@ import static org.jclouds.io.Payloads.newStringPayload;
import static org.jclouds.rest.RestContextFactory.contextSpec; import static org.jclouds.rest.RestContextFactory.contextSpec;
import static org.jclouds.rest.RestContextFactory.createContextBuilder; import static org.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -2536,6 +2537,35 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(domain.getElem(), "Hello World"); assertEquals(domain.getElem(), "Hello World");
} }
@Test(expectedExceptions = NullPointerException.class)
public void testAddHostNullWithHost() throws Exception{
assertNull(RestAnnotationProcessor.addHostIfMissing(null,null));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testAddHostWithHostHasNoHost() throws Exception{
assertNull(RestAnnotationProcessor.addHostIfMissing(null,new URI("/no/host")));
}
@Test
public void testAddHostNullOriginal() throws Exception{
assertNull(RestAnnotationProcessor.addHostIfMissing(null,new URI("http://foo")));
}
@Test
public void testAddHostOriginalHasHost() throws Exception{
URI original = new URI("http://hashost/foo");
URI result = RestAnnotationProcessor.addHostIfMissing(original,new URI("http://foo"));
assertEquals(original,result);
}
@Test
public void testAddHostIfMissing() throws Exception{
URI result = RestAnnotationProcessor.addHostIfMissing(new URI("/bar"),new URI("http://foo"));
assertEquals(new URI("http://foo/bar"),result);
}
DateService dateService = new SimpleDateFormatDateService(); DateService dateService = new SimpleDateFormatDateService();
@BeforeClass @BeforeClass