Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
82bce98
make that a method to allow future hooks into route maps
jasonewall Dec 28, 2014
c7804fd
move routemanager to routing package
jasonewall Dec 28, 2014
4144295
Merge branch 'master' into route-references
jasonewall Dec 29, 2014
c5249a3
move route manager test to routing package as well
jasonewall Jan 2, 2015
6a7d474
basic path names working
jasonewall Jan 3, 2015
2e849be
path names work with params and nested routes
jasonewall Jan 3, 2015
fa1b963
make package local for great security
jasonewall Jan 3, 2015
1b7794c
I guess we'll never need these to be public
jasonewall Jan 3, 2015
6480133
optimize imports
jasonewall Jan 3, 2015
b675740
move coercionmap to coercion package since it doesnt extend Map
jasonewall Jan 3, 2015
20d7d4d
Generating paths from replacing params in objects
jasonewall Jan 3, 2015
771acb6
use autoclose block for that class loader
jasonewall Jan 4, 2015
4473d91
register route managers with the servletcontext
jasonewall Jan 4, 2015
cd7521f
start of uri building API
jasonewall Jan 4, 2015
f4b5767
forgot this class a few commits back
jasonewall Jan 4, 2015
3391abd
a little less efficient but cleaner code
jasonewall Jan 5, 2015
d6a3a48
add todo about warning
jasonewall Jan 8, 2015
d9d9755
all the Uri elements
jasonewall Jan 8, 2015
7a6f52f
rename to a factory (this is the beginning of the end)
jasonewall Jan 11, 2015
45c0bf3
implement link tag to reference path names in route manager
jasonewall Jan 11, 2015
806f83c
add to attribute to link tag to fill in route params
jasonewall Jan 11, 2015
ef46851
optimize imports
jasonewall Jan 11, 2015
1d0f4cc
params tags working for non-object setting
jasonewall Jan 23, 2015
cac5dc4
separate viewcontext setup for test
jasonewall Jan 25, 2015
ff99274
start refactoring action base tag
jasonewall Sep 5, 2015
3e333f9
move ViewSupport to root jsp package
jasonewall Sep 5, 2015
425a8b8
move tag classes to root jsp package
jasonewall Sep 5, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example_application/adapters/config/ApplicationRoutes.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config;

import controllers.PeopleController;
import hex.action.RouteManager;
import hex.action.routing.RouteManager;

