Skip to content

Commit 01b88fe

Browse files
authoredApr 25, 2022
Release MessageBodyWorkers when Response gets closed. (#5039)
Signed-off-by: jansupol <jan.supol@oracle.com>
1 parent 1b93b50 commit 01b88fe

File tree

5 files changed

+79
-68
lines changed

5 files changed

+79
-68
lines changed
 

‎core-common/src/main/java/org/glassfish/jersey/message/internal/InboundMessageContext.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -754,6 +754,9 @@ public Link.Builder getLinkBuilder(String relation) {
754754
* @return context message body workers.
755755
*/
756756
public MessageBodyWorkers getWorkers() {
757+
if (workers == null) {
758+
throw new ProcessingException(LocalizationMessages.RESPONSE_CLOSED());
759+
}
757760
return workers;
758761
}
759762

@@ -948,6 +951,7 @@ public boolean bufferEntity() throws ProcessingException {
948951
*/
949952
public void close() {
950953
entityContent.close(true);
954+
setWorkers(null);
951955
}
952956

953957
/**

‎core-server/src/main/java/org/glassfish/jersey/server/ContainerResponse.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -388,6 +388,7 @@ public void close() {
388388
closed = true;
389389
messageContext.close();
390390
requestContext.getResponseWriter().commit();
391+
requestContext.setWorkers(null);
391392
}
392393
}
393394

‎core-server/src/test/java/org/glassfish/jersey/server/RequestContextBuilder.java

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -79,31 +79,33 @@ public void setEntity(final Object entity) {
7979
public void setWorkers(final MessageBodyWorkers workers) {
8080
super.setWorkers(workers);
8181
final byte[] entityBytes;
82-
if (entity != null) {
83-
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
84-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
85-
OutputStream stream = null;
86-
try {
87-
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
88-
new Annotation[0], getMediaType(),
89-
myMap,
90-
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
91-
} catch (final IOException | WebApplicationException ex) {
92-
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
93-
} finally {
94-
if (stream != null) {
95-
try {
96-
stream.close();
97-
} catch (final IOException e) {
98-
// ignore
82+
if (workers != null) {
83+
if (entity != null) {
84+
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
85+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
86+
OutputStream stream = null;
87+
try {
88+
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
89+
new Annotation[0], getMediaType(),
90+
myMap,
91+
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
92+
} catch (final IOException | WebApplicationException ex) {
93+
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
94+
} finally {
95+
if (stream != null) {
96+
try {
97+
stream.close();
98+
} catch (final IOException e) {
99+
// ignore
100+
}
99101
}
100102
}
103+
entityBytes = baos.toByteArray();
104+
} else {
105+
entityBytes = new byte[0];
101106
}
102-
entityBytes = baos.toByteArray();
103-
} else {
104-
entityBytes = new byte[0];
107+
setEntityStream(new ByteArrayInputStream(entityBytes));
105108
}
106-
setEntityStream(new ByteArrayInputStream(entityBytes));
107109
}
108110
}
109111

‎tests/e2e-inject/cdi-inject-weld/src/test/java/org/glassfish/jersey/tests/e2e/inject/cdi/weld/RequestContextBuilder.java

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -80,31 +80,33 @@ public void setEntity(final Object entity) {
8080
public void setWorkers(final MessageBodyWorkers workers) {
8181
super.setWorkers(workers);
8282
final byte[] entityBytes;
83-
if (entity != null) {
84-
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
85-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
86-
OutputStream stream = null;
87-
try {
88-
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
89-
new Annotation[0], getMediaType(),
90-
myMap,
91-
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
92-
} catch (final IOException | WebApplicationException ex) {
93-
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
94-
} finally {
95-
if (stream != null) {
96-
try {
97-
stream.close();
98-
} catch (final IOException e) {
99-
// ignore
83+
if (workers != null) {
84+
if (entity != null) {
85+
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
86+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
87+
OutputStream stream = null;
88+
try {
89+
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
90+
new Annotation[0], getMediaType(),
91+
myMap,
92+
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
93+
} catch (final IOException | WebApplicationException ex) {
94+
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
95+
} finally {
96+
if (stream != null) {
97+
try {
98+
stream.close();
99+
} catch (final IOException e) {
100+
// ignore
101+
}
100102
}
101103
}
104+
entityBytes = baos.toByteArray();
105+
} else {
106+
entityBytes = new byte[0];
102107
}
103-
entityBytes = baos.toByteArray();
104-
} else {
105-
entityBytes = new byte[0];
108+
setEntityStream(new ByteArrayInputStream(entityBytes));
106109
}
107-
setEntityStream(new ByteArrayInputStream(entityBytes));
108110
}
109111
}
110112

‎tests/e2e-inject/cdi2-se/src/test/java/org/glassfish/jersey/tests/e2e/inject/cdi/se/RequestContextBuilder.java

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -80,31 +80,33 @@ public void setEntity(final Object entity) {
8080
public void setWorkers(final MessageBodyWorkers workers) {
8181
super.setWorkers(workers);
8282
final byte[] entityBytes;
83-
if (entity != null) {
84-
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
85-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
86-
OutputStream stream = null;
87-
try {
88-
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
89-
new Annotation[0], getMediaType(),
90-
myMap,
91-
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
92-
} catch (final IOException | WebApplicationException ex) {
93-
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
94-
} finally {
95-
if (stream != null) {
96-
try {
97-
stream.close();
98-
} catch (final IOException e) {
99-
// ignore
83+
if (workers != null) {
84+
if (entity != null) {
85+
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
86+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
87+
OutputStream stream = null;
88+
try {
89+
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
90+
new Annotation[0], getMediaType(),
91+
myMap,
92+
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
93+
} catch (final IOException | WebApplicationException ex) {
94+
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
95+
} finally {
96+
if (stream != null) {
97+
try {
98+
stream.close();
99+
} catch (final IOException e) {
100+
// ignore
101+
}
100102
}
101103
}
104+
entityBytes = baos.toByteArray();
105+
} else {
106+
entityBytes = new byte[0];
102107
}
103-
entityBytes = baos.toByteArray();
104-
} else {
105-
entityBytes = new byte[0];
108+
setEntityStream(new ByteArrayInputStream(entityBytes));
106109
}
107-
setEntityStream(new ByteArrayInputStream(entityBytes));
108110
}
109111
}
110112

0 commit comments

Comments
 (0)