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
Route Groups #245
Changes from 7 commits
bdeab2c
7987f15
ff2e995
b1d8bb2
ff68cb0
7890b7d
c061937
c657b15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
package ro.pippo.core.route; | ||
|
||
import ro.pippo.core.HttpConstants; | ||
import ro.pippo.core.util.StringUtils; | ||
|
||
/** | ||
* @author Decebal Suiu | ||
|
@@ -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; | ||
|
@@ -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 ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe better is
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
There was a problem hiding this comment.
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
inRouter
interface, callgetRouter().addRouteGroup()
here and move the implementation inDefaultRouter
. The idea is that I want to add the tests in Router.