1
1
2
2
package io .helidon .benchmark .nima .services ;
3
3
4
- import java .io .IOException ;
5
- import java .util .Collections ;
4
+ import java .util .Comparator ;
6
5
import java .util .List ;
7
6
8
7
import com .fizzed .rocker .runtime .ArrayOfByteArraysOutput ;
15
14
16
15
import static io .helidon .benchmark .nima .Main .CONTENT_TYPE_HTML ;
17
16
import static io .helidon .benchmark .nima .Main .SERVER ;
18
- import static io .helidon .http .HeaderNames .CONTENT_LENGTH ;
19
17
20
18
public class FortuneHandler implements Handler {
21
19
@@ -32,28 +30,11 @@ public FortuneHandler(DbRepository repository) {
32
30
public void handle (ServerRequest req , ServerResponse res ) {
33
31
res .header (SERVER );
34
32
res .header (CONTENT_TYPE_HTML );
35
-
36
- // render using template and get list of buffers
37
33
List <Fortune > fortuneList = repository .getFortunes ();
38
34
fortuneList .add (ADDITIONAL_FORTUNE );
39
- Collections .sort (fortuneList );
40
- ArrayOfByteArraysOutput output = fortunes .template (fortuneList ).render (ArrayOfByteArraysOutput .FACTORY );
41
- List <byte []> entity = output .getArrays ();
42
-
43
- // compute entity length and set header
44
- int length = 0 ;
45
- for (byte [] bytes : entity ) {
46
- length += bytes .length ;
47
- }
48
- res .header (CONTENT_LENGTH , String .valueOf (length ));
49
-
50
- // write entity to output
51
- try (var out = res .outputStream ()) {
52
- for (byte [] bytes : entity ) {
53
- out .write (bytes );
54
- }
55
- } catch (IOException e ) {
56
- throw new RuntimeException (e );
57
- }
35
+ fortuneList .sort (Comparator .comparing (Fortune ::getMessage ));
36
+ res .send (fortunes .template (fortuneList )
37
+ .render (ArrayOfByteArraysOutput .FACTORY )
38
+ .toByteArray ());
58
39
}
59
40
}
0 commit comments