2929import java .util .logging .Level ;
3030import java .util .logging .Logger ;
3131
32+ import javax .inject .Inject ;
33+ import javax .inject .Provider ;
3234import javax .ws .rs .core .Application ;
35+ import javax .ws .rs .core .GenericType ;
3336import javax .ws .rs .core .MultivaluedMap ;
3437import javax .ws .rs .core .Response ;
3538import javax .ws .rs .core .SecurityContext ;
3639import javax .ws .rs .core .UriBuilder ;
3740
3841import org .glassfish .jersey .internal .MapPropertiesDelegate ;
42+ import org .glassfish .jersey .internal .inject .AbstractBinder ;
43+ import org .glassfish .jersey .internal .inject .ReferencingFactory ;
44+ import org .glassfish .jersey .internal .util .collection .Ref ;
3945import org .glassfish .jersey .jdkhttp .internal .LocalizationMessages ;
46+ import org .glassfish .jersey .process .internal .RequestScoped ;
4047import org .glassfish .jersey .server .ApplicationHandler ;
4148import org .glassfish .jersey .server .ContainerException ;
4249import org .glassfish .jersey .server .ContainerRequest ;
6067public class JdkHttpHandlerContainer implements HttpHandler , Container {
6168
6269 private static final Logger LOGGER = Logger .getLogger (JdkHttpHandlerContainer .class .getName ());
70+ private static final GenericType <Ref <HttpExchange >> httpExchangeType = new GenericType <>() {};
6371
6472 private volatile ApplicationHandler appHandler ;
6573
@@ -69,7 +77,7 @@ public class JdkHttpHandlerContainer implements HttpHandler, Container {
6977 * @param application JAX-RS / Jersey application to be deployed on the container.
7078 */
7179 JdkHttpHandlerContainer (final Application application ) {
72- this . appHandler = new ApplicationHandler (application );
80+ this (application , null );
7381 }
7482
7583 /**
@@ -79,7 +87,7 @@ public class JdkHttpHandlerContainer implements HttpHandler, Container {
7987 * @param parentContext DI provider specific context with application's registered bindings.
8088 */
8189 JdkHttpHandlerContainer (final Application application , final Object parentContext ) {
82- this .appHandler = new ApplicationHandler (application , null , parentContext );
90+ this .appHandler = new ApplicationHandler (application , new JdkBinder () , parentContext );
8391 }
8492
8593 @ Override
@@ -131,6 +139,9 @@ public void handle(final HttpExchange exchange) throws IOException {
131139 requestContext .setEntityStream (exchange .getRequestBody ());
132140 requestContext .getHeaders ().putAll (exchange .getRequestHeaders ());
133141 requestContext .setWriter (responseWriter );
142+ requestContext .setRequestScopedInitializer ((injectionManager ) -> {
143+ injectionManager .<Ref <HttpExchange >>getInstance (httpExchangeType .getType ()).set (exchange );
144+ });
134145 try {
135146 appHandler .handle (requestContext );
136147 } finally {
@@ -208,7 +219,7 @@ public void reload() {
208219 public void reload (final ResourceConfig configuration ) {
209220 appHandler .onShutdown (this );
210221
211- appHandler = new ApplicationHandler (configuration );
222+ appHandler = new ApplicationHandler (configuration , new JdkBinder () );
212223 appHandler .onReload (this );
213224 appHandler .onStartup (this );
214225 }
@@ -236,6 +247,24 @@ void onServerStop() {
236247 this .appHandler .onShutdown (this );
237248 }
238249
250+ private static class JdkBinder extends AbstractBinder {
251+ @ Override
252+ protected void configure () {
253+ bindFactory (ReferencingFactory .<HttpExchange >referenceFactory ()).to (httpExchangeType )
254+ .in (RequestScoped .class );
255+ bindFactory (HttpExchangeReferencingFactory .class ).to (HttpExchange .class )
256+ .proxy (false ).in (RequestScoped .class );
257+ }
258+ }
259+
260+ private static class HttpExchangeReferencingFactory
261+ extends ReferencingFactory <HttpExchange > {
262+ @ Inject
263+ public HttpExchangeReferencingFactory (final Provider <Ref <HttpExchange >> referenceFactory ) {
264+ super (referenceFactory );
265+ }
266+ }
267+
239268 private static final class ResponseWriter implements ContainerResponseWriter {
240269
241270 private final HttpExchange exchange ;
0 commit comments