Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

AEROGEAR-1006 #55

Closed
wants to merge 3 commits into from

4 participants

@sebastienblanc
Collaborator

No description provided.

@danbev
Collaborator

This will be processed after the 1.0.0 release.

@matzew
Owner

(general) comment: do we need JavaDoc on the param() methods ?

@danbev

Could you format this method so that it has the same number of indents as the other methods.

@danbev

Note sure if this is covered by our checkstyle but a space after the ',' is what most of the code base is using and it would be nice to consistent with that I think.

@danbev

Could you format this method so that it has the same number of indents as the other methods.

Collaborator

well the indent seems okay for this method, can you confirm (you were maybe misleaded by the previous commit) ?

Collaborator

Yes you are right. Sorry my mistake.

@danbev

Could you add an extra space after the ',':
return Optional.fromNullable(createInstance(parameter.getType(),routeContext.getRequest().getHeader(parameter.getName())));

@danbev
Collaborator

+1

@danbev
Collaborator

Merged in 33b9d6f

@danbev danbev closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 11, 2013
  1. @sebastienblanc

    AEROGEAR-1006

    sebastienblanc authored
Commits on Mar 12, 2013
  1. @sebastienblanc
Commits on Mar 22, 2013
  1. @sebastienblanc

    correct indents

    sebastienblanc authored
