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 7bbcf5b715..2c6ceeea61 100755 --- a/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java +++ b/core/src/main/java/org/jclouds/rest/internal/RestAnnotationProcessor.java @@ -468,17 +468,17 @@ public class RestAnnotationProcessor { private void addMatrixParams(UriBuilder builder, Collection> tokenValues, Method method, Object... args) { if (declaring.isAnnotationPresent(MatrixParams.class)) { - MatrixParams query = declaring.getAnnotation(MatrixParams.class); - addMatrix(builder, query, tokenValues); + MatrixParams matrix = declaring.getAnnotation(MatrixParams.class); + addMatrix(builder, matrix, tokenValues); } if (method.isAnnotationPresent(MatrixParams.class)) { - MatrixParams query = method.getAnnotation(MatrixParams.class); - addMatrix(builder, query, tokenValues); + MatrixParams matrix = method.getAnnotation(MatrixParams.class); + addMatrix(builder, matrix, tokenValues); } - for (Entry query : getMatrixParamKeyValues(method, args).entries()) { - builder.queryParam(query.getKey(), replaceTokens(query.getValue(), tokenValues)); + for (Entry matrix : getMatrixParamKeyValues(method, args).entries()) { + builder.matrixParam(matrix.getKey(), replaceTokens(matrix.getValue(), tokenValues)); } } @@ -963,41 +963,100 @@ public class RestAnnotationProcessor { matrixParamValues.putAll(constants); Map> indexToMatrixParam = methodToindexOfParamToMatrixParamAnnotations .get(method); + + Map> indexToParamExtractor = methodToindexOfParamToParamParserAnnotations + .get(method); for (Entry> entry : indexToMatrixParam.entrySet()) { for (Annotation key : entry.getValue()) { + Set extractors = indexToParamExtractor.get(entry.getKey()); String paramKey = ((MatrixParam) key).value(); - String paramValue = args[entry.getKey()].toString(); + String paramValue; + if (extractors != null && extractors.size() > 0) { + ParamParser extractor = (ParamParser) extractors.iterator().next(); + paramValue = injector.getInstance(extractor.value()).apply(args[entry.getKey()]); + } else { + paramValue = args[entry.getKey()].toString(); + } matrixParamValues.put(paramKey, paramValue); } } + + if (method.isAnnotationPresent(MatrixParam.class) + && method.isAnnotationPresent(ParamParser.class)) { + String paramKey = method.getAnnotation(MatrixParam.class).value(); + String paramValue = injector.getInstance(method.getAnnotation(ParamParser.class).value()) + .apply(args); + matrixParamValues.put(paramKey, paramValue); + + } return matrixParamValues; } private Multimap getFormParamKeyValues(Method method, Object... args) { Multimap formParamValues = LinkedHashMultimap.create(); + formParamValues.putAll(constants); Map> indexToFormParam = methodToindexOfParamToFormParamAnnotations .get(method); + + Map> indexToParamExtractor = methodToindexOfParamToParamParserAnnotations + .get(method); for (Entry> entry : indexToFormParam.entrySet()) { for (Annotation key : entry.getValue()) { + Set extractors = indexToParamExtractor.get(entry.getKey()); String paramKey = ((FormParam) key).value(); - String paramValue = args[entry.getKey()].toString(); + String paramValue; + if (extractors != null && extractors.size() > 0) { + ParamParser extractor = (ParamParser) extractors.iterator().next(); + paramValue = injector.getInstance(extractor.value()).apply(args[entry.getKey()]); + } else { + paramValue = args[entry.getKey()].toString(); + } formParamValues.put(paramKey, paramValue); } } + + if (method.isAnnotationPresent(FormParam.class) + && method.isAnnotationPresent(ParamParser.class)) { + String paramKey = method.getAnnotation(FormParam.class).value(); + String paramValue = injector.getInstance(method.getAnnotation(ParamParser.class).value()) + .apply(args); + formParamValues.put(paramKey, paramValue); + + } return formParamValues; } private Multimap getQueryParamKeyValues(Method method, Object... args) { Multimap queryParamValues = LinkedHashMultimap.create(); + queryParamValues.putAll(constants); Map> indexToQueryParam = methodToindexOfParamToQueryParamAnnotations .get(method); + + Map> indexToParamExtractor = methodToindexOfParamToParamParserAnnotations + .get(method); for (Entry> entry : indexToQueryParam.entrySet()) { for (Annotation key : entry.getValue()) { + Set extractors = indexToParamExtractor.get(entry.getKey()); String paramKey = ((QueryParam) key).value(); - String paramValue = args[entry.getKey()].toString(); + String paramValue; + if (extractors != null && extractors.size() > 0) { + ParamParser extractor = (ParamParser) extractors.iterator().next(); + paramValue = injector.getInstance(extractor.value()).apply(args[entry.getKey()]); + } else { + paramValue = args[entry.getKey()].toString(); + } queryParamValues.put(paramKey, paramValue); } } + + if (method.isAnnotationPresent(QueryParam.class) + && method.isAnnotationPresent(ParamParser.class)) { + String paramKey = method.getAnnotation(QueryParam.class).value(); + String paramValue = injector.getInstance(method.getAnnotation(ParamParser.class).value()) + .apply(args); + queryParamValues.put(paramKey, paramValue); + + } return queryParamValues; } 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 13fe6572b9..8c2f37ac46 100755 --- a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java @@ -46,9 +46,11 @@ import java.util.concurrent.Future; import javax.inject.Named; import javax.inject.Qualifier; import javax.ws.rs.Consumes; +import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.HttpMethod; +import javax.ws.rs.MatrixParam; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; @@ -613,6 +615,24 @@ public class RestAnnotationProcessorTest { @PathParam("path") @ParamParser(FirstCharacter.class) String path) { } + @GET + @Path("/") + public void oneQueryParamExtractor( + @QueryParam("one") @ParamParser(FirstCharacter.class) String one) { + } + + @POST + @Path("/") + public void oneFormParamExtractor( + @FormParam("one") @ParamParser(FirstCharacter.class) String one) { + } + + @GET + @Path("/") + public void oneMatrixParamExtractor( + @MatrixParam("one") @ParamParser(FirstCharacter.class) String one) { + } + @GET @Path("{path}") @PathParam("path") @@ -622,13 +642,48 @@ public class RestAnnotationProcessorTest { } @Test - public void testParamExtractor() throws SecurityException, NoSuchMethodException { + public void testPathParamExtractor() throws SecurityException, NoSuchMethodException, + IOException { Method method = TestPath.class.getMethod("onePathParamExtractor", String.class); GeneratedHttpRequest httpMethod = factory(TestPath.class).createRequest(method, new Object[] { "localhost" }); - assertEquals(httpMethod.getEndpoint().getPath(), "/l"); - assertEquals(httpMethod.getMethod(), HttpMethod.GET); - assertEquals(httpMethod.getHeaders().size(), 0); + assertRequestLineEquals(httpMethod, "GET http://localhost:8080/l HTTP/1.1"); + assertHeadersEqual(httpMethod, ""); + assertEntityEquals(httpMethod, null); + } + + @Test + public void testQueryParamExtractor() throws SecurityException, NoSuchMethodException, + IOException { + Method method = TestPath.class.getMethod("oneQueryParamExtractor", String.class); + GeneratedHttpRequest httpMethod = factory(TestPath.class).createRequest(method, + "localhost"); + assertRequestLineEquals(httpMethod, "GET http://localhost:8080/?one=l HTTP/1.1"); + assertHeadersEqual(httpMethod, ""); + assertEntityEquals(httpMethod, null); + } + + @Test + public void testMatrixParamExtractor() throws SecurityException, NoSuchMethodException, + IOException { + Method method = TestPath.class.getMethod("oneMatrixParamExtractor", String.class); + GeneratedHttpRequest httpMethod = factory(TestPath.class).createRequest(method, + new Object[] { "localhost" }); + assertRequestLineEquals(httpMethod, "GET http://localhost:8080/;one=l HTTP/1.1"); + assertHeadersEqual(httpMethod, ""); + assertEntityEquals(httpMethod, null); + } + + @Test + public void testFormParamExtractor() throws SecurityException, NoSuchMethodException, + IOException { + Method method = TestPath.class.getMethod("oneFormParamExtractor", String.class); + GeneratedHttpRequest httpMethod = factory(TestPath.class).createRequest(method, + new Object[] { "localhost" }); + assertRequestLineEquals(httpMethod, "POST http://localhost:8080/ HTTP/1.1"); + assertHeadersEqual(httpMethod, + "Content-Length: 5\nContent-Type: application/x-www-form-urlencoded\n"); + assertEntityEquals(httpMethod, "one=l"); } @Test