mirror of https://github.com/apache/jclouds.git
Issue 320: added utils().json() to access json parsing
This commit is contained in:
parent
85a59468d8
commit
503eb869f8
compute/src/main/java/org/jclouds/compute/internal
core/src/main/java/org/jclouds/rest
gogrid/src/test/java/org/jclouds/gogrid/handlers
rimuhosting/src/main/java/org/jclouds/rimuhosting/miro/functions
|
@ -27,6 +27,7 @@ import org.jclouds.Constants;
|
||||||
import org.jclouds.compute.Utils;
|
import org.jclouds.compute.Utils;
|
||||||
import org.jclouds.date.DateService;
|
import org.jclouds.date.DateService;
|
||||||
import org.jclouds.encryption.EncryptionService;
|
import org.jclouds.encryption.EncryptionService;
|
||||||
|
import org.jclouds.json.Json;
|
||||||
import org.jclouds.logging.Logger.LoggerFactory;
|
import org.jclouds.logging.Logger.LoggerFactory;
|
||||||
import org.jclouds.rest.HttpAsyncClient;
|
import org.jclouds.rest.HttpAsyncClient;
|
||||||
import org.jclouds.rest.HttpClient;
|
import org.jclouds.rest.HttpClient;
|
||||||
|
@ -44,12 +45,12 @@ public class UtilsImpl extends org.jclouds.rest.internal.UtilsImpl implements Ut
|
||||||
private Factory sshFactory;
|
private Factory sshFactory;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
UtilsImpl(HttpClient simpleClient, HttpAsyncClient simpleAsyncClient,
|
UtilsImpl(Json json, HttpClient simpleClient, HttpAsyncClient simpleAsyncClient,
|
||||||
EncryptionService encryption, DateService date,
|
EncryptionService encryption, DateService date,
|
||||||
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads,
|
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads,
|
||||||
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioThreads,
|
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioThreads,
|
||||||
LoggerFactory loggerFactory) {
|
LoggerFactory loggerFactory) {
|
||||||
super(simpleClient, simpleAsyncClient, encryption, date, userThreads, ioThreads,
|
super(json, simpleClient, simpleAsyncClient, encryption, date, userThreads, ioThreads,
|
||||||
loggerFactory);
|
loggerFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.util.concurrent.ExecutorService;
|
||||||
|
|
||||||
import org.jclouds.date.DateService;
|
import org.jclouds.date.DateService;
|
||||||
import org.jclouds.encryption.EncryptionService;
|
import org.jclouds.encryption.EncryptionService;
|
||||||
|
import org.jclouds.json.Json;
|
||||||
import org.jclouds.logging.Logger.LoggerFactory;
|
import org.jclouds.logging.Logger.LoggerFactory;
|
||||||
import org.jclouds.rest.internal.UtilsImpl;
|
import org.jclouds.rest.internal.UtilsImpl;
|
||||||
|
|
||||||
|
@ -36,6 +37,13 @@ import com.google.inject.ImplementedBy;
|
||||||
@ImplementedBy(UtilsImpl.class)
|
@ImplementedBy(UtilsImpl.class)
|
||||||
public interface Utils {
|
public interface Utils {
|
||||||
|
|
||||||
|
Json getJson();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* #see #getJson
|
||||||
|
*/
|
||||||
|
Json json();
|
||||||
|
|
||||||
HttpAsyncClient getHttpAsyncClient();
|
HttpAsyncClient getHttpAsyncClient();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,6 +26,7 @@ import javax.inject.Named;
|
||||||
import org.jclouds.Constants;
|
import org.jclouds.Constants;
|
||||||
import org.jclouds.date.DateService;
|
import org.jclouds.date.DateService;
|
||||||
import org.jclouds.encryption.EncryptionService;
|
import org.jclouds.encryption.EncryptionService;
|
||||||
|
import org.jclouds.json.Json;
|
||||||
import org.jclouds.logging.Logger.LoggerFactory;
|
import org.jclouds.logging.Logger.LoggerFactory;
|
||||||
import org.jclouds.rest.HttpAsyncClient;
|
import org.jclouds.rest.HttpAsyncClient;
|
||||||
import org.jclouds.rest.HttpClient;
|
import org.jclouds.rest.HttpClient;
|
||||||
|
@ -39,6 +40,7 @@ import com.google.inject.Singleton;
|
||||||
@Singleton
|
@Singleton
|
||||||
public class UtilsImpl implements Utils {
|
public class UtilsImpl implements Utils {
|
||||||
|
|
||||||
|
private final Json json;
|
||||||
private final HttpClient simpleClient;
|
private final HttpClient simpleClient;
|
||||||
private final HttpAsyncClient simpleAsyncClient;
|
private final HttpAsyncClient simpleAsyncClient;
|
||||||
private final EncryptionService encryption;
|
private final EncryptionService encryption;
|
||||||
|
@ -48,11 +50,11 @@ public class UtilsImpl implements Utils {
|
||||||
private final LoggerFactory loggerFactory;
|
private final LoggerFactory loggerFactory;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
protected UtilsImpl(HttpClient simpleClient, HttpAsyncClient simpleAsyncClient,
|
protected UtilsImpl(Json json, HttpClient simpleClient, HttpAsyncClient simpleAsyncClient,
|
||||||
EncryptionService encryption, DateService date,
|
EncryptionService encryption, DateService date,
|
||||||
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads,
|
@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userThreads,
|
||||||
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioThreads,
|
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioThreads, LoggerFactory loggerFactory) {
|
||||||
LoggerFactory loggerFactory) {
|
this.json = json;
|
||||||
this.simpleClient = simpleClient;
|
this.simpleClient = simpleClient;
|
||||||
this.simpleAsyncClient = simpleAsyncClient;
|
this.simpleAsyncClient = simpleAsyncClient;
|
||||||
this.encryption = encryption;
|
this.encryption = encryption;
|
||||||
|
@ -132,4 +134,14 @@ public class UtilsImpl implements Utils {
|
||||||
return loggerFactory;
|
return loggerFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Json getJson() {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Json json() {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,8 @@ import java.io.InputStream;
|
||||||
import org.jclouds.gogrid.mock.HttpCommandMock;
|
import org.jclouds.gogrid.mock.HttpCommandMock;
|
||||||
import org.jclouds.http.HttpCommand;
|
import org.jclouds.http.HttpCommand;
|
||||||
import org.jclouds.http.HttpResponse;
|
import org.jclouds.http.HttpResponse;
|
||||||
import org.jclouds.http.functions.config.SaxParserModule;
|
|
||||||
import org.jclouds.io.Payloads;
|
import org.jclouds.io.Payloads;
|
||||||
|
import org.jclouds.json.config.GsonModule;
|
||||||
import org.testng.TestException;
|
import org.testng.TestException;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
@ -51,8 +51,7 @@ public class GoGridErrorHandlerTest {
|
||||||
public void testHandler() {
|
public void testHandler() {
|
||||||
InputStream is = getClass().getResourceAsStream("/test_error_handler.json");
|
InputStream is = getClass().getResourceAsStream("/test_error_handler.json");
|
||||||
|
|
||||||
|
GoGridErrorHandler handler = Guice.createInjector(new GsonModule()).getInstance(GoGridErrorHandler.class);
|
||||||
GoGridErrorHandler handler = Guice.createInjector(new SaxParserModule()).getInstance(GoGridErrorHandler.class);
|
|
||||||
|
|
||||||
HttpCommand command = createHttpCommand();
|
HttpCommand command = createHttpCommand();
|
||||||
handler.handleError(command, new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
|
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");
|
assertNotNull(createdException, "There should've been an exception generated");
|
||||||
String message = createdException.getMessage();
|
String message = createdException.getMessage();
|
||||||
assertTrue(message.contains("No object found that matches your input criteria."),
|
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"),
|
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
|
// make sure the InputStream is closed
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -27,12 +27,12 @@ import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import org.jclouds.http.HttpResponseException;
|
import org.jclouds.http.HttpResponseException;
|
||||||
|
import org.jclouds.json.Json;
|
||||||
import org.jclouds.rest.AuthorizationException;
|
import org.jclouds.rest.AuthorizationException;
|
||||||
import org.jclouds.rimuhosting.miro.domain.internal.RimuHostingResponse;
|
import org.jclouds.rimuhosting.miro.domain.internal.RimuHostingResponse;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,11 +44,11 @@ import com.google.gson.reflect.TypeToken;
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
public class ParseRimuHostingException implements Function<Exception, Object> {
|
public class ParseRimuHostingException implements Function<Exception, Object> {
|
||||||
private Gson gson;
|
private Json json;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ParseRimuHostingException(Gson gson) {
|
public ParseRimuHostingException(Json json) {
|
||||||
this.gson = gson;
|
this.json = json;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -59,7 +59,7 @@ public class ParseRimuHostingException implements Function<Exception, Object> {
|
||||||
Type setType = new TypeToken<Map<String, RimuHostingResponse>>() {
|
Type setType = new TypeToken<Map<String, RimuHostingResponse>>() {
|
||||||
}.getType();
|
}.getType();
|
||||||
String test = responseException.getContent();
|
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);
|
RimuHostingResponse firstResponse = Iterables.get(responseMap.values(), 0);
|
||||||
String errorClass = firstResponse.getErrorInfo().getErrorClass();
|
String errorClass = firstResponse.getErrorInfo().getErrorClass();
|
||||||
if (errorClass.equals("PermissionException"))
|
if (errorClass.equals("PermissionException"))
|
||||||
|
|
Loading…
Reference in New Issue