示例#1
0
文件: Router.java 项目: unbounce/yoke
  private void addPattern(
      String verb, String input, Middleware[] handler, List<PatternBinding> bindings) {
    // We need to search for any :<token name> tokens in the String and replace them with named
    // capture groups
    Matcher m = Pattern.compile(":([A-Za-z][A-Za-z0-9_]*)").matcher(input);
    StringBuffer sb = new StringBuffer();
    Set<String> groups = new HashSet<>();
    while (m.find()) {
      String group = m.group().substring(1);
      if (groups.contains(group)) {
        throw new IllegalArgumentException(
            "Cannot use identifier " + group + " more than once in pattern string");
      }
      m.appendReplacement(sb, "(?<$1>[^\\/]+)");
      groups.add(group);
    }
    m.appendTail(sb);
    // ignore tailing slash if not part of the input, not really REST but common on other frameworks
    if (sb.charAt(sb.length() - 1) != '/') {
      sb.append("\\/?$");
    }

    Pattern regex = Pattern.compile(sb.toString());
    boolean exists = false;
    // verify if the binding already exists, if yes add to it
    for (PatternBinding pb : bindings) {
      if (pb.isFor(input)) {
        exists = true;
        pb.addMiddleware(handler);
        break;
      }
    }

    if (!exists) {
      PatternBinding binding = new PatternBinding(hashCode(), verb, input, regex, groups, handler);
      bindings.add(binding);
    }

    // also pass the vertx object to the routes
    for (Middleware h : handler) {
      if (!h.isInitialized() && isInitialized()) {
        h.init(yoke, mount);
      }
    }
  }
示例#2
0
文件: Router.java 项目: unbounce/yoke
  private void addRegEx(
      String verb, Pattern regex, Middleware handler[], List<PatternBinding> bindings) {
    boolean exists = false;
    // verify if the binding already exists, if yes add to it
    for (PatternBinding pb : bindings) {
      if (pb.isFor(regex)) {
        pb.addMiddleware(handler);
        exists = true;
        break;
      }
    }

    if (!exists) {
      PatternBinding binding = new PatternBinding(hashCode(), verb, null, regex, null, handler);
      bindings.add(binding);
    }

    // also pass the vertx object to the routes
    for (Middleware h : handler) {
      if (!h.isInitialized() && isInitialized()) {
        h.init(yoke, mount);
      }
    }
  }