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

Commit b8cb188

Browse files
committed
JERSEY-2812: When Jersey deployed as servlet filter, a server-side thread processing a http request to an async resource got stuck provided the AsyncResponse was not resumed in the resource.
Change-Id: I24765f7fbf02ba692fc26486a1996b4918aaa87f
1 parent 9d5c217 commit b8cb188

File tree

17 files changed

+550
-35
lines changed

17 files changed

+550
-35
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ public void init() throws ServletException {
373373
* @param response the {@link javax.servlet.http.HttpServletResponse} object that
374374
* contains the response the Web component returns
375375
* to the client.
376-
* @return lazily initialized response status code {@link Value value provider}.
376+
* @return lazily initialized response status code {@link Value value provider}. If not resolved in the moment of call to
377+
* {@link Value#get()}, {@code -1} is returned.
377378
* @throws IOException if an input or output error occurs
378379
* while the Web component is handling the
379380
* HTTP request.
@@ -548,11 +549,14 @@ private void doFilter(final HttpServletRequest request, final HttpServletRespons
548549

549550
// If forwarding is configured and response is a 404 with no entity
550551
// body then call the next filter in the chain
551-
if (webComponent.forwardOn404 && !response.isCommitted()
552+
553+
if (webComponent.forwardOn404
554+
&& !response.isCommitted()
552555
// TODO when switched to servlet-api-3.0 and higher, use response.getStatus() to retrieve the status
553556
// statusValue.get() forwards the call to
554-
// org.glassfish.jersey.servlet.internal.ResponseWriter#getResponseContext() which may block the thread
555-
// as a consequence, we must call it only if we're sure it will not block unintentionally
557+
// org.glassfish.jersey.servlet.internal.ResponseWriter#getResponseContext() which may block the thread.
558+
// As a consequence, we must call it only if we're sure it will not block.
559+
// See Jersey2730ITCase and Jersey2812ITCase tests.
556560
&& statusValue.get() == Response.Status.NOT_FOUND.getStatusCode()) {
557561
// lets clear the response to OK before we forward to the next in the chain
558562
// as OK is the default set by servlet containers before filters/servlets do any wor

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) th
359359
* @param servletResponse the {@link javax.servlet.http.HttpServletResponse} object that
360360
* contains the response the Web component returns
361361
* to the client.
362-
* @return lazily initialized response status code {@link Value value provider}.
362+
* @return lazily initialized response status code {@link Value value provider}. If not resolved in the moment of call to
363+
* {@link Value#get()}, {@code -1} is returned.
363364
* @throws java.io.IOException if an input or output error occurs
364365
* while the Web component is handling the
365366
* HTTP request.
@@ -404,7 +405,7 @@ public void initialize(final ServiceLocator locator) {
404405
return Values.lazy(new Value<Integer>() {
405406
@Override
406407
public Integer get() {
407-
return responseWriter.getResponseStatus();
408+
return responseWriter.responseContextResolved() ? responseWriter.getResponseStatus() : -1;
408409
}
409410
});
410411
} catch (final HeaderValueException hve) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ public int getResponseStatus() {
268268
return getResponseContext().getStatus();
269269
}
270270

271+
public boolean responseContextResolved() {
272+
return responseContext.isDone();
273+
}
274+
271275
private ContainerResponse getResponseContext() {
272276
try {
273277
return responseContext.get();

tests/integration/jersey-2730/pom.xml renamed to tests/integration/async-jersey-filter/pom.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@
5050
<version>2.18-SNAPSHOT</version>
5151
</parent>
5252

53-
<artifactId>jersey-2730</artifactId>
53+
<artifactId>async-jersey-filter</artifactId>
5454
<packaging>war</packaging>
55-
<name>jersey-tests-integration-jersey-2730</name>
55+
<name>jersey-tests-integration-jersey-async-filter</name>
5656

5757
<description>
58-
This web project reproduces ticket JERSEY-2730
58+
This web project reproduces tickets:
59+
JERSEY-2730
60+
JERSEY-2812
5961
</description>
6062

6163
<dependencies>
@@ -68,6 +70,12 @@
6870
<artifactId>jersey-test-framework-provider-external</artifactId>
6971
<scope>test</scope>
7072
</dependency>
73+
<dependency>
74+
<groupId>javax.servlet</groupId>
75+
<artifactId>javax.servlet-api</artifactId>
76+
<version>${servlet3.version}</version>
77+
<scope>provided</scope>
78+
</dependency>
7179
</dependencies>
7280

7381
<build>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
* holder.
3939
*/
4040

41-
package org.glassfish.jersey.tests.integration.jersey2730;
41+
package org.glassfish.jersey.tests.integration.async;
4242

4343
import javax.ws.rs.ApplicationPath;
4444

4545
import org.glassfish.jersey.server.ResourceConfig;
4646

47+
import org.glassfish.jersey.tests.integration.jersey2730.TestExceptionResource;
4748
import org.glassfish.jersey.tests.integration.jersey2730.exception.MappedExceptionMapper;
49+
import org.glassfish.jersey.tests.integration.jersey2812.TestWaitResource;
4850

4951
/**
5052
* Jersey application for JERSEY-2730.
@@ -57,5 +59,6 @@ public class TestApplication extends ResourceConfig {
5759
public TestApplication() {
5860
register(TestExceptionResource.class);
5961
register(MappedExceptionMapper.class);
62+
register(TestWaitResource.class);
6063
}
6164
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import javax.inject.Singleton;
4848

4949
import org.glassfish.jersey.servlet.internal.ResponseWriter;
50-
5150
import org.glassfish.jersey.tests.integration.jersey2730.exception.MappedException;
5251
import org.glassfish.jersey.tests.integration.jersey2730.exception.UnmappedException;
5352
import org.glassfish.jersey.tests.integration.jersey2730.exception.UnmappedRuntimeException;

0 commit comments

Comments
 (0)