下面列出了怎么用org.eclipse.jetty.server.HttpConfiguration的API类实例代码及写法,或者点击链接到github查看源代码。
private static Server startServer(ContextHandlerCollection contexts, int port) throws Exception {
System.setProperty(MessagingPropertyKeys.PROPERTY_SERVLET_HOST_PATH, "http://localhost:" + port);
setBounceProxyUrl();
setDirectoriesUrl();
logger.info("HOST PATH: {}", System.getProperty(MessagingPropertyKeys.PROPERTY_SERVLET_HOST_PATH));
final Server jettyServer = new Server();
ServerConnector connector = new ServerConnector(jettyServer,
new HttpConnectionFactory(new HttpConfiguration()));
connector.setPort(port);
connector.setAcceptQueueSize(1);
jettyServer.setConnectors(new Connector[]{ connector });
jettyServer.setHandler(contexts);
jettyServer.start();
logger.trace("Started jetty server: {}", jettyServer.dump());
return jettyServer;
}
private ServerConnector createHttpChannelConnector(
Server server, HttpConfiguration httpConfig) {
ServerConnector conn = new ServerConnector(server,
conf.getInt(HTTP_ACCEPTOR_COUNT_KEY, HTTP_ACCEPTOR_COUNT_DEFAULT),
conf.getInt(HTTP_SELECTOR_COUNT_KEY, HTTP_SELECTOR_COUNT_DEFAULT));
ConnectionFactory connFactory = new HttpConnectionFactory(httpConfig);
conn.addConnectionFactory(connFactory);
if(Shell.WINDOWS) {
// result of setting the SO_REUSEADDR flag is different on Windows
// http://msdn.microsoft.com/en-us/library/ms740621(v=vs.85).aspx
// without this 2 NN's can start on the same machine and listen on
// the same port with indeterminate routing of incoming requests to them
conn.setReuseAddress(false);
}
return conn;
}
@Override
public void startComponent() {
// Initialize servlet using RESTEasy and it's Guice bridge.
// The listener must be injected by the same Guice module which also binds the REST endpoints.
ServletContextHandler servletHandler = new ServletContextHandler();
servletHandler.addEventListener(listener);
servletHandler.addServlet(HttpServletDispatcher.class, "/*");
// Configure Jetty: Remove 'server' header from response and set listen port.
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSendServerVersion(false);
ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
connector.setPort(port);
// Starting up Jetty to serve the REST API.
server.addConnector(connector);
server.setHandler(servletHandler);
if (!LambdaUtils.tryTo(server::start, ex -> logger.error(ex, "Failed to start REST API."))) {
throw new IllegalStateException("Failed to start REST API.");
}
}
private ServerConnector createHttpChannelConnector(
Server server, HttpConfiguration httpConfig) {
ServerConnector conn = new ServerConnector(server,
conf.getInt(HTTP_ACCEPTOR_COUNT_KEY, HTTP_ACCEPTOR_COUNT_DEFAULT),
conf.getInt(HTTP_SELECTOR_COUNT_KEY, HTTP_SELECTOR_COUNT_DEFAULT));
ConnectionFactory connFactory = new HttpConnectionFactory(httpConfig);
conn.addConnectionFactory(connFactory);
if(Shell.WINDOWS) {
// result of setting the SO_REUSEADDR flag is different on Windows
// http://msdn.microsoft.com/en-us/library/ms740621(v=vs.85).aspx
// without this 2 NN's can start on the same machine and listen on
// the same port with indeterminate routing of incoming requests to them
conn.setReuseAddress(false);
}
return conn;
}
public static void main(String[] args) throws Exception
{
// jetty config
Properties properties = new Properties();
properties.load(CrudApiHandler.class.getClassLoader().getResourceAsStream("jetty.properties"));
HttpConfiguration config = new HttpConfiguration();
config.setSendServerVersion( false );
HttpConnectionFactory factory = new HttpConnectionFactory( config );
Server server = new Server();
ServerConnector connector = new ServerConnector(server,factory);
server.setConnectors( new Connector[] { connector } );
connector.setHost(properties.getProperty("host"));
connector.setPort(Integer.parseInt(properties.getProperty("port")));
server.addConnector(connector);
server.setHandler(new CrudApiHandler());
server.start();
server.join();
}
public void start(){
try{
jettyServer=new Server(port);
HttpConfiguration httpConfiguration = new HttpConfiguration();
ConnectionFactory c = new HttpConnectionFactory(httpConfiguration);
ServerConnector serverConnector = new ServerConnector(jettyServer, c);
serverConnector.setPort(port);
jettyServer.setConnectors(new Connector[] { serverConnector });
jettyServer.setHandler(requestHandler);
jettyServer.start();
if(statusButton!=null){
updateStatusIcon();
iconThread=new IconThread();
iconThread.start();
}
}
catch(Exception e){
wandora.handleError(e);
}
}
private ServerConnector createHttpChannelConnector(
Server server, HttpConfiguration httpConfig) {
ServerConnector conn = new ServerConnector(server,
conf.getInt(HTTP_ACCEPTOR_COUNT_KEY, HTTP_ACCEPTOR_COUNT_DEFAULT),
conf.getInt(HTTP_SELECTOR_COUNT_KEY, HTTP_SELECTOR_COUNT_DEFAULT));
ConnectionFactory connFactory = new HttpConnectionFactory(httpConfig);
conn.addConnectionFactory(connFactory);
if(Shell.WINDOWS) {
// result of setting the SO_REUSEADDR flag is different on Windows
// http://msdn.microsoft.com/en-us/library/ms740621(v=vs.85).aspx
// without this 2 NN's can start on the same machine and listen on
// the same port with indeterminate routing of incoming requests to them
conn.setReuseAddress(false);
}
return conn;
}
/**
* Sets the request and header buffer sizex if they are not zero
*/
private void setBufferSizes(HttpConfiguration buffers) {
if (requestHeaderSize > 0) {
LOGGER.info("Request header size set to {} for {}", requestHeaderSize, buffers.getClass().getCanonicalName());
buffers.setRequestHeaderSize(requestHeaderSize);
}
if (responseBufferSize > 0) {
LOGGER.info("Response buffer size set to {} for {}", responseBufferSize, buffers.getClass().getCanonicalName());
buffers.setOutputBufferSize(responseBufferSize);
}
if (responseHeaderSize > 0) {
LOGGER.info("Response header size set to {} for {}", responseHeaderSize, buffers.getClass().getCanonicalName());
buffers.setResponseHeaderSize(responseHeaderSize);
}
}
public HttpConfiguration newHttpConfiguration() {
// HTTP Configuration
boolean sendServerVersion = Boolean.parseBoolean(
System.getProperty(AthenzConsts.ATHENZ_PROP_SEND_SERVER_VERSION, "false"));
boolean sendDateHeader = Boolean.parseBoolean(
System.getProperty(AthenzConsts.ATHENZ_PROP_SEND_DATE_HEADER, "false"));
int outputBufferSize = Integer.parseInt(
System.getProperty(AthenzConsts.ATHENZ_PROP_OUTPUT_BUFFER_SIZE, "32768"));
int requestHeaderSize = Integer.parseInt(
System.getProperty(AthenzConsts.ATHENZ_PROP_REQUEST_HEADER_SIZE, "8192"));
int responseHeaderSize = Integer.parseInt(
System.getProperty(AthenzConsts.ATHENZ_PROP_RESPONSE_HEADER_SIZE, "8192"));
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setOutputBufferSize(outputBufferSize);
httpConfig.setRequestHeaderSize(requestHeaderSize);
httpConfig.setResponseHeaderSize(responseHeaderSize);
httpConfig.setSendServerVersion(sendServerVersion);
httpConfig.setSendDateHeader(sendDateHeader);
return httpConfig;
}
@Test
public void testHttpConfigurationValidHttpsPort() {
AthenzJettyContainer container = new AthenzJettyContainer();
container.createServer(100);
System.setProperty(AthenzConsts.ATHENZ_PROP_SEND_SERVER_VERSION, "true");
System.setProperty(AthenzConsts.ATHENZ_PROP_SEND_DATE_HEADER, "false");
System.setProperty(AthenzConsts.ATHENZ_PROP_OUTPUT_BUFFER_SIZE, "128");
System.setProperty(AthenzConsts.ATHENZ_PROP_REQUEST_HEADER_SIZE, "256");
System.setProperty(AthenzConsts.ATHENZ_PROP_RESPONSE_HEADER_SIZE, "512");
HttpConfiguration httpConfig = container.newHttpConfiguration();
assertNotNull(httpConfig);
assertEquals(httpConfig.getOutputBufferSize(), 128);
assertFalse(httpConfig.getSendDateHeader());
assertTrue(httpConfig.getSendServerVersion());
assertEquals(httpConfig.getRequestHeaderSize(), 256);
assertEquals(httpConfig.getResponseHeaderSize(), 512);
// it defaults to https even if we have no value specified
assertEquals(httpConfig.getSecureScheme(), "https");
}
private void configureConnectors(final Server server) throws ServerConfigurationException {
// create the http configuration
final HttpConfiguration httpConfiguration = new HttpConfiguration();
final int headerSize = DataUnit.parseDataSize(props.getWebMaxHeaderSize(), DataUnit.B).intValue();
httpConfiguration.setRequestHeaderSize(headerSize);
httpConfiguration.setResponseHeaderSize(headerSize);
httpConfiguration.setSendServerVersion(props.shouldSendServerVersion());
// Check if both HTTP and HTTPS connectors are configured and fail if both are configured
if (bothHttpAndHttpsConnectorsConfigured(props)) {
logger.error("NiFi only supports one mode of HTTP or HTTPS operation, not both simultaneously. " +
"Check the nifi.properties file and ensure that either the HTTP hostname and port or the HTTPS hostname and port are empty");
startUpFailure(new IllegalStateException("Only one of the HTTP and HTTPS connectors can be configured at one time"));
}
if (props.getSslPort() != null) {
configureHttpsConnector(server, httpConfiguration);
} else if (props.getPort() != null) {
configureHttpConnector(server, httpConfiguration);
} else {
logger.error("Neither the HTTP nor HTTPS connector was configured in nifi.properties");
startUpFailure(new IllegalStateException("Must configure HTTP or HTTPS connector"));
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
Server server = new Server();
HttpConfiguration config = new HttpConfiguration();
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(config));
int port = 8080;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
}
http.setPort(port);
server.addConnector(http);
ProtectionDomain domain = Main.class.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(location.toExternalForm());
server.setHandler(webapp);
server.start();
server.join();
}
@Test
public void testHttpConnectorsHttpsOnly() {
System.setProperty(AthenzConsts.ATHENZ_PROP_KEYSTORE_PATH, "file:///tmp/keystore");
System.setProperty(AthenzConsts.ATHENZ_PROP_KEYSTORE_TYPE, "PKCS12");
System.setProperty(AthenzConsts.ATHENZ_PROP_KEYSTORE_PASSWORD, "pass123");
System.setProperty(AthenzConsts.ATHENZ_PROP_TRUSTSTORE_PATH, "file:///tmp/truststore");
System.setProperty(AthenzConsts.ATHENZ_PROP_TRUSTSTORE_TYPE, "PKCS12");
System.setProperty(AthenzConsts.ATHENZ_PROP_TRUSTSTORE_PASSWORD, "pass123");
System.setProperty(AthenzConsts.ATHENZ_PROP_KEYMANAGER_PASSWORD, "pass123");
System.setProperty(AthenzConsts.ATHENZ_PROP_IDLE_TIMEOUT, "10001");
AthenzJettyContainer container = new AthenzJettyContainer();
container.createServer(100);
HttpConfiguration httpConfig = container.newHttpConfiguration();
container.addHTTPConnectors(httpConfig, 0, 8082, 0);
Server server = container.getServer();
Connector[] connectors = server.getConnectors();
assertEquals(connectors.length, 1);
assertTrue(connectors[0].getProtocols().contains("http/1.1"));
assertTrue(connectors[0].getProtocols().contains("ssl"));
}
/**
* Verifies all the needed bits are present in Jetty XML configuration (as HTTPS must be enabled by users).
*/
private void verifyConfiguration(final HttpScheme httpScheme) {
try {
if (HttpScheme.HTTP == httpScheme) {
bean(HTTP_CONFIG_ID, HttpConfiguration.class);
bean(HTTP_CONNECTOR_ID, ServerConnector.class);
}
else if (HttpScheme.HTTPS == httpScheme) {
bean(SSL_CONTEXT_FACTORY_ID, SslContextFactory.class);
bean(HTTPS_CONFIG_ID, HttpConfiguration.class);
bean(HTTPS_CONNECTOR_ID, ServerConnector.class);
}
else {
throw new UnsupportedHttpSchemeException(httpScheme);
}
}
catch (IllegalStateException e) {
throw new IllegalStateException("Jetty HTTPS is not enabled in Nexus", e);
}
}
public ServerConnector setup(final Server server, final InetSocketAddress address) {
final HttpConfiguration config = this.config.build();
final ConnectionFactory[] factories = this.factories
.stream()
.map(f -> f.setup(config))
.toArray(size -> new ConnectionFactory[size]);
final ServerConnector c = new ServerConnector(server, factories);
c.setHost(this.address.map(a -> a.getHostString()).orElseGet(address::getHostString));
c.setPort(this.address.map(a -> a.getPort()).orElseGet(address::getPort));
defaultProtocol.ifPresent(c::setDefaultProtocol);
return c;
}
@BeforeClass
public static void startHttpsServer() throws Exception {
skipIfHeadlessEnvironment();
server = new Server();
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(ErrorCases.class.getResource("keystore").getPath());
sslContextFactory.setKeyStorePassword("activeeon");
HttpConfiguration httpConfig = new HttpConfiguration();
HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
httpsConfig.addCustomizer(new SecureRequestCustomizer());
ServerConnector sslConnector = new ServerConnector(server,
new ConnectionFactory[] { new SslConnectionFactory(sslContextFactory,
HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(httpsConfig) });
server.addConnector(sslConnector);
server.start();
serverUrl = "https://localhost:" + sslConnector.getLocalPort() + "/rest";
}
private ServerConnector createSSLConnector() {
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(m_keystore);
sslContextFactory.setKeyStorePassword(m_keystorepassword);
sslContextFactory.setTrustStorePath(m_truststore);
sslContextFactory.setTrustStorePassword(m_truststorepassword);
sslContextFactory.setNeedClientAuth(m_clientauthentication);
sslContextFactory.setIncludeCipherSuites(m_tls_cipher_suites);
HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer());
SslConnectionFactory sslConnFactory = new SslConnectionFactory(sslContextFactory, "http/1.1");
HttpConnectionFactory httpConnFactory = new HttpConnectionFactory(https_config);
ServerConnector sslConnector = new ServerConnector(m_jettyServer, sslConnFactory, httpConnFactory);
return sslConnector;
}
@Override
public void start() {
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSecureScheme("https");
httpConfig.setSecurePort(securePort);
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
http.setPort(port);
http.setIdleTimeout(500000);
Path keystore = getKeyStore();
if (!Files.exists(keystore)) {
throw new RuntimeException(
"Cannot find keystore for SSL cert: " + keystore.toAbsolutePath());
}
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keystore.toAbsolutePath().toString());
sslContextFactory.setKeyStorePassword("password");
sslContextFactory.setKeyManagerPassword("password");
HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
httpsConfig.addCustomizer(new SecureRequestCustomizer());
ServerConnector https = new ServerConnector(
server,
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(httpsConfig));
https.setPort(securePort);
https.setIdleTimeout(500000);
server.setConnectors(new Connector[]{http, https});
try {
server.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private ServerConnector createHttpsChannelConnector(
Server server, HttpConfiguration httpConfig) {
httpConfig.setSecureScheme(HTTPS_SCHEME);
httpConfig.addCustomizer(new SecureRequestCustomizer());
ServerConnector conn = createHttpChannelConnector(server, httpConfig);
SslContextFactory.Server sslContextFactory =
new SslContextFactory.Server();
sslContextFactory.setNeedClientAuth(needsClientAuth);
if (keyPassword != null) {
sslContextFactory.setKeyManagerPassword(keyPassword);
}
if (keyStore != null) {
sslContextFactory.setKeyStorePath(keyStore);
sslContextFactory.setKeyStoreType(keyStoreType);
if (keyStorePassword != null) {
sslContextFactory.setKeyStorePassword(keyStorePassword);
}
}
if (trustStore != null) {
sslContextFactory.setTrustStorePath(trustStore);
sslContextFactory.setTrustStoreType(trustStoreType);
if (trustStorePassword != null) {
sslContextFactory.setTrustStorePassword(trustStorePassword);
}
}
if (null != excludeCiphers && !excludeCiphers.isEmpty()) {
sslContextFactory.setExcludeCipherSuites(
StringUtils.getTrimmedStrings(excludeCiphers));
LOG.info("Excluded Cipher List: {}", excludeCiphers);
}
conn.addFirstConnectionFactory(new SslConnectionFactory(sslContextFactory,
HttpVersion.HTTP_1_1.asString()));
return conn;
}
ArmeriaConnector(Server server, com.linecorp.armeria.server.Server armeriaServer) {
super(server, -1, -1, new ArmeriaConnectionFactory());
this.armeriaServer = armeriaServer;
final HttpConfiguration httpConfig = server.getBean(HttpConfiguration.class);
this.httpConfig = httpConfig != null ? httpConfig : new HttpConfiguration();
addBean(this.httpConfig);
setDefaultProtocol(PROTOCOL_NAME);
}
public void init() {
// connector configuration
SslContextFactory sslContextFactory = new SslContextFactory();
if (null != keyStorePath) {
sslContextFactory.setKeyStorePath(keyStorePath);
}
if (null != keyStorePassword) {
sslContextFactory.setKeyStorePassword(keyStorePassword);
}
if (null != keyManagerPassword) {
sslContextFactory.setKeyManagerPassword(keyManagerPassword);
}
SslConnectionFactory sslConnectionFactory =
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
HttpConnectionFactory httpConnectionFactory =
new HttpConnectionFactory(new HttpConfiguration());
ServerConnector sslConnector =
new ServerConnector(server, sslConnectionFactory, httpConnectionFactory);
sslConnector.setHost(host);
sslConnector.setPort(port);
server.addConnector(sslConnector);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
ServletHolder servletHolder = new ServletHolder(new ExchangeServlet(sessions, ringBuffer));
context.addServlet(servletHolder, "/trade/*");
// context.addServlet(DefaultServlet.class, "/");
server.setHandler(context);
}
@BeforeEach
void setUp() throws Exception {
server = new Server();
Path keyStorePath = Paths.get(ReporterFactoryTest.class.getResource("/keystore").toURI());
final SslContextFactory sslContextFactory = new SslContextFactory(keyStorePath.toAbsolutePath().toString());
sslContextFactory.setKeyStorePassword("password");
sslContextFactory.getSslContext();
final HttpConfiguration httpConfiguration = new HttpConfiguration();
httpConfiguration.setSecureScheme("https");
httpConfiguration.setSecurePort(0);
final HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration);
httpsConfiguration.addCustomizer(new SecureRequestCustomizer());
final ServerConnector httpsConnector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(httpsConfiguration));
httpsConnector.setPort(0);
server.addConnector(httpsConnector);
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) {
baseRequest.setHandled(true);
requestHandled.set(true);
}
});
server.start();
configuration = SpyConfiguration.createSpyConfig();
reporterConfiguration = configuration.getConfig(ReporterConfiguration.class);
when(reporterConfiguration.getServerUrls()).thenReturn(Collections.singletonList(new URL("https://localhost:" + getPort())));
}
private ServerConnector createHttpsChannelConnector(
Server server, HttpConfiguration httpConfig) {
httpConfig.setSecureScheme(HTTPS_SCHEME);
httpConfig.addCustomizer(new SecureRequestCustomizer());
ServerConnector conn = createHttpChannelConnector(server, httpConfig);
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setNeedClientAuth(needsClientAuth);
sslContextFactory.setKeyManagerPassword(keyPassword);
if (keyStore != null) {
sslContextFactory.setKeyStorePath(keyStore);
sslContextFactory.setKeyStoreType(keyStoreType);
sslContextFactory.setKeyStorePassword(keyStorePassword);
}
if (trustStore != null) {
sslContextFactory.setTrustStorePath(trustStore);
sslContextFactory.setTrustStoreType(trustStoreType);
sslContextFactory.setTrustStorePassword(trustStorePassword);
}
if(null != excludeCiphers && !excludeCiphers.isEmpty()) {
sslContextFactory.setExcludeCipherSuites(
StringUtils.getTrimmedStrings(excludeCiphers));
LOG.info("Excluded Cipher List:" + excludeCiphers);
}
conn.addFirstConnectionFactory(new SslConnectionFactory(sslContextFactory,
HttpVersion.HTTP_1_1.asString()));
return conn;
}
private ServerConnector createHttpsChannelConnector(
Server server, HttpConfiguration httpConfig) {
httpConfig.setSecureScheme(HTTPS_SCHEME);
httpConfig.addCustomizer(new SecureRequestCustomizer());
ServerConnector conn = createHttpChannelConnector(server, httpConfig);
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setNeedClientAuth(needsClientAuth);
sslContextFactory.setKeyManagerPassword(keyPassword);
if (keyStore != null) {
sslContextFactory.setKeyStorePath(keyStore);
sslContextFactory.setKeyStoreType(keyStoreType);
sslContextFactory.setKeyStorePassword(keyStorePassword);
}
if (trustStore != null) {
sslContextFactory.setTrustStorePath(trustStore);
sslContextFactory.setTrustStoreType(trustStoreType);
sslContextFactory.setTrustStorePassword(trustStorePassword);
}
if(null != excludeCiphers && !excludeCiphers.isEmpty()) {
sslContextFactory.setExcludeCipherSuites(
StringUtils.getTrimmedStrings(excludeCiphers));
LOG.info("Excluded Cipher List:" + excludeCiphers);
}
conn.addFirstConnectionFactory(new SslConnectionFactory(sslContextFactory,
HttpVersion.HTTP_1_1.asString()));
return conn;
}
/**
* Configures an HTTPS connector and adds it to the server.
*
* @param server the Jetty server instance
* @param httpConfiguration the configuration object for the HTTPS protocol settings
*/
private void configureHttpsConnector(Server server, HttpConfiguration httpConfiguration) {
String hostname = props.getProperty(NiFiProperties.WEB_HTTPS_HOST);
final Integer port = props.getSslPort();
String connectorLabel = "HTTPS";
final Map<String, String> httpsNetworkInterfaces = props.getHttpsNetworkInterfaces();
ServerConnectorCreator<Server, HttpConfiguration, ServerConnector> scc = (s, c) -> createUnconfiguredSslServerConnector(s, c, port);
configureGenericConnector(server, httpConfiguration, hostname, port, connectorLabel, httpsNetworkInterfaces, scc);
}
protected Connector getConnector(String host, int port) throws IOException {
HttpConfiguration http_config = new HttpConfiguration();
// this is to enable large header sizes when Kerberos is enabled with AD
final int bufferSize = AtlasConfiguration.WEBSERVER_REQUEST_BUFFER_SIZE.getInt();;
http_config.setResponseHeaderSize(bufferSize);
http_config.setRequestHeaderSize(bufferSize);
ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(http_config));
connector.setPort(port);
connector.setHost(host);
return connector;
}
protected static void addHttpConnector(Server server, Integer port) throws Exception {
HttpConfiguration config = new HttpConfiguration();
config.setOutputBufferSize(32768);
config.setRequestHeaderSize(8192 * 2);
config.setResponseHeaderSize(8192 * 2);
config.setSendServerVersion(true);
config.setSendDateHeader(false);
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(config));
http.setIdleTimeout(30000);
http.setPort(port);
server.addConnector(http);
}
/**
* Configures an HTTP connector and adds it to the server.
*
* @param server the Jetty server instance
* @param httpConfiguration the configuration object for the HTTP protocol settings
*/
private void configureHttpConnector(Server server, HttpConfiguration httpConfiguration) {
String hostname = props.getProperty(NiFiProperties.WEB_HTTP_HOST);
final Integer port = props.getPort();
String connectorLabel = "HTTP";
final Map<String, String> httpNetworkInterfaces = props.getHttpNetworkInterfaces();
ServerConnectorCreator<Server, HttpConfiguration, ServerConnector> scc = (s, c) -> new ServerConnector(s, new HttpConnectionFactory(c));
configureGenericConnector(server, httpConfiguration, hostname, port, connectorLabel, httpNetworkInterfaces, scc);
}
public HttpsServer start(int httpsPort, String httpsHostname) {
server.setHandler(
new AbstractHandler() {
@Override
public void handle(String path,
Request request,
HttpServletRequest httpRequest,
HttpServletResponse response) throws IOException {
response.setHeader(CONTENT_TYPE, responseContentType);
try (PrintWriter writer = response.getWriter()) {
writer.println(responseContent);
}
}
});
// SSL HTTP Configuration
HttpConfiguration httpsConfig = new HttpConfiguration(); //httpConfig);
httpsConfig.setSecureScheme("https");
httpsConfig.setSecurePort(httpsPort);
ServerConnector sslConnector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(httpsConfig)
);
sslConnector.setPort(httpsPort);
sslConnector.setHost(httpsHostname);
server.addConnector(sslConnector);
try {
server.start();
}
catch (Exception e) {
throw new RuntimeException("Failed to start https server", e);
}
return this;
}
@VisibleForTesting
HttpConfiguration configureForwardRequestCustomizer(HttpConfiguration httpConf) {
if (conf.get(HTTP_ENABLE_FORWARDED_REQUESTS_KEY, HTTP_ENABLE_FORWARDED_REQUESTS_DEFAULT)) {
httpConf.addCustomizer(new ForwardedRequestCustomizer());
}
return httpConf;
}