Skip to content

Commit 86bb8be

Browse files
committed
Helidon container example
Signed-off-by: Maxim Nesen <[email protected]>
1 parent f800907 commit 86bb8be

File tree

14 files changed

+630
-8
lines changed

14 files changed

+630
-8
lines changed

bom/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@
123123
<artifactId>jersey-container-jetty-http2</artifactId>
124124
<version>${project.version}</version>
125125
</dependency>
126+
<dependency>
127+
<groupId>org.glassfish.jersey.containers</groupId>
128+
<artifactId>jersey-container-helidon-http</artifactId>
129+
<version>${project.version}</version>
130+
</dependency>
126131
<dependency>
127132
<groupId>org.glassfish.jersey.containers</groupId>
128133
<artifactId>jersey-container-grizzly2-http</artifactId>
@@ -389,6 +394,11 @@
389394
<artifactId>jersey-test-framework-provider-external</artifactId>
390395
<version>${project.version}</version>
391396
</dependency>
397+
<dependency>
398+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
399+
<artifactId>jersey-test-framework-provider-helidon</artifactId>
400+
<version>${project.version}</version>
401+
</dependency>
392402
<dependency>
393403
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
394404
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>

containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpContainerBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private void configureBaseUri() {
199199
.host(baseUri.getHost())
200200
.port(baseUri.getPort());
201201
} else {
202-
if (webServerBuilder.port() < 0) {
202+
if (webServerBuilder.port() <= 0) {
203203
webServerBuilder.port(Container.DEFAULT_HTTP_PORT);
204204
}
205205

containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonHttpServer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import jakarta.ws.rs.core.Application;
2020
import org.glassfish.jersey.server.JerseySeBootstrapConfiguration;
21+
import org.glassfish.jersey.server.ResourceConfig;
2122
import org.glassfish.jersey.server.spi.Container;
2223
import org.glassfish.jersey.server.spi.WebServer;
2324

@@ -26,10 +27,14 @@
2627
import java.util.concurrent.CompletionException;
2728
import java.util.concurrent.CompletionStage;
2829

29-
final class HelidonHttpServer implements WebServer {
30+
public final class HelidonHttpServer implements WebServer {
3031

3132
private final HelidonHttpContainer helidonHttpContainer;
3233

34+
HelidonHttpServer(Application application) {
35+
this(HelidonHttpContainerBuilder.builder().application(application), JerseySeBootstrapConfiguration.builder().build());
36+
}
37+
3338
HelidonHttpServer(Application application, JerseySeBootstrapConfiguration seBootstrapConfig) {
3439
this(HelidonHttpContainerBuilder.builder().application(application), seBootstrapConfig);
3540
}

containers/helidon/src/main/java/org/glassfish/jersey/helidon/HelidonJerseyRoutingService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.helidon.http.HeaderNames;
2626
import io.helidon.http.HeaderValues;
2727
import io.helidon.http.InternalServerException;
28+
import io.helidon.http.ServerResponseHeaders;
2829
import io.helidon.http.Status;
2930
import io.helidon.webserver.KeyPerformanceIndicatorSupport;
3031
import io.helidon.webserver.http.HttpRules;
@@ -94,7 +95,7 @@ private static String basePath(UriPath path) {
9495
}
9596

9697
private ApplicationHandler appHandler() {
97-
return bridge.getContainer().getApplicationHandler();
98+
return container().getApplicationHandler();
9899
}
99100

100101
private Container container() {
@@ -114,9 +115,7 @@ public void beforeStart() {
114115
@Override
115116
public void afterStop() {
116117
try {
117-
final InjectionManager ij = appHandler().getInjectionManager();
118-
ij.shutdown();
119-
appHandler().onShutdown(bridge.getContainer());
118+
appHandler().onShutdown(container());
120119
} catch (Exception e) {
121120
if (LOGGER.isLoggable(System.Logger.Level.DEBUG)) {
122121
LOGGER.log(System.Logger.Level.DEBUG, "Exception during shutdown of Jersey", e);
@@ -138,6 +137,11 @@ private void handle(final ServerRequest req, final ServerResponse res) {
138137
}
139138

140139
private void doHandle(final Context ctx, final ServerRequest req, final ServerResponse res) {
140+
ServerResponseHeaders savedResponseHeaders = null;
141+
if (req.listenerContext().config().restoreResponseHeaders()) {
142+
savedResponseHeaders = ServerResponseHeaders.create(res.headers());
143+
}
144+
141145
final BaseUriRequestUri uris = BaseUriRequestUri.resolve(req);
142146
final ContainerRequest requestContext = new ContainerRequest(uris.baseUri,
143147
uris.requestUri,
@@ -187,6 +191,9 @@ private void doHandle(final Context ctx, final ServerRequest req, final ServerRe
187191
final RoutingResponse routing = (RoutingResponse) res;
188192
if (routing.reset()) {
189193
res.status(Status.OK_200);
194+
if (savedResponseHeaders != null) {
195+
savedResponseHeaders.forEach(res::header);
196+
}
190197
routing.next();
191198
}
192199
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[//]: # " Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved. "
2+
[//]: # " "
3+
[//]: # " This program and the accompanying materials are made available under the "
4+
[//]: # " terms of the Eclipse Distribution License v. 1.0, which is available at "
5+
[//]: # " http://www.eclipse.org/org/documents/edl-v10.php. "
6+
[//]: # " "
7+
[//]: # " SPDX-License-Identifier: BSD-3-Clause "
8+
9+
Hello World Example
10+
===================
11+
12+
This example demonstrates Hello World example running on top of Helidon container and using Helidon connector as a client
13+
implementation for testing. JAX-RS resource returns the usual text `Hello World!`.
14+
15+
Contents
16+
--------
17+
18+
The mapping of the URI path space is presented in the following table:
19+
20+
URI path | Resource class | HTTP methods | Notes
21+
-------------------- | ------------------- | ------------ | --------------------------------------------------------
22+
**_/helloworld_** | HelloWorldResource | GET | Returns `Hello World!`
23+
24+
Running the Example
25+
-------------------
26+
27+
Run the example as follows:
28+
29+
> mvn clean compile exec:java
30+
31+
This deploys the example using [Helidon](http://helidon.io/) container.
32+
33+
- <http://localhost:8080/helloworld>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
5+
6+
This program and the accompanying materials are made available under the
7+
terms of the Eclipse Distribution License v. 1.0, which is available at
8+
http://www.eclipse.org/org/documents/edl-v10.php.
9+
10+
SPDX-License-Identifier: BSD-3-Clause
11+
12+
-->
13+
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
15+
<modelVersion>4.0.0</modelVersion>
16+
17+
<parent>
18+
<groupId>org.glassfish.jersey.examples</groupId>
19+
<artifactId>project</artifactId>
20+
<version>3.1.99-SNAPSHOT</version>
21+
</parent>
22+
23+
<artifactId>helloworld-helidon</artifactId>
24+
<packaging>jar</packaging>
25+
<name>jersey-examples-helloworld-helidon</name>
26+
27+
<description>Jersey "Hello world" example on Helidon container.</description>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.glassfish.jersey.containers</groupId>
32+
<artifactId>jersey-container-helidon-http</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.glassfish.jersey.inject</groupId>
36+
<artifactId>jersey-hk2</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.glassfish.jersey.test-framework</groupId>
40+
<artifactId>jersey-test-framework-util</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
46+
<artifactId>jersey-test-framework-provider-helidon</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.glassfish.jersey.connectors</groupId>
51+
<artifactId>jersey-helidon-connector</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>io.helidon.jersey</groupId>
56+
<artifactId>helidon-jersey-connector</artifactId>
57+
<version>${helidon.connector.version}</version>
58+
<scope>test</scope>
59+
</dependency>
60+
61+
</dependencies>
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.codehaus.mojo</groupId>
67+
<artifactId>exec-maven-plugin</artifactId>
68+
<configuration>
69+
<mainClass>org.glassfish.jersey.examples.helloworld.helidon.App</mainClass>
70+
</configuration>
71+
</plugin>
72+
<plugin>
73+
<artifactId>maven-surefire-plugin</artifactId>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
78+
<profiles>
79+
<profile>
80+
<id>pre-release</id>
81+
<build>
82+
<plugins>
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-assembly-plugin</artifactId>
86+
</plugin>
87+
</plugins>
88+
</build>
89+
</profile>
90+
</profiles>
91+
92+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
package org.glassfish.jersey.examples.helloworld.helidon;
12+
13+
import java.net.URI;
14+
import java.util.logging.Level;
15+
import java.util.logging.Logger;
16+
17+
import jakarta.ws.rs.SeBootstrap;
18+
import org.glassfish.jersey.helidon.HelidonHttpContainerProvider;
19+
import org.glassfish.jersey.helidon.HelidonHttpServer;
20+
import org.glassfish.jersey.server.JerseySeBootstrapConfiguration;
21+
import org.glassfish.jersey.server.ResourceConfig;
22+
23+
/**
24+
* Hello world!
25+
*/
26+
public class App {
27+
28+
static final String ROOT_PATH = "helloworld";
29+
30+
private static final URI BASE_URI = URI.create("http://localhost:8080/");
31+
32+
public static void main(String[] args) {
33+
try {
34+
System.out.println("\"Hello World\" Jersey Example App on Helidon container.");
35+
36+
ResourceConfig resourceConfig = new ResourceConfig(HelloWorldResource.class);
37+
final SeBootstrap.Configuration config =
38+
JerseySeBootstrapConfiguration.builder().host("localhost").port(8080).build();
39+
/*
40+
final WebServer server = HelidonHttpContainerBuilder.builder()
41+
.host("localhost").port(8080).application(resourceConfig).build();
42+
*/
43+
final HelidonHttpServer server = new HelidonHttpContainerProvider()
44+
.createServer(HelidonHttpServer.class, resourceConfig, config);
45+
server.start();
46+
47+
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
48+
@Override
49+
public void run() {
50+
server.stop();
51+
}
52+
}));
53+
54+
System.out.println(String.format("Application started. \nTry out %s%s\nStop the application using "
55+
+ "CTRL+C.", BASE_URI, ROOT_PATH));
56+
Thread.currentThread().join();
57+
} catch (InterruptedException ex) {
58+
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
59+
}
60+
61+
}
62+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
package org.glassfish.jersey.examples.helloworld.helidon;
12+
13+
import jakarta.ws.rs.Consumes;
14+
import jakarta.ws.rs.DefaultValue;
15+
import jakarta.ws.rs.GET;
16+
import jakarta.ws.rs.POST;
17+
import jakarta.ws.rs.Path;
18+
import jakarta.ws.rs.Produces;
19+
import jakarta.ws.rs.QueryParam;
20+
21+
@Path("helloworld")
22+
public class HelloWorldResource {
23+
public static final String CLICHED_MESSAGE = "Hello World!";
24+
25+
@GET
26+
@Produces("text/plain")
27+
public String getHello() {
28+
return CLICHED_MESSAGE;
29+
}
30+
31+
@GET
32+
@Path("query1")
33+
@Produces("text/plain")
34+
public String getQueryParameter(@DefaultValue("error1") @QueryParam(value = "test1") String test1,
35+
@DefaultValue("error2") @QueryParam(value = "test2") String test2) {
36+
return test1 + test2;
37+
}
38+
39+
@POST
40+
@Path("query2")
41+
@Consumes("text/plain")
42+
@Produces("text/plain")
43+
public String postQueryParameter(@DefaultValue("error1") @QueryParam(value = "test1") String test1,
44+
@DefaultValue("error2") @QueryParam(value = "test2") String test2, String entity) {
45+
return entity + test1 + test2;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)