Permalink
Browse files

initial parameter support

  • Loading branch information...
1 parent 04431ae commit ec3c39a35627829134bcc047fe491c12f05ea04a @qmx qmx committed Apr 12, 2012
@@ -1,6 +1,12 @@
package org.jboss.aerogear.controller;
+import br.com.caelum.iogi.Iogi;
+import br.com.caelum.iogi.parameters.Parameter;
+import br.com.caelum.iogi.reflection.Target;
+import br.com.caelum.iogi.util.DefaultLocaleProvider;
+import br.com.caelum.iogi.util.NullDependencyProvider;
import org.jboss.aerogear.controller.router.Routes;
+import org.jboss.aerogear.controller.util.StringUtils;
import org.jboss.aerogear.controller.view.View;
import javax.enterprise.inject.spi.Bean;
@@ -11,12 +17,15 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import java.util.LinkedList;
+import java.util.Map;
public class DefaultRouter implements Router {
private Routes routes;
private final BeanManager beanManager;
private ViewResolver viewResolver;
+ private Iogi iogi = new Iogi(new NullDependencyProvider(), new DefaultLocaleProvider());
@Inject
public DefaultRouter(RoutingModule routes, BeanManager beanManager, ViewResolver viewResolver) {
@@ -45,7 +54,8 @@ private RequestMethod extractMethod(HttpServletRequest httpServletRequest) {
public void dispatch(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException {
try {
Route route = routes.routeFor(extractMethod(request), extractPath(request));
- Object result = route.getTargetMethod().invoke(getController(route));
+ Object[] params = extractParameters(request, route);
+ Object result = route.getTargetMethod().invoke(getController(route), params);
String viewPath = viewResolver.resolveViewPathFor(route);
View view = new View(viewPath, result);
if (view.hasModelData()) {
@@ -57,6 +67,29 @@ public void dispatch(HttpServletRequest request, HttpServletResponse response, F
}
}
+ private Object[] extractParameters(HttpServletRequest request, Route route) {
+ LinkedList<Parameter> parameters = new LinkedList<Parameter>();
+ Map<String, String[]> parameterMap = request.getParameterMap();
+ for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
+ String[] value = entry.getValue();
+ if (value.length == 1) {
+ parameters.add(new Parameter(entry.getKey(), value[0]));
+ } else {
+ System.out.println("oops, multivalued params not supported yet");
+ continue;
+ }
+ }
+ Class<?>[] parameterTypes = route.getTargetMethod().getParameterTypes();
+ if (parameterTypes.length == 1) {
+ Class<?> parameterType = parameterTypes[0];
+ Target<?> target = Target.create(parameterType, StringUtils.downCaseFirst(parameterType.getSimpleName()));
+ Object instantiate = iogi.instantiate(target, parameters.toArray(new Parameter[]{}));
+ return new Object[]{instantiate};
+ }
+
+ return new Object[0]; //To change body of created methods use File | Settings | File Templates.
+ }
+
private Object getController(Route route) {
Bean next = beanManager.getBeans(route.getTargetClass()).iterator().next();
return next.create(beanManager.createCreationalContext(next));
@@ -1,5 +1,6 @@
package org.jboss.aerogear.controller;
+
public interface RouteBuilder {
OnMethods from(String path);
@@ -3,6 +3,7 @@
import org.jboss.aerogear.controller.router.Routes;
import org.junit.Test;
+import static org.fest.assertions.Assertions.assertThat;
import static org.jboss.aerogear.controller.RequestMethod.GET;
import static org.jboss.aerogear.controller.RequestMethod.POST;
@@ -25,17 +26,24 @@ public void configuration() {
.from("/lol")
.on(GET, POST)
.to(SampleController.class).lol();
- route()
- .from("/car/{car.id}")
- .on(GET)
- .to(SampleController.class).save(param("{car.id}"));
+
}
}.build();
}
- private static <T> T param(final String s) {
- return (T) null;
+ @Test
+ public void routesWithParameters() {
+ Routes routes = new AbstractRoutingModule() {
+ @Override
+ public void configuration() {
+ route()
+ .from("/cars")
+ .on(POST)
+ .to(SampleController.class).save(param(Car.class));
+ }
+ }.build();
+ assertThat(routes.hasRouteFor(POST, "/cars")).isTrue();
}
public static class SampleController {
@@ -65,5 +73,12 @@ public Car(String name) {
public String getName() {
return name;
}
+
+ @Override
+ public String toString() {
+ return "Car{" +
+ "name='" + name + '\'' +
+ '}';
+ }
}
}

0 comments on commit ec3c39a

Please sign in to comment.