Skip to content

Commit

Permalink
Context propagation across HTTP. (#4612)
Browse files Browse the repository at this point in the history
* Context propagation across HTTP.

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
  • Loading branch information
tomas-langer authored Jul 28, 2022
1 parent beed5a1 commit 9e11f0d
Show file tree
Hide file tree
Showing 32 changed files with 1,806 additions and 8 deletions.
10 changes: 10 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<artifactId>helidon-webserver-static-content</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-context-propagation</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics-prometheus</artifactId>
Expand Down Expand Up @@ -130,6 +135,11 @@
<artifactId>helidon-webclient-security</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient-context-propagation</artifactId>
<version>${helidon.version}</version>
</dependency>
<!-- Fault tolerance -->
<dependency>
<groupId>io.helidon.fault-tolerance</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2022 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.
Expand Down Expand Up @@ -89,6 +89,7 @@ class ConfigMetadataHandler {
private TypeMirror builderType;
private TypeMirror configType;
private TypeMirror erasedListType;
private TypeMirror erasedIterableType;
private TypeMirror erasedSetType;
private TypeMirror erasedMapType;

Expand All @@ -111,6 +112,7 @@ synchronized void init(ProcessingEnvironment processingEnv) {
configType = elementUtils.getTypeElement("io.helidon.config.Config").asType();
erasedListType = typeUtils.erasure(elementUtils.getTypeElement(List.class.getName()).asType());
erasedSetType = typeUtils.erasure(elementUtils.getTypeElement(Set.class.getName()).asType());
erasedIterableType = typeUtils.erasure(elementUtils.getTypeElement(Iterable.class.getName()).asType());
erasedMapType = typeUtils.erasure(elementUtils.getTypeElement(Map.class.getName()).asType());
}

Expand Down Expand Up @@ -184,7 +186,8 @@ - a standalone class (probably with public static create(Config) method)
- an interface/abstract class only used for inheritance
*/

ConfiguredType type = new ConfiguredType(targetClass,
ConfiguredType type = new ConfiguredType(className,
targetClass,
standalone,
keyPrefix,
description,
Expand Down Expand Up @@ -253,6 +256,19 @@ private BuilderTypeInfo findBuilder(TypeElement classElement) {
}
}
}
TypeMirror superMirror = classElement.getSuperclass();
String buildTarget = findBuildMethodTarget(classElement);
if (superMirror.getKind() != TypeKind.NONE) {
TypeElement superclass = (TypeElement) typeUtils.asElement(typeUtils.erasure(superMirror));
found = findBuilder(superclass);
if (found.isBuilder) {
if (buildTarget == null) {
return found;
} else {
return new BuilderTypeInfo(buildTarget);
}
}
}
return new BuilderTypeInfo();
}

Expand Down Expand Up @@ -469,6 +485,23 @@ private void processTargetType(TypeElement typeElement,
}
}

private String findBuildMethodTarget(TypeElement typeElement) {
return elementUtils.getAllMembers(typeElement)
.stream()
.filter(it -> it.getKind() == ElementKind.METHOD)
.map(ExecutableElement.class::cast)
// public
.filter(it -> it.getModifiers().contains(Modifier.PUBLIC))
// static
.filter(it -> !it.getModifiers().contains(Modifier.STATIC))
.filter(it -> it.getSimpleName().contentEquals("build"))
.filter(it -> it.getParameters().isEmpty())
.filter(it -> it.getReturnType().getKind() != TypeKind.VOID)
.findFirst()
.map(it -> it.getReturnType().toString())
.orElse(null);
}

