From a72ba8405db56adc6ad92dbe8fdf9cbb863d7373 Mon Sep 17 00:00:00 2001 From: Jason King Date: Tue, 15 Nov 2011 16:24:29 +0000 Subject: [PATCH] Issue 695: Add host to URI if missing via EndpointParam annotation --- .../internal/RestAnnotationProcessor.java | 14 +++++++-- .../internal/RestAnnotationProcessorTest.java | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java index 1de7fa947e..1fc970da04 100644 --- a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java +++ b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java @@ -19,6 +19,7 @@ package org.jclouds.rest.internal; 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.Iterables.concat; import static com.google.common.collect.Iterables.get; @@ -755,9 +756,18 @@ public class RestAnnotationProcessor { } else { 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> futureBooleanLiteral = new TypeLiteral>() { diff --git a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java index 68c346e37c..3fef693a26 100644 --- a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java @@ -32,6 +32,7 @@ import static org.jclouds.io.Payloads.newStringPayload; import static org.jclouds.rest.RestContextFactory.contextSpec; import static org.jclouds.rest.RestContextFactory.createContextBuilder; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; import java.io.File; import java.io.IOException; @@ -2536,6 +2537,35 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest { 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(); @BeforeClass