Issue 320: added utils().json() to access json parsing

This commit is contained in:
Adrian Cole 2010-07-28 11:54:54 -07:00
parent 85a59468d8
commit 503eb869f8
5 changed files with 37 additions and 17 deletions

View File

@ -27,6 +27,7 @@ import org.jclouds.Constants;
import org.jclouds.compute.Utils;
import org.jclouds.date.DateService;
import org.jclouds.encryption.EncryptionService;
import org.jclouds.json.Json;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.rest.HttpAsyncClient;
import org.jclouds.rest.HttpClient;
@ -44,12 +45,12 @@ public class UtilsImpl extends org.jclouds.rest.internal.UtilsImpl implements Ut
private Factory sshFactory;
@Inject
UtilsImpl(HttpClient simpleClient, HttpAsyncClient simpleAsyncClient,
UtilsImpl(Json json, HttpClient simpleClient, HttpAsyncClient simpleAsyncClient,
EncryptionService encryption, DateService date,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads,
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioThreads,
LoggerFactory loggerFactory) {
super(simpleClient, simpleAsyncClient, encryption, date, userThreads, ioThreads,
super(json, simpleClient, simpleAsyncClient, encryption, date, userThreads, ioThreads,
loggerFactory);
}

View File

@ -22,6 +22,7 @@ import java.util.concurrent.ExecutorService;
import org.jclouds.date.DateService;
import org.jclouds.encryption.EncryptionService;
import org.jclouds.json.Json;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.rest.internal.UtilsImpl;
@ -36,6 +37,13 @@ import com.google.inject.ImplementedBy;
@ImplementedBy(UtilsImpl.class)
public interface Utils {
Json getJson();
/**
* #see #getJson
*/
Json json();
HttpAsyncClient getHttpAsyncClient();
/**

View File

@ -26,6 +26,7 @@ import javax.inject.Named;
import org.jclouds.Constants;
import org.jclouds.date.DateService;
import org.jclouds.encryption.EncryptionService;
import org.jclouds.json.Json;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.rest.HttpAsyncClient;
import org.jclouds.rest.HttpClient;
@ -39,6 +40,7 @@ import com.google.inject.Singleton;
@Singleton
public class UtilsImpl implements Utils {
private final Json json;
private final HttpClient simpleClient;
private final HttpAsyncClient simpleAsyncClient;
private final EncryptionService encryption;
@ -48,11 +50,11 @@ public class UtilsImpl implements Utils {
private final LoggerFactory loggerFactory;
@Inject
protected UtilsImpl(HttpClient simpleClient, HttpAsyncClient simpleAsyncClient,
EncryptionService encryption, DateService date,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads,
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioThreads,
LoggerFactory loggerFactory) {
protected UtilsImpl(Json json, HttpClient simpleClient, HttpAsyncClient simpleAsyncClient,
EncryptionService encryption, DateService date,
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads,
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioThreads, LoggerFactory loggerFactory) {
this.json = json;
this.simpleClient = simpleClient;
this.simpleAsyncClient = simpleAsyncClient;
this.encryption = encryption;
@ -132,4 +134,14 @@ public class UtilsImpl implements Utils {
return loggerFactory;
}
@Override
public Json getJson() {
return json;
}
@Override
public Json json() {
return json;
}
}

View File

@ -33,8 +33,8 @@ import java.io.InputStream;
import org.jclouds.gogrid.mock.HttpCommandMock;
import org.jclouds.http.HttpCommand;
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.TestException;
import org.testng.annotations.Test;
@ -51,8 +51,7 @@ public class GoGridErrorHandlerTest {
public void testHandler() {
InputStream is = getClass().getResourceAsStream("/test_error_handler.json");
GoGridErrorHandler handler = Guice.createInjector(new SaxParserModule()).getInstance(GoGridErrorHandler.class);
GoGridErrorHandler handler = Guice.createInjector(new GsonModule()).getInstance(GoGridErrorHandler.class);
HttpCommand command = createHttpCommand();
handler.handleError(command, new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
@ -62,9 +61,9 @@ public class GoGridErrorHandlerTest {
assertNotNull(createdException, "There should've been an exception generated");
String message = createdException.getMessage();
assertTrue(message.contains("No object found that matches your input criteria."),
"Didn't find the expected error cause in the exception message");
"Didn't find the expected error cause in the exception message");
assertTrue(message.contains("IllegalArgumentException"),
"Didn't find the expected error code in the exception message");
"Didn't find the expected error code in the exception message");
// make sure the InputStream is closed
try {

View File

@ -27,12 +27,12 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.http.HttpResponseException;
import org.jclouds.json.Json;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rimuhosting.miro.domain.internal.RimuHostingResponse;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
@ -44,11 +44,11 @@ import com.google.gson.reflect.TypeToken;
*/
@Singleton
public class ParseRimuHostingException implements Function<Exception, Object> {
private Gson gson;
private Json json;
@Inject
public ParseRimuHostingException(Gson gson) {
this.gson = gson;
public ParseRimuHostingException(Json json) {
this.json = json;
}
@Override
@ -59,7 +59,7 @@ public class ParseRimuHostingException implements Function<Exception, Object> {
Type setType = new TypeToken<Map<String, RimuHostingResponse>>() {
}.getType();
String test = responseException.getContent();
Map<String, RimuHostingResponse> responseMap = gson.fromJson(test, setType);
Map<String, RimuHostingResponse> responseMap = json.fromJson(test, setType);
RimuHostingResponse firstResponse = Iterables.get(responseMap.values(), 0);
String errorClass = firstResponse.getErrorInfo().getErrorClass();
if (errorClass.equals("PermissionException"))