use of com.sun.net.httpserver.HttpExchange in project pinot by linkedin.
the class ServerTableSizeReaderTest method createHandler.
private HttpHandler createHandler(final int status, final TableSizeInfo tableSize, final int sleepTimeMs) {
return new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
if (sleepTimeMs > 0) {
try {
Thread.sleep(sleepTimeMs);
} catch (InterruptedException e) {
LOGGER.info("Handler interrupted during sleep");
}
}
String json = new ObjectMapper().writeValueAsString(tableSize);
httpExchange.sendResponseHeaders(status, json.length());
OutputStream responseBody = httpExchange.getResponseBody();
responseBody.write(json.getBytes());
responseBody.close();
}
};
}
use of com.sun.net.httpserver.HttpExchange in project pinot by linkedin.
the class TableSizeReaderTest method createHandler.
private HttpHandler createHandler(final int status, final List<SegmentSizeInfo> segmentSizes, final int sleepTimeMs) {
return new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
if (sleepTimeMs > 0) {
try {
Thread.sleep(sleepTimeMs);
} catch (InterruptedException e) {
LOGGER.info("Handler interrupted during sleep");
}
}
TableSizeInfo tableInfo = new TableSizeInfo("myTable", 0);
tableInfo.segments = segmentSizes;
for (SegmentSizeInfo segmentSize : segmentSizes) {
tableInfo.diskSizeInBytes += segmentSize.diskSizeInBytes;
}
String json = new ObjectMapper().writeValueAsString(tableInfo);
httpExchange.sendResponseHeaders(status, json.length());
OutputStream responseBody = httpExchange.getResponseBody();
responseBody.write(json.getBytes());
responseBody.close();
}
};
}
use of com.sun.net.httpserver.HttpExchange in project heron by twitter.
the class WebSink method startHttpServer.
/**
* Start a http server on supplied port that will serve the metrics, as json,
* on the specified path.
*
* @param path
* @param port
*/
protected void startHttpServer(String path, int port) {
try {
httpServer = HttpServer.create(new InetSocketAddress(port), 0);
httpServer.createContext(path, new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
metricsCache.cleanUp();
byte[] response = MAPPER.writeValueAsString(metricsCache.asMap()).getBytes();
httpExchange.sendResponseHeaders(HTTP_STATUS_OK, response.length);
OutputStream os = httpExchange.getResponseBody();
os.write(response);
os.close();
LOG.log(Level.INFO, "Received metrics request.");
}
});
httpServer.start();
} catch (IOException e) {
throw new RuntimeException("Failed to create Http server on port " + port, e);
}
}
use of com.sun.net.httpserver.HttpExchange in project heron by twitter.
the class NetworkUtilsTest method testReadHttpRequestBodyFail.
@Test
public void testReadHttpRequestBodyFail() throws Exception {
HttpExchange exchange = Mockito.mock(HttpExchange.class);
Headers headers = Mockito.mock(Headers.class);
Mockito.doReturn(headers).when(exchange).getRequestHeaders();
Mockito.doReturn("-1").when(headers).getFirst(Matchers.anyString());
Assert.assertArrayEquals(new byte[0], NetworkUtils.readHttpRequestBody(exchange));
Mockito.doReturn("10").when(headers).getFirst(Matchers.anyString());
InputStream inputStream = Mockito.mock(InputStream.class);
Mockito.doReturn(inputStream).when(exchange).getRequestBody();
Mockito.doThrow(new IOException("Designed IO exception for testing")).when(inputStream).read(Matchers.any(byte[].class), Matchers.anyInt(), Matchers.anyInt());
Assert.assertArrayEquals(new byte[0], NetworkUtils.readHttpRequestBody(exchange));
Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
}
use of com.sun.net.httpserver.HttpExchange in project heron by twitter.
the class NetworkUtilsTest method testReadHttpRequestBody.
@Test
public void testReadHttpRequestBody() throws Exception {
byte[] expectedBytes = "TO READ".getBytes();
InputStream is = Mockito.spy(new ByteArrayInputStream(expectedBytes));
HttpExchange exchange = Mockito.mock(HttpExchange.class);
Headers headers = Mockito.mock(Headers.class);
Mockito.doReturn(Integer.toString(expectedBytes.length)).when(headers).getFirst(Matchers.anyString());
Mockito.doReturn(headers).when(exchange).getRequestHeaders();
Mockito.doReturn(is).when(exchange).getRequestBody();
Assert.assertArrayEquals(expectedBytes, NetworkUtils.readHttpRequestBody(exchange));
Mockito.verify(is, Mockito.atLeastOnce()).close();
}
Aggregations