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

Improve documentation on how to write a response body in Jetty 12 #10293

Closed
cowwoc opened this issue Aug 10, 2023 · 4 comments · Fixed by #10434
Closed

Improve documentation on how to write a response body in Jetty 12 #10293

cowwoc opened this issue Aug 10, 2023 · 4 comments · Fixed by #10434

Comments

@cowwoc
Copy link
Contributor

cowwoc commented Aug 10, 2023

Jetty version
12.0.0

Java version
20

Question
What is the correct write to write a response body in Jetty 12 using the Response object? I saw void write(boolean last, ByteBuffer byteBuffer, Callback callback) but the Javadoc doesn't indicate what exactly it is writing (the body? the headers? something else?) and it's not clear how to best carry out this operation in a synchronous manner (for those of us who wants to stick with synchronous calls).

@cowwoc
Copy link
Contributor Author

cowwoc commented Aug 10, 2023

This is the best I could come up with but I'm hoping there is a cleaner solution:

CountDownLatch latch = new CountDownLatch(1);
response.write(true, StandardCharsets.UTF_8.encode(body), Callback.from(latch::countDown,
throwable ->
{
	throw RuntimeException(throwable);
}));
latch.await();

@joakime
Copy link
Contributor

joakime commented Aug 10, 2023

There are a few ways to write response body.
The Response.write() methods will write the body (and commit the response, by writing headers if not written yet)

Also of note, the Response is a Content.Sink, so check out the static methods there.

See some example code snippets at

https://github.com/eclipse/jetty.project/blob/jetty-12.0.x/documentation/jetty-documentation/src/main/java/org/eclipse/jetty/docs/programming/server/HandlerDocs.java

Eg: you an use an OutputStream if you wanted, or a JDK Flow API, or a bunch of other options.

@cowwoc
Copy link
Contributor Author

cowwoc commented Aug 10, 2023

@joakime Thank you. By the way, I think you have a bug at https://eclipse.dev/jetty/javadoc/jetty-12/org/eclipse/jetty/server/Handler.html

The Javadoc contains this code snippet:

response.write(true, callback, "hello");

As far as I can tell, there is no such method.

Also, https://eclipse.dev/jetty/javadoc/jetty-12/org/eclipse/jetty/server/Request.Handler.html#handle(org.eclipse.jetty.server.Request,org.eclipse.jetty.server.Response,org.eclipse.jetty.util.Callback) contains ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)). I suggest replacing this with StandardCharsets.UTF_8.encode("Hello World\n") which is shorter and easier to read.

@joakime joakime changed the title How to write a response body in Jetty 12? Improve documentation on how to write a response body in Jetty 12 Aug 11, 2023
@sbordet sbordet self-assigned this Aug 27, 2023
sbordet added a commit that referenced this issue Aug 29, 2023
…in Jetty 12.

Updated documentation about:
* Content.Source
* Content.Sink
* Handler
* Request/Response

Updated few APIs to make easier to write applications.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
sbordet added a commit that referenced this issue Aug 29, 2023
…in Jetty 12.

Updated documentation about:
* Content.Source
* Content.Sink
* Handler
* Request/Response

Updated few APIs to make easier to write applications.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
lorban pushed a commit that referenced this issue Aug 30, 2023
…in Jetty 12.

Updated documentation about:
* Content.Source
* Content.Sink
* Handler
* Request/Response

Updated few APIs to make easier to write applications.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
@sbordet
Copy link
Contributor

sbordet commented Aug 30, 2023

Fixed by #10434.

@sbordet sbordet closed this as completed Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants