public Server build() {
Server server = new Server();
// set up HTTP connector
if (this.httpPort != null) {
HttpConfiguration httpConfig = new HttpConfiguration();
if (this.httpsPort != null) {
httpConfig.setSecureScheme("https");
// set redirect port for those pages requiring confidential
// (SSL) access
httpConfig.setSecurePort(this.httpsPort);
}
httpConfig.setOutputBufferSize(32768);
ServerConnector http = new ServerConnector(server,
new HttpConnectionFactory(httpConfig));
http.setPort(this.httpPort);
server.addConnector(http);
}
// set up HTTPS connector with SSL key store (and optionally a trust
// store)
if (this.httpsPort != null) {
checkArgument(this.sslKeyStoreType != null,
"https configuration missing key store type is null");
checkArgument(this.sslKeyStorePath != null,
"https configuration missing SSL key store path");
checkArgument(this.sslKeyStorePassword != null,
"https configuration missing SSL key store password");
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStoreType(this.sslKeyStoreType.name());
sslContextFactory.setKeyStorePath(this.sslKeyStorePath);
sslContextFactory.setKeyStorePassword(this.sslKeyStorePassword);
if (this.sslKeyPassword != null) {
sslContextFactory.setKeyManagerPassword(this.sslKeyPassword);
} else {
sslContextFactory
.setKeyManagerPassword(this.sslKeyStorePassword);
}
if (this.sslTrustStorePath != null) {
checkArgument(this.sslTrustStoreType != null,
"missing trust store type for trust store");
checkArgument(this.sslTrustStorePassword != null,
"missing password for trust store");
sslContextFactory.setTrustStoreType(this.sslTrustStoreType
.name());
sslContextFactory.setTrustStorePath(this.sslTrustStorePath);
sslContextFactory
.setTrustStorePassword(this.sslTrustStorePassword);
}
if (this.sslRequireClientCert) {
checkArgument(this.sslTrustStorePath != null,
"Client certificate authentication specified without "
+ "specifying a trust store");
checkArgument(this.sslTrustStorePassword != null,
"Client certificate authentication specified without "
+ "specifying a trust store password");
}
// if true: requires client to authenticate with certificate
sslContextFactory.setNeedClientAuth(this.sslRequireClientCert);
// if true: authenticates client certificate if provided
sslContextFactory.setWantClientAuth(false);
// HTTPS config
HttpConfiguration httpsConfig = new HttpConfiguration();
httpsConfig.addCustomizer(new SecureRequestCustomizer());
httpsConfig.setOutputBufferSize(32768);
// HTTPS connector
ServerConnector https = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, "http/1.1"),
new HttpConnectionFactory(httpsConfig));
https.setPort(this.httpsPort);