Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release MessageBodyWorkers when Response gets closed. #5039

Merged
merged 3 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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