-
Notifications
You must be signed in to change notification settings - Fork 25.7k
ensure the XContentBuilder is always closed in RestBuilderListener #21124
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
Changes from 1 commit
773f502
5a17d14
c07e568
685195d
c718dac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,19 +28,25 @@ | |
| */ | ||
| public abstract class RestBuilderListener<Response> extends RestResponseListener<Response> { | ||
|
|
||
| // pkg-private static boolean that enables testing of the auto-close functionality | ||
| static boolean assertionsEnabled = true; | ||
|
|
||
| public RestBuilderListener(RestChannel channel) { | ||
| super(channel); | ||
| } | ||
|
|
||
| @Override | ||
| public final RestResponse buildResponse(Response response) throws Exception { | ||
| try (XContentBuilder builder = channel.newBuilder()){ | ||
| return buildResponse(response, builder); | ||
| try (XContentBuilder builder = channel.newBuilder()) { | ||
| final RestResponse restResponse = buildResponse(response, builder); | ||
| assert assertionsEnabled == false || builder.isClosed() : "callers should ensure the XContentBuilder is closed themselves"; | ||
|
||
| return restResponse; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Builds a response to send back over the channel. | ||
| * Builds a response to send back over the channel. Implementors should ensure that they close the provided {@link XContentBuilder} | ||
| * using the {@link XContentBuilder#close()} method. | ||
| */ | ||
| public abstract RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be enough to only add
isClosedto XContentGenerator? and maybe add a javadoc?