Repository: incubator-brooklyn Updated Branches: refs/heads/master b34a53e74 -> 2556b29ab
Determine type of entity in rest response from api annotation Previously the Response was read into a String, which makes it difficult to convert back into the domain object. Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/72627412 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/72627412 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/72627412 Branch: refs/heads/master Commit: 7262741265566da944ec648ce35a3c59664adef7 Parents: 5ceb8aa Author: Robert Moss <[email protected]> Authored: Mon Jun 15 16:31:21 2015 +0100 Committer: Robert Moss <[email protected]> Committed: Mon Jun 15 16:31:21 2015 +0100 ---------------------------------------------------------------------- .../java/brooklyn/rest/api/EntityConfigApi.java | 20 ++++++++++----- .../java/brooklyn/rest/client/BrooklynApi.java | 27 +++++++++++++------- .../util/http/BuiltResponsePreservingError.java | 4 +-- 3 files changed, 34 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/72627412/usage/rest-api/src/main/java/brooklyn/rest/api/EntityConfigApi.java ---------------------------------------------------------------------- diff --git a/usage/rest-api/src/main/java/brooklyn/rest/api/EntityConfigApi.java b/usage/rest-api/src/main/java/brooklyn/rest/api/EntityConfigApi.java index b8b75b4..6452a48 100644 --- a/usage/rest-api/src/main/java/brooklyn/rest/api/EntityConfigApi.java +++ b/usage/rest-api/src/main/java/brooklyn/rest/api/EntityConfigApi.java @@ -18,6 +18,19 @@ */ package brooklyn.rest.api; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + import brooklyn.rest.apidoc.Apidoc; import brooklyn.rest.domain.EntityConfigSummary; @@ -26,12 +39,6 @@ import com.wordnik.swagger.core.ApiErrors; import com.wordnik.swagger.core.ApiOperation; import com.wordnik.swagger.core.ApiParam; -import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; - -import java.util.List; -import java.util.Map; - @Path("/v1/applications/{application}/entities/{entity}/config") @Apidoc("Entity Config") @Produces(MediaType.APPLICATION_JSON) @@ -64,6 +71,7 @@ public interface EntityConfigApi { @ApiParam(value = "Return raw config data instead of display values", required = false) @QueryParam("raw") @DefaultValue("false") final Boolean raw); + //To call this endpoint set the Accept request field e.g curl -H "Accept: application/json" ... @GET @Path("/{config}") @ApiOperation(value = "Fetch config value (json)", responseClass = "Object") http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/72627412/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 9765ecf..fb2322c 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 @@ -39,6 +39,7 @@ import org.jboss.resteasy.client.ClientRequest; import org.jboss.resteasy.client.ClientResponse; import org.jboss.resteasy.client.ProxyFactory; import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor; +import org.jboss.resteasy.specimpl.BuiltResponse; import org.jboss.resteasy.util.GenericType; import brooklyn.rest.api.AccessApi; @@ -59,6 +60,8 @@ import brooklyn.rest.api.VersionApi; import brooklyn.util.exceptions.Exceptions; import brooklyn.util.http.BuiltResponsePreservingError; +import com.wordnik.swagger.core.ApiOperation; + /** * @author Adam Lowe */ @@ -116,22 +119,23 @@ public class BrooklynApi { final T result0 = ProxyFactory.create(clazz, target, clientExecutor); return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[] { clazz }, new InvocationHandler() { @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - Object result1; + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { - result1 = method.invoke(result0, args); + Object result1 = method.invoke(result0, args); + if (result1 instanceof Response) { + String responseClass = method.getAnnotation(ApiOperation.class).responseClass(); + Class<?> type = Class.forName(responseClass); + // wrap the original response so it self-closes + result1 = BuiltResponsePreservingError.copyResponseAndClose((Response) result1, type); + } + return result1; } catch (Throwable e) { if (e instanceof InvocationTargetException) { // throw the original exception e = ((InvocationTargetException)e).getTargetException(); } throw Exceptions.propagate(e); - } - if (result1 instanceof Response) { - // wrap the original response so it self-closes - result1 = BuiltResponsePreservingError.copyResponseAndClose((Response) result1); - } - return result1; + } } }); } @@ -197,6 +201,11 @@ 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)) { throw new IllegalArgumentException("Response should be instance of ClientResponse, is: " + response.getClass()); } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/72627412/usage/rest-client/src/main/java/brooklyn/util/http/BuiltResponsePreservingError.java ---------------------------------------------------------------------- diff --git a/usage/rest-client/src/main/java/brooklyn/util/http/BuiltResponsePreservingError.java b/usage/rest-client/src/main/java/brooklyn/util/http/BuiltResponsePreservingError.java index 1dffb58..de21dc9 100644 --- a/usage/rest-client/src/main/java/brooklyn/util/http/BuiltResponsePreservingError.java +++ b/usage/rest-client/src/main/java/brooklyn/util/http/BuiltResponsePreservingError.java @@ -45,7 +45,7 @@ public class BuiltResponsePreservingError extends BuiltResponse { } @SuppressWarnings("deprecation") - public static Response copyResponseAndClose(Response source) { + public static <T> Response copyResponseAndClose(Response source, Class<T> type) { int status = -1; Headers<Object> headers = new Headers<Object>(); Object entity = null; @@ -54,7 +54,7 @@ public class BuiltResponsePreservingError extends BuiltResponse { headers.putAll(source.getHeaders()); if (source instanceof org.jboss.resteasy.client.ClientResponse) { // ClientResponse requires strong type info, which we don't yet have - entity = ((org.jboss.resteasy.client.ClientResponse<?>)source).getEntity(String.class); + entity = ((org.jboss.resteasy.client.ClientResponse<?>)source).getEntity(type); } else { entity = source.getEntity(); }
