Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route Groups #245

Merged
merged 8 commits into from Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions pippo-core/src/main/java/ro/pippo/core/Application.java
Expand Up @@ -25,6 +25,7 @@
import ro.pippo.core.route.Route;
import ro.pippo.core.route.RouteContext;
import ro.pippo.core.route.RouteDispatcher;
import ro.pippo.core.route.RouteGroup;
import ro.pippo.core.route.RouteHandler;
import ro.pippo.core.route.RoutePostDispatchListenerList;
import ro.pippo.core.route.RoutePreDispatchListenerList;
Expand All @@ -33,6 +34,7 @@
import ro.pippo.core.util.HttpCacheToolkit;
import ro.pippo.core.util.MimeTypes;
import ro.pippo.core.util.ServiceLocator;
import ro.pippo.core.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -268,6 +270,13 @@ public void addRoute(Route route) {
getRouter().addRoute(route);
}

public void addGroup(RouteGroup routeGroup) {
routeGroup.getRoutes().forEach(this::addRoute);
Copy link
Member

Choose a reason for hiding this comment

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

Add a new method addRouteGroup in Router interface, call getRouter().addRouteGroup() here and move the implementation in DefaultRouter. The idea is that I want to add the tests in Router.

for (RouteGroup child : routeGroup.getChildren()) {
child.getRoutes().forEach(this::addRoute);
}
}

/**
* It's a shortcut for {@link #addPublicResourceRoute(String)} with parameter <code>"/public"</code>.
*/
Expand Down
28 changes: 26 additions & 2 deletions pippo-core/src/main/java/ro/pippo/core/route/Route.java
Expand Up @@ -16,6 +16,7 @@
package ro.pippo.core.route;

import ro.pippo.core.HttpConstants;
import ro.pippo.core.util.StringUtils;

/**
* @author Decebal Suiu
Expand All @@ -28,6 +29,7 @@ public class Route {

private boolean runAsFinally;
private String name;
private RouteGroup group;

public Route(String requestMethod, String uriPattern, RouteHandler routeHandler) {
this.requestMethod = requestMethod;
Expand Down Expand Up @@ -117,13 +119,28 @@ public String getRequestMethod() {
}

public String getUriPattern() {
return uriPattern;
// return uriPattern;
return getCompletePath(); //should i modify this method or modify methods in DefaultRouter to use getCompletePath ?
Copy link
Member

Choose a reason for hiding this comment

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

it's OK

}

public RouteHandler getRouteHandler() {
return routeHandler;
}

// path with group
public String getCompletePath() {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe better is getFullUriPattern.
Other possible idea is:

  • move content of getComplete in getUriPattern
  • remove getComplete
  • create a method getRelativeUriPattern that return uriPattern variable

RouteGroup group = this.group;
String path = this.uriPattern;
while (group != null) {
path = StringUtils.addStart(StringUtils.addStart(path, "/"), group.getUriPattern());
group = group.getParent();
}
if (StringUtils.isNullOrEmpty(path)) {
return path;
}
return path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
Copy link
Member

Choose a reason for hiding this comment

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

I think that we can use StringUtils for this line

}

public boolean isRunAsFinally() {
return runAsFinally;
}
Expand All @@ -150,6 +167,13 @@ public void setName(String name) {
this.name = name;
}


public Route inGroup(RouteGroup group) {
this.group = group;
group.getRoutes().add(this);
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -158,7 +182,7 @@ public boolean equals(Object o) {
Route route = (Route) o;

if (!requestMethod.equals(route.requestMethod)) return false;
if (!uriPattern.equals(route.uriPattern)) return false;
if (!getUriPattern().equals(route.getUriPattern())) return false;

return true;
}
Expand Down
133 changes: 133 additions & 0 deletions pippo-core/src/main/java/ro/pippo/core/route/RouteGroup.java
@@ -0,0 +1,133 @@
/*
* Copyright (C) 2015 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.core.route;

import ro.pippo.core.PippoRuntimeException;

import java.util.ArrayList;
import java.util.List;

/**
* @author ScienJus
* @date 16/2/9.
*/
public class RouteGroup {

private String uriPattern;

private List<Route> routes;

private RouteGroup parent;

private List<RouteGroup> children;

public RouteGroup(String uriPattern) {
this.uriPattern = uriPattern;
this.routes = new ArrayList<>();
this.children = new ArrayList<>();
}

public String getUriPattern() {
return this.uriPattern;
}

public RouteGroup getParent() {
return parent;
}

public List<RouteGroup> getChildren() {
return children;
}

public Route GET(String uriPattern, RouteHandler routeHandler) {
if (routeHandler instanceof ResourceHandler) {
throw new PippoRuntimeException("Please use 'addResourceRoute()'");
}

return Route.GET(uriPattern, routeHandler).inGroup(this);
}

public Route GET(RouteHandler routeHandler) {
return GET("", routeHandler);
}

public Route POST(String uriPattern, RouteHandler routeHandler) {
return Route.POST(uriPattern, routeHandler).inGroup(this);
}

public Route POST(RouteHandler routeHandler) {
return POST("", routeHandler);
}

public Route DELETE(String uriPattern, RouteHandler routeHandler) {
return Route.DELETE(uriPattern, routeHandler).inGroup(this);
}

public Route DELETE(RouteHandler routeHandler) {
return DELETE("", routeHandler);
}

public Route HEAD(String uriPattern, RouteHandler routeHandler) {
return Route.HEAD(uriPattern, routeHandler).inGroup(this);
}

public Route HEAD(RouteHandler routeHandler) {
return HEAD("", routeHandler);
}

public Route PUT(String uriPattern, RouteHandler routeHandler) {
return Route.PUT(uriPattern, routeHandler).inGroup(this);
}

public Route PUT(RouteHandler routeHandler) {
return PUT("", routeHandler);
}

public Route PATCH(String uriPattern, RouteHandler routeHandler) {
return Route.PATCH(uriPattern, routeHandler).inGroup(this);
}

public Route PATCH(RouteHandler routeHandler) {
return PATCH("", routeHandler);
}

public Route ALL(String uriPattern, RouteHandler routeHandler) {
return Route.ALL(uriPattern, routeHandler).inGroup(this);
}
public Route ALL(RouteHandler routeHandler) {
return ALL("", routeHandler);
}

public List<Route> getRoutes() {
return routes;
}

public RouteGroup addRoute(Route route) {
route.inGroup(this);
return this;
}

public RouteGroup addGroup(RouteGroup routeGroup) {
routeGroup.inGroup(this);
return this;
}

public RouteGroup inGroup(RouteGroup routeGroup) {
this.parent = routeGroup;
routeGroup.getChildren().add(this);
return this;
}
}