Skip to content

Commit

Permalink
Helidon 4.x container (II)
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
  • Loading branch information
senivam committed Jun 27, 2024
1 parent 961c784 commit aa527d4
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 98 deletions.
5 changes: 5 additions & 0 deletions containers/helidon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,18 @@

public class HelidonHttpContainer implements Container, WebServer {

private static final int DEFAULT_PORT = 8080;
private static final String DEFAULT_HOST = "0.0.0.0";

private final WebServer webServer;
private WebServer webServer;

private ApplicationHandler applicationHandler;

HelidonHttpContainer(URI baseUri, Application application) {
this.applicationHandler = new ApplicationHandler(application, new WebServerBinder());
int port = baseUri == null ? DEFAULT_PORT : baseUri.getPort();
final String host = baseUri == null ? DEFAULT_HOST : baseUri.getHost();
final String path = baseUri == null ? null : baseUri.getPath();
final HttpRouting.Builder routingBuilder = path == null ? HttpRouting.builder().register(
JerseySupport.create(this)
) : HttpRouting.builder().register(path,
JerseySupport.create(this)
);
this.webServer = WebServer.builder()
.port(port)
.host(host)
.routing(routingBuilder).build();
private HelidonJerseyBridge bridge;

HelidonHttpContainer(Application application, HelidonJerseyBridge bridge) {
this.applicationHandler = new ApplicationHandler(application, new WebServerBinder(), bridge.getParentContext());
this.bridge = bridge;
webServer = bridge.getBuilder().build();
bridge.setContainer(this);
}

@Override
Expand All @@ -70,15 +61,11 @@ public void reload() {

@Override
public void reload(ResourceConfig configuration) {
applicationHandler.onShutdown(this);
// applicationHandler.onShutdown(this);

applicationHandler = new ApplicationHandler(configuration);
applicationHandler.onReload(this);
applicationHandler.onStartup(this);
if (webServer.isRunning()) {
webServer.stop();
webServer.start();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,180 @@

package org.glassfish.jersey.helidon;

import io.helidon.common.tls.Tls;
import io.helidon.common.tls.TlsConfig;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.WebServerConfig;
import io.helidon.webserver.http.HttpRouting;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.UriBuilder;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import java.net.URI;

/**
* Helidon container builder
*/
class HelidonHttpContainerBuilder {
public class HelidonHttpContainerBuilder {

private URI baseUri;

private Application application;

private String host;

private String path;

private int port;
private Tls tls;

private final WebServerConfig.Builder webServerBuilder;

private final HelidonJerseyBridge bridge;

private SSLParameters sslParameters;

private SSLContext sslContext;

private static final int DEFAULT_PORT = 8080;

private HelidonHttpContainerBuilder() {
bridge = new HelidonJerseyBridge();
webServerBuilder = bridge.getBuilder();
}

public WebServerConfig.Builder helidonBuilder() {
return webServerBuilder;
}


public static HelidonHttpContainerBuilder builder() {
return new HelidonHttpContainerBuilder();
}

public HelidonHttpContainerBuilder withUri(URI baseUri) {
public HelidonHttpContainerBuilder uri(URI baseUri) {
this.baseUri = baseUri;
return this;
}

public HelidonHttpContainerBuilder withApplication(Application application) {
public URI uri() {
return this.baseUri;
}

public HelidonHttpContainerBuilder application(Application application) {
this.application = application;
return this;
}

public HelidonHttpContainerBuilder withPort(int port) {
this.port = port;
public Application application() {
return this.application;
}

public HelidonHttpContainerBuilder path(String path) {
this.path = path;
return this;
}

public HelidonHttpContainerBuilder withHost(String host) {
this.host = host;
public String path() {
return this.path;
}

public HelidonHttpContainerBuilder sslParameters(SSLParameters sslParameters) {
this.sslParameters = sslParameters;
return this;
}

public HelidonHttpContainerBuilder withPath(String path) {
this.path = path;
public SSLParameters sslParameters() {
return this.sslParameters;
}

public HelidonHttpContainerBuilder sslContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}

public SSLContext sslContext() {
return this.sslContext;
}

public HelidonHttpContainerBuilder port(int port) {
webServerBuilder.port(port);
return this;
}

public HelidonHttpContainerBuilder host(String host) {
webServerBuilder.host(host);
return this;
}

public HelidonHttpContainerBuilder parentContext(Object parentContext) {
bridge.setParentContext(parentContext);
return this;
}

public Object parentContext() {
return bridge.getParentContext();
}

public HelidonHttpContainer build() {
if (this.baseUri == null) {
baseUri = UriBuilder.fromPath(path).port(port).host(host).build();
configureBaseUri();
webServerBuilder.routing(configureRouting());
this.tls = configureTls();
if (tls != null) {
webServerBuilder.tls(configureTls());
}
return new HelidonHttpContainer(baseUri, application);
return new HelidonHttpContainer(application, bridge);
}

private TlsConfig.Builder addSSLParameterss(TlsConfig.Builder builder) {
if (sslParameters != null) {
return builder.sslParameters(sslParameters);
}
return builder;
}

private TlsConfig.Builder addSSLContext(TlsConfig.Builder builder) {
if (sslContext != null) {
return builder.sslContext(sslContext);
}
return builder;
}

private Tls configureTls() {
if (this.tls == null
&& (sslParameters != null || sslContext != null)) {
this.tls = addSSLParameterss(
addSSLContext(
TlsConfig.builder())
).build();
}
return this.tls;
}

private HttpRouting.Builder configureRouting() {

final HttpRouting.Builder builder = HttpRouting.builder();
final JerseyRoutingService support = JerseyRoutingService.create(this.bridge);
if (path != null) {
builder.register(path, support);
} else if (baseUri != null && baseUri.getPath() != null) {
builder.register(baseUri.getPath(), support);
} else {
builder.register(support);
}

return builder;
}

private void configureBaseUri() {
if (baseUri != null) {
webServerBuilder
.host(baseUri.getHost())
.port(baseUri.getPort());
} else {
if (webServerBuilder.port() < 0) {
webServerBuilder.port(DEFAULT_PORT);
}

}

}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public <T> T createContainer(Class<T> type, Application application) throws Proc
if (type != WebServer.class && type != HelidonHttpContainer.class) {
return null;
}
return type.cast(new HelidonHttpContainer(null, application));
return type.cast(new HelidonHttpContainer(application, new HelidonJerseyBridge()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*
*/

package org.glassfish.jersey.helidon;

import io.helidon.webserver.WebServerConfig;
import org.glassfish.jersey.server.spi.Container;

class HelidonJerseyBridge {
private final WebServerConfig.Builder builder = WebServerConfig.builder();

private Container container;

private Object parentContext;

public WebServerConfig.Builder getBuilder() {
return builder;
}

public Container getContainer() {
return container;
}

public void setContainer(Container container) {
this.container = container;
}

public Object getParentContext() {
return parentContext;
}

public void setParentContext(Object parentContext) {
this.parentContext = parentContext;
}
}
Loading

0 comments on commit aa527d4

Please sign in to comment.