Skip to content

Commit

Permalink
add runAsFinally flag on Route (work in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
decebals committed Jan 29, 2015
1 parent 338874d commit aa4f836
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
24 changes: 12 additions & 12 deletions pippo-core/src/main/java/ro/pippo/core/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,26 +260,26 @@ public void PATCH(String uriPattern, Class<? extends Controller> controllerClass
addRoute(uriPattern, HttpConstants.Method.PATCH, controllerClass, methodName);
}

public void ALL(String uriPattern, RouteHandler routeHandler) {
addRoute(uriPattern, HttpConstants.Method.ALL, routeHandler);
public Route ALL(String uriPattern, RouteHandler routeHandler) {
return addRoute(uriPattern, HttpConstants.Method.ALL, routeHandler);
}

public void ALL(String uriPattern, Class<? extends Controller> controllerClass, String methodName) {
addRoute(uriPattern, HttpConstants.Method.ALL, controllerClass, methodName);
public Route ALL(String uriPattern, Class<? extends Controller> controllerClass, String methodName) {
return addRoute(uriPattern, HttpConstants.Method.ALL, controllerClass, methodName);
}

public void addRoute(String uriPattern, String requestMethod, Class<? extends Controller> controllerClass, String methodName) {
public Route addRoute(String uriPattern, String requestMethod, Class<? extends Controller> controllerClass, String methodName) {
RouteHandler routeHandler = getControllerHandlerFactory().createHandler(controllerClass, methodName);
addRoute(uriPattern, requestMethod, routeHandler);
Route route = addRoute(uriPattern, requestMethod, routeHandler);

return route;
}

public void addRoute(String uriPattern, String requestMethod, RouteHandler routeHandler) {
public Route addRoute(String uriPattern, String requestMethod, RouteHandler routeHandler) {
Route route = new Route(uriPattern, requestMethod, routeHandler);
try {
getRouter().addRoute(route);
} catch (Exception e) {
log.error("Cannot add route '{}'", route, e);
}
getRouter().addRoute(route);

return route;
}

public ControllerHandlerFactory getControllerHandlerFactory() {
Expand Down
8 changes: 5 additions & 3 deletions pippo-core/src/main/java/ro/pippo/core/PippoFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package ro.pippo.core;

import ro.pippo.core.route.DefaultRouteHandlerChainFactory;
import ro.pippo.core.route.RouteHandlerChain;
import ro.pippo.core.route.RouteHandlerChainFactory;
import ro.pippo.core.route.RouteMatch;
import ro.pippo.core.route.Router;
Expand Down Expand Up @@ -277,7 +278,7 @@ private boolean shouldIgnorePath(HttpServletRequest request) {
}

private void initIgnorePaths(FilterConfig filterConfig) {
ignorePaths = new HashSet<String>();
ignorePaths = new HashSet<>();
String paths = filterConfig.getInitParameter(IGNORE_PATHS_PARAM);
if (paths != null && !paths.isEmpty()) {
String[] parts = paths.split(",");
Expand Down Expand Up @@ -381,6 +382,7 @@ private RequestFactory getRequestFactory() {
if (factory == null) {
factory = new DefaultRequestFactory();
}

return factory;
}

Expand All @@ -389,6 +391,7 @@ private ResponseFactory getResponseFactory() {
if (factory == null) {
factory = new DefaultResponseFactory();
}

return factory;
}

Expand All @@ -397,6 +400,7 @@ private RouteHandlerChainFactory getRouteHandlerChainFactory() {
if (factory == null) {
factory = new DefaultRouteHandlerChainFactory();
}

return factory;
}

Expand Down Expand Up @@ -432,14 +436,12 @@ private final String readPippoVersion() {
String pippoVersion;

try {

Properties prop = new Properties();
URL url = ClasspathUtils.locateOnClasspath(PippoConstants.LOCATION_OF_PIPPO_BUILTIN_PROPERTIES);
InputStream stream = url.openStream();
prop.load(stream);

pippoVersion = prop.getProperty(PIPPO_VERSION_PROPERTY_KEY);

} catch (Exception e) {
//this should not happen. Never.
throw new PippoRuntimeException("Something is wrong with your build. Cannot find resource {}",
Expand Down
13 changes: 13 additions & 0 deletions pippo-core/src/main/java/ro/pippo/core/route/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Route {
private String uriPattern;
private String requestMethod;
private RouteHandler routeHandler;
private boolean runAsFinally;

public Route(String uriPattern, String requestMethod, RouteHandler routeHandler) {
this.uriPattern = uriPattern;
Expand All @@ -42,6 +43,18 @@ public RouteHandler getRouteHandler() {
return routeHandler;
}

public boolean isRunAsFinally() {
return runAsFinally;
}

/**
* Mark this route to be invoked even when exceptions were raised in previous routes.
* This flag make sense only for an after filter.
*/
public void runAsFinally() {
runAsFinally = true;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
4 changes: 2 additions & 2 deletions pippo-core/src/main/java/ro/pippo/core/route/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public interface Router {
*/
public void setContextPath(String contextPath);

public void addRoute(Route route) throws Exception;
public void addRoute(Route route);

public void removeRoute(Route route) throws Exception;
public void removeRoute(Route route);

public List<RouteMatch> findRoutes(String requestUri, String requestMethod);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ public void handle(Request request, Response response, RouteHandlerChain chain)

// send files from a local folder (try a request like 'src/main/java/ro/pippo/demo/basic/BasicApplication.java')
GET(new FileResourceHandler("/src", "src"));

// use a finally filter (invoked even when exceptions were raised in previous routes)
ALL("/.*", new RouteHandler() {

@Override
public void handle(Request request, Response response, RouteHandlerChain chain) {
System.out.println(">>> Cleanup here");
}

}).runAsFinally();
}

}

0 comments on commit aa4f836

Please sign in to comment.