added @Transform

This commit is contained in:
Adrian Cole 2012-07-15 20:10:49 -07:00
parent daef7b4ce9
commit 872046ed4c
3 changed files with 71 additions and 0 deletions

View File

@ -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<? extends Function<?, ?>> value();
}

View File

@ -124,6 +124,7 @@ import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.ResponseParser; import org.jclouds.rest.annotations.ResponseParser;
import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.SkipEncoding; import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.annotations.Transform;
import org.jclouds.rest.annotations.Unwrap; import org.jclouds.rest.annotations.Unwrap;
import org.jclouds.rest.annotations.VirtualHost; import org.jclouds.rest.annotations.VirtualHost;
import org.jclouds.rest.annotations.WrapWith; import org.jclouds.rest.annotations.WrapWith;
@ -298,6 +299,11 @@ public class RestAnnotationProcessor<T> {
} else { } else {
transformer = injector.getInstance(getParserOrThrowException(method)); 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; return transformer;
} }

View File

@ -125,6 +125,7 @@ import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.ResponseParser; import org.jclouds.rest.annotations.ResponseParser;
import org.jclouds.rest.annotations.SelectJson; import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.SkipEncoding; import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.annotations.Transform;
import org.jclouds.rest.annotations.Unwrap; import org.jclouds.rest.annotations.Unwrap;
import org.jclouds.rest.annotations.VirtualHost; import org.jclouds.rest.annotations.VirtualHost;
import org.jclouds.rest.annotations.WrapWith; import org.jclouds.rest.annotations.WrapWith;
@ -1037,6 +1038,20 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
@SelectJson("jobid") @SelectJson("jobid")
ListenableFuture<Long> selectLong(); ListenableFuture<Long> selectLong();
@GET
@Path("/")
@SelectJson("jobid")
@Transform(AddOne.class)
ListenableFuture<Long> selectLongAddOne();
static class AddOne implements Function<Long, Long> {
@Override
public Long apply(Long o) {
return o + 1;
}
}
@GET @GET
@Path("/") @Path("/")
@SelectJson("runit") @SelectJson("runit")
@ -1262,6 +1277,18 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
.payload("{ \"destroyvirtualmachineresponse\" : {\"jobid\":4} }").build()), new Long(4)); .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<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) 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 { static class TestRequestFilter1 implements HttpRequestFilter {
public HttpRequest filter(HttpRequest request) throws HttpException { public HttpRequest filter(HttpRequest request) throws HttpException {
return request; return request;