|
58 | 58 | import java.util.logging.Level;
|
59 | 59 | import java.util.logging.Logger;
|
60 | 60 |
|
61 |
| -import javax.ws.rs.RuntimeType; |
62 |
| -import javax.ws.rs.core.Form; |
63 |
| -import javax.ws.rs.core.MediaType; |
64 |
| -import javax.ws.rs.core.MultivaluedMap; |
65 |
| -import javax.ws.rs.core.Response; |
66 |
| -import javax.ws.rs.core.SecurityContext; |
67 |
| - |
68 | 61 | import javax.inject.Inject;
|
69 | 62 | import javax.inject.Provider;
|
70 | 63 | import javax.inject.Singleton;
|
|
75 | 68 | import javax.servlet.http.HttpServletRequest;
|
76 | 69 | import javax.servlet.http.HttpServletResponse;
|
77 | 70 |
|
| 71 | +import javax.ws.rs.RuntimeType; |
| 72 | +import javax.ws.rs.core.Form; |
| 73 | +import javax.ws.rs.core.MediaType; |
| 74 | +import javax.ws.rs.core.MultivaluedMap; |
| 75 | +import javax.ws.rs.core.Response; |
| 76 | +import javax.ws.rs.core.SecurityContext; |
| 77 | + |
78 | 78 | import org.glassfish.jersey.internal.ServiceFinderBinder;
|
79 | 79 | import org.glassfish.jersey.internal.inject.Providers;
|
80 | 80 | import org.glassfish.jersey.internal.inject.ReferencingFactory;
|
|
86 | 86 | import org.glassfish.jersey.message.internal.MediaTypes;
|
87 | 87 | import org.glassfish.jersey.process.internal.RequestScoped;
|
88 | 88 | import org.glassfish.jersey.server.ApplicationHandler;
|
| 89 | +import org.glassfish.jersey.server.BackgroundSchedulerLiteral; |
89 | 90 | import org.glassfish.jersey.server.ContainerRequest;
|
90 | 91 | import org.glassfish.jersey.server.ResourceConfig;
|
91 | 92 | import org.glassfish.jersey.server.ServerProperties;
|
92 |
| -import org.glassfish.jersey.server.BackgroundSchedulerLiteral; |
93 | 93 | import org.glassfish.jersey.server.internal.InternalServerProperties;
|
94 | 94 | import org.glassfish.jersey.server.spi.RequestScopedInitializer;
|
95 | 95 | import org.glassfish.jersey.servlet.internal.LocalizationMessages;
|
96 | 96 | import org.glassfish.jersey.servlet.internal.PersistenceUnitBinder;
|
97 | 97 | import org.glassfish.jersey.servlet.internal.ResponseWriter;
|
98 | 98 | import org.glassfish.jersey.servlet.internal.ServletContainerProviderFactory;
|
99 | 99 | import org.glassfish.jersey.servlet.internal.Utils;
|
| 100 | +import org.glassfish.jersey.servlet.internal.spi.RequestContextProvider; |
| 101 | +import org.glassfish.jersey.servlet.internal.spi.RequestScopedInitializerProvider; |
100 | 102 | import org.glassfish.jersey.servlet.internal.spi.ServletContainerProvider;
|
101 | 103 | import org.glassfish.jersey.servlet.spi.AsyncContextDelegate;
|
102 | 104 | import org.glassfish.jersey.servlet.spi.AsyncContextDelegateProvider;
|
@@ -139,6 +141,23 @@ public void complete() {
|
139 | 141 | }
|
140 | 142 | };
|
141 | 143 |
|
| 144 | + private final RequestScopedInitializerProvider requestScopedInitializer; |
| 145 | + private final boolean requestResponseBindingExternalized; |
| 146 | + |
| 147 | + private final RequestScopedInitializerProvider DEFAULT_REQUEST_SCOPE_INITIALIZER_PROVIDER = |
| 148 | + new RequestScopedInitializerProvider() { |
| 149 | + @Override |
| 150 | + public RequestScopedInitializer get(final RequestContextProvider context) { |
| 151 | + return new RequestScopedInitializer() { |
| 152 | + @Override |
| 153 | + public void initialize(final ServiceLocator locator) { |
| 154 | + locator.<Ref<HttpServletRequest>>getService(REQUEST_TYPE).set(context.getHttpServletRequest()); |
| 155 | + locator.<Ref<HttpServletResponse>>getService(RESPONSE_TYPE).set(context.getHttpServletResponse()); |
| 156 | + } |
| 157 | + }; |
| 158 | + } |
| 159 | + }; |
| 160 | + |
142 | 161 | /**
|
143 | 162 | * Return the first found {@link AsyncContextDelegateProvider}
|
144 | 163 | * (via {@link Providers#getAllProviders(org.glassfish.hk2.api.ServiceLocator, Class)}) or {@code #DEFAULT_ASYNC_DELEGATE} if
|
@@ -197,15 +216,22 @@ private WebComponentBinder(final Map<String, Object> applicationProperties) {
|
197 | 216 |
|
198 | 217 | @Override
|
199 | 218 | protected void configure() {
|
200 |
| - bindFactory(HttpServletRequestReferencingFactory.class).to(HttpServletRequest.class) |
201 |
| - .proxy(true).proxyForSameScope(false).in(RequestScoped.class); |
202 |
| - bindFactory(ReferencingFactory.<HttpServletRequest>referenceFactory()) |
203 |
| - .to(new TypeLiteral<Ref<HttpServletRequest>>() {}).in(RequestScoped.class); |
204 | 219 |
|
205 |
| - bindFactory(HttpServletResponseReferencingFactory.class).to(HttpServletResponse.class) |
206 |
| - .proxy(true).proxyForSameScope(false).in(RequestScoped.class); |
207 |
| - bindFactory(ReferencingFactory.<HttpServletResponse>referenceFactory()) |
208 |
| - .to(new TypeLiteral<Ref<HttpServletResponse>>() {}).in(RequestScoped.class); |
| 220 | + if (!requestResponseBindingExternalized) { |
| 221 | + |
| 222 | + // request |
| 223 | + bindFactory(HttpServletRequestReferencingFactory.class).to(HttpServletRequest.class) |
| 224 | + .proxy(true).proxyForSameScope(false).in(RequestScoped.class); |
| 225 | + |
| 226 | + bindFactory(ReferencingFactory.<HttpServletRequest>referenceFactory()) |
| 227 | + .to(new TypeLiteral<Ref<HttpServletRequest>>() {}).in(RequestScoped.class); |
| 228 | + |
| 229 | + // response |
| 230 | + bindFactory(HttpServletResponseReferencingFactory.class).to(HttpServletResponse.class) |
| 231 | + .proxy(true).proxyForSameScope(false).in(RequestScoped.class); |
| 232 | + bindFactory(ReferencingFactory.<HttpServletResponse>referenceFactory()) |
| 233 | + .to(new TypeLiteral<Ref<HttpServletResponse>>() {}).in(RequestScoped.class); |
| 234 | + } |
209 | 235 |
|
210 | 236 | bindFactory(new Factory<ServletContext>() {
|
211 | 237 | @Override
|
@@ -327,8 +353,27 @@ public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) th
|
327 | 353 | resourceConfig = createResourceConfig(webConfig);
|
328 | 354 | }
|
329 | 355 |
|
| 356 | + |
| 357 | + final ServletContainerProvider[] allServletContainerProviders = |
| 358 | + ServletContainerProviderFactory.getAllServletContainerProviders(); |
| 359 | + |
330 | 360 | // SPI/extension hook to configure ResourceConfig
|
331 |
| - configure(resourceConfig); |
| 361 | + configure(resourceConfig, allServletContainerProviders); |
| 362 | + |
| 363 | + boolean rrbExternalized = false; |
| 364 | + RequestScopedInitializerProvider rsiProvider = null; |
| 365 | + |
| 366 | + for (final ServletContainerProvider servletContainerProvider : allServletContainerProviders) { |
| 367 | + if (servletContainerProvider.bindsServletRequestResponse()) { |
| 368 | + rrbExternalized = true; |
| 369 | + } |
| 370 | + if (rsiProvider == null) { // try to take the first non-null provider |
| 371 | + rsiProvider = servletContainerProvider.getRequestScopedInitializerProvider(); |
| 372 | + } |
| 373 | + } |
| 374 | + |
| 375 | + requestScopedInitializer = rsiProvider != null ? rsiProvider : DEFAULT_REQUEST_SCOPE_INITIALIZER_PROVIDER; |
| 376 | + requestResponseBindingExternalized = rrbExternalized; |
332 | 377 |
|
333 | 378 | final AbstractBinder webComponentBinder = new WebComponentBinder(resourceConfig.getProperties());
|
334 | 379 | resourceConfig.register(webComponentBinder);
|
@@ -391,13 +436,19 @@ public Value<Integer> service(
|
391 | 436 | asyncExtensionDelegate.createDelegate(servletRequest, servletResponse),
|
392 | 437 | backgroundTaskScheduler);
|
393 | 438 |
|
394 |
| - requestContext.setRequestScopedInitializer(new RequestScopedInitializer() { |
| 439 | + requestContext.setRequestScopedInitializer(requestScopedInitializer.get(new RequestContextProvider() { |
| 440 | + |
395 | 441 | @Override
|
396 |
| - public void initialize(final ServiceLocator locator) { |
397 |
| - locator.<Ref<HttpServletRequest>>getService(REQUEST_TYPE).set(servletRequest); |
398 |
| - locator.<Ref<HttpServletResponse>>getService(RESPONSE_TYPE).set(servletResponse); |
| 442 | + public HttpServletRequest getHttpServletRequest() { |
| 443 | + return servletRequest; |
399 | 444 | }
|
400 |
| - }); |
| 445 | + |
| 446 | + @Override |
| 447 | + public HttpServletResponse getHttpServletResponse() { |
| 448 | + return servletResponse; |
| 449 | + } |
| 450 | + })); |
| 451 | + |
401 | 452 | requestContext.setWriter(responseWriter);
|
402 | 453 |
|
403 | 454 | appHandler.handle(requestContext);
|
@@ -520,9 +571,9 @@ private static ResourceConfig createResourceConfig(final WebConfig config) throw
|
520 | 571 | * @param resourceConfig Jersey application configuration.
|
521 | 572 | * @throws ServletException if an error has occurred.
|
522 | 573 | */
|
523 |
| - private static void configure(final ResourceConfig resourceConfig) throws ServletException { |
524 |
| - final ServletContainerProvider[] allServletContainerProviders = |
525 |
| - ServletContainerProviderFactory.getAllServletContainerProviders(); |
| 574 | + private void configure(final ResourceConfig resourceConfig, |
| 575 | + final ServletContainerProvider[] allServletContainerProviders) throws ServletException { |
| 576 | + |
526 | 577 | for (final ServletContainerProvider servletContainerProvider : allServletContainerProviders) {
|
527 | 578 | servletContainerProvider.configure(resourceConfig);
|
528 | 579 | }
|
|
0 commit comments