Repository: incubator-brooklyn Updated Branches: refs/heads/master b3bdacb64 -> c6901da92
BrooklynApi.getEntity handles instances of BuiltResponsePreservingError Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/6ca5d505 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/6ca5d505 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/6ca5d505 Branch: refs/heads/master Commit: 6ca5d505cf8303363608d9cbc20631dee0e763e8 Parents: ea1d6cb Author: Sam Corbett <[email protected]> Authored: Tue Jun 30 11:20:33 2015 +0100 Committer: Sam Corbett <[email protected]> Committed: Tue Jun 30 11:31:33 2015 +0100 ---------------------------------------------------------------------- usage/rest-client/pom.xml | 4 ++ .../java/brooklyn/rest/client/BrooklynApi.java | 43 ++++++++++++++------ .../brooklyn/rest/filter/LoggingFilter.java | 6 +-- 3 files changed, 37 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6ca5d505/usage/rest-client/pom.xml ---------------------------------------------------------------------- diff --git a/usage/rest-client/pom.xml b/usage/rest-client/pom.xml index e409e67..9d76bf4 100644 --- a/usage/rest-client/pom.xml +++ b/usage/rest-client/pom.xml @@ -104,6 +104,10 @@ <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + </dependency> <dependency> <groupId>org.testng</groupId> http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6ca5d505/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java ---------------------------------------------------------------------- diff --git a/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java b/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java index 408addd..f57b4bd 100644 --- a/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java +++ b/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java @@ -44,6 +44,8 @@ import org.jboss.resteasy.util.GenericType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.Gson; + import brooklyn.rest.api.AccessApi; import brooklyn.rest.api.ActivityApi; import brooklyn.rest.api.ApplicationApi; @@ -222,24 +224,39 @@ public class BrooklynApi { } public static <T> T getEntity(Response response, Class<T> type) { - if (response instanceof BuiltResponse) { - Object entity = response.getEntity(); - return type.cast(entity); + if (response instanceof ClientResponse) { + ClientResponse<?> clientResponse = (ClientResponse<?>) response; + return clientResponse.getEntity(type); + } else if (response instanceof BuiltResponse) { + // Handle BuiltResponsePreservingError turning objects into Strings + if (response.getEntity() instanceof String && !type.equals(String.class)) { + return new Gson().fromJson(response.getEntity().toString(), type); + } } - - if (!(response instanceof ClientResponse)) { - throw new IllegalArgumentException("Response should be instance of ClientResponse, is: " + response.getClass()); + // Last-gasp attempt. + return type.cast(response.getEntity()); + } + + public static <T> T getEntity(Response response, GenericType<T> type) { + if (response instanceof ClientResponse) { + ClientResponse<?> clientResponse = (ClientResponse<?>) response; + return clientResponse.getEntity(type); + } else if (response instanceof BuiltResponse) { + // Handle BuiltResponsePreservingError turning objects into Strings + if (response.getEntity() instanceof String) { + return new Gson().fromJson(response.getEntity().toString(), type.getGenericType()); + } } - ClientResponse<?> clientResponse = (ClientResponse<?>) response; - return (T) clientResponse.getEntity(type); + // Last-gasp attempt. + return type.getType().cast(response.getEntity()); } + /** + * @deprecated Use {@link #getEntity(Response, GenericType)} instead. + */ + @Deprecated public static <T> T getEntityGeneric(Response response, GenericType<T> type) { - if (!(response instanceof ClientResponse)) { - throw new IllegalArgumentException("Response should be instance of ClientResponse, is: " + response.getClass()); - } - ClientResponse<?> clientResponse = (ClientResponse<?>) response; - return (T) clientResponse.getEntity(type); + return getEntity(response, type); } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6ca5d505/usage/rest-server/src/main/java/brooklyn/rest/filter/LoggingFilter.java ---------------------------------------------------------------------- diff --git a/usage/rest-server/src/main/java/brooklyn/rest/filter/LoggingFilter.java b/usage/rest-server/src/main/java/brooklyn/rest/filter/LoggingFilter.java index bbe5d37..d841fdb 100644 --- a/usage/rest-server/src/main/java/brooklyn/rest/filter/LoggingFilter.java +++ b/usage/rest-server/src/main/java/brooklyn/rest/filter/LoggingFilter.java @@ -146,9 +146,9 @@ public class LoggingFilter implements Filter { message.append("******"); } else { message.append(httpRequest.getHeader(headerName)); - if (headerNames.hasMoreElements()) { - message.append(", "); - } + } + if (headerNames.hasMoreElements()) { + message.append(", "); } } }
