Skip to content

Commit

Permalink
Expose Context.outputStream() such that outputStream can be used dire…
Browse files Browse the repository at this point in the history
…ctly (#109)

For example, with Jsonb we can obtain the specific type once and then
write directly to the outputSteam like:

           ctx.status(200).contentType("application/json");
           var result = HelloDto.rob();
           jsonTypeHelloDto.toJson(result, ctx.outputStream());

... where jsonTypeHelloDto is obtained like:

  static final Jsonb jsonb = Jsonb.builder().build();
  static final JsonType<HelloDto> jsonTypeHelloDto = jsonb.type(HelloDto.class);

With this approach, it effectively bypasses the underlying io.avaje.jex.spi.JsonService with the view that this can be more flexible and more efficient.
  • Loading branch information
rbygrave authored Dec 1, 2024
1 parent 04ece11 commit e0f489f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
6 changes: 6 additions & 0 deletions avaje-jex/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jsonb-generator</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
10 changes: 10 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static java.util.Collections.emptyMap;

import java.io.InputStream;
import java.io.OutputStream;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Iterator;
Expand Down Expand Up @@ -294,6 +295,15 @@ default String userAgent() {
*/
Context write(InputStream is);

/**
* Return the outputStream to write content. It is expected that
* the {@link #contentType(String)} has been set prior to obtaining
* and writing to the outputStream.
*
* @return The outputStream to write content to.
*/
OutputStream outputStream();

/**
* Render a template typically as html.
*
Expand Down
1 change: 1 addition & 0 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ public String protocol() {
return exchange.getProtocol();
}

@Override
public OutputStream outputStream() {
var out = mgr.createOutputStream(this);
if (compressionConfig.compressionEnabled()) {
Expand Down
3 changes: 3 additions & 0 deletions avaje-jex/src/test/java/io/avaje/jex/jdk/HelloDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.avaje.jex.jdk;

import io.avaje.jsonb.Json;

@Json
public class HelloDto {

public long id;
Expand Down
39 changes: 36 additions & 3 deletions avaje-jex/src/test/java/io/avaje/jex/jdk/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.concurrent.locks.LockSupport;
import java.util.stream.Stream;

import io.avaje.jsonb.JsonType;
import io.avaje.jsonb.Jsonb;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

Expand All @@ -26,12 +28,19 @@ private static AutoCloseIterator<HelloDto> createBeanIterator() {
return new AutoCloseIterator<>(HELLO_BEANS.iterator());
}

static TestPair pair = init();
static final TestPair pair = init();
static final Jsonb jsonb = Jsonb.builder().build();
static final JsonType<HelloDto> jsonTypeHelloDto = jsonb.type(HelloDto.class);

static TestPair init() {
Jex app = Jex.create()
.routing(routing -> routing
.get("/", ctx -> ctx.json(HelloDto.rob()).status(200))
.get("/", ctx -> ctx.status(200).json(HelloDto.rob()))
.get("/usingOutputStream", ctx -> {
ctx.status(200).contentType("application/json");
var result = HelloDto.rob();
jsonTypeHelloDto.toJson(result, ctx.outputStream());
})
.get("/iterate", ctx -> ctx.jsonStream(ITERATOR))
.get("/stream", ctx -> ctx.jsonStream(HELLO_BEANS.stream()))
.post("/", ctx -> ctx.text("bean[" + ctx.bodyAsClass(HelloDto.class) + "]")));
Expand All @@ -58,9 +67,33 @@ void get() {
.GET().asString();

final HttpHeaders headers = hres.headers();
assertThat(headers.firstValue("Content-Type").get()).isEqualTo("application/json");
assertThat(headers.firstValue("Content-Type").orElseThrow()).isEqualTo("application/json");
}

@Test
void usingOutputStream() {

var bean = pair.request().path("usingOutputStream")
.GET()
.bean(HelloDto.class);

assertThat(bean.id).isEqualTo(42);
assertThat(bean.name).isEqualTo("rob");

final HttpResponse<String> hres = pair.request()
.GET().asString();

final HttpHeaders headers = hres.headers();
assertThat(headers.firstValue("Content-Type").orElseThrow()).isEqualTo("application/json");

bean = pair.request().path("usingOutputStream")
.GET()
.bean(HelloDto.class);
assertThat(bean.id).isEqualTo(42);
assertThat(bean.name).isEqualTo("rob");
}


@Test
void stream_viaIterator() {
final Stream<HelloDto> beanStream = pair.request()
Expand Down

0 comments on commit e0f489f

Please sign in to comment.