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

Allow set default convention endpoints behavior #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions src/main/java/org/tynamo/routing/Behavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.tynamo.routing;

public enum Behavior {
DEFAULT, REDIRECT, NOT_FOUND
}
15 changes: 10 additions & 5 deletions src/main/java/org/tynamo/routing/Route.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tynamo.routing;

import org.apache.tapestry5.services.LocalizationSetter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -12,13 +14,14 @@ public class Route {
private static final String URI_PARAM_REGEX = "\\{\\s*(" + URI_PARAM_NAME_REGEX + ")\\s*(:\\s*(" + URI_PARAM_REGEX_REGEX + "))?\\}";
private static final Pattern URI_PARAM_PATTERN = Pattern.compile(URI_PARAM_REGEX);

private final String canonicalizedPageName;
private final String pathExpression;
private final String canonicalizedPageName;
private final Behavior behavior;
private final Pattern pattern;

public Route(final String pathExpression, final String canonicalizedPageName) {

public Route(final String pathExpression, final String canonicalizedPageName, final Behavior behavior) {
this.canonicalizedPageName = canonicalizedPageName;
this.behavior = behavior;

// remove ending slash unless it's the root path
this.pathExpression = pathExpression.length() > 1 && pathExpression.charAt(pathExpression.length() - 1) == SLASH ? pathExpression.substring(0, pathExpression.length() - 1) : pathExpression;
Expand All @@ -31,11 +34,9 @@ public Route(final String pathExpression, final String canonicalizedPageName) {

String regex = buildExpression(this.pathExpression);
pattern = Pattern.compile(regex);

}

static String buildExpression(String expression) {

String[] split = URI_PARAM_PATTERN.split(expression);
Matcher withPathParam = URI_PARAM_PATTERN.matcher(expression);
int i = 0;
Expand Down Expand Up @@ -66,6 +67,10 @@ public String getCanonicalizedPageName() {
return canonicalizedPageName;
}

public Behavior getBehavior() {
return behavior;
}

public Pattern getPattern() {
return pattern;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/tynamo/routing/annotations/At.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.tynamo.routing.Behavior;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface At {

String value();

Behavior behavior() default Behavior.DEFAULT;

String[] order() default {};

}
3 changes: 3 additions & 0 deletions src/main/java/org/tynamo/routing/annotations/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.tynamo.routing.Behavior;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Route {

String value();

Behavior behavior() default Behavior.DEFAULT;

String[] order() default {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public RouteDecoderImpl(LocalizationSetter localizationSetter,
ContextValueEncoder valueEncoder,
Logger logger) {


this.localizationSetter = localizationSetter;
this.encodeLocaleIntoPath = encodeLocaleIntoPath;

Expand All @@ -56,7 +55,6 @@ private String getLocaleFromPath(String path) {
}

private String removeAppFolderAndLocaleFromPath(final Request request) {

String path = request.getPath();

if (this.applicationFolder.length() > 0) {
Expand All @@ -74,7 +72,6 @@ private String removeAppFolderAndLocaleFromPath(final Request request) {

@Override
public PageRenderRequestParameters decodePageRenderRequest(final Route route, final Request request) {

Matcher matcher = route.getPattern().matcher(removeAppFolderAndLocaleFromPath(request));
if (!matcher.matches()) return null;

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/tynamo/routing/services/RouteFactory.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.tynamo.routing.services;

import org.tynamo.routing.Route;
import org.tynamo.routing.Behavior;

public interface RouteFactory {

Route create(String pathExpression, String canonicalized);

Route create(String pathExpression, String canonicalized, Behavior behavior);

@Deprecated
Route create(String pathExpression, Class page);

@Deprecated
Route create(String pathExpression, Class page, Behavior behavior);

}
13 changes: 12 additions & 1 deletion src/main/java/org/tynamo/routing/services/RouteFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.tapestry5.services.ComponentClassResolver;
import org.tynamo.routing.Route;
import org.tynamo.routing.Behavior;

public class RouteFactoryImpl implements RouteFactory {

Expand All @@ -13,11 +14,21 @@ public RouteFactoryImpl(ComponentClassResolver componentClassResolver) {

@Override
public Route create(String pathExpression, String canonicalized) {
return new Route(pathExpression, canonicalized);
return create(pathExpression, canonicalized, Behavior.DEFAULT);
}

@Override
public Route create(String pathExpression, String canonicalized, Behavior behavior) {
return new Route(pathExpression, canonicalized, behavior);
}

@Override
public Route create(String pathExpression, Class page) {
return create(pathExpression, page, Behavior.DEFAULT);
}

@Override
public Route create(String pathExpression, Class page, Behavior behavior) {
String pageName = componentClassResolver.resolvePageClassNameToPageName(page.getName());
String canonicalized = componentClassResolver.canonicalizePageName(pageName);
return create(pathExpression, canonicalized);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/tynamo/routing/services/RouteWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.tapestry5.services.transform.TransformationSupport;
import org.tynamo.routing.annotations.At;
import org.tynamo.routing.annotations.Route;
import org.tynamo.routing.Behavior;

public class RouteWorker implements ComponentClassTransformWorker2 {

Expand All @@ -25,22 +26,25 @@ public void transform(PlasticClass plasticClass, TransformationSupport support,

String pathExpression = null;
String[] order = {};
Behavior behavior = Behavior.DEFAULT;

if (plasticClass.hasAnnotation(At.class)) {
At ann = plasticClass.getAnnotation(At.class);
pathExpression = ann.value();
behavior = ann.behavior();
order = ann.order();
} else if (plasticClass.hasAnnotation(Route.class)) {
Route ann = plasticClass.getAnnotation(Route.class);
pathExpression = ann.value();
behavior = ann.behavior();
order = ann.order();
}

if (pathExpression != null) {
String pageName = componentClassResolver.resolvePageClassNameToPageName(plasticClass.getClassName());
String canonicalized = componentClassResolver.canonicalizePageName(pageName);

org.tynamo.routing.Route route = routeFactory.create(pathExpression, canonicalized);
org.tynamo.routing.Route route = routeFactory.create(pathExpression, canonicalized, behavior);

annotatedPagesManager.add(route, order);
}
Expand Down
50 changes: 43 additions & 7 deletions src/main/java/org/tynamo/routing/services/RouterDispatcher.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.tynamo.routing.services;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Primary;
import org.apache.tapestry5.services.*;
import org.apache.tapestry5.services.ComponentEventLinkEncoder;
import org.apache.tapestry5.services.ComponentRequestHandler;
import org.apache.tapestry5.services.Dispatcher;
import org.apache.tapestry5.services.PageRenderRequestParameters;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Response;
import org.tynamo.routing.Route;

import java.io.IOException;

Expand All @@ -12,20 +17,51 @@
public class RouterDispatcher implements Dispatcher {

private final ComponentRequestHandler componentRequestHandler;
private final ComponentEventLinkEncoder linkEncoder;
private final RouteSource routeSource;

public RouterDispatcher(ComponentRequestHandler componentRequestHandler, RouteSource routeSource) {
public RouterDispatcher(final ComponentRequestHandler componentRequestHandler, final ComponentEventLinkEncoder linkEncoder, final RouteSource routeSource) {
this.componentRequestHandler = componentRequestHandler;
this.linkEncoder = linkEncoder;
this.routeSource = routeSource;
}

@Log
public boolean dispatch(Request request, final Response response) throws IOException {
PageRenderRequestParameters parameters = routeSource.decodePageRenderRequest(request);
if (parameters != null) {
componentRequestHandler.handlePageRender(parameters);
public boolean dispatch(final Request request, final Response response) throws IOException {
PageRenderRequestParameters pageParameters = linkEncoder.decodePageRenderRequest(request);
PageRenderRequestParameters routeParameters = routeSource.decodePageRenderRequest(request);
Route pageParametersRoute = pageParameters == null ? null : routeSource.getRoute(pageParameters.getLogicalPageName());
Route routeParametersRoute = routeParameters == null ? null : routeSource.getRoute(routeParameters.getLogicalPageName());

if (pageParameters != null && pageParametersRoute == null) {
return false;
}
if (pageParameters != null && pageParametersRoute != null && !pageParameters.getLogicalPageName().endsWith("Index")) {
return handleRoute(pageParametersRoute, pageParameters, request, response);
}
if (routeParametersRoute != null) {
componentRequestHandler.handlePageRender(routeParameters);
return true;
}
if (pageParametersRoute != null) {
boolean handled = handleRoute(pageParametersRoute, pageParameters, request, response);
if (handled) return true;
}

return false;
}

private boolean handleRoute(Route route, PageRenderRequestParameters parameters, Request request, Response response) throws IOException {
switch (route.getBehavior()) {
case REDIRECT:
response.sendRedirect(linkEncoder.createPageRenderLink(parameters).toAbsoluteURI());
return true;
case NOT_FOUND:
response.sendError(404, "Not Found");
return true;
case DEFAULT:
default:
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import org.apache.tapestry5.internal.services.LinkImpl;
import org.apache.tapestry5.internal.services.RequestSecurityManager;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.services.*;
import org.apache.tapestry5.services.BaseURLSource;
import org.apache.tapestry5.services.ContextPathEncoder;
import org.apache.tapestry5.services.PageRenderRequestParameters;
import org.apache.tapestry5.services.PersistentLocale;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Response;
import org.apache.tapestry5.services.linktransform.PageRenderLinkTransformer;
import org.tynamo.routing.Route;

Expand All @@ -17,24 +22,25 @@
public class RouterLinkTransformer implements PageRenderLinkTransformer {

private static final char SLASH = '/';
private static final int BUFFER_SIZE = 100;

private final RouteSource routeSource;
private final Request request;
private final RequestSecurityManager requestSecurityManager;
private final Response response;
private final ContextPathEncoder contextPathEncoder;
private final BaseURLSource baseURLSource;
private final PersistentLocale persistentLocale;
private final String contextPath;
private final boolean encodeLocaleIntoPath;
private final String applicationFolder;

private static final int BUFFER_SIZE = 100;
private PersistentLocale persistentLocale;
private boolean encodeLocaleIntoPath;

public RouterLinkTransformer(RouteSource routeSource,
Request request,
RequestSecurityManager requestSecurityManager, Response response,
ContextPathEncoder contextPathEncoder, BaseURLSource baseURLSource,
PersistentLocale persistentLocale,
@Symbol(SymbolConstants.CONTEXT_PATH) final String contextPath,
@Symbol(SymbolConstants.ENCODE_LOCALE_INTO_PATH) boolean encodeLocaleIntoPath,
@Symbol(SymbolConstants.APPLICATION_FOLDER) final String applicationFolder) {
this.routeSource = routeSource;
Expand All @@ -44,6 +50,7 @@ public RouterLinkTransformer(RouteSource routeSource,
this.contextPathEncoder = contextPathEncoder;
this.baseURLSource = baseURLSource;
this.persistentLocale = persistentLocale;
this.contextPath = contextPath;
this.encodeLocaleIntoPath = encodeLocaleIntoPath;
this.applicationFolder = applicationFolder;
}
Expand All @@ -54,17 +61,16 @@ public PageRenderRequestParameters decodePageRenderRequest(Request request) {
}

public Link transformPageRenderLink(Link defaultLink, PageRenderRequestParameters parameters) {

String activePageName = parameters.getLogicalPageName();

Route route = routeSource.getRoute(activePageName);

if (route != null) {
StringBuilder builder = new StringBuilder(BUFFER_SIZE);

if (!"".equals(request.getContextPath())) {
if (!"".equals(contextPath)) {
// Build up the absolute URI.
builder.append(request.getContextPath());
builder.append(contextPath);
}

encodeAppFolderAndLocale(builder);
Expand All @@ -86,8 +92,7 @@ public Link transformPageRenderLink(Link defaultLink, PageRenderRequestParameter
return null;
}

private void encodeAppFolderAndLocale(StringBuilder builder)
{
private void encodeAppFolderAndLocale(StringBuilder builder) {
if (!applicationFolder.equals(""))
{
builder.append(SLASH).append(applicationFolder);
Expand All @@ -106,7 +111,6 @@ private void encodeAppFolderAndLocale(StringBuilder builder)
}

private String[] encode(EventContext context) {

assert context != null;
int count = context.getCount();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void provideURLRewriting(OrderedConfiguration<PageRenderLinkTransf

public static void contributeMasterDispatcher(OrderedConfiguration<Dispatcher> configuration,
@Autobuild RouterDispatcher dispatcher) {
configuration.add(RouterDispatcher.class.getSimpleName(), dispatcher, "after:PageRender");
configuration.add(RouterDispatcher.class.getSimpleName(), dispatcher, "after:ComponentEvent", "before:PageRender");
}

@Contribute(SymbolProvider.class)
Expand Down
Loading