mirror of https://github.com/apache/jclouds.git
Allow to parse parameters with @ParamParser before binding them to the request
This commit is contained in:
parent
dda1b5baab
commit
fd258352ea
|
@ -944,6 +944,7 @@ public class RestAnnotationProcessor<T> {
|
||||||
|
|
||||||
public GeneratedHttpRequest<T> decorateRequest(GeneratedHttpRequest<T> request) throws NegativeArraySizeException,
|
public GeneratedHttpRequest<T> decorateRequest(GeneratedHttpRequest<T> request) throws NegativeArraySizeException,
|
||||||
ExecutionException {
|
ExecutionException {
|
||||||
|
Cache<Integer, Set<Annotation>> indexToParamExtractor = methodToIndexOfParamToParamParserAnnotations.get(request.getJavaMethod());
|
||||||
OUTER: for (Entry<Integer, Set<Annotation>> entry : concat(//
|
OUTER: for (Entry<Integer, Set<Annotation>> entry : concat(//
|
||||||
filterValues(methodToIndexOfParamToBinderParamAnnotation.get(request.getJavaMethod()).asMap(), notEmpty)
|
filterValues(methodToIndexOfParamToBinderParamAnnotation.get(request.getJavaMethod()).asMap(), notEmpty)
|
||||||
.entrySet(), //
|
.entrySet(), //
|
||||||
|
@ -978,7 +979,17 @@ public class RestAnnotationProcessor<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (input != null) {
|
if (input != null) {
|
||||||
request = binder.bindToRequest(request, input);
|
// Allow the input parameter to be parsed by the ParamParser before binding
|
||||||
|
Set<Annotation> extractors = indexToParamExtractor.get(entry.getKey());
|
||||||
|
Object parsedValue = null;
|
||||||
|
if (extractors != null && extractors.size() > 0) {
|
||||||
|
ParamParser extractor = (ParamParser) extractors.iterator().next();
|
||||||
|
parsedValue = injector.getInstance(extractor.value()).apply(input);
|
||||||
|
} else {
|
||||||
|
parsedValue = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
request = binder.bindToRequest(request, parsedValue);
|
||||||
}
|
}
|
||||||
if (shouldBreak)
|
if (shouldBreak)
|
||||||
break OUTER;
|
break OUTER;
|
||||||
|
|
|
@ -2537,6 +2537,48 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
|
||||||
assertEquals(domain.getElem(), "Hello World");
|
assertEquals(domain.getElem(), "Hello World");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class AppendTextParser implements Function<Object, String> {
|
||||||
|
@Override
|
||||||
|
public String apply(Object input)
|
||||||
|
{
|
||||||
|
return input.toString() + "PARSED";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface TestBindWithParamParser {
|
||||||
|
@GET
|
||||||
|
@Path("/foo")
|
||||||
|
public ListenableFuture<String> bindWithoutParsing(
|
||||||
|
@BinderParam(BindToStringPayload.class) String param);
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("/bar")
|
||||||
|
public ListenableFuture<String> bindAfterParsing(
|
||||||
|
@BinderParam(BindToStringPayload.class) @ParamParser(AppendTextParser.class) String param);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBindWithoutParamParser() throws SecurityException, NoSuchMethodException, IOException {
|
||||||
|
RestAnnotationProcessor<TestBindWithParamParser> processor = factory(TestBindWithParamParser.class);
|
||||||
|
Method method = TestBindWithParamParser.class.getMethod("bindWithoutParsing", String.class);
|
||||||
|
GeneratedHttpRequest<TestBindWithParamParser> request = processor.createRequest(method, "test");
|
||||||
|
|
||||||
|
assertRequestLineEquals(request, "GET http://localhost:9999/foo HTTP/1.1");
|
||||||
|
assertNonPayloadHeadersEqual(request, "");
|
||||||
|
assertPayloadEquals(request, "test", "application/unknown", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBindWithParamParser() throws SecurityException, NoSuchMethodException, IOException {
|
||||||
|
RestAnnotationProcessor<TestBindWithParamParser> processor = factory(TestBindWithParamParser.class);
|
||||||
|
Method method = TestBindWithParamParser.class.getMethod("bindAfterParsing", String.class);
|
||||||
|
GeneratedHttpRequest<TestBindWithParamParser> request = processor.createRequest(method, "test");
|
||||||
|
|
||||||
|
assertRequestLineEquals(request, "GET http://localhost:9999/bar HTTP/1.1");
|
||||||
|
assertNonPayloadHeadersEqual(request, "");
|
||||||
|
assertPayloadEquals(request, "testPARSED", "application/unknown", false);
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expectedExceptions = NullPointerException.class)
|
@Test(expectedExceptions = NullPointerException.class)
|
||||||
public void testAddHostNullWithHost() throws Exception{
|
public void testAddHostNullWithHost() throws Exception{
|
||||||
assertNull(RestAnnotationProcessor.addHostIfMissing(null,null));
|
assertNull(RestAnnotationProcessor.addHostIfMissing(null,null));
|
||||||
|
|
Loading…
Reference in New Issue