コード例 #1
0
ファイル: Router.java プロジェクト: unbounce/yoke
  @Override
  public void handle(@NotNull final YokeRequest request, @NotNull final Handler<Object> next) {

    switch (request.method()) {
      case "GET":
        route(request, next, getBindings);
        break;
      case "PUT":
        route(request, next, putBindings);
        break;
      case "POST":
        route(request, next, postBindings);
        break;
      case "DELETE":
        route(request, next, deleteBindings);
        break;
      case "OPTIONS":
        route(request, next, optionsBindings);
        break;
      case "HEAD":
        route(request, next, headBindings);
        break;
      case "TRACE":
        route(request, next, traceBindings);
        break;
      case "PATCH":
        route(request, next, patchBindings);
        break;
      case "CONNECT":
        route(request, next, connectBindings);
        break;
    }
  }
コード例 #2
0
ファイル: Router.java プロジェクト: unbounce/yoke
  private void route(
      final YokeRequest request, final PatternBinding binding, final Handler<Object> next) {
    final Matcher m = binding.pattern.matcher(request.path());
    final Vertx vertx = vertx();

    if (m.matches()) {
      final MultiMap params = request.params();

      if (binding.paramNames != null) {
        // Named params
        new AsyncIterator<String>(binding.paramNames) {
          @Override
          public void handle(String param) {
            if (hasNext()) {
              params.set(param, m.group(param));
              final Middleware paramMiddleware = paramProcessors.get(param);
              if (paramMiddleware != null) {
                // do not block main loop
                vertx.runOnContext(
                    new Handler<Void>() {
                      @Override
                      public void handle(Void event) {
                        paramMiddleware.handle(
                            request,
                            new Handler<Object>() {
                              @Override
                              public void handle(Object err) {
                                if (err == null) {
                                  next();
                                } else {
                                  next.handle(err);
                                }
                              }
                            });
                      }
                    });
              } else {
                next();
              }
            } else {
              // middlewares
              new AsyncIterator<Middleware>(binding.middleware) {
                @Override
                public void handle(final Middleware middleware) {
                  if (hasNext()) {
                    // do not block main loop
                    vertx.runOnContext(
                        new Handler<Void>() {
                          @Override
                          public void handle(Void event) {
                            middleware.handle(
                                request,
                                new Handler<Object>() {
                                  @Override
                                  public void handle(Object err) {
                                    if (err == null) {
                                      next();
                                    } else {
                                      next.handle(err);
                                    }
                                  }
                                });
                          }
                        });
                  } else {
                    next.handle(null);
                  }
                }
              };
            }
          }
        };
      } else {
        // Un-named params
        for (int i = 0; i < m.groupCount(); i++) {
          params.set("param" + i, m.group(i + 1));
        }

        // middlewares
        new AsyncIterator<Middleware>(binding.middleware) {
          @Override
          public void handle(final Middleware middleware) {
            if (hasNext()) {
              // do not block main loop
              vertx.runOnContext(
                  new Handler<Void>() {
                    @Override
                    public void handle(Void event) {
                      middleware.handle(
                          request,
                          new Handler<Object>() {
                            @Override
                            public void handle(Object err) {
                              if (err == null) {
                                next();
                              } else {
                                next.handle(err);
                              }
                            }
                          });
                    }
                  });
            } else {
              next.handle(null);
            }
          }
        };
      }
    } else {
      next.handle(null);
    }
  }