public DefaultSockJSServer(final VertxInternal vertx, final HttpServer httpServer) { this.vertx = vertx; this.sessions = vertx.sharedData().getMap("_vertx.sockjssessions"); // Any previous request and websocket handlers will become default handlers // if nothing else matches rm.noMatch(httpServer.requestHandler()); wsMatcher.noMatch( new Handler<WebSocketMatcher.Match>() { Handler<ServerWebSocket> wsHandler = httpServer.websocketHandler(); public void handle(WebSocketMatcher.Match match) { if (wsHandler != null) { wsHandler.handle(match.ws); } } }); httpServer.requestHandler( new Handler<HttpServerRequest>() { @Override public void handle(HttpServerRequest req) { if (log.isTraceEnabled()) { log.trace("Got request in sockjs server: " + req.uri); } rm.handle(req); } }); httpServer.websocketHandler(wsMatcher); }
public void start() { server = new HttpServer(); server.requestHandler( new Handler<HttpServerRequest>() { public void handle(HttpServerRequest req) { if (req.path.equals("/")) req.response.sendFile("sockjs/index.html"); // Serve the html } }); SockJSServer sockServer = new SockJSServer(server); sockServer.installApp( new AppConfig().setPrefix("/testapp"), new Handler<SockJSSocket>() { public void handle(final SockJSSocket sock) { sock.dataHandler( new Handler<Buffer>() { public void handle(Buffer data) { sock.writeBuffer(data); // Echo it back } }); } }); server.listen(8080); }
public DefaultSockJSServer(final VertxInternal vertx, final HttpServer httpServer) { this.vertx = vertx; this.sessions = vertx.sharedData().getMap("_vertx.sockjssessions"); // Any previous request and websocket handlers will become default handlers // if nothing else matches rm.noMatch(httpServer.requestHandler()); wsMatcher.noMatch( new Handler<WebSocketMatcher.Match>() { Handler<ServerWebSocket> wsHandler = httpServer.websocketHandler(); public void handle(WebSocketMatcher.Match match) { wsHandler.handle(match.ws); } }); httpServer.requestHandler(rm); httpServer.websocketHandler(wsMatcher); }
private HttpServer registerHandler( final int portNumber, final Handler<HttpServerRequest> handler) { final HttpServer httpServer = getHttpServer(portNumber); try { if (handler != null) { httpServer.requestHandler(handler); } return httpServer; } catch (final Exception e) { return null; } }
public void request( final String method, final String url, final MultiMap headers, final Buffer body, final Handler<Response> handler) { try { final URI uri = new URI(url); final boolean urlEncoded = "application/x-www-form-urlencoded".equalsIgnoreCase(headers.get("content-type")); final Response response = new Response(vertx, handler); // start yoke fakeServer .requestHandler() .handle( new HttpServerRequest() { MultiMap params = null; MultiMap attributes = null; @Override public HttpVersion version() { return HttpVersion.HTTP_1_1; } @Override public String method() { return method.toUpperCase(); } @Override public String uri() { return uri.getPath() + "?" + uri.getQuery() + "#" + uri.getFragment(); } @Override public String path() { return uri.getPath(); } @Override public String query() { return uri.getQuery(); } @Override public HttpServerResponse response() { return response; } @Override public MultiMap headers() { return headers; } @Override public MultiMap params() { if (params == null) { QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri()); Map<String, List<String>> prms = queryStringDecoder.parameters(); params = new CaseInsensitiveMultiMap(); if (!prms.isEmpty()) { for (Map.Entry<String, List<String>> entry : prms.entrySet()) { params.add(entry.getKey(), entry.getValue()); } } } return params; } @Override public InetSocketAddress remoteAddress() { return new InetSocketAddress("127.0.0.1", 80); } @Override public X509Certificate[] peerCertificateChain() throws SSLPeerUnverifiedException { return null; } @Override public URI absoluteURI() { return uri; } @Override public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) { bodyHandler.handle(body); return this; } @Override public HttpServerRequest dataHandler(Handler<Buffer> handler) { handler.handle(body); return this; } @Override public HttpServerRequest pause() { throw new UnsupportedOperationException("This mock does not support pause"); } @Override public HttpServerRequest resume() { throw new UnsupportedOperationException("This mock does not support resume"); } @Override public HttpServerRequest endHandler(Handler<Void> endHandler) { endHandler.handle(null); return this; } @Override public NetSocket netSocket() { throw new UnsupportedOperationException("This mock does not support netSocket"); } @Override public HttpServerRequest expectMultiPart(boolean expect) { // NOOP return this; } @Override public HttpServerRequest uploadHandler( Handler<HttpServerFileUpload> uploadHandler) { throw new UnsupportedOperationException( "This mock does not support uploadHandler"); } @Override public MultiMap formAttributes() { if (attributes == null) { attributes = new CaseInsensitiveMultiMap(); if (urlEncoded) { QueryStringDecoder queryStringDecoder = new QueryStringDecoder(body.toString(), false); Map<String, List<String>> prms = queryStringDecoder.parameters(); if (!prms.isEmpty()) { for (Map.Entry<String, List<String>> entry : prms.entrySet()) { attributes.add(entry.getKey(), entry.getValue()); } } } } return attributes; } @Override public HttpServerRequest exceptionHandler(Handler<Throwable> handler) { throw new UnsupportedOperationException( "This mock does not support exceptionHandler"); } }); } catch (URISyntaxException e) { throw new RuntimeException(e); } }