|
@@ -1,12 +1,14 @@
|
|
|
|
|
|
package io.helidon.benchmark.nima.services;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
|
|
|
import com.fizzed.rocker.runtime.ArrayOfByteArraysOutput;
|
|
|
import io.helidon.benchmark.nima.models.DbRepository;
|
|
|
import io.helidon.benchmark.nima.models.Fortune;
|
|
|
+import io.helidon.common.buffers.BufferData;
|
|
|
import io.helidon.webserver.http.Handler;
|
|
|
import io.helidon.webserver.http.ServerRequest;
|
|
|
import io.helidon.webserver.http.ServerResponse;
|
|
@@ -14,6 +16,7 @@ import views.fortunes;
|
|
|
|
|
|
import static io.helidon.benchmark.nima.Main.CONTENT_TYPE_HTML;
|
|
|
import static io.helidon.benchmark.nima.Main.SERVER;
|
|
|
+import static io.helidon.http.HeaderNames.CONTENT_LENGTH;
|
|
|
|
|
|
public class FortuneHandler implements Handler {
|
|
|
|
|
@@ -30,11 +33,19 @@ public class FortuneHandler implements Handler {
|
|
|
public void handle(ServerRequest req, ServerResponse res) {
|
|
|
res.header(SERVER);
|
|
|
res.header(CONTENT_TYPE_HTML);
|
|
|
+
|
|
|
List<Fortune> fortuneList = repository.getFortunes();
|
|
|
fortuneList.add(ADDITIONAL_FORTUNE);
|
|
|
Collections.sort(fortuneList);
|
|
|
- res.send(fortunes.template(fortuneList)
|
|
|
- .render(ArrayOfByteArraysOutput.FACTORY)
|
|
|
- .toByteArray());
|
|
|
+ ArrayOfByteArraysOutput output = fortunes.template(fortuneList).render(ArrayOfByteArraysOutput.FACTORY);
|
|
|
+ List<byte[]> entity = output.getArrays();
|
|
|
+ BufferData bufferData = BufferData.create(entity.stream().map(BufferData::create).toList());
|
|
|
+ int length = bufferData.available();
|
|
|
+ res.header(CONTENT_LENGTH, String.valueOf(length));
|
|
|
+ try (var out = res.outputStream()) {
|
|
|
+ bufferData.writeTo(out);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
}
|