1414 * limitations under the License.
1515 */
1616
17- package io .helidon .microprofile . server ;
17+ package io .helidon .jersey . webserver ;
1818
1919import java .io .IOException ;
2020import java .io .OutputStream ;
2626import java .util .Collections ;
2727import java .util .List ;
2828import java .util .Map ;
29+ import java .util .Objects ;
2930import java .util .Optional ;
3031import java .util .Set ;
3132import java .util .WeakHashMap ;
3233import java .util .concurrent .CountDownLatch ;
3334import java .util .concurrent .TimeUnit ;
3435import java .util .function .Supplier ;
3536
37+ import io .helidon .common .config .Config ;
3638import io .helidon .common .context .Context ;
3739import io .helidon .common .context .Contexts ;
3840import io .helidon .common .uri .UriInfo ;
4345import io .helidon .http .InternalServerException ;
4446import io .helidon .http .ServerResponseHeaders ;
4547import io .helidon .http .Status ;
46- import io .helidon .microprofile .server .HelidonHK2InjectionManagerFactory .InjectionManagerWrapper ;
4748import io .helidon .webserver .KeyPerformanceIndicatorSupport ;
4849import io .helidon .webserver .http .HttpRules ;
4950import io .helidon .webserver .http .HttpService ;
5657import jakarta .ws .rs .core .GenericType ;
5758import jakarta .ws .rs .core .Response ;
5859import jakarta .ws .rs .core .SecurityContext ;
59- import org .eclipse .microprofile .config .Config ;
60- import org .eclipse .microprofile .config .ConfigProvider ;
6160import org .glassfish .jersey .CommonProperties ;
6261import org .glassfish .jersey .internal .MapPropertiesDelegate ;
6362import org .glassfish .jersey .internal .inject .InjectionManager ;
7170import org .glassfish .jersey .server .spi .Container ;
7271import org .glassfish .jersey .server .spi .ContainerResponseWriter ;
7372
74- class JaxRsService implements HttpService {
73+ /**
74+ * WebServer {@link io.helidon.webserver.http.HttpService} that adds support for a JAX-RS application.
75+ */
76+ public class JaxRsService implements HttpService {
7577 /**
7678 * If set to {@code "true"}, Jersey will ignore responses in exceptions.
7779 */
@@ -97,26 +99,87 @@ private JaxRsService(ResourceConfig resourceConfig,
9799 this .application = getApplication (resourceConfig );
98100 }
99101
100- static JaxRsService create (ResourceConfig resourceConfig , InjectionManager injectionManager ) {
102+ /**
103+ * Create a new JAX-RS integration service with the default injection manager.
104+ *
105+ * @param config configuration, used to configure Jersey
106+ * @param resourceConfig containing application resources
107+ * @return a new service to register with the WebServer
108+ */
109+ public static JaxRsService create (Config config ,
110+ ResourceConfig resourceConfig ) {
111+ Objects .requireNonNull (config );
112+ Objects .requireNonNull (resourceConfig );
113+
114+ return doCreate (config , resourceConfig , null );
115+ }
116+
117+ /**
118+ * Create a new JAX-RS integration service with a custom injection manager.
119+ *
120+ * @param config configuration, used to configure Jersey
121+ * @param resourceConfig containing application resources
122+ * @param injectionManager injection manager to use
123+ * @return a new service to register with the WebServer
124+ * @see #create(io.helidon.common.config.Config, org.glassfish.jersey.server.ResourceConfig)
125+ */
126+ public static JaxRsService create (Config config ,
127+ ResourceConfig resourceConfig ,
128+ InjectionManager injectionManager ) {
129+ Objects .requireNonNull (config );
130+ Objects .requireNonNull (resourceConfig );
131+ Objects .requireNonNull (injectionManager );
132+
133+ return doCreate (config , resourceConfig , injectionManager );
134+ }
135+
136+ @ Override
137+ public void routing (HttpRules rules ) {
138+ rules .any (this ::handle );
139+ }
140+
141+ @ Override
142+ public void beforeStart () {
143+ appHandler .onStartup (container );
144+ INJECTION_MANAGERS .add (appHandler .getInjectionManager ());
145+ }
146+
147+ @ Override
148+ public void afterStop () {
149+ try {
150+ InjectionManager ij = appHandler .getInjectionManager ();
151+ if (INJECTION_MANAGERS .remove (ij )) {
152+ appHandler .onShutdown (container );
153+ }
154+ } catch (Exception e ) {
155+ if (LOGGER .isLoggable (Level .DEBUG )) {
156+ LOGGER .log (Level .DEBUG , "Exception during shutdown of Jersey" , e );
157+ }
158+ LOGGER .log (Level .WARNING , "Exception while shutting down Jersey's application handler " + e .getMessage ());
159+ }
160+ }
101161
102- Config config = ConfigProvider .getConfig ();
162+ private static JaxRsService doCreate (Config config ,
163+ ResourceConfig resourceConfig ,
164+ InjectionManager injectionManager ) {
103165
104166 // Silence warnings from Jersey by disabling the default data source provider. See 9019.
105167 // To pass TCK we support a system property to control whether or not we disable the default provider
106168 // We also support the property via MicroProfile config in case a user wants to control the property.
107- boolean disableDatasourceProvider = config .getOptionalValue (DISABLE_DATASOURCE_PROVIDER , Boolean .class )
169+ boolean disableDatasourceProvider = config .get (DISABLE_DATASOURCE_PROVIDER )
170+ .asBoolean ()
108171 .orElseGet (() -> Boolean .parseBoolean (System .getProperty (DISABLE_DATASOURCE_PROVIDER , "true" )));
172+
109173 if (!resourceConfig .hasProperty (CommonProperties .PROVIDER_DEFAULT_DISABLE ) && disableDatasourceProvider ) {
110174 resourceConfig .addProperties (Map .of (CommonProperties .PROVIDER_DEFAULT_DISABLE , "DATASOURCE" ));
111175 }
112176 if (!resourceConfig .hasProperty (ServerProperties .WADL_FEATURE_DISABLE )) {
113177 resourceConfig .addProperties (Map .of (ServerProperties .WADL_FEATURE_DISABLE , "true" ));
114178 }
115179
116- InjectionManager ij = injectionManager == null ? null : new InjectionManagerWrapper (injectionManager , resourceConfig );
117180 ApplicationHandler appHandler = new ApplicationHandler (resourceConfig ,
118181 new WebServerBinder (),
119- ij );
182+ injectionManager );
120183 Container container = new HelidonJerseyContainer (appHandler );
121184
122185 // This configuration via system properties is for the Jersey Client API. Any
@@ -125,7 +188,7 @@ static JaxRsService create(ResourceConfig resourceConfig, InjectionManager injec
125188 // See https://github.com/eclipse-ee4j/jersey/pull/4641.
126189 if (!System .getProperties ().contains (IGNORE_EXCEPTION_RESPONSE )) {
127190 System .setProperty (CommonProperties .ALLOW_SYSTEM_PROPERTIES_PROVIDER , "true" );
128- String ignore = config .getOptionalValue (IGNORE_EXCEPTION_RESPONSE , String . class ).orElse ("true" );
191+ String ignore = config .get (IGNORE_EXCEPTION_RESPONSE ). asString ( ).orElse ("true" );
129192 System .setProperty (IGNORE_EXCEPTION_RESPONSE , ignore );
130193 }
131194
@@ -146,32 +209,6 @@ private static String basePath(UriPath path) {
146209 }
147210 }
148211
149- @ Override
150- public void routing (HttpRules rules ) {
151- rules .any (this ::handle );
152- }
153-
154- @ Override
155- public void beforeStart () {
156- appHandler .onStartup (container );
157- INJECTION_MANAGERS .add (appHandler .getInjectionManager ());
158- }
159-
160- @ Override
161- public void afterStop () {
162- try {
163- InjectionManager ij = appHandler .getInjectionManager ();
164- if (INJECTION_MANAGERS .remove (ij )) {
165- appHandler .onShutdown (container );
166- }
167- } catch (Exception e ) {
168- if (LOGGER .isLoggable (Level .DEBUG )) {
169- LOGGER .log (Level .DEBUG , "Exception during shutdown of Jersey" , e );
170- }
171- LOGGER .log (Level .WARNING , "Exception while shutting down Jersey's application handler " + e .getMessage ());
172- }
173- }
174-
175212 /**
176213 * Extracts the actual {@code Application} instance.
177214 *
@@ -202,7 +239,7 @@ private void handle(ServerRequest req, ServerResponse res) {
202239 }
203240
204241 private void doHandle (Context ctx , ServerRequest req , ServerResponse res ) {
205- // save headers in case MP request processing fails
242+ // save headers in case JAX-RS request processing fails
206243 ServerResponseHeaders savedResponseHeaders = null ;
207244 if (req .listenerContext ().config ().restoreResponseHeaders ()) {
208245 savedResponseHeaders = ServerResponseHeaders .create (res .headers ());
0 commit comments