Skip to content

Commit

Permalink
Rest of media support providers refactored to match new API.
Browse files Browse the repository at this point in the history
Signed-off-by: Tomáš Kraus <tomas.kraus@oracle.com>
  • Loading branch information
Tomas-Kraus committed Mar 31, 2023
1 parent 09141a9 commit 7eeb2c6
Show file tree
Hide file tree
Showing 22 changed files with 684 additions and 493 deletions.
17 changes: 0 additions & 17 deletions common/config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
<artifactId>helidon-common-config</artifactId>
<name>Helidon Common Config</name>

<properties>
<!-- Helidon common should be backward compatible with Java 11 -->
<version.java>11</version.java>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -51,16 +46,4 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<!-- This module is Java 11, we must remove enable-preview from parent project -->
<additionalOptions combine.self="override"/>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ public Config get(String key) throws ConfigException {
String[] split = key.split("\\.");
Config.Key node = this.key;

for (int i = 0; i < split.length; i++) {
node = new KeyImpl(node, split[i]);
for (String s : split) {
node = new KeyImpl(node, s);
}

return new EmptyNode(node);
Expand Down Expand Up @@ -215,6 +215,11 @@ public <T> ConfigValue<List<T>> asList(Class<T> type) throws ConfigException {
return new EmptyValue<>(this.key);
}

@Override
public <C extends Config> ConfigValue<List<C>> asNodeList() throws ConfigException {
return new EmptyValue<>(this.key);
}

@Override
public ConfigValue<Map<String, String>> asMap() throws ConfigException {
return new EmptyValue<>(this.key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public MediaSupport create(Config config) {
@Override
public double weight() {
// very low weight, as this covers all, but higher than JSON-B
return 11;
return 15;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ private JacksonSupport(ObjectMapper objectMapper, JacksonReader reader, JacksonW
this.writer = writer;
}


/**
* Creates a new {@link JacksonSupport}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,119 +16,38 @@

package io.helidon.nima.http.media.jsonb;

import io.helidon.common.GenericType;
import io.helidon.common.Weighted;
import io.helidon.common.http.Headers;
import io.helidon.common.http.Http;
import io.helidon.common.http.HttpMediaType;
import io.helidon.common.http.WritableHeaders;
import io.helidon.common.media.type.MediaTypes;
import io.helidon.nima.http.media.EntityReader;
import io.helidon.nima.http.media.EntityWriter;
import io.helidon.common.config.Config;
import io.helidon.nima.http.media.MediaSupport;
import io.helidon.nima.http.media.spi.MediaSupportProvider;

import jakarta.json.JsonObject;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;

import static io.helidon.common.http.Http.HeaderValues.CONTENT_TYPE_JSON;

/**
* {@link java.util.ServiceLoader} provider implementation for JSON Binding media support.
*/
public class JsonbMediaSupportProvider implements MediaSupportProvider, Weighted {
private static final GenericType<JsonObject> JSON_OBJECT_TYPE = GenericType.create(JsonObject.class);

private static final Jsonb JSON_B = JsonbBuilder.create();

private final JsonbReader reader = new JsonbReader(JSON_B);
private final JsonbWriter writer = new JsonbWriter(JSON_B);

@Override
public <T> ReaderResponse<T> reader(GenericType<T> type, Headers requestHeaders) {
if (requestHeaders.contentType()
.map(it -> it.test(MediaTypes.APPLICATION_JSON))
.orElse(true)) {
if (type.equals(JSON_OBJECT_TYPE)) {
// leave this to JSON-P
return ReaderResponse.unsupported();
}
return new ReaderResponse<>(SupportLevel.COMPATIBLE, this::reader);
}

return ReaderResponse.unsupported();
}

@Override
public <T> WriterResponse<T> writer(GenericType<T> type,
Headers requestHeaders,
WritableHeaders<?> responseHeaders) {
if (JSON_OBJECT_TYPE.equals(type)) {
return WriterResponse.unsupported();
}

// check if accepted
for (HttpMediaType acceptedType : requestHeaders.acceptedTypes()) {
if (acceptedType.test(MediaTypes.APPLICATION_JSON)) {
return new WriterResponse<>(SupportLevel.COMPATIBLE, this::writer);
}
}

if (requestHeaders.acceptedTypes().isEmpty()) {
return new WriterResponse<>(SupportLevel.COMPATIBLE, this::writer);
}

return WriterResponse.unsupported();
/**
* This class should be only instantiated as part of java {@link java.util.ServiceLoader}.
*/
@Deprecated
public JsonbMediaSupportProvider() {
super();
}

@Override
public <T> ReaderResponse<T> reader(GenericType<T> type,
Headers requestHeaders,
Headers responseHeaders) {
if (JSON_OBJECT_TYPE.equals(type)) {
return ReaderResponse.unsupported();
}

// check if accepted
for (HttpMediaType acceptedType : requestHeaders.acceptedTypes()) {
if (acceptedType.test(MediaTypes.APPLICATION_JSON) || acceptedType.mediaType().isWildcardType()) {
return new ReaderResponse<>(SupportLevel.COMPATIBLE, this::reader);
}
}

if (requestHeaders.acceptedTypes().isEmpty()) {
return new ReaderResponse<>(SupportLevel.COMPATIBLE, this::reader);
}

return ReaderResponse.unsupported();
public String configKey() {
return "jsonb";
}

@Override
public <T> WriterResponse<T> writer(GenericType<T> type, WritableHeaders<?> requestHeaders) {
if (type.equals(JSON_OBJECT_TYPE)) {
return WriterResponse.unsupported();
}
if (requestHeaders.contains(Http.Header.CONTENT_TYPE)) {
if (requestHeaders.contains(CONTENT_TYPE_JSON)) {
return new WriterResponse<>(SupportLevel.COMPATIBLE, this::writer);
}
} else {
return new WriterResponse<>(SupportLevel.SUPPORTED, this::writer);
}
return WriterResponse.unsupported();
public MediaSupport create(Config config) {
return JsonbSupport.create(config);
}

@Override
public double weight() {
// very low weight, as this covers all
// very low weight, as this covers all, also lower than Jackson
return 10;
}

<T> EntityReader<T> reader() {
return reader;
}

<T> EntityWriter<T> writer() {
return writer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.nima.http.media.jsonb;

import io.helidon.common.GenericType;
import io.helidon.common.config.Config;
import io.helidon.common.http.Headers;
import io.helidon.common.http.Http;
import io.helidon.common.http.HttpMediaType;
import io.helidon.common.http.WritableHeaders;
import io.helidon.common.media.type.MediaTypes;
import io.helidon.nima.http.media.EntityReader;
import io.helidon.nima.http.media.EntityWriter;
import io.helidon.nima.http.media.MediaSupport;

import jakarta.json.JsonObject;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;

import static io.helidon.common.http.Http.HeaderValues.CONTENT_TYPE_JSON;

/**
* {@link java.util.ServiceLoader} provider implementation for JSON Binding media support.
*/
public class JsonbSupport implements MediaSupport {
private static final GenericType<JsonObject> JSON_OBJECT_TYPE = GenericType.create(JsonObject.class);

private static final Jsonb JSON_B = JsonbBuilder.create();

private final JsonbReader reader = new JsonbReader(JSON_B);
private final JsonbWriter writer = new JsonbWriter(JSON_B);

private JsonbSupport() {
}

/**
* Creates a new {@link JsonbSupport}.
*
* @param config must not be {@code null}
* @return a new {@link JsonbSupport}
*/
public static MediaSupport create(Config config) {
return new JsonbSupport();
}

@Override
public <T> ReaderResponse<T> reader(GenericType<T> type, Headers requestHeaders) {
if (requestHeaders.contentType()
.map(it -> it.test(MediaTypes.APPLICATION_JSON))
.orElse(true)) {
if (type.equals(JSON_OBJECT_TYPE)) {
// leave this to JSON-P
return ReaderResponse.unsupported();
}
return new ReaderResponse<>(SupportLevel.COMPATIBLE, this::reader);
}

return ReaderResponse.unsupported();
}

@Override
public <T> WriterResponse<T> writer(GenericType<T> type,
Headers requestHeaders,
WritableHeaders<?> responseHeaders) {
if (JSON_OBJECT_TYPE.equals(type)) {
return WriterResponse.unsupported();
}

// check if accepted
for (HttpMediaType acceptedType : requestHeaders.acceptedTypes()) {
if (acceptedType.test(MediaTypes.APPLICATION_JSON)) {
return new WriterResponse<>(SupportLevel.COMPATIBLE, this::writer);
}
}

if (requestHeaders.acceptedTypes().isEmpty()) {
return new WriterResponse<>(SupportLevel.COMPATIBLE, this::writer);
}

return WriterResponse.unsupported();
}

@Override
public <T> ReaderResponse<T> reader(GenericType<T> type,
Headers requestHeaders,
Headers responseHeaders) {
if (JSON_OBJECT_TYPE.equals(type)) {
return ReaderResponse.unsupported();
}

// check if accepted
for (HttpMediaType acceptedType : requestHeaders.acceptedTypes()) {
if (acceptedType.test(MediaTypes.APPLICATION_JSON) || acceptedType.mediaType().isWildcardType()) {
return new ReaderResponse<>(SupportLevel.COMPATIBLE, this::reader);
}
}

if (requestHeaders.acceptedTypes().isEmpty()) {
return new ReaderResponse<>(SupportLevel.COMPATIBLE, this::reader);
}

return ReaderResponse.unsupported();
}

@Override
public <T> WriterResponse<T> writer(GenericType<T> type, WritableHeaders<?> requestHeaders) {
if (type.equals(JSON_OBJECT_TYPE)) {
return WriterResponse.unsupported();
}
if (requestHeaders.contains(Http.Header.CONTENT_TYPE)) {
if (requestHeaders.contains(CONTENT_TYPE_JSON)) {
return new WriterResponse<>(SupportLevel.COMPATIBLE, this::writer);
}
} else {
return new WriterResponse<>(SupportLevel.SUPPORTED, this::writer);
}
return WriterResponse.unsupported();
}

<T> EntityReader<T> reader() {
return reader;
}

<T> EntityWriter<T> writer() {
return writer;
}
}
Loading

0 comments on commit 7eeb2c6

Please sign in to comment.