3434import io .helidon .nima .common .tls .Tls ;
3535import io .helidon .nima .http .encoding .ContentEncodingContext ;
3636import io .helidon .nima .http .media .MediaContext ;
37+ import io .helidon .nima .http .media .MediaSupport ;
3738import io .helidon .nima .webserver .http .DirectHandlers ;
3839import io .helidon .nima .webserver .http .HttpRouting ;
3940import io .helidon .nima .webserver .spi .ServerConnectionProvider ;
@@ -170,8 +171,8 @@ class Builder implements io.helidon.common.Builder<Builder, WebServer>, Router.R
170171 private final DefaultServerConfig .Builder configBuilder = DefaultServerConfig .builder ();
171172
172173 private Config providersConfig = Config .empty ();
173- // MediaContext should be updated with config processing or during final build if not set.
174- private MediaContext mediaContext ;
174+ private MediaContext mediaContext = MediaContext . create ();
175+ private MediaContext . Builder mediaContextBuilder ;
175176 private ContentEncodingContext contentEncodingContext = ContentEncodingContext .create ();
176177
177178 private boolean shutdownHook = true ;
@@ -198,8 +199,17 @@ public WebServer build() {
198199 .build ();
199200 }
200201 if (mediaContext == null ) {
201- mediaContext (MediaContext .create ());
202+ if (mediaContextBuilder == null ) {
203+ mediaContext = MediaContext .create ();
204+ } else {
205+ mediaContext = mediaContextBuilder .build ();
206+ }
207+ } else {
208+ if (mediaContextBuilder != null ) {
209+ mediaContext = mediaContextBuilder .fallback (mediaContext ).build ();
210+ }
202211 }
212+ mediaContextBuilder = null ;
203213
204214 return new LoomServer (this , configBuilder .build (), directHandlers .build ());
205215 }
@@ -264,10 +274,15 @@ public Builder config(Config config) {
264274 .as (ContentEncodingContext ::create )
265275 .ifPresent (this ::contentEncodingContext );
266276 // Configure media support
267- config .get ("media-support" )
268- .as (MediaContext ::create )
269- // MediaContext always needs to be refreshed after config change
270- .ifPresent (this ::mediaContext );
277+ Config mediaSupportConfig = config .get ("media-support" );
278+ if (mediaSupportConfig .exists ()) {
279+ // we are directly updating the builder, and we do not need to fallback to defaults
280+ // also configuration overrides any manual setup
281+ mediaContext = null ;
282+ mediaContextBuilder = MediaContext .builder ();
283+ mediaSupportConfig .ifExists (mediaContextBuilder ::config );
284+ }
285+
271286 // Store providers config node for later usage.
272287 providersConfig = config .get ("connection-providers" );
273288 return this ;
@@ -424,11 +439,35 @@ public boolean hasSocket(String socketName) {
424439 }
425440
426441 /**
427- * Configure the default {@link MediaContext}.
428- * This method discards all previously registered MediaContext.
442+ * Add an explicit media support to the list.
443+ * By default, all discovered media supports will be available to the server. Use this method only when
444+ * the media support is not discoverable by service loader, or when using explicit
445+ * {@link #mediaContext(io.helidon.nima.http.media.MediaContext)}.
446+ *
447+ * @param mediaSupport media support to add
448+ * @return updated builder
449+ */
450+ public Builder addMediaSupport (MediaSupport mediaSupport ) {
451+ Objects .requireNonNull (mediaSupport );
452+ if (mediaContextBuilder == null ) {
453+ mediaContextBuilder = MediaContext .builder ()
454+ .discoverServices (false );
455+ }
456+
457+ mediaContextBuilder .addMediaSupport (mediaSupport );
458+ return this ;
459+ }
460+
461+ /**
462+ * Configure the {@link MediaContext}.
463+ * This method discards previously registered {@link io.helidon.nima.http.media.MediaContext}
464+ * and all previously registered {@link io.helidon.nima.http.media.MediaSupport}.
465+ * If an explicit media support is configured using {@link #addMediaSupport(io.helidon.nima.http.media.MediaSupport)},
466+ * this context will be used as a fallback for a new one created from configured values.
429467 *
430468 * @param mediaContext media context
431469 * @return updated instance of the builder
470+ * @see #addMediaSupport(io.helidon.nima.http.media.MediaSupport)
432471 */
433472 public Builder mediaContext (MediaContext mediaContext ) {
434473 Objects .requireNonNull (mediaContext );
0 commit comments