1
1
2
2
package io .helidon .benchmark .nima .services ;
3
3
4
+ import java .io .IOException ;
4
5
import java .util .Collections ;
5
6
import java .util .List ;
6
7
7
8
import com .fizzed .rocker .runtime .ArrayOfByteArraysOutput ;
8
9
import io .helidon .benchmark .nima .models .DbRepository ;
9
10
import io .helidon .benchmark .nima .models .Fortune ;
11
+ import io .helidon .common .buffers .BufferData ;
10
12
import io .helidon .webserver .http .Handler ;
11
13
import io .helidon .webserver .http .ServerRequest ;
12
14
import io .helidon .webserver .http .ServerResponse ;
13
15
import views .fortunes ;
14
16
15
17
import static io .helidon .benchmark .nima .Main .CONTENT_TYPE_HTML ;
16
18
import static io .helidon .benchmark .nima .Main .SERVER ;
19
+ import static io .helidon .http .HeaderNames .CONTENT_LENGTH ;
17
20
18
21
public class FortuneHandler implements Handler {
19
22
@@ -30,11 +33,19 @@ public FortuneHandler(DbRepository repository) {
30
33
public void handle (ServerRequest req , ServerResponse res ) {
31
34
res .header (SERVER );
32
35
res .header (CONTENT_TYPE_HTML );
36
+
33
37
List <Fortune > fortuneList = repository .getFortunes ();
34
38
fortuneList .add (ADDITIONAL_FORTUNE );
35
39
Collections .sort (fortuneList );
36
- res .send (fortunes .template (fortuneList )
37
- .render (ArrayOfByteArraysOutput .FACTORY )
38
- .toByteArray ());
40
+ ArrayOfByteArraysOutput output = fortunes .template (fortuneList ).render (ArrayOfByteArraysOutput .FACTORY );
41
+ List <byte []> entity = output .getArrays ();
42
+ BufferData bufferData = BufferData .create (entity .stream ().map (BufferData ::create ).toList ());
43
+ int length = bufferData .available ();
44
+ res .header (CONTENT_LENGTH , String .valueOf (length ));
45
+ try (var out = res .outputStream ()) {
46
+ bufferData .writeTo (out );
47
+ } catch (IOException e ) {
48
+ throw new RuntimeException (e );
49
+ }
39
50
}
40
51
}
0 commit comments