static Pattern argsPattern = new Pattern("\\{<([^>]+)>([a-zA-Z_0-9]+)\\}");
static Pattern paramPattern = new Pattern("([a-zA-Z_0-9]+):'(.*)'");
public void compute() {
this.host = "";
this.hostPattern = new Pattern(".*");
if (action.startsWith("staticDir:") || action.startsWith("staticFile:")) {
// Is there is a host argument, append it.
if (!path.startsWith("/")) {
String p = this.path;
this.path = p.substring(p.indexOf("/"));
this.host = p.substring(0, p.indexOf("/"));
if (this.host.contains("{")) {
Logger.warn("Static route cannot have a dynamic host name");
return;
}
this.hostPattern = new Pattern(host.replaceAll("\\.", "\\\\."));
}
if (!method.equalsIgnoreCase("*") && !method.equalsIgnoreCase("GET")) {
Logger.warn("Static route only support GET method");
return;
}
}
// staticDir
if (action.startsWith("staticDir:")) {
if (!this.path.endsWith("/") && !this.path.equals("/")) {
Logger.warn("The path for a staticDir route must end with / (%s)", this);
this.path += "/";
}
this.pattern = new Pattern("^" + path + "({resource}.*)$");
this.staticDir = action.substring("staticDir:".length());
} else if (action.startsWith("staticFile:")) {
this.pattern = new Pattern("^" + path + "$");
this.staticFile = true;
this.staticDir = action.substring("staticFile:".length());
} else {
// URL pattern
// Is there is a host argument, append it.
if (!path.startsWith("/")) {
String p = this.path;
this.path = p.substring(p.indexOf("/"));
this.host = p.substring(0, p.indexOf("/"));
String pattern = host.replaceAll("\\.", "\\\\.").replaceAll("\\{.*\\}", "(.*)");
if (Logger.isTraceEnabled()) {
Logger.trace("pattern [" + pattern + "]");
Logger.trace("host [" + host + "]");
}
Matcher m = new Pattern(pattern).matcher(host);
this.hostPattern = new Pattern(pattern);
if (m.matches()) {
if (this.host.contains("{")) {
String name = m.group(1).replace("{", "").replace("}", "");
if (!name.equals("_")) {
hostArg = new Arg();
hostArg.name = name;
if (Logger.isTraceEnabled()) {
Logger.trace("hostArg name [" + name + "]");
}
// The default value contains the route version of the host ie {client}.bla.com
// It is temporary and it indicates it is an url route.
// TODO Check that default value is actually used for other cases.
hostArg.defaultValue = host;
hostArg.constraint = new Pattern(".*");
if (Logger.isTraceEnabled()) {
Logger.trace("adding hostArg [" + hostArg + "]");
}
args.add(hostArg);
}
}
}
}
String patternString = path;
patternString = customRegexPattern.replacer("\\{<[^/]+>$1\\}").replace(patternString);
Matcher matcher = argsPattern.matcher(patternString);
while (matcher.find()) {
Arg arg = new Arg();
arg.name = matcher.group(2);
arg.constraint = new Pattern(matcher.group(1));
args.add(arg);
}
patternString = argsPattern.replacer("({$2}$1)").replace(patternString);
this.pattern = new Pattern(patternString);
// Action pattern
patternString = action;
patternString = patternString.replace(".", "[.]");
for (Arg arg : args) {
if (patternString.contains("{" + arg.name + "}")) {
patternString = patternString.replace("{" + arg.name + "}", "({" + arg.name + "}" + arg.constraint.toString() + ")");
actionArgs.add(arg.name);
}
}
actionPattern = new Pattern(patternString, REFlags.IGNORE_CASE);
}
}