Skip to content

Commit

Permalink
Release MessageBodyWorkers when Response gets closed. (#5039)
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Apr 25, 2022
1 parent 1b93b50 commit 01b88fe
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -754,6 +754,9 @@ public Link.Builder getLinkBuilder(String relation) {
* @return context message body workers.
*/
public MessageBodyWorkers getWorkers() {
if (workers == null) {
throw new ProcessingException(LocalizationMessages.RESPONSE_CLOSED());
}
return workers;
}

Expand Down Expand Up @@ -948,6 +951,7 @@ public boolean bufferEntity() throws ProcessingException {
*/
public void close() {
entityContent.close(true);
setWorkers(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -388,6 +388,7 @@ public void close() {
closed = true;
messageContext.close();
requestContext.getResponseWriter().commit();
requestContext.setWorkers(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -79,31 +79,33 @@ public void setEntity(final Object entity) {
public void setWorkers(final MessageBodyWorkers workers) {
super.setWorkers(workers);
final byte[] entityBytes;
if (entity != null) {
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream stream = null;
try {
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
new Annotation[0], getMediaType(),
myMap,
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
} catch (final IOException | WebApplicationException ex) {
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (stream != null) {
try {
stream.close();
} catch (final IOException e) {
// ignore
if (workers != null) {
if (entity != null) {
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream stream = null;
try {
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
new Annotation[0], getMediaType(),
myMap,
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
} catch (final IOException | WebApplicationException ex) {
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (stream != null) {
try {
stream.close();
} catch (final IOException e) {
// ignore
}
}
}
entityBytes = baos.toByteArray();
} else {
entityBytes = new byte[0];
}
entityBytes = baos.toByteArray();
} else {
entityBytes = new byte[0];
setEntityStream(new ByteArrayInputStream(entityBytes));
}
setEntityStream(new ByteArrayInputStream(entityBytes));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -80,31 +80,33 @@ public void setEntity(final Object entity) {
public void setWorkers(final MessageBodyWorkers workers) {
super.setWorkers(workers);
final byte[] entityBytes;
if (entity != null) {
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream stream = null;
try {
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
new Annotation[0], getMediaType(),
myMap,
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
} catch (final IOException | WebApplicationException ex) {
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (stream != null) {
try {
stream.close();
} catch (final IOException e) {
// ignore
if (workers != null) {
if (entity != null) {
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream stream = null;
try {
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
new Annotation[0], getMediaType(),
myMap,
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
} catch (final IOException | WebApplicationException ex) {
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (stream != null) {
try {
stream.close();
} catch (final IOException e) {
// ignore
}
}
}
entityBytes = baos.toByteArray();
} else {
entityBytes = new byte[0];
}
entityBytes = baos.toByteArray();
} else {
entityBytes = new byte[0];
setEntityStream(new ByteArrayInputStream(entityBytes));
}
setEntityStream(new ByteArrayInputStream(entityBytes));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -80,31 +80,33 @@ public void setEntity(final Object entity) {
public void setWorkers(final MessageBodyWorkers workers) {
super.setWorkers(workers);
final byte[] entityBytes;
if (entity != null) {
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream stream = null;
try {
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
new Annotation[0], getMediaType(),
myMap,
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
} catch (final IOException | WebApplicationException ex) {
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (stream != null) {
try {
stream.close();
} catch (final IOException e) {
// ignore
if (workers != null) {
if (entity != null) {
final MultivaluedMap<String, Object> myMap = new MultivaluedHashMap<String, Object>(getHeaders());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream stream = null;
try {
stream = workers.writeTo(entity, entity.getClass(), entityType.getType(),
new Annotation[0], getMediaType(),
myMap,
propertiesDelegate, baos, Collections.<WriterInterceptor>emptyList());
} catch (final IOException | WebApplicationException ex) {
Logger.getLogger(TestContainerRequest.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (stream != null) {
try {
stream.close();
} catch (final IOException e) {
// ignore
}
}
}
entityBytes = baos.toByteArray();
} else {
entityBytes = new byte[0];
}
entityBytes = baos.toByteArray();
} else {
entityBytes = new byte[0];
setEntityStream(new ByteArrayInputStream(entityBytes));
}
setEntityStream(new ByteArrayInputStream(entityBytes));
}
}

Expand Down

0 comments on commit 01b88fe

Please sign in to comment.