hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jax <jaxles...@gmail.com>
Subject Re: Https server
Date Fri, 18 Jul 2014 17:22:28 GMT
Thanks for the reply!
I got it working. 

First created certs

::create the server keystore 
/usr/java/default/bin/keytool -genkeypair -alias mykeystore -keystore
keystore -keyalg RSA -dname "CN=blahblah,OU=dmas,O=blah,L=san
francisco,S=CA,C=US" -keypass pass -storepass pass

::check the contents of the keystore
/usr/java/default/bin/keytool -list -keystore keystore -storepass pass -v

::export the server cert if you need to
/usr/java/default/bin/keytool -exportcert -alias mykeystore -keystore
keystore -file server.cer -rfc -storepass pass

public class NHttpServer {

    public static void main(String[] args) throws Exception {

        int port = 443;

        // Create HTTP protocol processing chain
        HttpProcessor httpproc = HttpProcessorBuilder.create()
                .add(new ResponseDate())
                .add(new ResponseServer())
                .add(new ResponseContent())
                .add(new ResponseConnControl()).build();
        // Create request handler registry
        UriHttpAsyncRequestHandlerMapper reqistry = new
UriHttpAsyncRequestHandlerMapper();
        // Register the default handler for all URIs
        reqistry.register("/getStatus", new StatusRequestHandler());
        // Create server-side HTTP protocol handler
        HttpAsyncService protocolHandler = new HttpAsyncService(httpproc,
reqistry) {

            @Override
            public void connected(final NHttpServerConnection conn) {
                System.out.println(conn + ": connection open");
                super.connected(conn);
            }

            @Override
            public void closed(final NHttpServerConnection conn) {
                System.out.println(conn + ": connection closed");
                super.closed(conn);
            }

        };

      // Create HTTP connection factory
      NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory;

      ClassLoader cl = NHttpServer.class.getClassLoader();
      URL url = cl.getResource("keystore");
      if (url == null) {
          System.out.println("Keystore not found");
          System.exit(1);
      }

      KeyStore keystore  = KeyStore.getInstance("jks");
      keystore.load(url.openStream(), "pass".toCharArray());
      KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
              KeyManagerFactory.getDefaultAlgorithm());
      kmfactory.init(keystore, "pass".toCharArray());
      KeyManager[] keymanagers = kmfactory.getKeyManagers();
      SSLContext sslcontext = SSLContext.getInstance("TLS");
      sslcontext.init(keymanagers, null, null);
      connFactory = new SSLNHttpServerConnectionFactory(sslcontext,
              null, ConnectionConfig.DEFAULT);

        // Create server-side I/O event dispatch
        IOEventDispatch ioEventDispatch = new
DefaultHttpServerIODispatch(protocolHandler, connFactory);
        // Set I/O reactor defaults
        IOReactorConfig config = IOReactorConfig.custom()
            .setIoThreadCount(1)
            .setSoTimeout(3000)
            .setConnectTimeout(3000)
            .build();
        // Create server-side I/O reactor
        ListeningIOReactor ioReactor = new
DefaultListeningIOReactor(config);
        try {
            // Listen of the given port
            ioReactor.listen(new InetSocketAddress(port));
            // Ready to go!
            ioReactor.execute(ioEventDispatch);
        } catch (InterruptedIOException ex) {
            System.err.println("Interrupted");
        } catch (IOException e) {
            System.err.println("I/O error: " + e.getMessage());
        }
        System.out.println("Shutdown");
    }

  }



--
View this message in context: http://httpcomponents.10934.n7.nabble.com/Https-server-tp23868p23881.html
Sent from the HttpClient-User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message