Route { protected static String convertRawUriToRegex(String rawUri) { Matcher groupMatcher = Pattern.compile("\\(([^?].*)\\)").matcher(rawUri); String converted = groupMatcher.replaceAll("\\(?:$1\\)"); Matcher matcher = PATTERN_FOR_VARIABLE_PARTS_OF_ROUTE.matcher(converted); StringBuffer stringBuffer = new StringBuffer(); while (matcher.find()) { String namedVariablePartOfRoute = matcher.group(3); String namedVariablePartOfORouteReplacedWithRegex; if (namedVariablePartOfRoute != null) { namedVariablePartOfORouteReplacedWithRegex = "(" + Matcher.quoteReplacement(namedVariablePartOfRoute) + ")"; } else { namedVariablePartOfORouteReplacedWithRegex = VARIABLE_ROUTES_DEFAULT_REGEX; } matcher.appendReplacement(stringBuffer, namedVariablePartOfORouteReplacedWithRegex); } matcher.appendTail(stringBuffer); return stringBuffer.toString(); } Route(String httpMethod,
            String uri,
            Method controllerMethod,
            FilterChain filterChain); String getUrl(); String getHttpMethod(); boolean isHttpMethod(String method); boolean isHttpMethodGet(); boolean isHttpMethodPost(); boolean isHttpMethodPut(); boolean isHttpMethodDelete(); boolean isHttpMethodHead(); boolean isHttpMethodOptions(); boolean isHttpMethodWebSocket(); String getUri(); Class<?> getControllerClass(); Method getControllerMethod(); FilterChain getFilterChain(); Map<String,RouteParameter> getParameters(); boolean matches(String httpMethod, String uri); Map<String, String> getPathParametersEncoded(String uri); static final String HTTP_METHOD_GET; static final String HTTP_METHOD_POST; static final String HTTP_METHOD_PUT; static final String HTTP_METHOD_HEAD; static final String HTTP_METHOD_DELETE; static final String HTTP_METHOD_OPTIONS; static final String HTTP_METHOD_WEBSOCKET; }
@Test public void convertRawUriToRegex() { assertThat( Route.convertRawUriToRegex("/me/{username: .*}"), CoreMatchers.equalTo("/me/(.*)")); assertThat( Route.convertRawUriToRegex("/me/{username: [a-zA-Z][a-zA-Z_0-9]}"), CoreMatchers.equalTo("/me/([a-zA-Z][a-zA-Z_0-9])")); assertThat( Route.convertRawUriToRegex("/me/{username}"), CoreMatchers.equalTo("/me/([^/]*)")); assertThat( Route.convertRawUriToRegex("/me/{id: \\d+}"), CoreMatchers.equalTo("/me/(\\d+)")); assertThat( Route.convertRawUriToRegex("(/users)?/{user}"), CoreMatchers.equalTo("(?:/users)?/([^/]*)") ); }