Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Add createHttpServer methods with a PortRange parameter #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.glassfish.grizzly.http.server.ServerConfiguration;
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
import org.glassfish.grizzly.utils.Charsets;
import org.glassfish.grizzly.PortRange;

import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;

Expand Down Expand Up @@ -92,6 +93,20 @@ public static HttpServer createHttpServer(final URI uri) {
return createHttpServer(uri, (GrizzlyHttpContainer) null, false, null, true);
}

/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest as well as the port will be ignored.
* @param portRange port range on which the {@link ApplicationHandler} will be bound. One available port will be
* randomly chosen between this port range.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri, final PortRange portRange) {
return createHttpServer(uri, portRange, (GrizzlyHttpContainer) null, false, null, true);
}

/**
* Create new {@link HttpServer} instance.
*
Expand All @@ -106,6 +121,22 @@ public static HttpServer createHttpServer(final URI uri, final boolean start) {
return createHttpServer(uri, (GrizzlyHttpContainer) null, false, null, start);
}

/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest as well as the port will be ignored.
* @param portRange port range on which the {@link ApplicationHandler} will be bound. One available port will be
* randomly chosen between this port range.
* @param start if set to false, server will not get started, which allows to configure the underlying transport
* layer, see above for details.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri, final PortRange portRange, final boolean start) {
return createHttpServer(uri, portRange, (GrizzlyHttpContainer) null, false, null, start);
}

/**
* Create new {@link HttpServer} instance.
*
Expand All @@ -125,6 +156,29 @@ public static HttpServer createHttpServer(final URI uri, final ResourceConfig co
);
}

/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest as well as the port will be ignored.
* @param portRange port range on which the {@link ApplicationHandler} will be bound. One available port will be
* randomly chosen between this port range.
* @param configuration web application configuration.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri, final PortRange portRange,
final ResourceConfig configuration) {
return createHttpServer(
uri,
portRange,
new GrizzlyHttpContainer(configuration),
false,
null,
true
);
}

/**
* Create new {@link HttpServer} instance.
*
Expand All @@ -145,6 +199,30 @@ public static HttpServer createHttpServer(final URI uri, final ResourceConfig co
start);
}

/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest as well as the port will be ignored.
* @param portRange port range on which the {@link ApplicationHandler} will be bound. One available port will be
* randomly chosen between this port range.
* @param configuration web application configuration.
* @param start if set to false, server will not get started, which allows to configure the underlying
* transport layer, see above for details.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri, final PortRange portRange,
final ResourceConfig configuration, final boolean start) {
return createHttpServer(
uri,
portRange,
new GrizzlyHttpContainer(configuration),
false,
null,
start);
}

/**
* Create new {@link HttpServer} instance.
*
Expand All @@ -168,6 +246,33 @@ public static HttpServer createHttpServer(final URI uri,
true);
}

/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest as well as the port will be ignored.
* @param portRange port range on which the {@link ApplicationHandler} will be bound. One available port will be
* randomly chosen between this port range.
* @param configuration web application configuration.
* @param secure used for call {@link NetworkListener#setSecure(boolean)}.
* @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri,
final PortRange portRange,
final ResourceConfig configuration,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator) {
return createHttpServer(
uri,
portRange,
new GrizzlyHttpContainer(configuration),
secure,
sslEngineConfigurator,
true);
}

/**
* Create new {@link HttpServer} instance.
*
Expand All @@ -194,6 +299,36 @@ public static HttpServer createHttpServer(final URI uri,
start);
}

/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest as well as the port will be ignored.
* @param portRange port range on which the {@link ApplicationHandler} will be bound. One available port will be
* randomly chosen between this port range.
* @param configuration web application configuration.
* @param secure used for call {@link NetworkListener#setSecure(boolean)}.
* @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
* @param start if set to false, server will not get started, which allows to configure the
* underlying transport, see above for details.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri,
final PortRange portRange,
final ResourceConfig configuration,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator,
final boolean start) {
return createHttpServer(
uri,
portRange,
new GrizzlyHttpContainer(configuration),
secure,
sslEngineConfigurator,
start);
}

/**
* Create new {@link HttpServer} instance.
*
Expand All @@ -218,6 +353,34 @@ public static HttpServer createHttpServer(final URI uri,
return createHttpServer(uri, new GrizzlyHttpContainer(config, parentLocator), secure, sslEngineConfigurator, true);
}

/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest as well as the port will be ignored.
* @param portRange port range on which the {@link ApplicationHandler} will be bound. One available port will be
* randomly chosen between this port range.
* @param config web application configuration.
* @param secure used for call {@link NetworkListener#setSecure(boolean)}.
* @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
* @param parentLocator {@link org.glassfish.hk2.api.ServiceLocator} to become a parent of the locator used by
* {@link org.glassfish.jersey.server.ApplicationHandler}
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
* @see GrizzlyHttpContainer
* @see org.glassfish.hk2.api.ServiceLocator
* @since 2.3.29
*/
public static HttpServer createHttpServer(final URI uri,
final PortRange portRange,
final ResourceConfig config,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator,
final ServiceLocator parentLocator) {
return createHttpServer(uri, portRange, new GrizzlyHttpContainer(config, parentLocator), secure,
sslEngineConfigurator, true);
}