This page is out of date. Refresh to see the latest.
View
5 src/main/java/org/jboss/aerogear/controller/router/AbstractRoutingModule.java
@@ -85,6 +85,11 @@ public String param(String id) {
return null;
}
+ public <T> T param(String id, Class<T> type) {
+ addParameter(Parameter.param(id, type));
+ return null;
+ }
+
public String param(String id, String defaultValue) {
addParameter(Parameter.param(id, defaultValue, String.class));
return null;
View
44 src/main/java/org/jboss/aerogear/controller/util/ParameterExtractor.java
@@ -35,6 +35,8 @@
import javax.servlet.http.Cookie;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
@@ -50,7 +52,7 @@
* @param routeContext the {@link org.jboss.aerogear.controller.router.RouteContext}.
* @return {@code Object[]} an array of Object matching the route targets parameters.
*/
- public static Map<String, Object> extractArguments(final RouteContext routeContext, final Map<String, Consumer> consumers) {
+ public static Map<String, Object> extractArguments(final RouteContext routeContext, final Map<String, Consumer> consumers) throws Exception {
final Map<String, Object> args = new LinkedHashMap<String, Object>();
for (Parameter<?> parameter : routeContext.getRoute().getParameters()) {
switch (parameter.getParameterType()) {
@@ -73,10 +75,10 @@
if (addIfPresent(extractCookieParam(routeContext, requestParameter), requestParameter.getName(), args)) {
break;
}
- if (addIfPresent(requestParameter.getDefaultValue(), requestParameter.getName(), args)) {
+ if (addIfPresent(extractDefaultParam(requestParameter), requestParameter.getName(), args)) {
break;
}
- if (addIfPresent(extractPathParam(routeContext), requestParameter.getName(), args)) {
+ if (addIfPresent(extractPathParam(routeContext, parameter.getType()), requestParameter.getName(), args)) {
break;
}
throw LoggerMessages.MESSAGES.missingParameterInRequest(requestParameter.getName());
@@ -85,6 +87,13 @@
return args;
}
+ private static Optional<?> extractDefaultParam(RequestParameter<?> requestParameter) throws Exception {
+ if(requestParameter.getDefaultValue().isPresent()) {
+ return Optional.of(createInstance(requestParameter.getType(), requestParameter.getDefaultValue().get().toString()));
+ }
+ return requestParameter.getDefaultValue();
+ }
+
private static Object extractBody(final RouteContext routeContext, final Parameter<?> parameter,
final Map<String, Consumer> consumers) {
final Set<String> mediaTypes = routeContext.getRoute().consumes();
@@ -100,14 +109,17 @@ private static Object extractBody(final RouteContext routeContext, final Paramet
/**
* Extracts a path parameter from the passed in request path.
*
+ *
* @param routeContext the {@link org.jboss.aerogear.controller.router.RouteContext} to extract a path parameter from.
+ * @param type
* @return {@code Optional<String>} containing the extracted path param if present in the request path.
*/
- public static Optional<String> extractPathParam(final RouteContext routeContext) {
+ public static Optional<?> extractPathParam(final RouteContext routeContext, Class<?> type) throws Exception {
final String requestPath = routeContext.getRequestPath();
final int paramOffset = routeContext.getRoute().getPath().indexOf('{');
if (paramOffset != -1 && paramOffset < requestPath.length()) {
- return Optional.of(requestPath.subSequence(paramOffset, requestPath.length()).toString());
+ String pathParam = requestPath.subSequence(paramOffset, requestPath.length()).toString();
+ return Optional.of(createInstance(type, pathParam));
}
return Optional.absent();
}
@@ -146,31 +158,39 @@ private static boolean addIfPresent(final Optional<?> op, final String paramName
return false;
}
- private static Optional<?> extractHeaderParam(final RouteContext routeContext, final RequestParameter<?> parameter) {
- return Optional.fromNullable(routeContext.getRequest().getHeader(parameter.getName()));
+ private static Optional<?> extractHeaderParam(final RouteContext routeContext, final RequestParameter<?> parameter) throws Exception {
+ if(routeContext.getRequest().getHeader(parameter.getName()) != null){
+ return Optional.fromNullable(createInstance(parameter.getType(), routeContext.getRequest().getHeader(parameter.getName())));
+ }
+ return Optional.absent();
}
- private static Optional<?> extractCookieParam(final RouteContext routeContext, final RequestParameter<?> parameter) {
+ private static Optional<?> extractCookieParam(final RouteContext routeContext, final RequestParameter<?> parameter) throws Exception {
final Cookie[] cookies = routeContext.getRequest().getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(parameter.getName())) {
- return Optional.fromNullable(cookie.getValue());
+ return Optional.fromNullable(createInstance(parameter.getType(), cookie.getValue()));
}
}
}
return Optional.absent();
}
- private static Optional<?> extractParam(final RouteContext routeContext, final RequestParameter<?> parameter) {
+ private static Optional<?> extractParam(final RouteContext routeContext, final RequestParameter<?> parameter) throws Exception {
final String[] values = routeContext.getRequest().getParameterMap().get(parameter.getName());
if (values != null) {
if (values.length == 1) {
- return Optional.of(values[0]);
+ return Optional.of(createInstance(parameter.getType(), values[0]));
} else {
throw LoggerMessages.MESSAGES.multivaluedParamsUnsupported(parameter.getName());
}
}
return Optional.absent();
}
-}
+
+ private static Object createInstance( Class<?> type, String arg) throws Exception {
+ Constructor constructor = type.getDeclaredConstructor(String.class);
+ return constructor.newInstance(arg);
+ }
+ }
View
11 src/test/java/org/jboss/aerogear/controller/RoutesTest.java
@@ -70,6 +70,17 @@ public void configuration() {
}
@Test
+ public void routesWithTypedPathParameters() {
+ Routes routes = new AbstractRoutingModule() {
+ @Override
+ public void configuration() {
+ route().from("/car/{id}").on(GET).to(SampleController.class).find(param("id",Long.class));
+ }
+ }.build();
+ assertThat(routes.hasRouteFor(GET, "/car/1", acceptHeaders(MediaType.HTML.getType()))).isTrue();
+ }
+
+ @Test
public void restfulRoute() {
final MediaType custom = new MediaType("application/custom", CustomResponder.class);
Routes routes = new AbstractRoutingModule() {
View
6 src/test/java/org/jboss/aerogear/controller/SampleController.java
@@ -36,6 +36,9 @@ public void index() {
public void client(String name) {
}
+ public void client(Long name) {
+ }
+
public void lol() {
}
@@ -55,6 +58,9 @@ public void find(String id) {
public void find(String color, String brand) {
}
+ public void find(Long id) {
+ }
+
@Paginated
public List<Integer> findBy(PaginationInfo pinfo, String query) {
return ints(pinfo.getOffset(), pinfo.getLimit(), 50);
View
17 src/test/java/org/jboss/aerogear/controller/router/DefaultRouteProcessorTest.java
@@ -97,6 +97,23 @@ public void configuration() {
}
@Test
+ public void testRestRouteWithTypedPathParam() throws Exception {
+ final RouteTester routeTester = RouteTester.from(new AbstractRoutingModule() {
+ @Override
+ public void configuration() {
+ route()
+ .from("/car/{id}").roles("admin")
+ .on(GET)
+ .produces(JSP, JSON)
+ .to(SampleController.class).find(param("id",Long.class));
+ }
+ });
+ routeTester.acceptHeader(JSON).processGetRequest("/car/3");
+ verify(routeTester.<SampleController>getController()).find(new Long(3));
+ verify(routeTester.jsonResponder()).respond(any(), any(RouteContext.class));
+ }
+
+ @Test
public void testFormParameters() throws Exception {
final RouteTester routeTester = RouteTester.from(new AbstractRoutingModule() {
@Override
View
96 src/test/java/org/jboss/aerogear/controller/router/parameter/ParametersTest.java
@@ -67,18 +67,18 @@ public void initMocks() {
}
@Test
- public void extractPathParameter() {
+ public void extractPathParameter() throws Exception {
when(route.getPath()).thenReturn("/cars/{id}");
when(routeContext.getRequestPath()).thenReturn("/cars/2");
- final Optional<String> param = ParameterExtractor.extractPathParam(routeContext);
+ final Optional<?> param = ParameterExtractor.extractPathParam(routeContext, String.class);
assertThat(param.get()).isEqualTo("2");
}
@Test
- public void extractPathParameterButNoParamInRequest() {
+ public void extractPathParameterButNoParamInRequest() throws Exception {
when(route.getPath()).thenReturn("/cars/{id}");
when(routeContext.getRequestPath()).thenReturn("/c");
- assertThat(ParameterExtractor.extractPathParam(routeContext).isPresent()).isFalse();
+ assertThat(ParameterExtractor.extractPathParam(routeContext, String.class).isPresent()).isFalse();
}
@Test
@@ -101,7 +101,7 @@ public void extractIogiParamsNone() throws Exception {
}
@Test
- public void extractPathParam() {
+ public void extractPathParam() throws Exception {
when(request.getParameterMap()).thenReturn(RequestParams.empty());
when(route.getParameters()).thenReturn(asList(Parameter.param("id", String.class)));
when(route.getPath()).thenReturn("/cars/{id}");
@@ -112,6 +112,28 @@ public void extractPathParam() {
}
@Test
+ public void extractPathParamWithLongType() throws Exception {
+ when(request.getParameterMap()).thenReturn(RequestParams.empty());
+ when(route.getParameters()).thenReturn(asList(Parameter.param("id", Long.class)));
+ when(route.getPath()).thenReturn("/cars/{id}");
+ when(routeContext.getRequestPath()).thenReturn("/cars/2");
+ final Map<String, Object> args = ParameterExtractor.extractArguments(routeContext,
+ Collections.<String, Consumer> emptyMap());
+ assertThat(args.get("id")).isEqualTo(new Long(2));
+ }
+
+ @Test
+ public void extractPathParamWithIntegerType() throws Exception {
+ when(request.getParameterMap()).thenReturn(RequestParams.empty());
+ when(route.getParameters()).thenReturn(asList(Parameter.param("id", Integer.class)));
+ when(route.getPath()).thenReturn("/cars/{id}");
+ when(routeContext.getRequestPath()).thenReturn("/cars/2");
+ final Map<String, Object> args = ParameterExtractor.extractArguments(routeContext,
+ Collections.<String, Consumer> emptyMap());
+ assertThat(args.get("id")).isEqualTo(new Integer(2));
+ }
+
+ @Test
public void extractFormParams() throws Exception {
when(request.getParameterMap()).thenReturn(RequestParams.param("name", "Newman").getParamMap());
when(route.getParameters()).thenReturn(asList(Parameter.param("name", String.class)));
@@ -131,6 +153,16 @@ public void extractFormParamsWithDefaultValue() throws Exception {
assertThat(args.get("name")).isEqualTo("defaultName");
}
+ @Test
+ public void extractFormParamsWithDefaultValueWithType() throws Exception {
+ when(request.getParameterMap()).thenReturn(RequestParams.empty());
+ when(route.getParameters()).thenReturn(asList(Parameter.param("name", new Long(2), Long.class)));
+ when(route.getTargetMethod()).thenReturn(SampleController.class.getMethod("client", Long.class));
+ final Map<String, Object> args = ParameterExtractor.extractArguments(routeContext,
+ Collections.<String, Consumer> emptyMap());
+ assertThat(args.get("name")).isEqualTo( new Long(2));
+ }
+
@Test(expected = RuntimeException.class)
public void shouldThrowIfFormParamIsMissingFromRequest() throws Exception {
when(route.getParameters()).thenReturn(asList(Parameter.param("name", null, String.class)));
@@ -152,7 +184,7 @@ public void extractFormParamIogi() throws Exception {
}
@Test
- public void extractHeaderParam() {
+ public void extractHeaderParam() throws Exception {
when(request.getHeader("x-header")).thenReturn("headerValue");
final List<Parameter<?>> parameters = asList(Parameter.param("x-header", "def", String.class));
when(route.getParameters()).thenReturn(parameters);
@@ -164,7 +196,7 @@ public void extractHeaderParam() {
}
@Test
- public void extractHeaderParamWithDefaultValue() {
+ public void extractHeaderParamWithDefaultValue() throws Exception {
final List<Parameter<?>> parameters = asList(Parameter.param("x-header", "defaultHeader", String.class));
when(route.getParameters()).thenReturn(parameters);
final Map<String, Object> args = ParameterExtractor.extractArguments(routeContext,
@@ -173,14 +205,14 @@ public void extractHeaderParamWithDefaultValue() {
}
@Test(expected = RuntimeException.class)
- public void shouldThrowIsHeaderParamIsMissingFromRequest() {
+ public void shouldThrowIsHeaderParamIsMissingFromRequest() throws Exception {
final List<Parameter<?>> parameters = asList(Parameter.param("x-header", String.class));
when(route.getParameters()).thenReturn(parameters);
ParameterExtractor.extractArguments(routeContext, Collections.<String, Consumer> emptyMap());
}
@Test
- public void extractQueryParams() {
+ public void extractQueryParams() throws Exception {
when(request.getParameterMap()).thenReturn(RequestParams.param("brand", "mini").add("year", "2006").getParamMap());
final List<Parameter<?>> parameters = asList(Parameter.param("brand", String.class));
parameters.add(Parameter.param("year", String.class));
@@ -191,8 +223,20 @@ public void extractQueryParams() {
assertThat(args.get("year")).isEqualTo("2006");
}
+ @Test
+ public void extractQueryParamsWithType() throws Exception {
+ when(request.getParameterMap()).thenReturn(RequestParams.param("price", "10").add("year", "2006").getParamMap());
+ final List<Parameter<?>> parameters = asList(Parameter.param("price", Long.class));
+ parameters.add(Parameter.param("year", Long.class));
+ when(route.getParameters()).thenReturn(parameters);
+ final Map<String, Object> args = ParameterExtractor.extractArguments(routeContext,
+ Collections.<String, Consumer> emptyMap());
+ assertThat(args.get("price")).isEqualTo(new Long("10"));
+ assertThat(args.get("year")).isEqualTo(new Long("2006"));
+ }
+
@Test(expected = RuntimeException.class)
- public void shouldThrowIfQueryParamsMissingFromRequest() {
+ public void shouldThrowIfQueryParamsMissingFromRequest() throws Exception {
final List<Parameter<?>> parameters = asList(Parameter.param("brand", String.class));
parameters.add(Parameter.param("year", String.class));
when(route.getParameters()).thenReturn(parameters);
@@ -200,7 +244,20 @@ public void shouldThrowIfQueryParamsMissingFromRequest() {
}
@Test
- public void extractQueryParamsWithDefaultValue() {
+ public void extractQueryParamsWithDefaultValue() throws Exception {
+ when(request.getParameterMap()).thenReturn(RequestParams.param("brand", "mini").getParamMap());
+ final List<Parameter<?>> parameters = asList(Parameter.param("brand", String.class));
+ parameters.add(Parameter.param("year", "2012", String.class));
+ when(route.getParameters()).thenReturn(parameters);
+ when(route.getParameters()).thenReturn(parameters);
+ final Map<String, Object> args = ParameterExtractor.extractArguments(routeContext,
+ Collections.<String, Consumer> emptyMap());
+ assertThat(args.get("brand")).isEqualTo("mini");
+ assertThat(args.get("year")).isEqualTo("2012");
+ }
+
+ @Test
+ public void extractQueryParamsWithDefaultValueWithType() throws Exception {
when(request.getParameterMap()).thenReturn(RequestParams.param("brand", "mini").getParamMap());
final List<Parameter<?>> parameters = asList(Parameter.param("brand", String.class));
parameters.add(Parameter.param("year", "2012", String.class));
@@ -213,7 +270,7 @@ public void extractQueryParamsWithDefaultValue() {
}
@Test
- public void extractCookieParam() {
+ public void extractCookieParam() throws Exception {
final Cookie cookie = mock(Cookie.class);
when(cookie.getName()).thenReturn("testCookie");
when(cookie.getValue()).thenReturn("cookieValue");
@@ -225,8 +282,21 @@ public void extractCookieParam() {
assertThat(args.get("testCookie")).isEqualTo("cookieValue");
}
+ @Test
+ public void extractCookieParamWithType() throws Exception {
+ final Cookie cookie = mock(Cookie.class);
+ when(cookie.getName()).thenReturn("testCookie");
+ when(cookie.getValue()).thenReturn("2");
+ when(request.getCookies()).thenReturn(new Cookie[] { cookie });
+ final List<Parameter<?>> parameters = asList(Parameter.param("testCookie", Long.class));
+ when(route.getParameters()).thenReturn(parameters);
+ final Map<String, Object> args = ParameterExtractor.extractArguments(routeContext,
+ Collections.<String, Consumer> emptyMap());
+ assertThat(args.get("testCookie")).isEqualTo(new Long(2));
+ }
+
@Test(expected = RuntimeException.class)
- public void extractCookieParamMissingFromRequest() {
+ public void extractCookieParamMissingFromRequest() throws Exception {
final List<Parameter<?>> parameters = asList(Parameter.param("testCookie", String.class));
when(route.getParameters()).thenReturn(parameters);
ParameterExtractor.extractArguments(routeContext, Collections.<String, Consumer> emptyMap());
View
9 src/test/java/org/jboss/aerogear/controller/router/parameter/RequestParameterTest.java
@@ -42,4 +42,13 @@ public void pathParameterWithDefaultValue() {
assertThat(parameter.getDefaultValue().get()).isEqualTo("def");
}
+ @Test
+ public void pathParameterWithDefaultValueWithType() {
+ final RequestParameter<Long> parameter = new RequestParameter<Long>("model", Parameter.Type.REQUEST, new Long(5),
+ Long.class);
+ assertThat(parameter.getName()).isEqualTo("model");
+ assertThat(parameter.getParameterType()).isEqualTo(Parameter.Type.REQUEST);
+ assertThat(parameter.getDefaultValue().get()).isEqualTo(new Long("5"));
+ }
+
}
Something went wrong with that request. Please try again.