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,
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<String, String> query : getMatrixParamKeyValues(method, args).entries()) {
builder.queryParam(query.getKey(), replaceTokens(query.getValue(), tokenValues));
for (Entry<String, String> matrix : getMatrixParamKeyValues(method, args).entries()) {
builder.matrixParam(matrix.getKey(), replaceTokens(matrix.getValue(), tokenValues));
}
}
@ -963,41 +963,100 @@ public class RestAnnotationProcessor<T> {
matrixParamValues.putAll(constants);
Map<Integer, Set<Annotation>> indexToMatrixParam = methodToindexOfParamToMatrixParamAnnotations
.get(method);
Map<Integer, Set<Annotation>> indexToParamExtractor = methodToindexOfParamToParamParserAnnotations
.get(method);
for (Entry<Integer, Set<Annotation>> entry : indexToMatrixParam.entrySet()) {
for (Annotation key : entry.getValue()) {
Set<Annotation> 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<String, String> getFormParamKeyValues(Method method, Object... args) {
Multimap<String, String> formParamValues = LinkedHashMultimap.create();
formParamValues.putAll(constants);
Map<Integer, Set<Annotation>> indexToFormParam = methodToindexOfParamToFormParamAnnotations
.get(method);
Map<Integer, Set<Annotation>> indexToParamExtractor = methodToindexOfParamToParamParserAnnotations
.get(method);
for (Entry<Integer, Set<Annotation>> entry : indexToFormParam.entrySet()) {
for (Annotation key : entry.getValue()) {
Set<Annotation> 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<String, String> getQueryParamKeyValues(Method method, Object... args) {
Multimap<String, String> queryParamValues = LinkedHashMultimap.create();
queryParamValues.putAll(constants);
Map<Integer, Set<Annotation>> indexToQueryParam = methodToindexOfParamToQueryParamAnnotations
.get(method);
Map<Integer, Set<Annotation>> indexToParamExtractor = methodToindexOfParamToParamParserAnnotations
.get(method);
for (Entry<Integer, Set<Annotation>> entry : indexToQueryParam.entrySet()) {
for (Annotation key : entry.getValue()) {
Set<Annotation> 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;
}

View File

@ -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