/**
* Create new {@link HttpServer} instance.
*
Expand All @@ -238,6 +401,29 @@ public static HttpServer createHttpServer(final URI uri,
return createHttpServer(uri, new GrizzlyHttpContainer(config, parentLocator), false, null, true);
}

/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest as well as the port will be ignored.
* @param portRange port range on which the {@link ApplicationHandler} will be bound. One available port will be
* randomly chosen between this port range.
* @param config web application configuration.
* @param parentLocator {@link org.glassfish.hk2.api.ServiceLocator} to become a parent of the locator used by
* {@link org.glassfish.jersey.server.ApplicationHandler}
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
* @see GrizzlyHttpContainer
* @see org.glassfish.hk2.api.ServiceLocator
* @since 2.12
*/
public static HttpServer createHttpServer(final URI uri,
final PortRange portRange,
final ResourceConfig config,
final ServiceLocator parentLocator) {
return createHttpServer(uri, portRange, new GrizzlyHttpContainer(config, parentLocator), false, null, true);
}

/**
* Create new {@link HttpServer} instance.
*
Expand All @@ -258,12 +444,45 @@ public static HttpServer createHttpServer(final URI uri,
final SSLEngineConfigurator sslEngineConfigurator,
final boolean start) {

return createHttpServer(uri, null, handler, secure, sslEngineConfigurator, start);
}

/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest as well as the port will be ignored.
* @param portRange port range on which the {@link ApplicationHandler} will be bound. One available port will be
* randomly chosen between this port range.
* @param handler {@link HttpHandler} instance.
* @param secure used for call {@link NetworkListener#setSecure(boolean)}.
* @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
* @param start if set to false, server will not get started, this allows end users to set
* additional properties on the underlying listener.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
* @see GrizzlyHttpContainer
*/
public static HttpServer createHttpServer(final URI uri,
final PortRange portRange,
final GrizzlyHttpContainer handler,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator,
final boolean start) {

final String host = (uri.getHost() == null) ? NetworkListener.DEFAULT_NETWORK_HOST : uri.getHost();
final int port = (uri.getPort() == -1)
? (secure ? Container.DEFAULT_HTTPS_PORT : Container.DEFAULT_HTTP_PORT)
: uri.getPort();

final NetworkListener listener = new NetworkListener("grizzly", host, port);
final NetworkListener listener;

if (portRange != null) {
listener = new NetworkListener("grizzly", host, portRange);
} else {
final int port = (uri.getPort() == -1)
? (secure ? Container.DEFAULT_HTTPS_PORT : Container.DEFAULT_HTTP_PORT)
: uri.getPort();

listener = new NetworkListener("grizzly", host, port);
}

listener.getTransport().getWorkerThreadPoolConfig().setThreadFactory(new ThreadFactoryBuilder()
.setNameFormat("grizzly-http-server-%d")
Expand Down