use of com.sun.net.httpserver.HttpServer in project elasticsearch by elastic.
the class RestClientMultipleHostsIntegTests method createHttpServer.
private static HttpServer createHttpServer() throws Exception {
HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
httpServer.start();
//returns a different status code depending on the path
for (int statusCode : getAllStatusCodes()) {
httpServer.createContext(pathPrefix + "/" + statusCode, new ResponseHandler(statusCode));
}
return httpServer;
}
use of com.sun.net.httpserver.HttpServer in project elasticsearch by elastic.
the class RestClientMultipleHostsIntegTests method stopHttpServers.
@AfterClass
public static void stopHttpServers() throws IOException {
restClient.close();
restClient = null;
for (HttpServer httpServer : httpServers) {
httpServer.stop(0);
}
httpServers = null;
}
use of com.sun.net.httpserver.HttpServer 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.HttpServer in project jersey by jersey.
the class JdkHttpServerFactory method createHttpServer.
private static HttpServer createHttpServer(final URI uri, final JdkHttpHandlerContainer handler, final SSLContext sslContext, final boolean start) {
if (uri == null) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_NULL());
}
final String scheme = uri.getScheme();
final boolean isHttp = "http".equalsIgnoreCase(scheme);
final boolean isHttps = "https".equalsIgnoreCase(scheme);
final HttpsConfigurator httpsConfigurator = sslContext != null ? new HttpsConfigurator(sslContext) : null;
if (isHttp) {
if (httpsConfigurator != null) {
// attempt to use https with http scheme
LOG.warning(LocalizationMessages.WARNING_CONTAINER_URI_SCHEME_SECURED());
}
} else if (isHttps) {
if (httpsConfigurator == null) {
if (start) {
// Starting https server w/o SSL is invalid, it will lead to error anyway.
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_HTTPS_NO_SSL());
} else {
// Creating the https server w/o SSL context, but not starting it is valid.
// However, server.setHttpsConfigurator() must be called before the start.
LOG.info(LocalizationMessages.INFO_CONTAINER_HTTPS_NO_SSL());
}
}
} else {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_SCHEME_UNKNOWN(uri));
}
final String path = uri.getPath();
if (path == null) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_NULL(uri));
} else if (path.isEmpty()) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_EMPTY(uri));
} else if (path.charAt(0) != '/') {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_START(uri));
}
final int port = (uri.getPort() == -1) ? (isHttp ? Container.DEFAULT_HTTP_PORT : Container.DEFAULT_HTTPS_PORT) : uri.getPort();
final HttpServer server;
try {
server = isHttp ? HttpServer.create(new InetSocketAddress(port), 0) : HttpsServer.create(new InetSocketAddress(port), 0);
} catch (final IOException ioe) {
throw new ProcessingException(LocalizationMessages.ERROR_CONTAINER_EXCEPTION_IO(), ioe);
}
if (isHttps && httpsConfigurator != null) {
((HttpsServer) server).setHttpsConfigurator(httpsConfigurator);
}
server.setExecutor(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("jdk-http-server-%d").setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler()).build()));
server.createContext(path, handler);
final HttpServer wrapper = isHttp ? createHttpServerWrapper(server, handler) : createHttpsServerWrapper((HttpsServer) server, handler);
if (start) {
wrapper.start();
}
return wrapper;
}
use of com.sun.net.httpserver.HttpServer in project jersey by jersey.
the class HelloWorldTest method testHelloWorld.
@Test
public void testHelloWorld() throws Exception {
HttpServer server = App.startServer();
WebTarget target = ClientBuilder.newClient().target(App.getBaseURI() + "helloworld");
assertEquals(HelloWorldResource.CLICHED_MESSAGE, target.request(MediaType.TEXT_PLAIN).get(String.class));
server.stop(0);
}
Aggregations