Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Config into MediaSupport#builder() method #1403

Merged
merged 8 commits into from
Feb 19, 2020
4 changes: 4 additions & 0 deletions media/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-mapper</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@
*/
package io.helidon.media.common;

import io.helidon.config.Config;

/**
* Media support.
*/
public final class MediaSupport {

private final MessageBodyReaderContext readerContext;
private final MessageBodyWriterContext writerContext;
private final boolean serverErrorsIncludeStackTraces;

private MediaSupport(MessageBodyReaderContext readerContext, MessageBodyWriterContext writerContext) {
this(readerContext, writerContext, false);
}

private MediaSupport(MessageBodyReaderContext readerContext,
MessageBodyWriterContext writerContext,
boolean serverErrorsIncludeStackTraces) {
this.readerContext = readerContext;
this.writerContext = writerContext;
this.serverErrorsIncludeStackTraces = serverErrorsIncludeStackTraces;
}

/**
Expand All @@ -44,6 +54,16 @@ public MessageBodyWriterContext writerContext() {
return writerContext;
}

/**
* Returns {@code true} if server errors will include stack trace
* information.
* @return {@code true} if server errors will include stack trace
* information
*/
public boolean serverErrorsIncludeStackTraces() {
return serverErrorsIncludeStackTraces;
}

/**
* Create a new instance with empty reader and writer contexts.
* @return MediaSupport
Expand All @@ -52,6 +72,15 @@ public static MediaSupport create() {
return builder().build();
}

/**
* Create a new instance with empty reader and writer contexts.
* @param config a {@link Config}
* @return MediaSupport
*/
public static MediaSupport create(Config config) {
return builder().config(config).build();
}

/**
* Create a new instance with the default readers and writers registered on
* the contexts.
Expand All @@ -61,9 +90,20 @@ public static MediaSupport createWithDefaults() {
return builder().registerDefaults().build();
}

/**
* Create a new instance with the default readers and writers registered on
* the contexts.
* @param config a {@link Config} that will be passed to {@link
* Builder#registerDefaults(Config)}
* @return MediaSupport
*/
public static MediaSupport createWithDefaults(Config config) {
return builder().config(config).registerDefaults().build();
}

/**
* Create a new {@link Builder} instance.
* @return Builder
* @return a new {@link Builder}
*/
public static Builder builder() {
return new Builder();
Expand All @@ -76,12 +116,41 @@ public static final class Builder implements io.helidon.common.Builder<MediaSupp

private final MessageBodyReaderContext readerContext;
private final MessageBodyWriterContext writerContext;
private boolean serverErrorsIncludeStackTraces;

Builder() {
readerContext = MessageBodyReaderContext.create();
writerContext = MessageBodyWriterContext.create();
}

/**
* Configures this {@link Builder} from the supplied {@link Config}.
* @param config a {@link Config}
* @return this {@link Builder}
*/
public Builder config(Config config) {
config.get("server-errors-include-stack-traces").asBoolean().ifPresent(this::serverErrorsIncludeStackTraces);
config.get("register-defaults").asBoolean().ifPresent(b -> {
if (b) {
registerDefaults();
}
});
return this;
}

/**
* Ensures that {@link MediaSupport} instances {@linkplain
* #build() built} by this {@link Builder} will include stack
* trace information when server errors are returned.
* @param serverErrorsIncludeStackTraces whether stack trace
* information should be included
* @return this {@link Builder}
*/
public Builder serverErrorsIncludeStackTraces(boolean serverErrorsIncludeStackTraces) {
this.serverErrorsIncludeStackTraces = serverErrorsIncludeStackTraces;
return this;
}

/**
* Register the default readers and writers.
* <h3>Default readers</h3>
Expand All @@ -101,9 +170,11 @@ public static final class Builder implements io.helidon.common.Builder<MediaSupp
* {@code Path.class}</li>
* <li>{@link FileBodyWriter} - generates payload from
* {@code File.class}</li>
* <li>{@link ThrowableBodyWriter} - generates payload from
* {@link Throwable Throwable.class}</li>
* </ul>
*
* @return this builder instance
* @return this {@link Builder}
*/
public Builder registerDefaults() {
// default readers
Expand All @@ -117,7 +188,7 @@ public Builder registerDefaults() {
.registerWriter(ByteChannelBodyWriter.create())
.registerWriter(PathBodyWriter.create())
.registerWriter(FileBodyWriter.create())
.registerWriter(ThrowableBodyWriter.create(false));
.registerWriter(ThrowableBodyWriter.create(serverErrorsIncludeStackTraces));
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion media/common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
requires io.helidon.common.mapper;
requires io.helidon.common.reactive;
requires io.helidon.common.http;
requires io.helidon.config;

exports io.helidon.media.common;
}