/** * Start this. */ public void start() { server.setHttpRequestConsumer(proxyService::handleRequest); server.setHttpRequestsIdleConsumer(aVoid -> flushServiceProxy(proxyService)); server.startServer(); }
default HttpServer startServer() { start(); return this; }
@Override public ServiceEndpointServer startServerAndWait() { doStart(); if (httpServer instanceof HttpServer) { ((HttpServer) httpServer).startServerAndWait(); } else { httpServer.start(); } return this; }
public HttpServer getHttpServer() { if (httpServer == null) { httpServer = getHttpServerBuilder() .setPort(getPort()) .setHost(getHost()) .build(); final Supplier<String> webPageContentsSupplier = getWebPageContentsSupplier(); httpServer.setShouldContinueHttpRequest(httpRequest -> { /* If not the page uri we want to then, just continue by returning true. */ if (!httpRequest.getUri().equals(getHtmlPageLocation())) { return true; } final String webPageContents = webPageContentsSupplier.get(); /* Send the HTML file out to the browser. */ httpRequest.getReceiver().response(200, "text/html", webPageContents); return false; }); } return httpServer; }
@Test public void test() throws Exception { final int openPortStartAt = PortUtils.findOpenPortStartAt(7777); HttpServer server = HttpServerBuilder.httpServerBuilder().setPort(openPortStartAt).build(); server.setHttpRequestConsumer(serverRequest -> { final MultiMap<String, String> headers = MultiMap.multiMap(); headers.add("foo", "bar").add("foo", "baz"); serverRequest.getReceiver().response(200, "application/json", "true", headers); }); server.startServerAndWait(); HttpClient client = HttpClientBuilder.httpClientBuilder() .setPort(openPortStartAt) .setHost("localhost").build(); client.start(); final HttpTextResponse httpResponse = client.get("/hi"); puts(httpResponse.headers()); boolean foundFoo = httpResponse.headers().keySet().contains("foo"); assertTrue(foundFoo); }
httpServer.setHttpRequestConsumer(httpRequest -> { }); httpServer.start();
server.setHttpRequestConsumer(serverRequest -> { server.startServer();
@Test public void testBinary() throws Exception { final int port = PortUtils.findOpenPortStartAt(4001); final HttpServer httpServer = HttpServerBuilder.httpServerBuilder().setPort(port).build(); final AtomicReference<Object> bodyRef = new AtomicReference<>(); final AtomicReference<byte[]> messageRef = new AtomicReference<>(); final CountDownLatch countDownLatch = new CountDownLatch(2); httpServer.setWebSocketMessageConsumer(webSocketMessage -> { bodyRef.set(webSocketMessage.body()); webSocketMessage.getSender().sendBytes("world".getBytes()); countDownLatch.countDown(); }); httpServer.startServerAndWait(); final HttpClient httpClient = HttpClientBuilder.httpClientBuilder().setPort(port).buildAndStart(); final WebSocket webSocket = httpClient.createWebSocket("/foo"); webSocket.setBinaryMessageConsumer(message -> { messageRef.set(message); countDownLatch.countDown(); }); webSocket.openAndWait(); webSocket.sendBinary("hello".getBytes()); countDownLatch.await(5, TimeUnit.SECONDS); assertEquals("world", new String(messageRef.get(), StandardCharsets.UTF_8)); assertEquals("hello", new String(((byte[]) bodyRef.get()), StandardCharsets.UTF_8)); }
/** * Stop this. */ @Override public void stop() { server.stop(); } }
@Test public void testFormSendUseBody() throws Exception { connect(); final HttpRequest request = new HttpRequestBuilder() .setUri("/services/mockservice/ping") .addParam("foo", "bar") .setFormPostAndCreateFormBody() .setTextReceiver((code, mimeType, body) -> { responseCode.set(code); responseReceived.set(true); }) .build(); server.setHttpRequestConsumer(serverRequest -> { requestReceived.set(true); messageBody.set(serverRequest.getBodyAsString() + " " + serverRequest.getFormParams().get("foo")); serverRequest.getReceiver().response(200, "application/json", "\"ok\""); }); run(); client.sendHttpRequest(request); client.flush(); waitForTrigger(20, o -> this.responseReceived.get()); puts("RESPONSE", responseCode, messageBody, responseReceived); assertEquals("foo=bar bar", messageBody.get()); assertEquals(200, responseCode.get()); stop(); }
public HttpServer build() { final HttpServer httpServer = getFactory().createHttpServer( this.getConfig(), this.getEndpointName(), this.getSystemManager(), this.getServiceDiscovery(), this.getHealthServiceAsync(), this.getServiceDiscoveryTtl(), this.getServiceDiscoveryTtlTimeUnit(), this.getResponseDecorators(), this.getHttpResponseCreator(), this.getRequestBodyContinuePredicate() ); if (requestContinuePredicate != null) { httpServer.setShouldContinueHttpRequest(requestContinuePredicate); } if (qBitSystemManager != null) { qBitSystemManager.registerServer(httpServer); } return httpServer; }
server.setHttpRequestConsumer(serverRequest -> { server.startServerAndWait();
httpServer.setHttpRequestConsumer(httpRequest -> { }); httpServer.start();
@Test public void testText() throws Exception { final int port = PortUtils.findOpenPortStartAt(4000); final HttpServer httpServer = HttpServerBuilder.httpServerBuilder().setPort(port).build(); final AtomicReference<Object> bodyRef = new AtomicReference<>(); final AtomicReference<String> messageRef = new AtomicReference<>(); final CountDownLatch countDownLatch = new CountDownLatch(2); httpServer.setWebSocketMessageConsumer(webSocketMessage -> { bodyRef.set(webSocketMessage.body()); webSocketMessage.getSender().sendText("world"); countDownLatch.countDown(); }); httpServer.startServerAndWait(); final HttpClient httpClient = HttpClientBuilder.httpClientBuilder().setPort(port).buildAndStart(); final WebSocket webSocket = httpClient.createWebSocket("/foo"); webSocket.setTextMessageConsumer(message -> { messageRef.set(message); countDownLatch.countDown(); }); webSocket.openAndWait(); webSocket.sendText("hello"); countDownLatch.await(5, TimeUnit.SECONDS); assertEquals("world", messageRef.get()); assertEquals("hello", bodyRef.get().toString()); }
private void stop() { client.stop(); server.stop(); }
@Test public void testFormSend() throws Exception { connect(); final HttpRequest request = new HttpRequestBuilder() .setUri("/services/mockservice/ping") .addParam("foo", "bar") .setFormPostAndCreateFormBody() .setTextReceiver((code, mimeType, body) -> { responseCode.set(code); responseReceived.set(true); }) .build(); server.setHttpRequestConsumer(serverRequest -> { requestReceived.set(true); puts("SERVER", serverRequest.getUri(), serverRequest.getBodyAsString()); messageBody.set(serverRequest.formParams().get("foo")); serverRequest.getReceiver().response(200, "application/json", "\"ok\""); }); run(); client.sendHttpRequest(request); client.flush(); waitForTrigger(20, o -> this.responseReceived.get()); puts("RESPONSE", responseCode, messageBody, responseReceived); assertEquals("bar", messageBody.get()); assertEquals(200, responseCode.get()); stop(); }
/** Start this. */ public void start() { server.setHttpRequestConsumer(proxyService::handleRequest); server.setHttpRequestsIdleConsumer(aVoid -> flushServiceProxy(proxyService)); server.startServer(); }
httpServer.setShouldContinueHttpRequest(this.getRequestContinuePredicate());
@Test public void testTextQueueWithBatchSize() throws Exception { final int port = PortUtils.findOpenPortStartAt(4000); final HttpServer httpServer = HttpServerBuilder.httpServerBuilder().setPort(port).build(); final AtomicReference<Object> bodyRef = new AtomicReference<>(); httpServer.setWebSocketMessageConsumer(webSocketMessage -> { bodyRef.set(webSocketMessage.body()); webSocketMessage.getSender().sendText("world"); }); httpServer.startServerAndWait(); final HttpClient httpClient = HttpClientBuilder.httpClientBuilder().setPort(port).buildAndStart(); final WebSocket webSocket = httpClient.createWebSocket("/foo"); final WebSocketTextQueue queue = new WebSocketTextQueue(webSocket, 100, 100, TimeUnit.MILLISECONDS); webSocket.openAndWait(); webSocket.sendText("hello"); String message = queue.receiveQueue().pollWait(); assertEquals("world", message); assertEquals("hello", bodyRef.get().toString()); }
public void run() { server.startServerAndWait(); client.startClient(); }