Java Code Examples for com.sun.net.httpserver.HttpsServer#start()

The following examples show how to use com.sun.net.httpserver.HttpsServer#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RefactoringMinerHttpsServer.java    From RefactoringMiner with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	Properties prop = new Properties();
	InputStream input = new FileInputStream("server.properties");
	prop.load(input);
	String hostName = prop.getProperty("hostname");
	int port = Integer.parseInt(prop.getProperty("port"));
	String keystore = prop.getProperty("keystore");
	String keyStorePass = prop.getProperty("keystore-password");
	
	InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getByName(hostName), port);
	HttpsServer server = HttpsServer.create(inetSocketAddress, 0);
	SSLContext sslContext = SSLContext.getInstance("TLS");

	// initialize the keystore
	char[] password = keyStorePass.toCharArray();
	KeyStore ks = KeyStore.getInstance("JKS");
	FileInputStream fis = new FileInputStream(keystore);
	ks.load(fis, password);

	// setup the key manager factory
	KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
	kmf.init(ks, password);

	// setup the trust manager factory
	TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
	tmf.init(ks);

	// setup the HTTPS context and parameters
	sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
	server.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
		public void configure(HttpsParameters params) {
			try {
				// initialize the SSL context
				SSLContext context = getSSLContext();
				SSLEngine engine = context.createSSLEngine();
				params.setNeedClientAuth(false);
				params.setCipherSuites(engine.getEnabledCipherSuites());
				params.setProtocols(engine.getEnabledProtocols());

				// Set the SSL parameters
				SSLParameters sslParameters = context.getSupportedSSLParameters();
				params.setSSLParameters(sslParameters);

			} catch (Exception ex) {
				System.out.println("Failed to create HTTPS port");
			}
		}
	});
	
	server.createContext("/RefactoringMiner", new MyHandler());
	server.setExecutor(new ThreadPoolExecutor(4, 8, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100)));
	server.start();
	System.out.println(InetAddress.getLocalHost());
}