com.streamhub.nio
Class SecureNIOServer

java.lang.Object
  extended by com.streamhub.nio.NIOServer
      extended by com.streamhub.nio.SecureNIOServer
All Implemented Interfaces:
Publisher, PushServer

public class SecureNIOServer
extends NIOServer
implements PushServer

A HTTPS version of NIOServer.

A SSLContext is passed to every constructor which defines where to load the certificate from and which protocol to use. The SSLContext must be initialized before being passed to the constructor.

This example shows how to load a keystore named .keystore from the current directory, which is protected by the password changeit. The protocol chosen in this case is SSL.

  SSLContext context = SSLContext.getInstance("SSL");
  KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
  Builder builder = Builder.newInstance(KeyStore.getDefaultType(), null, new File(".keystore"), new KeyStore.PasswordProtection("changeit"));
  KeyStore keystore = builder.getKeyStore();
  kmf.init(keystore, "changeit");
  // MUST initialize the SSLContext before passing to SecureNIOServer
  context.init(kmf.getKeyManagers(), null, null);
  PushServer server = new SecureNIOServer(443, context);
  server.start();
  System.out.println("HTTPS server started on port 443");
  System.out.println("Press any key to stop...");
  System.in.read();
  server.stop();
  System.out.println("Server stopped");

For more information on importing SSL certificates in to keystores and using SSL in Java refer to some of the following resources:

See Also:
NIOServer

Constructor Summary
SecureNIOServer(InetSocketAddress address, InetSocketAddress streamingAdapterAddress, SSLContext sslContext)
          Creates a new HTTPS server bound to address and a streaming adapter bound to streamingAdapterAddress
SecureNIOServer(InetSocketAddress address, InetSocketAddress streamingAdapterAddress, String licenseDir, SSLContext sslContext)
          Creates a new HTTPS server bound to address and a streaming adapter bound to streamingAdapterAddress.
SecureNIOServer(InetSocketAddress address, InetSocketAddress streamingAdapterAddress, URL licenseUrl, URL log4jConfigurationUrl, SSLContext sslContext)
          Creates a new HTTPS server bound to address and a streaming adapter bound to streamingAdapterAddress, loading the license and log4j xml configuration from the alternative locations specified by licenseUrl and log4jConfigurationUrl respectively.
SecureNIOServer(InetSocketAddress address, SSLContext sslContext)
          Creates a new HTTPS server bound to address.
SecureNIOServer(int port, SSLContext sslContext)
          Creates a new HTTPS server bound to the wildcard address.
 
Method Summary
 
Methods inherited from class com.streamhub.nio.NIOServer
addContext, addStaticContent, addStaticContent, getSubscriptionManager, isStarted, main, publish, setDefaultHeader, setDefaultPushHeader, start, stop
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.streamhub.api.PushServer
addContext, addStaticContent, addStaticContent, getSubscriptionManager, isStarted, setDefaultHeader, setDefaultPushHeader, start, stop
 
Methods inherited from interface com.streamhub.api.Publisher
publish
 

Constructor Detail

SecureNIOServer

public SecureNIOServer(int port,
                       SSLContext sslContext)
Creates a new HTTPS server bound to the wildcard address.

Parameters:
port - the port to listen on
sslContext - an initiated SSLContext defining the characteristics of the HTTPS certificate

SecureNIOServer

public SecureNIOServer(InetSocketAddress address,
                       SSLContext sslContext)
Creates a new HTTPS server bound to address.

Parameters:
address - the address to bind to
sslContext - an initiated SSLContext defining the characteristics of the HTTPS certificate

SecureNIOServer

public SecureNIOServer(InetSocketAddress address,
                       InetSocketAddress streamingAdapterAddress,
                       SSLContext sslContext)
Creates a new HTTPS server bound to address and a streaming adapter bound to streamingAdapterAddress

Parameters:
address - the address to bind the server to
streamingAdapterAddress - the address to listen for streaming adapters
sslContext - an initiated SSLContext defining the characteristics of the HTTPS certificate

SecureNIOServer

public SecureNIOServer(InetSocketAddress address,
                       InetSocketAddress streamingAdapterAddress,
                       String licenseDir,
                       SSLContext sslContext)
Creates a new HTTPS server bound to address and a streaming adapter bound to streamingAdapterAddress. To choose not to start a streaming adapter, pass null as the second argument.

Parameters:
address - the address to bind the server to
streamingAdapterAddress - the address to listen for streaming adapters or null if a streaming adapter is not required
licenseDir - an alternative directory to load the license from. The license file must be called "license.txt"
sslContext - an initiated SSLContext defining the characteristics of the HTTPS certificate

SecureNIOServer

public SecureNIOServer(InetSocketAddress address,
                       InetSocketAddress streamingAdapterAddress,
                       URL licenseUrl,
                       URL log4jConfigurationUrl,
                       SSLContext sslContext)
Creates a new HTTPS server bound to address and a streaming adapter bound to streamingAdapterAddress, loading the license and log4j xml configuration from the alternative locations specified by licenseUrl and log4jConfigurationUrl respectively. To choose not to start a streaming adapter, pass null as the second argument.

The following URL formats are supported for the licenseUrl and log4jConfigurationUrl parameters:

The default location for the license file is a file named "license.txt" in the current working directory (or "."). The default location for the log4j configuration is "conf/log4j.xml" relative to the current working directory.

Parameters:
address - the address to bind the server to
streamingAdapterAddress - the address to listen for streaming adapters or null if a streaming adapter is not required
licenseUrl - an alternative URL to load the license from
log4jConfigurationUrl - an alternative URL to load the log4j xml configuration from
sslContext - an initiated SSLContext defining the characteristics of the HTTPS certificate


Copyright © 2009 StreamHub. Tutorials, news and guides can be found on the StreamHub Comet Blog.