Skip to content

Commit

Permalink
Support for injection of ServerRequest and ServerResponse also via CD…
Browse files Browse the repository at this point in the history
…I. New functional test to verify both types of injection. See issue helidon-io#6698.
  • Loading branch information
spericas committed May 8, 2023
1 parent 97549e9 commit 6275c20
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 2 deletions.
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 @@ -17,13 +17,17 @@
package io.helidon.microprofile.server;

import java.lang.System.Logger.Level;
import java.lang.annotation.Annotation;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -41,24 +45,32 @@
import io.helidon.nima.webserver.KeyPerformanceIndicatorSupport;
import io.helidon.nima.webserver.WebServer;
import io.helidon.nima.webserver.context.ContextFeature;
import io.helidon.nima.webserver.http.HttpRequest;
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.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;
import jakarta.enterprise.inject.spi.DeploymentException;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.enterprise.inject.spi.ProcessManagedBean;
import jakarta.enterprise.inject.spi.ProcessProducerField;
import jakarta.enterprise.inject.spi.ProcessProducerMethod;
import jakarta.enterprise.util.AnnotationLiteral;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.ext.ParamConverterProvider;
import org.eclipse.microprofile.config.ConfigProvider;
Expand Down Expand Up @@ -582,4 +594,94 @@ private HttpRouting.Builder findRouting(String className,

return serverNamedRoutingBuilder(routingName);
}

// -- CDI bean producers for ServerRequest and ServerResponse -----------------------

private void afterBeanDiscovery(@Observes AfterBeanDiscovery event) {
event.addBean(new ServerRequestProducer());
event.addBean(new ServerResponseProducer());
}

/**
* Base class for server producers sharing common methods.
*/
static class ServerProducer {

@SuppressWarnings("all")
public static class DefaultAnnotationLiteral extends AnnotationLiteral<Default> implements Default {
private static final long serialVersionUID = 1L;
}

public Set<Annotation> getQualifiers() {
return Collections.singleton(new DefaultAnnotationLiteral());
}

public Class<? extends Annotation> getScope() {
return RequestScoped.class;
}

public Set<Class<? extends Annotation>> getStereotypes() {
return Collections.emptySet();
}

public Set<InjectionPoint> getInjectionPoints() {
return Collections.emptySet();
}

public boolean isAlternative() {
return false;
}

public String getName() {
return null;
}
}

/**
* Producer bean that returns {@code ServerRequest} from active context.
*/
static class ServerRequestProducer extends ServerProducer implements Bean<ServerRequest> {

@Override
public Class<?> getBeanClass() {
return ServerRequest.class;
}

@Override
public Set<Type> getTypes() {
return new HashSet<>(Arrays.asList(ServerRequest.class, HttpRequest.class, Object.class));
}

@Override
public ServerRequest create(CreationalContext<ServerRequest> creationalContext) {
return Contexts.context().orElseThrow().get(ServerRequest.class).orElseThrow();
}

public void destroy(ServerRequest request, CreationalContext<ServerRequest> creationalContext) {
}
}

/**
* Producer bean that returns {@code ServerResponse} from active context.
*/
static class ServerResponseProducer extends ServerProducer implements Bean<ServerResponse> {

@Override
public Class<?> getBeanClass() {
return ServerResponse.class;
}

@Override
public Set<Type> getTypes() {
return new HashSet<>(Arrays.asList(ServerResponse.class, Object.class));
}

@Override
public ServerResponse create(CreationalContext<ServerResponse> creationalContext) {
return Contexts.context().orElseThrow().get(ServerResponse.class).orElseThrow();
}

public void destroy(ServerResponse request, CreationalContext<ServerResponse> creationalContext) {
}
}
}
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));
}
}
Loading

0 comments on commit 6275c20

Please sign in to comment.