From 44edfcfb33205a71c4c37722e7311c184df6c6f2 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Wed, 3 Nov 2010 16:16:17 +0000 Subject: [PATCH] Added version support in RestClient. Revised CallPath to extract version number specified in the path. Moved version tests in CallPathProviderTest to CallPathTest. --- .../google/gson/rest/client/RestClient.java | 4 ++- .../gson/webservice/definition/CallPath.java | 35 ++++++++++++++++--- .../webservice/definition/CallPathTest.java | 19 +++++----- .../gson/wsf/inject/CallPathProvider.java | 13 +------ 4 files changed, 43 insertions(+), 28 deletions(-) rename wsf/src/test/java/com/google/gson/wsf/inject/CallPathProviderTest.java => wsdef/src/test/java/com/google/gson/webservice/definition/CallPathTest.java (62%) diff --git a/wsclient/src/main/java/com/google/gson/rest/client/RestClient.java b/wsclient/src/main/java/com/google/gson/rest/client/RestClient.java index 4996da4f..3cfde220 100644 --- a/wsclient/src/main/java/com/google/gson/rest/client/RestClient.java +++ b/wsclient/src/main/java/com/google/gson/rest/client/RestClient.java @@ -51,7 +51,9 @@ public class RestClient { } private URL getWebServiceUrl(RestCallSpec callSpec) { - String url = config.getServiceBaseUrl() + callSpec.getPath().get(); + double version = callSpec.getVersion(); + String versionPath = version == -1 ? "" : "/" + version; + String url = config.getServiceBaseUrl() + versionPath + callSpec.getPath().get(); try { return new URL(url); } catch (MalformedURLException e) { diff --git a/wsdef/src/main/java/com/google/gson/webservice/definition/CallPath.java b/wsdef/src/main/java/com/google/gson/webservice/definition/CallPath.java index d8349ddd..d9ed1b86 100644 --- a/wsdef/src/main/java/com/google/gson/webservice/definition/CallPath.java +++ b/wsdef/src/main/java/com/google/gson/webservice/definition/CallPath.java @@ -23,15 +23,38 @@ package com.google.gson.webservice.definition; public final class CallPath { private final String path; + private final double version; public CallPath(String path) { - this.path = path; + if (path == null) { + this.path = null; + version = -1D; + } else { + int index1 = path.indexOf('/'); + int index2 = path.substring(index1+1).indexOf('/'); + String versionStr = path.substring(index1+1, index2+1); + String callPathStr = path; + double givenVersion = -1D; + try { + // Skip over the version number from the URL + givenVersion = Double.parseDouble(versionStr); + callPathStr = path.substring(index2+1); + } catch (NumberFormatException e) { + // Assume that version number wasn't specified + } + this.path = callPathStr; + this.version = givenVersion; + } } public String get() { return path; } - + + public double getVersion() { + return version; + } + @Override public int hashCode() { return path.hashCode(); @@ -45,9 +68,13 @@ public final class CallPath { if (obj == null) { return false; } - return getClass() == obj.getClass() && path == ((CallPath)obj).path; + return getClass() == obj.getClass() && equal(path, ((CallPath)obj).path); } - + + private static boolean equal(String s1, String s2) { + return s1 == s2 || (s1 != null && s2 != null && s1.equals(s2)); + } + @Override public String toString() { return path; diff --git a/wsf/src/test/java/com/google/gson/wsf/inject/CallPathProviderTest.java b/wsdef/src/test/java/com/google/gson/webservice/definition/CallPathTest.java similarity index 62% rename from wsf/src/test/java/com/google/gson/wsf/inject/CallPathProviderTest.java rename to wsdef/src/test/java/com/google/gson/webservice/definition/CallPathTest.java index baf11939..f4fea72e 100644 --- a/wsf/src/test/java/com/google/gson/wsf/inject/CallPathProviderTest.java +++ b/wsdef/src/test/java/com/google/gson/webservice/definition/CallPathTest.java @@ -13,24 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.google.gson.wsf.inject; +package com.google.gson.webservice.definition; import junit.framework.TestCase; -/** - * Unit test for {@link CallPathProvider} - * - * @author Inderjeet Singh - */ -public class CallPathProviderTest extends TestCase { +public class CallPathTest extends TestCase { public void testVersionIsSkipped() { - CallPathProvider provider = new CallPathProvider("/1.0/rest/service1"); - assertEquals("/rest/service1", provider.get().get()); + CallPath path = new CallPath("/1.0/rest/service1"); + assertEquals("/rest/service1", path.get()); + assertEquals(1D, path.getVersion()); } public void testVersionNotPresent() { - CallPathProvider provider = new CallPathProvider("/rest/service1"); - assertEquals("/rest/service1", provider.get().get()); + CallPath path = new CallPath("/rest/service1"); + assertEquals("/rest/service1", path.get()); + assertEquals(-1D, path.getVersion()); } } diff --git a/wsf/src/main/java/com/google/gson/wsf/inject/CallPathProvider.java b/wsf/src/main/java/com/google/gson/wsf/inject/CallPathProvider.java index 0bca2ae7..7f39b9a7 100644 --- a/wsf/src/main/java/com/google/gson/wsf/inject/CallPathProvider.java +++ b/wsf/src/main/java/com/google/gson/wsf/inject/CallPathProvider.java @@ -35,18 +35,7 @@ public final class CallPathProvider implements Provider { } public CallPathProvider(String pathInfo) { - int index1 = pathInfo.indexOf('/'); - int index2 = pathInfo.substring(index1+1).indexOf('/'); - String versionStr = pathInfo.substring(index1+1, index2+1); - String callPathStr = pathInfo; - try { - // Skip over the version number from the URL - Double.parseDouble(versionStr); - callPathStr = pathInfo.substring(index2+1); - } catch (NumberFormatException e) { - // Assume that version number wasn't specified - } - this.callPath = new CallPath(callPathStr); + this.callPath = new CallPath(pathInfo); } @Override