示例#1
0
  private Map<String, String> getParameters(PatternBinding binding, String requestUri) {
    if (binding.getParameterNames().isEmpty()) {
      return Collections.EMPTY_MAP;
    }

    Map<String, String> parameters = new HashMap<>();
    List<String> parameterNames = binding.getParameterNames();
    Matcher matcher = binding.getPattern().matcher(requestUri);
    matcher.matches();
    int groupCount = matcher.groupCount();
    if (groupCount > 0) {
      for (int i = 0; i < parameterNames.size(); i++) {
        parameters.put(parameterNames.get(i), matcher.group(i + 1));
      }
    }

    return parameters;
  }
示例#2
0
  @Override
  public List<RouteMatch> findRoutes(String requestUri, String requestMethod) {
    log.trace("Finding route matches for {} '{}'", requestMethod, requestUri);

    List<RouteMatch> routeMatches = new ArrayList<>();

    List<PatternBinding> bindings = getBindings(requestMethod);
    for (Route route : routes) { // to preserve the routes order
      for (PatternBinding binding : bindings) {
        if (route.equals(binding.getRoute())
            && binding.getPattern().matcher(requestUri).matches()) {
          // TODO improve (it's possible to have the same uriPattern for many routes => same
          // parameters)
          routeMatches.add(new RouteMatch(route, getParameters(binding, requestUri)));
          break;
        }
      }
    }

    log.debug("Found {} route matches for {} '{}'", routeMatches.size(), requestMethod, requestUri);

    return routeMatches;
  }