-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure the RequestScope and other singleton bindings are registered j…
…ust once (#5624) * Ensure the RequestScope and other singleton bindings are registered just once in HK2 Signed-off-by: jansupol <jan.supol@oracle.com> --------- Signed-off-by: jansupol <jan.supol@oracle.com>
- Loading branch information
Showing
2 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
.../src/test/java/org/glassfish/jersey/tests/e2e/inject/SingleRequestScopeInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.tests.e2e.inject; | ||
|
||
import org.glassfish.jersey.internal.inject.InjectionManager; | ||
import org.glassfish.jersey.internal.util.collection.Ref; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.glassfish.jersey.test.JerseyTest; | ||
import org.glassfish.jersey.test.jetty.JettyTestContainerFactory; | ||
import org.glassfish.jersey.test.spi.TestContainerException; | ||
import org.glassfish.jersey.test.spi.TestContainerFactory; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.inject.Inject; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import javax.ws.rs.container.DynamicFeature; | ||
import javax.ws.rs.container.ResourceInfo; | ||
import javax.ws.rs.core.Application; | ||
import javax.ws.rs.core.FeatureContext; | ||
import javax.ws.rs.core.GenericType; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class SingleRequestScopeInjectionTest extends JerseyTest { | ||
@Path("hello") | ||
public static class HelloResource { | ||
@GET | ||
public String getHello() { | ||
return "Hello World!"; | ||
} | ||
} | ||
@Override | ||
protected Application configure() { | ||
ResourceConfig resourceConfig = new ResourceConfig(HelloResource.class); | ||
resourceConfig.register(new InjectedFilterRegistrar(InjectedFilter.class)); | ||
return resourceConfig; | ||
} | ||
@Override | ||
protected TestContainerFactory getTestContainerFactory() throws TestContainerException { | ||
return new JettyTestContainerFactory(); | ||
} | ||
@Test | ||
public void test() { | ||
final String hello = target("hello").request().get(String.class); | ||
assertEquals("Hello World!", hello); | ||
} | ||
public static class InjectedFilter implements ContainerRequestFilter { | ||
@Inject | ||
private InjectionManager injectionManager; | ||
@Override | ||
public void filter(ContainerRequestContext requestContext) throws IOException { | ||
Ref<HttpServletRequest> requestRef = | ||
injectionManager.getInstance((new GenericType<Ref<HttpServletRequest>>() {}).getType()); | ||
if (requestRef == null || requestRef.get() == null) { | ||
throw new IllegalStateException("Request not injected"); | ||
} | ||
} | ||
} | ||
public static class InjectedFilterRegistrar implements DynamicFeature { | ||
private final Class<?> filterToRegister; | ||
public InjectedFilterRegistrar(Class<?> filterToRegister) { | ||
this.filterToRegister = filterToRegister; | ||
} | ||
@Override | ||
public void configure(ResourceInfo resourceInfo, FeatureContext context) { | ||
context.register(filterToRegister); | ||
} | ||
} | ||
} |