Skip to content

Commit

Permalink
HTTP/2 for Jetty container
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam committed Mar 27, 2023
1 parent f5d7b81 commit 405907b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,25 @@ public static Server createServer(final URI uri,
return createGeneralHttpServer(uri, sslContextFactory, handler, start, "http/1.1");
}

/**
* Creates HTTP/2 enabled {@link Server} instance that registers an {@link org.eclipse.jetty.server.Handler}.
*
* @param uri uri on which the {@link org.glassfish.jersey.server.ApplicationHandler} will be deployed. Only first path
* segment will be used as context path, the rest will be ignored.
* @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 {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
*
* @since 2.40
*/

public static Server createHttp2Server(final URI uri, final boolean start) throws ProcessingException {
return createHttp2Server(uri, null, null, start);
}

/**
* Create HTTP/2 enabled {@link Server} that registers an {@link org.eclipse.jetty.server.Handler} that
* in turn manages all root resource and provider classes declared by the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public static Server create(String u)
return create(URI.create(u));
}

public static Server createHttp2(String u)
throws Exception {
if (u == null) {
throw new IllegalArgumentException("The URI must not be null");
}

return createHttp2(URI.create(u));
}

/**
* Create a {@link Server} that registers the {@link ServletContainer}.
*
Expand All @@ -91,6 +100,15 @@ public static Server create(String u, Map<String, String> initParams)
return create(URI.create(u), initParams);
}

public static Server createHttp2(String u, Map<String, String> initParams)
throws Exception {
if (u == null) {
throw new IllegalArgumentException("The URI must not be null");
}

return createHttp2(URI.create(u), initParams);
}

/**
* Create a {@link Server} that registers the {@link ServletContainer}.
*
Expand All @@ -108,6 +126,11 @@ public static Server create(URI u)
return create(u, ServletContainer.class);
}

public static Server createHttp2(URI u)
throws Exception {
return createHttp2(u, ServletContainer.class);
}

/**
* Create a {@link Server} that registers the {@link ServletContainer}.
*
Expand All @@ -126,6 +149,11 @@ public static Server create(URI u, Map<String, String> initParams)
return create(u, ServletContainer.class, initParams);
}

public static Server createHttp2(URI u, Map<String, String> initParams)
throws Exception {
return createHttp2(u, ServletContainer.class, initParams);
}

/**
* Create a {@link Server} that registers the declared
* servlet class.
Expand All @@ -149,6 +177,15 @@ public static Server create(String u, Class<? extends Servlet> c)
return create(URI.create(u), c);
}

public static Server createHttp2(String u, Class<? extends Servlet> c)
throws Exception {
if (u == null) {
throw new IllegalArgumentException("The URI must not be null");
}

return createHttp2(URI.create(u), c);
}

/**
* Create a {@link Server} that registers the declared
* servlet class.
Expand All @@ -174,6 +211,16 @@ public static Server create(String u, Class<? extends Servlet> c,
return create(URI.create(u), c, initParams);
}

public static Server createHttp2(String u, Class<? extends Servlet> c,
Map<String, String> initParams)
throws Exception {
if (u == null) {
throw new IllegalArgumentException("The URI must not be null");
}

return createHttp2(URI.create(u), c, initParams);
}

/**
* Create a {@link Server} that registers the declared
* servlet class.
Expand All @@ -193,6 +240,11 @@ public static Server create(URI u, Class<? extends Servlet> c)
return create(u, c, null);
}

public static Server createHttp2(URI u, Class<? extends Servlet> c)
throws Exception {
return createHttp2(u, c, null);
}

/**
* Create a {@link Server} that registers the declared
* servlet class.
Expand All @@ -210,11 +262,17 @@ public static Server create(URI u, Class<? extends Servlet> c)
*/
public static Server create(URI u, Class<? extends Servlet> c, Map<String, String> initParams)
throws Exception {
return create(u, c, null, initParams, null);
return create(u, c, null, initParams, null, "http/1.1");
}

public static Server createHttp2(URI u, Class<? extends Servlet> c, Map<String, String> initParams)
throws Exception {
return create(u, c, null, initParams, null, "h2");
}

private static Server create(URI u, Class<? extends Servlet> c, Servlet servlet,
Map<String, String> initParams, Map<String, String> contextInitParams)
Map<String, String> initParams, Map<String, String> contextInitParams,
String protocol)
throws Exception {
if (u == null) {
throw new IllegalArgumentException("The URI must not be null");
Expand Down Expand Up @@ -252,7 +310,8 @@ private static Server create(URI u, Class<? extends Servlet> c, Servlet servlet,
holder.setInitParameters(initParams);
}

Server server = JettyHttpContainerFactory.createServer(u, false);
final Server server = (protocol.equalsIgnoreCase("h2")) ? JettyHttpContainerFactory.createHttp2Server(u, false)
: JettyHttpContainerFactory.createServer(u, false);
server.setHandler(context);
server.start();
return server;
Expand All @@ -279,6 +338,14 @@ public static Server create(URI u, Servlet servlet, Map<String, String> initPara
if (servlet == null) {
throw new IllegalArgumentException("The servlet must not be null");
}
return create(u, null, servlet, initParams, contextInitParams);
return create(u, null, servlet, initParams, contextInitParams, "http/1.1");
}

public static Server createHttp2(URI u, Servlet servlet, Map<String, String> initParams, Map<String, String> contextInitParams)
throws Exception {
if (servlet == null) {
throw new IllegalArgumentException("The servlet must not be null");
}
return create(u, null, servlet, initParams, contextInitParams, "h2");
}
}

0 comments on commit 405907b

Please sign in to comment.