Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 97df720

Browse files
japodGerrit Code Review
authored andcommitted
Merge "JERSEY-2855: HttpServletRequest problems in WLS - introduced new methods in ServletContainerProvider SPI to allow a non standard HTTP Servlet request/response binding in the HK2 layer and/or custom request reference initialization"
2 parents 9fe1a89 + c1d2c9a commit 97df720

File tree

26 files changed

+2381
-30
lines changed

26 files changed

+2381
-30
lines changed

containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@
5858
import java.util.logging.Level;
5959
import java.util.logging.Logger;
6060

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-
6861
import javax.inject.Inject;
6962
import javax.inject.Provider;
7063
import javax.inject.Singleton;
@@ -75,6 +68,13 @@
7568
import javax.servlet.http.HttpServletRequest;
7669
import javax.servlet.http.HttpServletResponse;
7770

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+
7878
import org.glassfish.jersey.internal.ServiceFinderBinder;
7979
import org.glassfish.jersey.internal.inject.Providers;
8080
import org.glassfish.jersey.internal.inject.ReferencingFactory;
@@ -86,17 +86,19 @@
8686
import org.glassfish.jersey.message.internal.MediaTypes;
8787
import org.glassfish.jersey.process.internal.RequestScoped;
8888
import org.glassfish.jersey.server.ApplicationHandler;
89+
import org.glassfish.jersey.server.BackgroundSchedulerLiteral;
8990
import org.glassfish.jersey.server.ContainerRequest;
9091
import org.glassfish.jersey.server.ResourceConfig;
9192
import org.glassfish.jersey.server.ServerProperties;
92-
import org.glassfish.jersey.server.BackgroundSchedulerLiteral;
9393
import org.glassfish.jersey.server.internal.InternalServerProperties;
9494
import org.glassfish.jersey.server.spi.RequestScopedInitializer;
9595
import org.glassfish.jersey.servlet.internal.LocalizationMessages;
9696
import org.glassfish.jersey.servlet.internal.PersistenceUnitBinder;
9797
import org.glassfish.jersey.servlet.internal.ResponseWriter;
9898
import org.glassfish.jersey.servlet.internal.ServletContainerProviderFactory;
9999
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;
100102
import org.glassfish.jersey.servlet.internal.spi.ServletContainerProvider;
101103
import org.glassfish.jersey.servlet.spi.AsyncContextDelegate;
102104
import org.glassfish.jersey.servlet.spi.AsyncContextDelegateProvider;
@@ -139,6 +141,23 @@ public void complete() {
139141
}
140142
};
141143

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+
142161
/**
143162
* Return the first found {@link AsyncContextDelegateProvider}
144163
* (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) {
197216

198217
@Override
199218
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);
204219

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+
}
209235

210236
bindFactory(new Factory<ServletContext>() {
211237
@Override
@@ -327,8 +353,27 @@ public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) th
327353
resourceConfig = createResourceConfig(webConfig);
328354
}
329355

356+
357+
final ServletContainerProvider[] allServletContainerProviders =
358+
ServletContainerProviderFactory.getAllServletContainerProviders();
359+
330360
// 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;
332377

333378
final AbstractBinder webComponentBinder = new WebComponentBinder(resourceConfig.getProperties());
334379
resourceConfig.register(webComponentBinder);
@@ -391,13 +436,19 @@ public Value<Integer> service(
391436
asyncExtensionDelegate.createDelegate(servletRequest, servletResponse),
392437
backgroundTaskScheduler);
393438

394-
requestContext.setRequestScopedInitializer(new RequestScopedInitializer() {
439+
requestContext.setRequestScopedInitializer(requestScopedInitializer.get(new RequestContextProvider() {
440+
395441
@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;
399444
}
400-
});
445+
446+
@Override
447+
public HttpServletResponse getHttpServletResponse() {
448+
return servletResponse;
449+
}
450+
}));
451+
401452
requestContext.setWriter(responseWriter);
402453

403454
appHandler.handle(requestContext);
@@ -520,9 +571,9 @@ private static ResourceConfig createResourceConfig(final WebConfig config) throw
520571
* @param resourceConfig Jersey application configuration.
521572
* @throws ServletException if an error has occurred.
522573
*/
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+
526577
for (final ServletContainerProvider servletContainerProvider : allServletContainerProviders) {
527578
servletContainerProvider.configure(resourceConfig);
528579
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.servlet.internal.spi;
41+
42+
import java.lang.reflect.Type;
43+
import java.util.Set;
44+
45+
import javax.servlet.ServletContext;
46+
import javax.servlet.ServletException;
47+
import javax.servlet.http.HttpServletRequest;
48+
import javax.servlet.http.HttpServletResponse;
49+
50+
import org.glassfish.jersey.internal.util.collection.Ref;
51+
import org.glassfish.jersey.server.ResourceConfig;
52+
import org.glassfish.jersey.server.spi.RequestScopedInitializer;
53+
54+
import org.glassfish.hk2.api.ServiceLocator;
55+
import org.glassfish.hk2.api.TypeLiteral;
56+
57+
/**
58+
* Basic {@link ServletContainerProvider} that provides
59+
* dummy no-op method implementation. It should be convenient to extend if you only need to implement
60+
* a subset of the original SPI methods.
61+
*
62+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
63+
*/
64+
public class NoOpServletContainerProvider implements ServletContainerProvider {
65+
66+
public final Type HTTP_SERVLET_REQUEST_TYPE = (new TypeLiteral<Ref<HttpServletRequest>>() {
67+
}).getType();
68+
public final Type HTTP_SERVLET_RESPONSE_TYPE = (new TypeLiteral<Ref<HttpServletResponse>>() {
69+
}).getType();
70+
71+
@Override
72+
public void preInit(final ServletContext servletContext, final Set<Class<?>> classes) throws ServletException {
73+
// no-op
74+
}
75+
76+
@Override
77+
public void postInit(
78+
final ServletContext servletContext, final Set<Class<?>> classes, final Set<String> servletNames) {
79+
// no-op
80+
}
81+
82+
@Override
83+
public void onRegister(
84+
final ServletContext servletContext, final Set<String> servletNames) throws ServletException {
85+
// no-op
86+
}
87+
88+
@Override
89+
public void configure(final ResourceConfig resourceConfig) throws ServletException {
90+
// no-op
91+
}
92+
93+
@Override
94+
public boolean bindsServletRequestResponse() {
95+
return false;
96+
}
97+
98+
@Override
99+
public RequestScopedInitializerProvider getRequestScopedInitializerProvider() {
100+
return null;
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.servlet.internal.spi;
41+
42+
import javax.servlet.http.HttpServletRequest;
43+
import javax.servlet.http.HttpServletResponse;
44+
45+
/**
46+
* Provide access to the actual servlet request/response.
47+
*
48+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
49+
* @see {@link RequestScopedInitializerProvider}
50+
*/
51+
public interface RequestContextProvider {
52+
53+
/**
54+
* Get me the actual HTTP Servlet request.
55+
*
56+
* @return actual HTTP Servlet request.
57+
*/
58+
public HttpServletRequest getHttpServletRequest();
59+
60+
/**
61+
* Get me the actual HTTP Servlet response.
62+
*
63+
* @return actual HTTP Servlet response.
64+
*/
65+
public HttpServletResponse getHttpServletResponse();
66+
}

0 commit comments

Comments
 (0)