private boolean hasCreate(TypeElement typeElement) {
return elementUtils.getAllMembers(typeElement)
.stream()
Expand Down Expand Up @@ -570,7 +603,8 @@ private OptionType type(ConfiguredOptionData annotation, ExecutableElement eleme
TypeMirror paramType = parameter.asType();
TypeMirror erasedType = typeUtils.erasure(paramType);

if (typeUtils.isSameType(erasedType, erasedListType) || typeUtils.isSameType(erasedType, erasedSetType)) {
if (typeUtils.isSameType(erasedType, erasedListType) || typeUtils.isSameType(erasedType, erasedSetType)
|| typeUtils.isSameType(erasedType, erasedIterableType)) {
DeclaredType type = (DeclaredType) paramType;
TypeMirror genericType = type.getTypeArguments().get(0);
return new OptionType(genericType.toString(), "LIST");
Expand Down Expand Up @@ -697,7 +731,7 @@ String toConfigKey(String methodName) {
}

static List<AllowedValue> allowedValues(Elements elementUtils, TypeElement typeElement) {
if (typeElement.getKind() == ElementKind.ENUM) {
if (typeElement != null && typeElement.getKind() == ElementKind.ENUM) {
return typeElement.getEnclosedElements()
.stream()
.filter(element -> element.getKind().equals(ElementKind.ENUM_CONSTANT))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2022 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.
Expand Down Expand Up @@ -27,17 +27,27 @@
final class ConfiguredType {
private final Set<ConfiguredProperty> allProperties = new HashSet<>();
private final List<ProducerMethod> producerMethods = new LinkedList<>();
/**
/*
* The type that is built by a builder, or created using create method.
*/
private final String targetClass;
/*
The type we are processing that has @Configured annotation
*/
private final String annotatedClass;
private final boolean standalone;
private final String prefix;
private final String description;
private final List<String> provides;
private final List<String> inherited = new LinkedList<>();

ConfiguredType(String targetClass, boolean standalone, String prefix, String description, List<String> provides) {
ConfiguredType(String annotatedClass,
String targetClass,
boolean standalone,
String prefix,
String description,
List<String> provides) {
this.annotatedClass = annotatedClass;
this.targetClass = targetClass;
this.standalone = standalone;
this.prefix = prefix;
Expand Down Expand Up @@ -75,6 +85,10 @@ String targetClass() {
return targetClass;
}

String annotatedClass() {
return annotatedClass;
}

boolean standalone() {
return standalone;
}
Expand All @@ -87,6 +101,7 @@ public void write(JArray typeArray) {
JObject typeObject = new JObject();

typeObject.add("type", targetClass());
typeObject.add("annotatedType", annotatedClass());
if (standalone()) {
typeObject.add("standalone", true);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/mp/oci/01_oci.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ javadocs. In particular the `oci.auth-strategies` property lets you control whic
== Accessing OCI Services
Since the Helidion OCI SDK extension supports injecting any OCI client
Since the Helidon OCI SDK extension supports injecting any OCI client
from the OCI SDK, you can use it to access any OCI service supported by the
OCI SDK. In addition to adding the Helidon OCI SDK Extension dependency
(as described above) you will need to add dependencies for the specific
Expand Down
64 changes: 64 additions & 0 deletions tests/integration/webserver/context-propagation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022 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.
-->

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>helidon-tests-integration-webserver</artifactId>
<groupId>io.helidon.tests.integration</groupId>
<version>2.5.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>helidon-tests-integration-webserver-context-propagation</artifactId>
<name>Helidon Integration Test WebServer Context Propagation</name>

<dependencies>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-context-propagation</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient-context-propagation</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.media</groupId>
<artifactId>helidon-media-jsonb</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2022 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.tests.integration.webserver.context.propagation;

import java.time.Duration;

import io.helidon.common.context.Context;
import io.helidon.config.Config;
import io.helidon.media.jsonb.JsonbSupport;
import io.helidon.webclient.WebClient;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.context.propagation.ContextPropagationFilter;

/**
* Main test class.
* Starts the server and configures context propagation on it.
*/
public class ContextPropagationMain {
static final String CLASSIFIER_VALUE = "io.helidon.tests.integration.value";
static final String CLASSIFIER_VALUES = "io.helidon.tests.integration.values";
static final String CLASSIFIER_DEFAULT = "io.helidon.tests.integration.default";
static final String CLASSIFIER_DEFAULTS = "io.helidon.tests.integration.defaults";
static final String NOT_PROPAGATED = "some.random.classifier";
static final Duration TIMEOUT = Duration.ofSeconds(10);
private static WebServer server;
private static WebClient client;

private ContextPropagationMain() {
}

/**
* Main method.
*
* @param args command line arguments are ignored
*/
public static void main(String[] args) {
Config config = Config.create();

server = WebServer.builder()
.config(config.get("server"))
.addMediaSupport(JsonbSupport.create())
.routing(Routing.builder()
.any(ContextPropagationFilter.create(config.get("server.context")))
.get("/", ContextPropagationMain::route)
.build())
.build()
.start()
.await(TIMEOUT);

client = WebClient.builder()
.config(config.get("client"))
.baseUri("http://localhost:" + server.port())
.build();
}

static WebServer server() {
return server;
}

static WebClient client() {
return client;
}

private static void route(ServerRequest req, ServerResponse res) {
Context ctx = req.context();
DataDto dto = new DataDto();

ctx.get(CLASSIFIER_VALUE, String.class).ifPresent(dto::setValue);
ctx.get(CLASSIFIER_VALUES, String[].class).ifPresent(dto::setValues);

ctx.get(CLASSIFIER_DEFAULT, String.class).ifPresent(dto::setDefaultValue);
ctx.get(CLASSIFIER_DEFAULTS, String[].class).ifPresent(dto::setDefaultValues);

ctx.get(NOT_PROPAGATED, String.class).ifPresent(dto::setNotPropagated);

res.send(dto);
}
}
Loading

0 comments on commit 9e11f0d

Please sign in to comment.