18
18
package org .apache .baremaps .server ;
19
19
20
20
import static com .google .common .net .HttpHeaders .*;
21
+ import static io .netty .handler .codec .http .HttpHeaders .Values .APPLICATION_JSON ;
22
+ import static io .netty .handler .codec .http .HttpHeaders .Values .BINARY ;
21
23
24
+ import com .linecorp .armeria .common .HttpData ;
25
+ import com .linecorp .armeria .common .HttpResponse ;
26
+ import com .linecorp .armeria .common .ResponseHeaders ;
27
+ import com .linecorp .armeria .server .annotation .Get ;
28
+ import com .linecorp .armeria .server .annotation .Param ;
22
29
import de .javagl .jgltf .model .NodeModel ;
23
- import java .io .IOException ;
24
- import java .io .InputStream ;
25
30
import java .util .ArrayList ;
26
31
import java .util .List ;
27
- import javax .inject .Inject ;
28
- import javax .inject .Singleton ;
29
32
import javax .sql .DataSource ;
30
- import javax .ws .rs .GET ;
31
- import javax .ws .rs .PathParam ;
32
- import javax .ws .rs .core .Response ;
33
+
33
34
import org .apache .baremaps .tdtiles .GltfBuilder ;
34
35
import org .apache .baremaps .tdtiles .TdTilesStore ;
35
36
import org .apache .baremaps .tdtiles .building .Building ;
36
37
import org .apache .baremaps .tdtiles .subtree .Availability ;
37
38
import org .apache .baremaps .tdtiles .subtree .Subtree ;
38
39
39
- @ Singleton
40
- @ javax .ws .rs .Path ("/" )
41
40
public class TdTilesResources {
41
+
42
+ private static final ResponseHeaders GLB_HEADERS = ResponseHeaders .builder (200 )
43
+ .add (CONTENT_TYPE , BINARY )
44
+ .add (ACCESS_CONTROL_ALLOW_ORIGIN , "*" )
45
+ .build ();
46
+
47
+ private static final ResponseHeaders JSON_HEADERS = ResponseHeaders .builder (200 )
48
+ .add (CONTENT_TYPE , APPLICATION_JSON )
49
+ .add (ACCESS_CONTROL_ALLOW_ORIGIN , "*" )
50
+ .build ();
51
+
42
52
private final TdTilesStore tdTilesStore ;
43
53
44
- @ Inject
45
54
public TdTilesResources (DataSource dataSource ) {
46
55
this .tdTilesStore = new TdTilesStore (dataSource );
47
56
}
48
57
49
- @ GET
50
- @ javax .ws .rs .Path ("/subtrees/{level}.{x}.{y}.json" )
51
- public Response getSubtree (@ PathParam ("level" ) int level , @ PathParam ("x" ) int x ,
52
- @ PathParam ("y" ) int y ) {
58
+ @ Get ("regex:^/subtrees/(?<level>[0-9]+).(?<x>[0-9]+).(?<y>[0-9]+).json" )
59
+ public HttpResponse getSubtree (@ Param ("level" ) int level , @ Param ("x" ) int x , @ Param ("y" ) int y ) {
53
60
if (level == 18 ) {
54
- return Response .ok ()
55
- .entity (
56
- new Subtree (new Availability (false ), new Availability (true ), new Availability (false )))
57
- .header (CONTENT_TYPE , "application/json" ).build ();
61
+ return HttpResponse .ofJson (JSON_HEADERS ,
62
+ new Subtree (new Availability (false ), new Availability (true ), new Availability (false )));
58
63
}
59
- return Response .ok ()
60
- .entity (new Subtree (new Availability (true ), new Availability (true ), new Availability (true )))
61
- .header (CONTENT_TYPE , "application/json" ).build ();
64
+ return HttpResponse .ofJson (JSON_HEADERS ,
65
+ new Subtree (new Availability (true ), new Availability (true ), new Availability (true )));
62
66
}
63
67
64
- @ GET
65
- @ javax .ws .rs .Path ("/content/content_{level}__{x}_{y}.glb" )
66
- public Response getContent (@ PathParam ("level" ) int level , @ PathParam ("x" ) int x ,
67
- @ PathParam ("y" ) int y ) throws Exception {
68
+ @ Get ("regex:^/content/content_(?<level>[0-9]+)__(?<x>[0-9]+)_(?<y>[0-9]+).glb" )
69
+ public HttpResponse getContent (@ Param ("level" ) int level , @ Param ("x" ) int x , @ Param ("y" ) int y )
70
+ throws Exception {
68
71
if (level < 14 ) {
69
- return Response .ok ().entity (
70
- GltfBuilder .createGltf (new ArrayList <>())).build ();
72
+ return HttpResponse .of (GLB_HEADERS , HttpData .wrap (GltfBuilder .createGltf (new ArrayList <>())));
71
73
}
72
74
float [] coords = xyzToLatLonRadians (x , y , level );
73
75
List <NodeModel > nodes = new ArrayList <>();
@@ -77,23 +79,7 @@ public Response getContent(@PathParam("level") int level, @PathParam("x") int x,
77
79
float tolerance = level > 17 ? 0.00001f : level > 15 ? 0.00002f : 0.00004f ;
78
80
nodes .add (GltfBuilder .createNode (building , tolerance ));
79
81
}
80
- return Response .ok ().entity (
81
- GltfBuilder .createGltf (nodes )).build ();
82
- }
83
-
84
- @ GET
85
- @ javax .ws .rs .Path ("/{path:.*}" )
86
- public Response get (@ PathParam ("path" ) String path ) {
87
- if (path .equals ("" ) || path .endsWith ("/" )) {
88
- path += "index.html" ;
89
- }
90
- path = String .format ("tdtiles/%s" , path );
91
- try (InputStream inputStream = getClass ().getClassLoader ().getResourceAsStream (path )) {
92
- var bytes = inputStream .readAllBytes ();
93
- return Response .ok ().entity (bytes ).build ();
94
- } catch (IOException e ) {
95
- return Response .status (404 ).build ();
96
- }
82
+ return HttpResponse .of (GLB_HEADERS , HttpData .wrap (GltfBuilder .createGltf (nodes )));
97
83
}
98
84
99
85
/**
0 commit comments