Skip to content

Commit

Permalink
Create ControllerRouteFactory component
Browse files Browse the repository at this point in the history
  • Loading branch information
decebals committed Nov 29, 2021
1 parent f29e997 commit d0793e2
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ControllerApplication extends Application {
private ControllerInvokeListenerList controllerInvokeListeners;

private ControllerFactory controllerFactory;
private ControllerRouteFactory controllerRouteFactory;
private List<MethodParameterExtractor> extractors;

public ControllerApplication() {
Expand Down Expand Up @@ -86,6 +87,21 @@ public ControllerApplication setControllerFactory(ControllerFactory controllerFa
return this;
}

public ControllerRouteFactory getControllerRouteFactory() {
if (controllerRouteFactory == null) {
controllerRouteFactory = new DefaultControllerRouteFactory(this);
}

return controllerRouteFactory;
}

public ControllerApplication setControllerRouteFactory(ControllerRouteFactory controllerRouteFactory) {
this.controllerRouteFactory = controllerRouteFactory;
log.debug("Controller route factory is '{}'", controllerRouteFactory.getClass().getName());

return this;
}

public ControllerApplication addExtractors(MethodParameterExtractor... extractors) {
getExtractors().addAll(Arrays.asList(extractors));

Expand All @@ -101,31 +117,31 @@ public List<MethodParameterExtractor> getExtractors() {
}

public ControllerApplication addControllers(String... packageNames) {
ControllerRegistry controllerRegistry = new ControllerRegistry(this);
ControllerRegistry controllerRegistry = new ControllerRegistry(getControllerRouteFactory());
controllerRegistry.register(packageNames);
controllerRegistry.getRoutes().forEach(this::addRoute);

return this;
}

public ControllerApplication addControllers(Package... packages) {
ControllerRegistry controllerRegistry = new ControllerRegistry(this);
ControllerRegistry controllerRegistry = new ControllerRegistry(getControllerRouteFactory());
controllerRegistry.register(packages);
controllerRegistry.getRoutes().forEach(this::addRoute);

return this;
}

public ControllerApplication addControllers(Class<? extends Controller>... controllerClasses) {
ControllerRegistry controllerRegistry = new ControllerRegistry(this);
ControllerRegistry controllerRegistry = new ControllerRegistry(getControllerRouteFactory());
controllerRegistry.register(controllerClasses);
controllerRegistry.getRoutes().forEach(this::addRoute);

return this;
}

public ControllerApplication addControllers(Controller... controllers) {
ControllerRegistry controllerRegistry = new ControllerRegistry(this);
ControllerRegistry controllerRegistry = new ControllerRegistry(getControllerRouteFactory());
controllerRegistry.register(controllers);
controllerRegistry.getRoutes().forEach(this::addRoute);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.slf4j.LoggerFactory;
import ro.pippo.controller.util.ClassUtils;
import ro.pippo.core.route.Route;
import ro.pippo.core.route.RouteHandler;
import ro.pippo.core.util.LangUtils;
import ro.pippo.core.util.StringUtils;

Expand Down Expand Up @@ -49,11 +48,11 @@ public class ControllerRegistry {
private final Set<Class<? extends Annotation>> httpMethodAnnotationClasses = new HashSet<>(Arrays.asList(
DELETE.class, GET.class, HEAD.class, OPTIONS.class, PATCH.class, POST.class, PUT.class));

private final ControllerApplication application;
private final ControllerRouteFactory controllerRouteFactory;
private final List<Route> routes;

public ControllerRegistry(ControllerApplication application) {
this.application = application;
public ControllerRegistry(ControllerRouteFactory controllerRouteFactory) {
this.controllerRouteFactory = controllerRouteFactory;

routes = new ArrayList<>();
}
Expand Down Expand Up @@ -218,13 +217,8 @@ private List<Route> createControllerRoutes(Map<Method, Class<? extends Annotatio
);
}

// create the route handler
RouteHandler handler = new ControllerHandler(application, method);

// create the route
Route route = new Route(httpMethod, fullPath, handler)
.bind("__controllerClass", controllerClass)
.bind("__controllerMethod", method);
// create controller method route
Route route = controllerRouteFactory.createRoute(httpMethod, fullPath, method);

// add the route to the list of routes
routes.add(route);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2021-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.pippo.controller;

import ro.pippo.core.route.Route;

import java.lang.reflect.Method;

/**
* Creates a {@link Route} for a {@link Controller}'s method.
*
* @author Decebal Suiu
*/
public interface ControllerRouteFactory {

Route createRoute(String requestMethod, String uriPattern, Method controllerMethod);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2021-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ro.pippo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.pippo.core.route.Route;
import ro.pippo.core.route.RouteHandler;

import java.lang.reflect.Method;

/**
* Default {@link ControllerRouteFactory} implementation.
*
* @author Decebal Suiu
*/
public class DefaultControllerRouteFactory implements ControllerRouteFactory {

private static final Logger log = LoggerFactory.getLogger(DefaultControllerRouteFactory.class);

private final ControllerApplication application;

public DefaultControllerRouteFactory(ControllerApplication application) {
this.application = application;
}

@Override
public Route createRoute(String requestMethod, String uriPattern, Method controllerMethod) {
// create the route handler
RouteHandler<?> handler = new ControllerHandler(application, controllerMethod);

// create the route
Route route = new Route(requestMethod, uriPattern, handler)
.bind("__controllerClass", controllerMethod.getDeclaringClass())
.bind("__controllerMethod", controllerMethod);

log.debug("Created route '{}' for '{}'", route, controllerMethod);

return route;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ControllerRegistryTest {

@Before
public void before() {
controllerRegistry = new ControllerRegistry(new ControllerApplication());
controllerRegistry = new ControllerRegistry(new DefaultControllerRouteFactory(new ControllerApplication()));
}

@After
Expand Down

1 comment on commit d0793e2

@mhagnumdw
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@decebals , now what is the best way to register the GuiceControllerFactory?

Before this commit it was like this:

public class PippoApplication extends ControllerApplication {
    @Override
    protected void onInit() {
        setControllerFactory(new GuiceControllerFactory(injector)); 
    }
}

Please sign in to comment.