fixed annotation processing of @ParamParser

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2268 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-11-12 18:41:46 +00:00
parent 9a654d7708
commit fedf3ac2cd
2 changed files with 127 additions and 13 deletions

View File

@ -468,17 +468,17 @@ public class RestAnnotationProcessor<T> {
private void addMatrixParams(UriBuilder builder, Collection<Entry<String, String>> tokenValues, private void addMatrixParams(UriBuilder builder, Collection<Entry<String, String>> tokenValues,
Method method, Object... args) { Method method, Object... args) {
if (declaring.isAnnotationPresent(MatrixParams.class)) { if (declaring.isAnnotationPresent(MatrixParams.class)) {
MatrixParams query = declaring.getAnnotation(MatrixParams.class); MatrixParams matrix = declaring.getAnnotation(MatrixParams.class);
addMatrix(builder, query, tokenValues); addMatrix(builder, matrix, tokenValues);
} }
if (method.isAnnotationPresent(MatrixParams.class)) { if (method.isAnnotationPresent(MatrixParams.class)) {
MatrixParams query = method.getAnnotation(MatrixParams.class); MatrixParams matrix = method.getAnnotation(MatrixParams.class);
addMatrix(builder, query, tokenValues); addMatrix(builder, matrix, tokenValues);
} }
for (Entry<String, String> query : getMatrixParamKeyValues(method, args).entries()) { for (Entry<String, String> matrix : getMatrixParamKeyValues(method, args).entries()) {
builder.queryParam(query.getKey(), replaceTokens(query.getValue(), tokenValues)); builder.matrixParam(matrix.getKey(), replaceTokens(matrix.getValue(), tokenValues));
} }
} }
@ -963,41 +963,100 @@ public class RestAnnotationProcessor<T> {
matrixParamValues.putAll(constants); matrixParamValues.putAll(constants);
Map<Integer, Set<Annotation>> indexToMatrixParam = methodToindexOfParamToMatrixParamAnnotations Map<Integer, Set<Annotation>> indexToMatrixParam = methodToindexOfParamToMatrixParamAnnotations
.get(method); .get(method);
Map<Integer, Set<Annotation>> indexToParamExtractor = methodToindexOfParamToParamParserAnnotations
.get(method);
for (Entry<Integer, Set<Annotation>> entry : indexToMatrixParam.entrySet()) { for (Entry<Integer, Set<Annotation>> entry : indexToMatrixParam.entrySet()) {
for (Annotation key : entry.getValue()) { for (Annotation key : entry.getValue()) {
Set<Annotation> extractors = indexToParamExtractor.get(entry.getKey());
String paramKey = ((MatrixParam) key).value(); 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); 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; return matrixParamValues;
} }
private Multimap<String, String> getFormParamKeyValues(Method method, Object... args) { private Multimap<String, String> getFormParamKeyValues(Method method, Object... args) {
Multimap<String, String> formParamValues = LinkedHashMultimap.create(); Multimap<String, String> formParamValues = LinkedHashMultimap.create();
formParamValues.putAll(constants);
Map<Integer, Set<Annotation>> indexToFormParam = methodToindexOfParamToFormParamAnnotations Map<Integer, Set<Annotation>> indexToFormParam = methodToindexOfParamToFormParamAnnotations
.get(method); .get(method);
Map<Integer, Set<Annotation>> indexToParamExtractor = methodToindexOfParamToParamParserAnnotations
.get(method);
for (Entry<Integer, Set<Annotation>> entry : indexToFormParam.entrySet()) { for (Entry<Integer, Set<Annotation>> entry : indexToFormParam.entrySet()) {
for (Annotation key : entry.getValue()) { for (Annotation key : entry.getValue()) {
Set<Annotation> extractors = indexToParamExtractor.get(entry.getKey());
String paramKey = ((FormParam) key).value(); 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); 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; return formParamValues;
} }
private Multimap<String, String> getQueryParamKeyValues(Method method, Object... args) { private Multimap<String, String> getQueryParamKeyValues(Method method, Object... args) {
Multimap<String, String> queryParamValues = LinkedHashMultimap.create(); Multimap<String, String> queryParamValues = LinkedHashMultimap.create();
queryParamValues.putAll(constants);
Map<Integer, Set<Annotation>> indexToQueryParam = methodToindexOfParamToQueryParamAnnotations Map<Integer, Set<Annotation>> indexToQueryParam = methodToindexOfParamToQueryParamAnnotations
.get(method); .get(method);
Map<Integer, Set<Annotation>> indexToParamExtractor = methodToindexOfParamToParamParserAnnotations
.get(method);
for (Entry<Integer, Set<Annotation>> entry : indexToQueryParam.entrySet()) { for (Entry<Integer, Set<Annotation>> entry : indexToQueryParam.entrySet()) {
for (Annotation key : entry.getValue()) { for (Annotation key : entry.getValue()) {
Set<Annotation> extractors = indexToParamExtractor.get(entry.getKey());
String paramKey = ((QueryParam) key).value(); 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); 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; return queryParamValues;
} }

View File

@ -46,9 +46,11 @@ import java.util.concurrent.Future;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Qualifier; import javax.inject.Qualifier;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam; import javax.ws.rs.HeaderParam;
import javax.ws.rs.HttpMethod; import javax.ws.rs.HttpMethod;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
@ -613,6 +615,24 @@ public class RestAnnotationProcessorTest {
@PathParam("path") @ParamParser(FirstCharacter.class) String path) { @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 @GET
@Path("{path}") @Path("{path}")
@PathParam("path") @PathParam("path")
@ -622,13 +642,48 @@ public class RestAnnotationProcessorTest {
} }
@Test @Test
public void testParamExtractor() throws SecurityException, NoSuchMethodException { public void testPathParamExtractor() throws SecurityException, NoSuchMethodException,
IOException {
Method method = TestPath.class.getMethod("onePathParamExtractor", String.class); Method method = TestPath.class.getMethod("onePathParamExtractor", String.class);
GeneratedHttpRequest<?> httpMethod = factory(TestPath.class).createRequest(method, GeneratedHttpRequest<?> httpMethod = factory(TestPath.class).createRequest(method,
new Object[] { "localhost" }); new Object[] { "localhost" });
assertEquals(httpMethod.getEndpoint().getPath(), "/l"); assertRequestLineEquals(httpMethod, "GET http://localhost:8080/l HTTP/1.1");
assertEquals(httpMethod.getMethod(), HttpMethod.GET); assertHeadersEqual(httpMethod, "");
assertEquals(httpMethod.getHeaders().size(), 0); 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 @Test