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

Support for injection of ServerRequest and ServerResponse also via CDI #6784

Merged
merged 6 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* 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.
Expand Down Expand Up @@ -184,7 +184,14 @@ private static URI baseUri(ServerRequest req) {
}

private void handle(ServerRequest req, ServerResponse res) {
Contexts.runInContext(req.context(), () -> doHandle(req.context(), req, res));
Context context = req.context();

// make these available in context for ServerCdiExtension
context.supply(ServerRequest.class, () -> req);
context.supply(ServerResponse.class, () -> res);

// call doHandle in active context
Contexts.runInContext(context, () -> doHandle(context, req, res));
}

private void doHandle(Context ctx, ServerRequest req, ServerResponse res) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,21 @@
import io.helidon.nima.webserver.context.ContextFeature;
import io.helidon.nima.webserver.http.HttpRouting;
import io.helidon.nima.webserver.http.HttpService;
import io.helidon.nima.webserver.http.ServerRequest;
import io.helidon.nima.webserver.http.ServerResponse;
import io.helidon.nima.webserver.staticcontent.StaticContentService;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.BeforeDestroyed;
import jakarta.enterprise.context.Initialized;
import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.CreationException;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.CDI;
Expand Down Expand Up @@ -361,6 +368,27 @@ private void startServer(@Observes @Priority(PLATFORM_AFTER + 100) @Initialized(
STARTUP_LOGGER.log(Level.TRACE, "Server created");
}

/**
* Make Nima's {@code ServerRequest} and {@code ServerResponse} available for injection
* via CDI by registering them as beans.
*
* @param event after bean discovery event
*/
private void afterBeanDiscovery(@Observes AfterBeanDiscovery event) {
event.addBean()
.qualifiers(Set.of(Default.Literal.INSTANCE, Any.Literal.INSTANCE))
.addTransitiveTypeClosure(ServerRequest.class)
.scope(RequestScoped.class)
.createWith(cc -> Contexts.context().flatMap(c -> c.get(ServerRequest.class))
.orElseThrow(() -> new CreationException("Unable to retrieve ServerRequest from context")));
event.addBean()
.qualifiers(Set.of(Default.Literal.INSTANCE, Any.Literal.INSTANCE))
.addTransitiveTypeClosure(ServerResponse.class)
.scope(RequestScoped.class)
.createWith(cc -> Contexts.context().flatMap(c -> c.get(ServerResponse.class))
.orElseThrow(() -> new CreationException("Unable to retrieve ServerResponse from context")));
}

private void registerJaxRsApplications(BeanManager beanManager) {
JaxRsCdiExtension jaxRs = beanManager.getExtension(JaxRsCdiExtension.class);

Expand Down
1 change: 1 addition & 0 deletions tests/functional/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<module>mp-compression</module>
<module>request-scope</module>
<module>request-scope-cdi</module>
<module>request-scope-injection</module>
<module>jax-rs-multiple-apps</module>
<module>param-converter-provider</module>
<module>config-profiles</module>
Expand Down
61 changes: 61 additions & 0 deletions tests/functional/request-scope-injection/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 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.
-->

<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>helidon-tests-functional-project</artifactId>
<groupId>io.helidon.tests.functional</groupId>
<version>4.0.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-tests-functional-request-scope-injection</artifactId>
<name>Helidon Functional Test: Helidon Request Scope Injection</name>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
<artifactId>helidon-microprofile-core</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-junit5</artifactId>
<scope>test</scope>
</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>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 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.tests.functional.context.injection;

import java.util.Objects;

import io.helidon.nima.webserver.http.ServerRequest;
import io.helidon.nima.webserver.http.ServerResponse;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;

/**
* Verifies that {@code ServerRequest} and {@code ServerResponse} are injectable
* both via {@code @Context} and {@code @Inject}.
*/
@Path("/check")
public class CheckInjectionResource {

@Context
private ServerRequest serverRequest;

@Context
private ServerResponse serverResponse;

@Inject
private ServerRequest serverRequestCdi;

@Inject
private ServerResponse serverResponseCdi;

@GET
public Response checkInjection() {
Objects.requireNonNull(serverRequest);
Objects.requireNonNull(serverResponse);
Objects.requireNonNull(serverRequestCdi);
Objects.requireNonNull(serverResponseCdi);
if (!serverRequestCdi.path().equals(serverRequest.path())
|| !serverResponseCdi.status().equals(serverResponse.status())) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
return Response.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 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.
*/

/**
* Simple app to verify injection of {@code ServerRequest} and {@code ServerResponse}.
*/
package io.helidon.tests.functional.context.injection;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 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.

-->
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd"
version="3.0"
bean-discovery-mode="annotated">
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 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.tests.functional.context.injection;

import io.helidon.microprofile.tests.junit5.HelidonTest;
import jakarta.inject.Inject;
import jakarta.ws.rs.client.WebTarget;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Unit test for {@link CheckInjectionResource}.
*/
@HelidonTest
class CheckInjectionTest {

private final WebTarget baseTarget;

@Inject
CheckInjectionTest(WebTarget baseTarget) {
this.baseTarget = baseTarget;
}

@Test
void testCheckInjection() {
WebTarget target = baseTarget.path("/check");
assertThat(target.request().get().getStatus(), is(200));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright (c) 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.
#

# Example Logging Configuration File
# For more information see $JAVA_HOME/jre/lib/logging.properties

# Send messages to the console
handlers=io.helidon.logging.jul.HelidonConsoleHandler

# HelidonConsoleHandler uses a SimpleFormatter subclass that replaces "!thread!" with the current thread
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n

# Global logging level. Can be overridden by specific loggers
.level=INFO

# Component specific log levels
AUDIT.level=FINEST