@Override public void init(Router router) { router.routeWithRegex(PATTERN_ANY).handler(CookieHandler.create()); router.routeWithRegex(PATTERN_ANY).handler(createBodyHandler()); router.routeWithRegex(PATTERN_ANY).failureHandler(this::onFailure).handler(this::onRequest); }
@Override public void init(Router router) { prefix = DynamicPropertyFactory.getInstance().getStringProperty(KEY_PREFIX, "api").get(); withVersion = DynamicPropertyFactory.getInstance().getBooleanProperty(KEY_WITH_VERSION, true).get(); prefixSegmentCount = DynamicPropertyFactory.getInstance().getIntProperty(KEY_PREFIX_SEGMENT_COUNT, 1).get(); String regex; if (withVersion) { regex = "/" + prefix + "/([^\\\\/]+)/([^\\\\/]+)/(.*)"; } else { regex = "/" + prefix + "/([^\\\\/]+)/(.*)"; } router.routeWithRegex(regex).handler(CookieHandler.create()); router.routeWithRegex(regex).handler(createBodyHandler()); router.routeWithRegex(regex).failureHandler(this::onFailure).handler(this::onRequest); }
@Override public void start(Future<Void> startFuture) { Router router = Router.router(vertx); router.route().handler(BodyHandler.create()); router.get("/*").handler(this::log); // 全局日志处理,会执行 next() 到下一个 router.get("/").handler(this::handleRoot); // 首页 router.get("/favicon.ico").handler(c -> c.fail(404)); // 针对浏览器返回404 router.get("/log").handler(this::showLog); // 显示日志 router.routeWithRegex("/([a-z0-9/]*)\\.?(txt|json|png|svg|)") .handler(this::handleGushici); // 核心API调用 router.route().last().handler(c -> c.fail(404)) // 其他返回404 .failureHandler(this::returnError); // 对上面所有的错误进行处理 vertx .createHttpServer() .requestHandler(router::accept) .listen( config().getInteger("http.port", 8080), result -> { if (result.succeeded()) { startFuture.complete(); } else { startFuture.fail(result.cause()); } } ); }
@Test public void testRegex3() throws Exception { router.routeWithRegex(".*foo.txt").handler(rc -> rc.response().setStatusMessage("ok").end()); testPattern("/dog/cat/foo.txt", "ok"); testRequest(HttpMethod.POST, "/dog/cat/foo.bar", 404, "Not Found"); }
@Test public void testRegexInSubRouter() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/api", subRouter); subRouter.routeWithRegex("\\/test").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/api/test", 200, "sausages"); }
@Test public void testRegex2() throws Exception { router.routeWithRegex("\\/([^\\/]+)\\/([^\\/]+)/blah").handler(rc -> { MultiMap params = rc.request().params(); rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end(); }); testPattern("/dog/cat/blah", "dogcat"); }
@Test public void testSubRouteRegex() throws Exception { Router subRouter = Router.router(vertx); router.routeWithRegex("/foo/.*").handler(subRouter::handleContext).failureHandler(subRouter::handleFailure); subRouter.route("/blah").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/foo/blah", 500, "Internal Server Error"); }
@Test public void testRegexWithNamedParams() throws Exception { router.routeWithRegex(HttpMethod.GET, "\\/(?<name>[^\\/]+)\\/(?<surname>[^\\/]+)").handler(rc -> { MultiMap params = rc.request().params(); rc.response().setStatusMessage(params.get("name") + params.get("surname")).end(); }); testPattern("/joe/doe", "joedoe"); }
@Test public void testRegexWithNamedParamsKeepsIndexedParams() throws Exception { router.routeWithRegex(HttpMethod.GET, "\\/(?<name>[^\\/]+)\\/(?<surname>[^\\/]+)").handler(rc -> { MultiMap params = rc.request().params(); rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end(); }); testPattern("/joe/doe", "joedoe"); }
@Test public void testRegex1() throws Exception { router.routeWithRegex("\\/([^\\/]+)\\/([^\\/]+)").handler(rc -> { MultiMap params = rc.request().params(); rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end(); }); testPattern("/dog/cat", "dogcat"); }
WebSocketTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options, Handler<SockJSSocket> sockHandler) { super(vertx, sessions, options); String wsRE = COMMON_PATH_ELEMENT_RE + "websocket"; router.getWithRegex(wsRE).handler(rc -> { HttpServerRequest req = rc.request(); String connectionHeader = req.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION); if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) { rc.response().setStatusCode(400); rc.response().end("Can \"Upgrade\" only to \"WebSocket\"."); } else { ServerWebSocket ws = rc.request().upgrade(); if (log.isTraceEnabled()) log.trace("WS, handler"); SockJSSession session = new SockJSSession(vertx, sessions, rc, options.getHeartbeatInterval(), sockHandler); session.register(req, new WebSocketListener(ws, session)); } }); router.getWithRegex(wsRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("WS, get: " + rc.request().uri()); rc.response().setStatusCode(400); rc.response().end("Can \"Upgrade\" only to \"WebSocket\"."); }); router.routeWithRegex(wsRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("WS, all: " + rc.request().uri()); rc.response().putHeader("Allow", "GET").setStatusCode(405).end(); }); }
@Test public void testRegex1WithMethod() throws Exception { router.routeWithRegex(HttpMethod.GET, "\\/([^\\/]+)\\/([^\\/]+)").handler(rc -> { MultiMap params = rc.request().params(); rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end(); }); testPattern("/dog/cat", "dogcat"); testRequest(HttpMethod.POST, "/dog/cat", 404, "Not Found"); }
/** * Add a route that matches the specified HTTP method and path regex * @param method the HTTP method to match * @param regex URI paths that begin with a match for this regex will match * @return the route */ public io.vertx.rxjava.ext.web.Route routeWithRegex(HttpMethod method, String regex) { io.vertx.rxjava.ext.web.Route ret = io.vertx.rxjava.ext.web.Route.newInstance(delegate.routeWithRegex(method, regex)); return ret; }
@Test public void testRegex3() throws Exception { router.routeWithRegex(".*foo.txt").handler(rc -> rc.response().setStatusMessage("ok").end()); testPattern("/dog/cat/foo.txt", "ok"); testRequest(HttpMethod.POST, "/dog/cat/foo.bar", 404, "Not Found"); }
@Test public void testRegexInSubRouter() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/api", subRouter); subRouter.routeWithRegex("\\/test").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/api/test", 200, "sausages"); }
@Test public void testSubRouteRegex() throws Exception { Router subRouter = Router.router(vertx); router.routeWithRegex("/foo/.*").handler(subRouter::handleContext).failureHandler(subRouter::handleFailure); subRouter.route("/blah").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/foo/blah", 500, "Internal Server Error"); }
@Test public void testRegex2() throws Exception { router.routeWithRegex("\\/([^\\/]+)\\/([^\\/]+)/blah").handler(rc -> { MultiMap params = rc.request().params(); rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end(); }); testPattern("/dog/cat/blah", "dogcat"); }
@Test public void testRegexWithNamedParams() throws Exception { router.routeWithRegex(HttpMethod.GET, "\\/(?<name>[^\\/]+)\\/(?<surname>[^\\/]+)").handler(rc -> { MultiMap params = rc.request().params(); rc.response().setStatusMessage(params.get("name") + params.get("surname")).end(); }); testPattern("/joe/doe", "joedoe"); }
@Test public void testRegex1() throws Exception { router.routeWithRegex("\\/([^\\/]+)\\/([^\\/]+)").handler(rc -> { MultiMap params = rc.request().params(); rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end(); }); testPattern("/dog/cat", "dogcat"); }
@Test public void testRegex1WithMethod() throws Exception { router.routeWithRegex(HttpMethod.GET, "\\/([^\\/]+)\\/([^\\/]+)").handler(rc -> { MultiMap params = rc.request().params(); rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end(); }); testPattern("/dog/cat", "dogcat"); testRequest(HttpMethod.POST, "/dog/cat", 404, "Not Found"); }