use of com.sun.net.httpserver.HttpsServer in project ribbon by Netflix.
the class MockHttpServer method before.
public void before(final Description description) throws Exception {
this.service = Executors.newFixedThreadPool(threadCount, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("TestHttpServer-%d").build());
InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 0);
if (hasSsl) {
byte[] sampleTruststore1 = Base64.decode(TEST_TS1);
byte[] sampleKeystore1 = Base64.decode(TEST_KS1);
keystore = File.createTempFile("SecureAcceptAllGetTest", ".keystore");
truststore = File.createTempFile("SecureAcceptAllGetTest", ".truststore");
FileOutputStream keystoreFileOut = new FileOutputStream(keystore);
try {
keystoreFileOut.write(sampleKeystore1);
} finally {
keystoreFileOut.close();
}
FileOutputStream truststoreFileOut = new FileOutputStream(truststore);
try {
truststoreFileOut.write(sampleTruststore1);
} finally {
truststoreFileOut.close();
}
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keystore), PASSWORD.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, PASSWORD.toCharArray());
KeyStore ts = KeyStore.getInstance("JKS");
ts.load(new FileInputStream(truststore), PASSWORD.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ts);
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
HttpsServer secureServer = HttpsServer.create(inetSocketAddress, 0);
secureServer.setHttpsConfigurator(new HttpsConfigurator(sc) {
public void configure(HttpsParameters params) {
SSLContext c = getSSLContext();
SSLParameters sslparams = c.getDefaultSSLParameters();
params.setSSLParameters(sslparams);
}
});
server = secureServer;
} else {
server = HttpServer.create(inetSocketAddress, 0);
}
server.setExecutor(service);
for (Entry<String, HttpHandler> handler : handlers.entrySet()) {
server.createContext(handler.getKey(), handler.getValue());
}
server.start();
localHttpServerPort = server.getAddress().getPort();
System.out.println(description.getClassName() + " TestServer is started: " + getServerUrl());
}
use of com.sun.net.httpserver.HttpsServer in project jersey by jersey.
the class JdkHttpsServerTest method testHttpsServerNoSslContextDelayedStart.
/**
* Test, that {@link HttpsServer} can be manually started even with (empty) SSLContext, but will throw an exception
* on request.
* @throws Exception
*/
@Test(expected = IOException.class)
public void testHttpsServerNoSslContextDelayedStart() throws Throwable {
server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, null, false);
assertThat(server, instanceOf(HttpsServer.class));
server.start();
final Client client = ClientBuilder.newBuilder().newClient();
try {
client.target(httpsUri).path("testHttps").request().get(String.class);
} catch (final ProcessingException e) {
throw e.getCause();
}
}
use of com.sun.net.httpserver.HttpsServer in project ignite by apache.
the class GridEmbeddedHttpServer method createAndStart.
/**
* Internal method which creates and starts the server.
*
* @param httpsMode True if the server to be started is HTTPS, false otherwise.
* @return Started server.
*/
private static GridEmbeddedHttpServer createAndStart(boolean httpsMode) throws Exception {
HttpServer httpSrv;
InetSocketAddress addrToBind = new InetSocketAddress(HOSTNAME_TO_BIND_SRV, getAvailablePort());
if (httpsMode) {
HttpsServer httpsSrv = HttpsServer.create(addrToBind, 0);
httpsSrv.setHttpsConfigurator(new HttpsConfigurator(GridTestUtils.sslContext()));
httpSrv = httpsSrv;
} else
httpSrv = HttpServer.create(addrToBind, 0);
GridEmbeddedHttpServer embeddedHttpSrv = new GridEmbeddedHttpServer();
embeddedHttpSrv.proto = httpsMode ? "https" : "http";
embeddedHttpSrv.httpSrv = httpSrv;
embeddedHttpSrv.httpSrv.start();
return embeddedHttpSrv;
}
use of com.sun.net.httpserver.HttpsServer in project cosmic by MissionCriticalCloud.
the class ConsoleProxySecureServerFactoryImpl method createHttpServerInstance.
@Override
public HttpServer createHttpServerInstance(final int port) throws IOException {
try {
final HttpsServer server = HttpsServer.create(new InetSocketAddress(port), 5);
server.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
@Override
public void configure(final HttpsParameters params) {
final SSLContext c = getSSLContext();
// get the default parameters
final SSLParameters sslparams = c.getDefaultSSLParameters();
params.setSSLParameters(sslparams);
// statement above could throw IAE if any params invalid.
// eg. if app has a UI and parameters supplied by a user.
}
});
s_logger.info("create HTTPS server instance on port: " + port);
return server;
} catch (final Exception ioe) {
s_logger.error(ioe.toString(), ioe);
}
return null;
}
use of com.sun.net.httpserver.HttpsServer 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;
}
Aggregations