mirror of https://github.com/apache/jclouds.git
Issue 320: removed some other tie-ins to Gson
This commit is contained in:
parent
57ba278cbb
commit
ad4ae74066
|
@ -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<DataBagItem> 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<ParseJson<DataBagItem>>() {
|
||||
}));
|
||||
mapper = injector.getInstance(Gson.class);
|
||||
mapper = injector.getInstance(Json.class);
|
||||
}
|
||||
|
||||
public void test1() {
|
||||
|
|
|
@ -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<T> implements Function<HttpResponse, T> {
|
|||
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
protected final Gson gson;
|
||||
protected final Json json;
|
||||
protected final TypeLiteral<T> type;
|
||||
|
||||
@Inject
|
||||
public ParseJson(Gson gson, TypeLiteral<T> type) {
|
||||
this.gson = gson;
|
||||
public ParseJson(Json json, TypeLiteral<T> type) {
|
||||
this.json = json;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
@ -67,8 +68,7 @@ public class ParseJson<T> implements Function<HttpResponse, T> {
|
|||
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<T> implements Function<HttpResponse, T> {
|
|||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T apply(InputStream stream) {
|
||||
public T apply(InputStream stream) throws IOException {
|
||||
return (T) apply(stream, type.getType());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> 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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String, String> 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<String, String> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
|
||||
assertEquals(response, ImmutableMap.of("MD5", "IGPBYI1uC6+AJJxC4r5YBA==", "test", "1"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<PageSet<ObjectInfo>> implements InvocationContext {
|
||||
public class ParseObjectInfoListFromJsonResponse extends ParseJson<PageSet<ObjectInfo>> implements InvocationContext {
|
||||
|
||||
private GeneratedHttpRequest<?> request;
|
||||
|
||||
@Inject
|
||||
public ParseObjectInfoListFromJsonResponse(Gson gson) {
|
||||
super(gson, new TypeLiteral<PageSet<ObjectInfo>>() {
|
||||
public ParseObjectInfoListFromJsonResponse(Json json) {
|
||||
super(json, new TypeLiteral<PageSet<ObjectInfo>>() {
|
||||
});
|
||||
}
|
||||
|
||||
public PageSet<ObjectInfo> 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<SortedSet<ObjectInfoImpl>>() {
|
||||
}.getType();
|
||||
|
||||
try {
|
||||
SortedSet<ObjectInfoImpl> list = gson.fromJson(new InputStreamReader(
|
||||
stream, "UTF-8"), listType);
|
||||
SortedSet<ObjectInfo> returnVal = Sets.newTreeSet(Iterables.transform(
|
||||
list, new Function<ObjectInfoImpl, ObjectInfo>() {
|
||||
SortedSet<ObjectInfoImpl> list = apply(stream, listType);
|
||||
SortedSet<ObjectInfo> returnVal = Sets.newTreeSet(Iterables.transform(list,
|
||||
new Function<ObjectInfoImpl, ObjectInfo>() {
|
||||
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<ObjectInfo>(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;
|
||||
}
|
||||
|
|
|
@ -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<Status> expects = ImmutableSortedSet
|
||||
|
|
Loading…
Reference in New Issue