From 872046ed4c6fbf1cde4c224f50d906dec3c196d4 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 15 Jul 2012 20:10:49 -0700 Subject: [PATCH] added @Transform --- .../jclouds/rest/annotations/Transform.java | 38 +++++++++++++++++++ .../internal/RestAnnotationProcessor.java | 6 +++ .../internal/RestAnnotationProcessorTest.java | 27 +++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 core/src/main/java/org/jclouds/rest/annotations/Transform.java diff --git a/core/src/main/java/org/jclouds/rest/annotations/Transform.java b/core/src/main/java/org/jclouds/rest/annotations/Transform.java new file mode 100644 index 0000000000..684b809b81 --- /dev/null +++ b/core/src/main/java/org/jclouds/rest/annotations/Transform.java @@ -0,0 +1,38 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.rest.annotations; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import com.google.common.base.Function; + +/** + * Shows the transformer type used to parse the first result of the HttpResponse + * + * @author Adrian Cole + */ +@Target(METHOD) +@Retention(RUNTIME) +public @interface Transform { + Class> value(); +} diff --git a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java index fe5b28992a..44bdbcc71b 100644 --- a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java +++ b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java @@ -124,6 +124,7 @@ import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.ResponseParser; import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.annotations.SkipEncoding; +import org.jclouds.rest.annotations.Transform; import org.jclouds.rest.annotations.Unwrap; import org.jclouds.rest.annotations.VirtualHost; import org.jclouds.rest.annotations.WrapWith; @@ -298,6 +299,11 @@ public class RestAnnotationProcessor { } else { transformer = injector.getInstance(getParserOrThrowException(method)); } + if (method.isAnnotationPresent(Transform.class)) { + transformer = Functions + .compose(Function.class.cast(injector.getInstance(method.getAnnotation(Transform.class).value())), + transformer); + } return transformer; } diff --git a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java index 4069e13d23..02258207d0 100644 --- a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java @@ -125,6 +125,7 @@ import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.ResponseParser; import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.annotations.SkipEncoding; +import org.jclouds.rest.annotations.Transform; import org.jclouds.rest.annotations.Unwrap; import org.jclouds.rest.annotations.VirtualHost; import org.jclouds.rest.annotations.WrapWith; @@ -1037,6 +1038,20 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { @SelectJson("jobid") ListenableFuture selectLong(); + @GET + @Path("/") + @SelectJson("jobid") + @Transform(AddOne.class) + ListenableFuture selectLongAddOne(); + + static class AddOne implements Function { + + @Override + public Long apply(Long o) { + return o + 1; + } + } + @GET @Path("/") @SelectJson("runit") @@ -1262,6 +1277,18 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { .payload("{ \"destroyvirtualmachineresponse\" : {\"jobid\":4} }").build()), new Long(4)); } + @SuppressWarnings("unchecked") + public void selectLongAddOne() throws SecurityException, NoSuchMethodException, IOException { + Method method = TestPut.class.getMethod("selectLongAddOne"); + HttpRequest request = factory(TestPut.class).createRequest(method); + + Function> parser = (Function>) RestAnnotationProcessor + .createResponseParser(parserFactory, injector, method, request); + + assertEquals(parser.apply(HttpResponse.builder().statusCode(200).message("ok") + .payload("{ \"destroyvirtualmachineresponse\" : {\"jobid\":4} }").build()), new Long(5)); + } + static class TestRequestFilter1 implements HttpRequestFilter { public HttpRequest filter(HttpRequest request) throws HttpException { return request;