/**
* Created by jason on 14-11-16.
Expand Down
10 changes: 6 additions & 4 deletions example_application/views/layouts/application.html.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
<h1>Hex Example Application</h1>
<h2>Menu</h2>
<ul>
<c:url var="rootPath" value="/"/>
<li><a href="${rootPath}">Home</a></li>
<c:url var="peoplePath" value="/people"/>
<li><a href="${peoplePath}">List of People</a></li>
<li><hex:link action="home_path">Home</hex:link></li>
<li>
<hex:link action="index_people_path">
List of People
</hex:link>
</li>
</ul>
<hex:view-content/>
<hex:view-content name="postBody"/>
Expand Down
6 changes: 2 additions & 4 deletions example_application/views/people/index.html.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
<tr>
<td>${p.firstName}</td>
<td>${p.lastName}</td>
<c:url value="/people/${p.id}" var="showPersonPath"/>
<td><a href="${showPersonPath}">View</a></td>
<td><hex:link to="${p}" action="show_people_path">View</hex:link></td>
</tr>
</c:forEach>
</tbody>
</table>

<c:url var="newPersonPath" value="/people/new"/>
<a href="${newPersonPath}">Create New Person</a>
<hex:link action="newForm_people_new_path">Create New Person</hex:link>
52 changes: 49 additions & 3 deletions hex_action/resources/META-INF/tld/hex.tld
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<uri>http://hex.org/tags</uri>
<tag>
<name>view-content</name>
<tag-class>hex.action.views.jsp.tags.ViewContentTag</tag-class>
<tag-class>hex.jsp.tags.ViewContentTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
Expand All @@ -16,7 +16,7 @@
</tag>
<tag>
<name>content-for</name>
<tag-class>hex.action.views.jsp.tags.ContentForTag</tag-class>
<tag-class>hex.jsp.tags.ContentForTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>name</name>
Expand All @@ -25,7 +25,53 @@
</tag>
<tag>
<name>view-init</name>
<tag-class>hex.action.views.jsp.tags.ViewInitTag</tag-class>
<tag-class>hex.jsp.tags.ViewInitTag</tag-class>
<body-content>empty</body-content>
</tag>
<tag>
<name>link</name>
<tag-class>hex.jsp.tags.LinkTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>action</name>
<required>true</required>
</attribute>
<attribute>
<name>anchor</name>
<required>false</required>
</attribute>
<attribute>
<name>to</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>query-param</name>
<tag-class>hex.jsp.tags.QueryParamTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>param</name>
<tag-class>hex.jsp.tags.ParamTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
10 changes: 8 additions & 2 deletions hex_action/src/hex/action/Application.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hex.action;

import hex.action.routing.RouteManager;
import hex.action.initialization.InitializationException;
import hex.action.initialization.InitializerRunner;
import hex.routing.Routing;
Expand All @@ -25,6 +26,7 @@ public class Application implements ServletContextListener {
public static final String ACTION_CONFIG = "hex_action.properties";
public static final String ROUTES = "routes";
public static final String ROOT = "hex.action.Application.ROOT";
public static final String ROUTING_CONFIG_CLASSES = "hex.action.Application.ROUTING_CONFIG_CLASSES";

@Override
public void contextInitialized(ServletContextEvent sce) {
Expand All @@ -38,7 +40,7 @@ public void contextInitialized(ServletContextEvent sce) {
private void initializeApplication(ServletContext application) throws InitializationException {
ClassLoader cl = getClass().getClassLoader();
try {
RoutingConfig routingConfig = initializeRoutes(cl);
RoutingConfig routingConfig = initializeRoutes(cl, application);
application.setAttribute(Routing.CONFIG, routingConfig);

InitializerRunner initializer = new InitializerRunner();
Expand All @@ -64,19 +66,23 @@ public void contextDestroyed(ServletContextEvent sce) {

}

public static RoutingConfig initializeRoutes(ClassLoader classLoader) throws ServletException {
public static RoutingConfig initializeRoutes(ClassLoader classLoader, ServletContext application) throws ServletException {
try {
Properties adapterProperties = new Properties();
adapterProperties.load(classLoader.getResourceAsStream(ACTION_CONFIG));

String[] routingConfigClasses = Stream.of(adapterProperties.getProperty(ROUTES).split(","))
.map(String::trim).toArray(String[]::new);

application.setAttribute(ROUTING_CONFIG_CLASSES, routingConfigClasses);

RoutingConfigBase routingConfig = new RoutingConfigBase();
for (String className : routingConfigClasses) {
RouteManager routeManager = (RouteManager) Class.forName(className, false, classLoader).newInstance();
routeManager.setHexActionProperties(adapterProperties);
routeManager.defineRoutes();
application.setAttribute(className, routeManager);
//TODO: This is smelly. Need to decide if supporting multiple RouteManagers in the same routing config is a good idea or not.
routeManager.getDefinedRoutes().forEach(routingConfig::addRoute);
}
return routingConfig;
Expand Down
4 changes: 4 additions & 0 deletions hex_action/src/hex/action/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class Controller {
* @return Name of the directory in which to find the view template.
*/
protected String templateDirectory() {
return getName();
}

String getName() {
Matcher m = CONTROLLER_NAMES.matcher(getClass().getSimpleName());
if(!m.matches()) throw new IllegalArgumentException(String.format(Errors.UNABLE_TO_IMPLY_VIEW_DIRECTORY, getClass().getSimpleName()));
return underscore(m.replaceAll(m.group(1)));
Expand Down
4 changes: 4 additions & 0 deletions hex_action/src/hex/action/ControllerAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public void prepareController(Controller controller, ServletRequest servletReque
}
}

