use of com.sun.net.httpserver.HttpHandler in project jdk8u_jdk by JetBrains.
the class ChunkedEncodingTest method startHttpServer.
/**
* Http Server
*/
static HttpServer startHttpServer() throws IOException {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
HttpHandler httpHandler = new SimpleHandler();
httpServer.createContext("/chunked/", httpHandler);
httpServer.start();
return httpServer;
}
use of com.sun.net.httpserver.HttpHandler in project jdk8u_jdk by JetBrains.
the class ClassLoad method main.
public static void main(String[] args) throws Exception {
boolean error = true;
// Start a dummy server to return 404
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
HttpHandler handler = new HttpHandler() {
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
while (is.read() != -1) ;
t.sendResponseHeaders(404, -1);
t.close();
}
};
server.createContext("/", handler);
server.start();
// Client request
try {
URL url = new URL("http://localhost:" + server.getAddress().getPort());
String name = args.length >= 2 ? args[1] : "foo.bar.Baz";
ClassLoader loader = new URLClassLoader(new URL[] { url });
System.out.println(url);
Class c = loader.loadClass(name);
System.out.println("Loaded class \"" + c.getName() + "\".");
} catch (ClassNotFoundException ex) {
System.out.println(ex);
error = false;
} finally {
server.stop(0);
}
if (error)
throw new RuntimeException("No ClassNotFoundException generated");
}
use of com.sun.net.httpserver.HttpHandler in project jersey by jersey.
the class RuntimeDelegateTest method testFetch.
@Test
public void testFetch() throws Exception {
final HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
final HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new Application() {
@Override
public Set<Class<?>> getClasses() {
return Collections.<Class<?>>singleton(Resource.class);
}
}, HttpHandler.class);
try {
server.createContext("/", handler);
server.start();
final Response response = ClientBuilder.newClient().target(UriBuilder.fromUri("http://localhost/").port(server.getAddress().getPort()).build()).request().get();
assertThat(response.readEntity(String.class), is("get"));
} finally {
server.stop(0);
}
}
use of com.sun.net.httpserver.HttpHandler 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.HttpHandler 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();
}
};
}
Aggregations