1+ /*
2+ * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
3+ *
4+ * This program and the accompanying materials are made available under the
5+ * terms of the Eclipse Public License v. 2.0, which is available at
6+ * http://www.eclipse.org/legal/epl-2.0.
7+ *
8+ * This Source Code may also be made available under the following Secondary
9+ * Licenses when the conditions for such availability set forth in the
10+ * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+ * version 2 with the GNU Classpath Exception, which is available at
12+ * https://www.gnu.org/software/classpath/license.html.
13+ *
14+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+ */
16+
17+ package org .glassfish .jersey .tests .e2e .inject ;
18+
19+ import org .glassfish .jersey .internal .inject .InjectionManager ;
20+ import org .glassfish .jersey .internal .util .collection .Ref ;
21+ import org .glassfish .jersey .server .ResourceConfig ;
22+ import org .glassfish .jersey .test .JerseyTest ;
23+ import org .glassfish .jersey .test .jetty .JettyTestContainerFactory ;
24+ import org .glassfish .jersey .test .spi .TestContainerException ;
25+ import org .glassfish .jersey .test .spi .TestContainerFactory ;
26+ import org .junit .jupiter .api .Test ;
27+
28+ import javax .inject .Inject ;
29+ import javax .servlet .http .HttpServletRequest ;
30+ import javax .ws .rs .GET ;
31+ import javax .ws .rs .Path ;
32+ import javax .ws .rs .container .ContainerRequestContext ;
33+ import javax .ws .rs .container .ContainerRequestFilter ;
34+ import javax .ws .rs .container .DynamicFeature ;
35+ import javax .ws .rs .container .ResourceInfo ;
36+ import javax .ws .rs .core .Application ;
37+ import javax .ws .rs .core .FeatureContext ;
38+ import javax .ws .rs .core .GenericType ;
39+
40+ import java .io .IOException ;
41+ import java .util .concurrent .atomic .AtomicInteger ;
42+
43+ import static org .junit .jupiter .api .Assertions .assertEquals ;
44+
45+ public class SingleRequestScopeInjectionTest extends JerseyTest {
46+ @ Path ("hello" )
47+ public static class HelloResource {
48+ @ GET
49+ public String getHello () {
50+ return "Hello World!" ;
51+ }
52+ }
53+ @ Override
54+ protected Application configure () {
55+ ResourceConfig resourceConfig = new ResourceConfig (HelloResource .class );
56+ resourceConfig .register (new InjectedFilterRegistrar (InjectedFilter .class ));
57+ return resourceConfig ;
58+ }
59+ @ Override
60+ protected TestContainerFactory getTestContainerFactory () throws TestContainerException {
61+ return new JettyTestContainerFactory ();
62+ }
63+ @ Test
64+ public void test () {
65+ final String hello = target ("hello" ).request ().get (String .class );
66+ assertEquals ("Hello World!" , hello );
67+ }
68+ public static class InjectedFilter implements ContainerRequestFilter {
69+ @ Inject
70+ private InjectionManager injectionManager ;
71+ @ Override
72+ public void filter (ContainerRequestContext requestContext ) throws IOException {
73+ Ref <HttpServletRequest > requestRef =
74+ injectionManager .getInstance ((new GenericType <Ref <HttpServletRequest >>() {}).getType ());
75+ if (requestRef == null || requestRef .get () == null ) {
76+ throw new IllegalStateException ("Request not injected" );
77+ }
78+ }
79+ }
80+ public static class InjectedFilterRegistrar implements DynamicFeature {
81+ private final Class <?> filterToRegister ;
82+ public InjectedFilterRegistrar (Class <?> filterToRegister ) {
83+ this .filterToRegister = filterToRegister ;
84+ }
85+ @ Override
86+ public void configure (ResourceInfo resourceInfo , FeatureContext context ) {
87+ context .register (filterToRegister );
88+ }
89+ }
90+ }
0 commit comments