Skip to content
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
10 changes: 0 additions & 10 deletions avaje-jex/src/main/java/io/avaje/jex/core/JdkContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,16 +493,6 @@ public URI uri() {
return exchange.getRequestURI();
}

@Override
public void write(byte[] bytes) {
try (var os = exchange.getResponseBody()) {
exchange.sendResponseHeaders(statusCode(), bytes.length == 0 ? -1 : bytes.length);
os.write(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public void write(byte[] bufferBytes, int length) {
try (var os = exchange.getResponseBody()) {
Expand Down
18 changes: 16 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/http/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.sun.net.httpserver.HttpExchange;

import io.avaje.jex.core.Constants;
import io.avaje.jex.core.json.JsonbOutput;
import io.avaje.jex.security.BasicAuthCredentials;
import io.avaje.jex.security.Role;
import io.avaje.jsonb.JsonType;

/** Provides access to functions for handling the request and response. */
public interface Context {
Expand Down Expand Up @@ -245,6 +247,16 @@ default Context headers(Map<String, String> headers) {
*/
void json(Object bean);

/**
* Optimized json write using avaje jsonb
*
* @param jsonType the serializer for the value.
* @param value the pojo to serialize
*/
default <T> void jsonb(JsonType<T> jsonType, T value) {
jsonType.toJson(value, JsonbOutput.of(this.contentType(ContentType.APPLICATION_JSON)));
}

/**
* Write the stream as a JSON stream with new line delimiters {@literal
* application/x-json-stream}.
Expand Down Expand Up @@ -447,10 +459,12 @@ default String userAgent() {
*
* @param bytes The byte array to write.
*/
void write(byte[] bytes);
default void write(byte[] bytes) {
write(bytes, bytes.length);
}

/**
* Writes the first bytes from this buffer directly to the response.
* Writes the given length of bytes from this buffer directly to the response.
*
* <p>The bytes written will be from position 0 to length.
*
Expand Down