public String getName() {
return actionName;
}

private void checkException(ServletRequest request) throws ServletException {
InitializationException e = (InitializationException)
request.getServletContext().getAttribute(InitializationException.class.getName());
Expand Down
63 changes: 0 additions & 63 deletions hex_action/src/hex/action/RouteManager.java

This file was deleted.

4 changes: 2 additions & 2 deletions hex_action/src/hex/action/ViewContext.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package hex.action;

import hex.utils.maps.CoercionMap;
import hex.utils.maps.PropertyMap;

import java.util.HashMap;
import java.util.Map;

/**
* Created by jason on 14-11-15.
*/
public class ViewContext extends HashMap<String,Object> implements CoercionMap<String> {
public class ViewContext extends HashMap<String,Object> implements PropertyMap {
private static final String BLANK = "";

private String content;
Expand Down
9 changes: 9 additions & 0 deletions hex_action/src/hex/action/routing/Errors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package hex.action.routing;

/**
* Created by jason on 15-01-04.
*/
public class Errors {
static final String ROUTE_NAME_NOT_FOUND =
"Route named (%s) not found in application routes.";
}
91 changes: 91 additions & 0 deletions hex_action/src/hex/action/routing/RouteManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package hex.action.routing;

import hex.action.Controller;
import hex.action.ControllerAction;
import hex.routing.HttpMethod;
import hex.routing.Route;

import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
* Created by jason on 14-11-14.
*/
public class RouteManager {
private final Map<String,Route> routeMap = new HashMap<>();

private final List<Route> definedRoutes = new ArrayList<>();

private Properties hexActionProperties;

public Properties getHexActionConfig() {
return hexActionProperties;
}

public void setHexActionProperties(Properties hexActionProperties) {
this.hexActionProperties = hexActionProperties;
}

public List<Route> getDefinedRoutes() {
return definedRoutes;
}

public RouteManager() {
}

public void defineRoutes() {

}

protected void get(String path, Supplier<Controller> controllerSupplier, String action) {
addRoute(HttpMethod.GET, path, getHandler(controllerSupplier, action));
}

protected void post(String path, Supplier<Controller> controllerSupplier, String action) {
addRoute(HttpMethod.POST, path, getHandler(controllerSupplier, action));
}

protected void put(String path, Supplier<Controller> controllerSupplier, String action) {
addRoute(HttpMethod.PUT, path, getHandler(controllerSupplier, action));
}

protected void delete(String path, Supplier<Controller> controllerSupplier, String action) {
addRoute(HttpMethod.DELETE, path, getHandler(controllerSupplier, action));
}

protected void matches(String path, Supplier<Controller> controllerSupplier, String action) {
addRoute(HttpMethod.ANY, path, getHandler(controllerSupplier, action));
}

private void addRoute(Predicate<HttpMethod> any, String path, ControllerAction handler) {
Route route = new Route(any, path, handler);
definedRoutes.add(route);
registerRouteName(handler.getName(), path, route);
}

private void addRoute(HttpMethod method, String path, ControllerAction handler) {
Route route = new Route(method, path, handler);
definedRoutes.add(route);
registerRouteName(handler.getName(), path, route);
}

private void registerRouteName(String actionName, String path, Route route) {
String pathName = Route.PATH_PARAM_PATTERN.matcher(path).replaceAll("")
.replaceFirst("/", "")
.replaceAll("/", "_");
String routeName = actionName;
if(pathName.length() > 0) routeName += "_" + pathName;
routeMap.put(routeName, route);
}

Route getRouteNamed(String routeName) {
return routeMap.get(routeName);
}

private ControllerAction getHandler(Supplier<Controller> controllerSupplier, String action) {
ControllerAction controllerAction = new ControllerAction(controllerSupplier, action);
controllerAction.setHexActionConfig(getHexActionConfig());
return controllerAction;
}
}
Loading