From ad4ae74066823d24a849affff0eed19296fffc1a Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 28 Jul 2010 11:53:27 -0700 Subject: [PATCH] Issue 320: removed some other tie-ins to Gson --- .../ParseDataBagItemFromJsonTest.java | 6 +-- .../org/jclouds/http/functions/ParseJson.java | 31 ++++++++------- .../functions/ParseAddressFromJsonTest.java | 3 +- .../functions/ParseAddressesFromJsonTest.java | 3 +- .../ParseMetadataFromJsonResponseTest.java | 13 +++---- ...ParseSessionTokenFromJsonResponseTest.java | 10 ++--- .../ParseUploadInfoFromJsonResponseTest.java | 13 +++---- .../ParseObjectInfoListFromJsonResponse.java | 39 +++++++------------ .../ParseStatusesFromJsonResponseTest.java | 4 +- 9 files changed, 56 insertions(+), 66 deletions(-) diff --git a/chef/core/src/test/java/org/jclouds/chef/functions/ParseDataBagItemFromJsonTest.java b/chef/core/src/test/java/org/jclouds/chef/functions/ParseDataBagItemFromJsonTest.java index fcc2c495cb..59cfda7e87 100644 --- a/chef/core/src/test/java/org/jclouds/chef/functions/ParseDataBagItemFromJsonTest.java +++ b/chef/core/src/test/java/org/jclouds/chef/functions/ParseDataBagItemFromJsonTest.java @@ -9,11 +9,11 @@ import org.jclouds.chef.domain.DataBagItem; import org.jclouds.http.HttpResponse; import org.jclouds.http.functions.ParseJson; import org.jclouds.io.Payloads; +import org.jclouds.json.Json; import org.jclouds.json.config.GsonModule; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; -import com.google.gson.Gson; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Key; @@ -25,14 +25,14 @@ import com.google.inject.TypeLiteral; @Test(groups = "unit", testName = "chef.ParseDataBagItemFromJsonTest") public class ParseDataBagItemFromJsonTest { private ParseJson handler; - private Gson mapper; + private Json mapper; @BeforeTest protected void setUpInjector() throws IOException { Injector injector = Guice.createInjector(new ChefParserModule(), new GsonModule()); handler = injector.getInstance(Key.get(new TypeLiteral>() { })); - mapper = injector.getInstance(Gson.class); + mapper = injector.getInstance(Json.class); } public void test1() { diff --git a/core/src/main/java/org/jclouds/http/functions/ParseJson.java b/core/src/main/java/org/jclouds/http/functions/ParseJson.java index 1eb8f4f261..774f9fbd89 100644 --- a/core/src/main/java/org/jclouds/http/functions/ParseJson.java +++ b/core/src/main/java/org/jclouds/http/functions/ParseJson.java @@ -20,9 +20,9 @@ package org.jclouds.http.functions; import static org.jclouds.http.HttpUtils.releasePayload; +import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; +import java.lang.reflect.Type; import javax.annotation.Resource; import javax.inject.Inject; @@ -30,10 +30,11 @@ import javax.inject.Singleton; import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponseException; +import org.jclouds.json.Json; import org.jclouds.logging.Logger; +import org.jclouds.util.Utils; import com.google.common.base.Function; -import com.google.gson.Gson; import com.google.inject.TypeLiteral; /** @@ -47,12 +48,12 @@ public class ParseJson implements Function { @Resource protected Logger logger = Logger.NULL; - protected final Gson gson; + protected final Json json; protected final TypeLiteral type; @Inject - public ParseJson(Gson gson, TypeLiteral type) { - this.gson = gson; + public ParseJson(Json json, TypeLiteral type) { + this.json = json; this.type = type; } @@ -67,8 +68,7 @@ public class ParseJson implements Function { StringBuilder message = new StringBuilder(); message.append("Error parsing input"); logger.error(e, message.toString()); - throw new HttpResponseException(message.toString() + "\n" + from, - null, from, e); + throw new HttpResponseException(message.toString() + "\n" + from, null, from, e); } finally { releasePayload(from); } @@ -76,12 +76,17 @@ public class ParseJson implements Function { } @SuppressWarnings("unchecked") - public T apply(InputStream stream) { + public T apply(InputStream stream) throws IOException { + return (T) apply(stream, type.getType()); + } + + @SuppressWarnings("unchecked") + public V apply(InputStream stream, Type type) throws IOException { try { - return (T) gson.fromJson(new InputStreamReader(stream, "UTF-8"), type - .getType()); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("jclouds requires UTF-8 encoding", e); + return (V) json.fromJson(Utils.toStringAndClose(stream), type); + } finally { + if (stream != null) + stream.close(); } } } \ No newline at end of file diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressFromJsonTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressFromJsonTest.java index aabe40e78b..71d0b22ddc 100644 --- a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressFromJsonTest.java +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressFromJsonTest.java @@ -27,6 +27,7 @@ import org.jclouds.http.HttpResponse; import org.jclouds.ibmdev.config.IBMDeveloperCloudParserModule; import org.jclouds.ibmdev.domain.Address; import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; @@ -45,7 +46,7 @@ public class ParseAddressFromJsonTest { @BeforeTest protected void setUpInjector() throws IOException { - Injector injector = Guice.createInjector(new IBMDeveloperCloudParserModule()); + Injector injector = Guice.createInjector(new IBMDeveloperCloudParserModule(), new GsonModule()); handler = injector.getInstance(ParseAddressFromJson.class); } diff --git a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressesFromJsonTest.java b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressesFromJsonTest.java index 54f05fec1d..a141bd37a6 100644 --- a/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressesFromJsonTest.java +++ b/ibmdev/src/test/java/org/jclouds/ibmdev/functions/ParseAddressesFromJsonTest.java @@ -26,6 +26,7 @@ import org.jclouds.http.HttpResponse; import org.jclouds.ibmdev.config.IBMDeveloperCloudParserModule; import org.jclouds.ibmdev.domain.Address; import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; @@ -44,7 +45,7 @@ public class ParseAddressesFromJsonTest { @BeforeTest protected void setUpInjector() throws IOException { - Injector injector = Guice.createInjector(new IBMDeveloperCloudParserModule()); + Injector injector = Guice.createInjector(new IBMDeveloperCloudParserModule(), new GsonModule()); handler = injector.getInstance(ParseAddressesFromJson.class); } diff --git a/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseMetadataFromJsonResponseTest.java b/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseMetadataFromJsonResponseTest.java index a5cdee2c29..bd4210e1dd 100644 --- a/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseMetadataFromJsonResponseTest.java +++ b/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseMetadataFromJsonResponseTest.java @@ -25,8 +25,8 @@ import java.net.UnknownHostException; import java.util.Map; import org.jclouds.http.HttpResponse; -import org.jclouds.http.functions.config.SaxParserModule; import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; import org.testng.annotations.Test; import com.google.common.collect.ImmutableMap; @@ -41,16 +41,13 @@ import com.google.inject.Injector; @Test(groups = "unit", testName = "sdn.ParseMetadataFromJsonResponseTest") public class ParseMetadataFromJsonResponseTest { - Injector i = Guice.createInjector(new SaxParserModule()); + Injector i = Guice.createInjector(new GsonModule()); public void testApplyInputStreamDetails() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/metadata.json"); - ParseMetadataFromJsonResponse parser = i - .getInstance(ParseMetadataFromJsonResponse.class); - Map response = parser.apply(new HttpResponse(200, "ok", - Payloads.newInputStreamPayload(is))); - assertEquals(response, ImmutableMap.of("MD5", "IGPBYI1uC6+AJJxC4r5YBA==", - "test", "1")); + ParseMetadataFromJsonResponse parser = i.getInstance(ParseMetadataFromJsonResponse.class); + Map response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); + assertEquals(response, ImmutableMap.of("MD5", "IGPBYI1uC6+AJJxC4r5YBA==", "test", "1")); } } diff --git a/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseSessionTokenFromJsonResponseTest.java b/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseSessionTokenFromJsonResponseTest.java index 20d8b14f9b..5cbcb25639 100644 --- a/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseSessionTokenFromJsonResponseTest.java +++ b/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseSessionTokenFromJsonResponseTest.java @@ -24,8 +24,8 @@ import java.io.InputStream; import java.net.UnknownHostException; import org.jclouds.http.HttpResponse; -import org.jclouds.http.functions.config.SaxParserModule; import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; import org.testng.annotations.Test; import com.google.inject.Guice; @@ -39,15 +39,13 @@ import com.google.inject.Injector; @Test(groups = "unit", testName = "sdn.ParseSessionTokenFromJsonResponseTest") public class ParseSessionTokenFromJsonResponseTest { - Injector i = Guice.createInjector(new SaxParserModule()); + Injector i = Guice.createInjector(new GsonModule()); public void testApplyInputStreamDetails() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/login.json"); - ParseSessionTokenFromJsonResponse parser = i - .getInstance(ParseSessionTokenFromJsonResponse.class); - String response = parser.apply(new HttpResponse(200, "ok", Payloads - .newInputStreamPayload(is))); + ParseSessionTokenFromJsonResponse parser = i.getInstance(ParseSessionTokenFromJsonResponse.class); + String response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); assertEquals(response, "e4b08449-4501-4b7a-af6a-d4e1e1bd7919"); } diff --git a/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseUploadInfoFromJsonResponseTest.java b/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseUploadInfoFromJsonResponseTest.java index 175f2e309e..25ad475f7d 100644 --- a/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseUploadInfoFromJsonResponseTest.java +++ b/nirvanix/sdn/core/src/test/java/org/jclouds/nirvanix/sdn/functions/ParseUploadInfoFromJsonResponseTest.java @@ -25,8 +25,8 @@ import java.net.URI; import java.net.UnknownHostException; import org.jclouds.http.HttpResponse; -import org.jclouds.http.functions.config.SaxParserModule; import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; import org.jclouds.nirvanix.sdn.domain.UploadInfo; import org.testng.annotations.Test; @@ -41,18 +41,15 @@ import com.google.inject.Injector; @Test(groups = "unit", testName = "sdn.ParseUploadInfoFromJsonResponse") public class ParseUploadInfoFromJsonResponseTest { - Injector i = Guice.createInjector(new SaxParserModule()); + Injector i = Guice.createInjector(new GsonModule()); public void testApplyInputStreamDetails() throws UnknownHostException { InputStream is = getClass().getResourceAsStream("/authtoken.json"); - ParseUploadInfoFromJsonResponse parser = i - .getInstance(ParseUploadInfoFromJsonResponse.class); - UploadInfo response = parser.apply(new HttpResponse(200, "ok", Payloads - .newInputStreamPayload(is))); + ParseUploadInfoFromJsonResponse parser = i.getInstance(ParseUploadInfoFromJsonResponse.class); + UploadInfo response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); assertEquals(response.getHost(), URI.create("https://node1.nirvanix.com")); - assertEquals(response.getToken(), - "siR-ALYd~BEcJ8GR2tE~oX3SEHO8~2WXKT5xjFk~YLS5OvJyHI21TN34rQ"); + assertEquals(response.getToken(), "siR-ALYd~BEcJ8GR2tE~oX3SEHO8~2WXKT5xjFk~YLS5OvJyHI21TN34rQ"); } } diff --git a/rackspace/src/main/java/org/jclouds/rackspace/cloudfiles/functions/ParseObjectInfoListFromJsonResponse.java b/rackspace/src/main/java/org/jclouds/rackspace/cloudfiles/functions/ParseObjectInfoListFromJsonResponse.java index 966c40f93f..a80877eecd 100644 --- a/rackspace/src/main/java/org/jclouds/rackspace/cloudfiles/functions/ParseObjectInfoListFromJsonResponse.java +++ b/rackspace/src/main/java/org/jclouds/rackspace/cloudfiles/functions/ParseObjectInfoListFromJsonResponse.java @@ -21,9 +21,8 @@ package org.jclouds.rackspace.cloudfiles.functions; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; +import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; import java.util.SortedSet; @@ -33,6 +32,7 @@ import org.jclouds.blobstore.domain.PageSet; import org.jclouds.blobstore.domain.internal.PageSetImpl; import org.jclouds.http.HttpRequest; import org.jclouds.http.functions.ParseJson; +import org.jclouds.json.Json; import org.jclouds.rackspace.cloudfiles.domain.ObjectInfo; import org.jclouds.rackspace.cloudfiles.domain.internal.ObjectInfoImpl; import org.jclouds.rackspace.cloudfiles.options.ListContainerOptions; @@ -42,7 +42,6 @@ import org.jclouds.rest.internal.GeneratedHttpRequest; import com.google.common.base.Function; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; -import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.google.inject.TypeLiteral; @@ -51,37 +50,31 @@ import com.google.inject.TypeLiteral; * * @author Adrian Cole */ -public class ParseObjectInfoListFromJsonResponse extends - ParseJson> implements InvocationContext { +public class ParseObjectInfoListFromJsonResponse extends ParseJson> implements InvocationContext { private GeneratedHttpRequest request; @Inject - public ParseObjectInfoListFromJsonResponse(Gson gson) { - super(gson, new TypeLiteral>() { + public ParseObjectInfoListFromJsonResponse(Json json) { + super(json, new TypeLiteral>() { }); } public PageSet apply(InputStream stream) { checkState(request != null, "request should be initialized at this point"); - checkState(request.getArgs() != null, - "request.getArgs() should be initialized at this point"); - checkArgument(request.getArgs()[0] instanceof String, - "arg[0] must be a container name"); + checkState(request.getArgs() != null, "request.getArgs() should be initialized at this point"); + checkArgument(request.getArgs()[0] instanceof String, "arg[0] must be a container name"); checkArgument(request.getArgs()[1] instanceof ListContainerOptions[], "arg[1] must be an array of ListContainerOptions"); - ListContainerOptions[] optionsList = (ListContainerOptions[]) request - .getArgs()[1]; - ListContainerOptions options = optionsList.length > 0 ? optionsList[0] - : ListContainerOptions.NONE; + ListContainerOptions[] optionsList = (ListContainerOptions[]) request.getArgs()[1]; + ListContainerOptions options = optionsList.length > 0 ? optionsList[0] : ListContainerOptions.NONE; Type listType = new TypeToken>() { }.getType(); try { - SortedSet list = gson.fromJson(new InputStreamReader( - stream, "UTF-8"), listType); - SortedSet returnVal = Sets.newTreeSet(Iterables.transform( - list, new Function() { + SortedSet list = apply(stream, listType); + SortedSet returnVal = Sets.newTreeSet(Iterables.transform(list, + new Function() { public ObjectInfo apply(ObjectInfoImpl from) { return from; } @@ -89,16 +82,14 @@ public class ParseObjectInfoListFromJsonResponse extends boolean truncated = options.getMaxResults() == returnVal.size(); String marker = truncated ? returnVal.last().getName() : null; return new PageSetImpl(returnVal, marker); - - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("jclouds requires UTF-8 encoding", e); + } catch (IOException e) { + throw new RuntimeException("problem reading response from request: " + request, e); } } @Override public ParseObjectInfoListFromJsonResponse setContext(HttpRequest request) { - checkArgument(request instanceof GeneratedHttpRequest, - "note this handler requires a GeneratedHttpRequest"); + checkArgument(request instanceof GeneratedHttpRequest, "note this handler requires a GeneratedHttpRequest"); this.request = (GeneratedHttpRequest) request; return this; } diff --git a/twitter/src/test/java/org/jclouds/twitter/functions/ParseStatusesFromJsonResponseTest.java b/twitter/src/test/java/org/jclouds/twitter/functions/ParseStatusesFromJsonResponseTest.java index 1894e8c02e..144d228e33 100644 --- a/twitter/src/test/java/org/jclouds/twitter/functions/ParseStatusesFromJsonResponseTest.java +++ b/twitter/src/test/java/org/jclouds/twitter/functions/ParseStatusesFromJsonResponseTest.java @@ -20,9 +20,9 @@ package org.jclouds.twitter.functions; import static org.testng.Assert.assertEquals; +import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.net.UnknownHostException; import java.util.SortedSet; import org.jclouds.date.DateService; @@ -52,7 +52,7 @@ public class ParseStatusesFromJsonResponseTest { DateService dateService = new SimpleDateFormatDateService(); - public void testApplyInputStreamDetails() throws UnknownHostException { + public void testApplyInputStreamDetails() throws IOException { InputStream is = getClass().getResourceAsStream("/test_mentions.json"); SortedSet expects = ImmutableSortedSet