use of com.sun.net.httpserver.HttpsConfigurator in project elasticsearch by elastic.
the class AzureDiscoveryClusterFormationTests method startHttpd.
/**
* Creates mock EC2 endpoint providing the list of started nodes to the DescribeInstances API call
*/
@BeforeClass
public static void startHttpd() throws Exception {
logDir = createTempDir();
SSLContext sslContext = getSSLContext();
httpsServer = MockHttpServer.createHttps(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
httpsServer.createContext("/subscription/services/hostedservices/myservice", (s) -> {
Headers headers = s.getResponseHeaders();
headers.add("Content-Type", "text/xml; charset=UTF-8");
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
StringWriter out = new StringWriter();
XMLStreamWriter sw;
try {
sw = xmlOutputFactory.createXMLStreamWriter(out);
sw.writeStartDocument();
String namespace = "http://schemas.microsoft.com/windowsazure";
sw.setDefaultNamespace(namespace);
sw.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "HostedService", namespace);
{
sw.writeStartElement("Deployments");
{
Path[] files = FileSystemUtils.files(logDir);
for (int i = 0; i < files.length; i++) {
Path resolve = files[i].resolve("transport.ports");
if (Files.exists(resolve)) {
List<String> addresses = Files.readAllLines(resolve);
Collections.shuffle(addresses, random());
String address = addresses.get(0);
int indexOfLastColon = address.lastIndexOf(':');
String host = address.substring(0, indexOfLastColon);
String port = address.substring(indexOfLastColon + 1);
sw.writeStartElement("Deployment");
{
sw.writeStartElement("Name");
sw.writeCharacters("mydeployment");
sw.writeEndElement();
sw.writeStartElement("DeploymentSlot");
sw.writeCharacters(DeploymentSlot.Production.name());
sw.writeEndElement();
sw.writeStartElement("Status");
sw.writeCharacters(DeploymentStatus.Running.name());
sw.writeEndElement();
sw.writeStartElement("RoleInstanceList");
{
sw.writeStartElement("RoleInstance");
{
sw.writeStartElement("RoleName");
sw.writeCharacters(UUID.randomUUID().toString());
sw.writeEndElement();
sw.writeStartElement("IpAddress");
sw.writeCharacters(host);
sw.writeEndElement();
sw.writeStartElement("InstanceEndpoints");
{
sw.writeStartElement("InstanceEndpoint");
{
sw.writeStartElement("Name");
sw.writeCharacters("myendpoint");
sw.writeEndElement();
sw.writeStartElement("Vip");
sw.writeCharacters(host);
sw.writeEndElement();
sw.writeStartElement("PublicPort");
sw.writeCharacters(port);
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
}
}
sw.writeEndElement();
}
sw.writeEndElement();
sw.writeEndDocument();
sw.flush();
final byte[] responseAsBytes = out.toString().getBytes(StandardCharsets.UTF_8);
s.sendResponseHeaders(200, responseAsBytes.length);
OutputStream responseBody = s.getResponseBody();
responseBody.write(responseAsBytes);
responseBody.close();
} catch (XMLStreamException e) {
Loggers.getLogger(AzureDiscoveryClusterFormationTests.class).error("Failed serializing XML", e);
throw new RuntimeException(e);
}
});
httpsServer.start();
}
use of com.sun.net.httpserver.HttpsConfigurator 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.HttpsConfigurator in project jersey by jersey.
the class JdkHttpsServerTest method testConfigureSslContextAfterStart.
/**
* Test, that {@link HttpsServer} cannot be configured with {@link HttpsConfigurator} after it has started.
* @throws Exception
*/
@Test(expected = IllegalStateException.class)
public void testConfigureSslContextAfterStart() throws Throwable {
server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, null, false);
assertThat(server, instanceOf(HttpsServer.class));
server.start();
((HttpsServer) server).setHttpsConfigurator(new HttpsConfigurator(getServerSslContext()));
}
use of com.sun.net.httpserver.HttpsConfigurator in project languagetool by languagetool-org.
the class HTTPSServer method getConfigurator.
private HttpsConfigurator getConfigurator(SSLContext sslContext) {
return new HttpsConfigurator(sslContext) {
@Override
public void configure(HttpsParameters params) {
SSLContext context = getSSLContext();
SSLParameters sslParams = context.getDefaultSSLParameters();
params.setNeedClientAuth(false);
params.setSSLParameters(sslParams);
}
};
}
use of com.sun.net.httpserver.HttpsConfigurator in project GNS by MobilityFirst.
the class GNSHttpsServer method tryPort.
/**
* Try to start the http server at the port.
*
* @param port
* @return true if it was started
*/
@Override
public boolean tryPort(int port) {
try {
InetSocketAddress addr = new InetSocketAddress(port);
httpsServer = HttpsServer.create(addr, 0);
SSLContext sslContext = createSSLContext();
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
@Override
public void configure(HttpsParameters parameters) {
// initialise the SSL context
SSLContext context = getSSLContext();
SSLEngine engine = context.createSSLEngine();
//parameters.setNeedClientAuth(false);
parameters.setCipherSuites(engine.getEnabledCipherSuites());
parameters.setProtocols(engine.getEnabledProtocols());
// get the default parameters
SSLParameters sslParameters = context.getDefaultSSLParameters();
sslParameters.setNeedClientAuth(true);
parameters.setNeedClientAuth(true);
parameters.setSSLParameters(sslParameters);
}
});
httpsServer.createContext("/", new EchoHttpHandler());
httpsServer.createContext("/" + GNS_PATH, new DefaultHttpHandler());
httpsServer.setExecutor(Executors.newCachedThreadPool());
httpsServer.start();
// Need to do this for the places where we expose the secure http service to the user
requestHandler.setHttpsServerPort(port);
LOG.log(Level.INFO, "HTTPS server is listening on port {0}", port);
return true;
} catch (BindException e) {
LOG.log(Level.FINE, "HTTPS server failed to start on port {0} due to {1}", new Object[] { port, e.getMessage() });
return false;
} catch (IOException | NoSuchAlgorithmException | KeyStoreException | CertificateException | UnrecoverableKeyException | KeyManagementException e) {
LOG.log(Level.FINE, "HTTPS server failed to start on port {0} due to {1}", new Object[] { port, e.getMessage() });
e.printStackTrace();
return false;
}
}
Aggregations