You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@baremaps.apache.org by bc...@apache.org on 2023/06/16 22:01:47 UTC

[incubator-baremaps] 01/02: Add class to serve assets

This is an automated email from the ASF dual-hosted git repository.

bchapuis pushed a commit to branch server-refactoring
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git

commit c04ae00cb89698347b4749895ee9b7c47b54ee5c
Author: Bertil Chapuis <bc...@gmail.com>
AuthorDate: Fri Jun 16 23:08:34 2023 +0200

    Add class to serve assets
---
 .../main/java/org/apache/baremaps/cli/map/Dev.java | 14 +++--
 .../java/org/apache/baremaps/cli/map/Serve.java    | 11 +++-
 .../apache/baremaps/server/ClassPathResources.java | 68 ++++++++++++++++++++++
 .../apache/baremaps/server/ServerResources.java    | 15 -----
 .../{DevResources.java => ViewerResources.java}    | 21 +------
 5 files changed, 90 insertions(+), 39 deletions(-)

diff --git a/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Dev.java b/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Dev.java
index b6ebd65d..4f083525 100644
--- a/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Dev.java
+++ b/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Dev.java
@@ -24,8 +24,9 @@ import javax.sql.DataSource;
 import org.apache.baremaps.cli.Options;
 import org.apache.baremaps.config.ConfigReader;
 import org.apache.baremaps.postgres.PostgresUtils;
+import org.apache.baremaps.server.ClassPathResources;
 import org.apache.baremaps.server.CorsFilter;
-import org.apache.baremaps.server.DevResources;
+import org.apache.baremaps.server.ViewerResources;
 import org.apache.baremaps.vectortile.tileset.Tileset;
 import org.glassfish.hk2.utilities.binding.AbstractBinder;
 import org.glassfish.jersey.server.ResourceConfig;
@@ -70,11 +71,16 @@ public class Dev implements Callable<Integer> {
 
     try (var dataSource = PostgresUtils.dataSource(database)) {
       // Configure the application
-      var application = new ResourceConfig().register(CorsFilter.class).register(DevResources.class)
-          .register(contextResolverFor(objectMapper)).register(new AbstractBinder() {
+      var application = new ResourceConfig()
+          .register(CorsFilter.class)
+          .register(ViewerResources.class)
+          .register(ClassPathResources.class)
+          .register(contextResolverFor(objectMapper))
+          .register(new AbstractBinder() {
             @Override
             protected void configure() {
-              bind("viewer").to(String.class).named("assets");
+              bind("assets").to(String.class).named("directory");
+              bind("viewer.html").to(String.class).named("index");
               bind(tileset.toAbsolutePath()).to(Path.class).named("tileset");
               bind(style.toAbsolutePath()).to(Path.class).named("style");
               bind(dataSource).to(DataSource.class);
diff --git a/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Serve.java b/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Serve.java
index ee52f3e4..018c5809 100644
--- a/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Serve.java
+++ b/baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Serve.java
@@ -24,6 +24,7 @@ import java.util.concurrent.Callable;
 import org.apache.baremaps.cli.Options;
 import org.apache.baremaps.config.ConfigReader;
 import org.apache.baremaps.postgres.PostgresUtils;
+import org.apache.baremaps.server.ClassPathResources;
 import org.apache.baremaps.server.CorsFilter;
 import org.apache.baremaps.server.ServerResources;
 import org.apache.baremaps.tilestore.TileCache;
@@ -76,10 +77,16 @@ public class Serve implements Callable<Integer> {
 
     // Configure the application
     var application =
-        new ResourceConfig().register(CorsFilter.class).register(ServerResources.class)
-            .register(contextResolverFor(objectMapper)).register(new AbstractBinder() {
+        new ResourceConfig()
+            .register(CorsFilter.class)
+            .register(ServerResources.class)
+            .register(ClassPathResources.class)
+            .register(contextResolverFor(objectMapper))
+            .register(new AbstractBinder() {
               @Override
               protected void configure() {
+                bind("assets").to(String.class).named("directory");
+                bind("server.html").to(String.class).named("index");
                 bind(Serve.this.tileset).to(Path.class).named("tileset");
                 bind(style).to(Path.class).named("style");
                 bind(tileCache).to(TileStore.class);
diff --git a/baremaps-server/src/main/java/org/apache/baremaps/server/ClassPathResources.java b/baremaps-server/src/main/java/org/apache/baremaps/server/ClassPathResources.java
new file mode 100644
index 00000000..f89f9460
--- /dev/null
+++ b/baremaps-server/src/main/java/org/apache/baremaps/server/ClassPathResources.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package org.apache.baremaps.server;
+
+import java.io.IOException;
+import java.io.InputStream;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+import javax.ws.rs.GET;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.core.Response;
+
+/**
+ * Serves static resources from the class path.
+ */
+@Singleton
+@javax.ws.rs.Path("/")
+public class ClassPathResources {
+
+  private final String directory;
+
+  private final String index;
+
+  /**
+   * Constructs a {@code ClassPathResources}.
+   *
+   * @param directory the directory
+   * @param index the index
+   */
+  @Inject
+  public ClassPathResources(
+      @Named("directory") String directory,
+      @Named("index") String index) {
+    this.directory = directory;
+    this.index = index;
+  }
+
+  /**
+   * Serves a static resource from the class path.
+   *
+   * @param path the path
+   * @return the response
+   */
+  @GET
+  @javax.ws.rs.Path("{path:.*}")
+  public Response get(@PathParam("path") String path) {
+    if (path.equals("") || path.endsWith("/")) {
+      path += index;
+    }
+    path = String.format("%s/%s", directory, path);
+    try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path)) {
+      return Response.ok().entity(inputStream.readAllBytes()).build();
+    } catch (NullPointerException | IOException e) {
+      return Response.status(404).build();
+    }
+  }
+}
diff --git a/baremaps-server/src/main/java/org/apache/baremaps/server/ServerResources.java b/baremaps-server/src/main/java/org/apache/baremaps/server/ServerResources.java
index c488afa9..dda8e904 100644
--- a/baremaps-server/src/main/java/org/apache/baremaps/server/ServerResources.java
+++ b/baremaps-server/src/main/java/org/apache/baremaps/server/ServerResources.java
@@ -95,19 +95,4 @@ public class ServerResources {
       return Response.status(404).build();
     }
   }
-
-  @GET
-  @javax.ws.rs.Path("{path:.*}")
-  public Response get(@PathParam("path") String path) throws IOException {
-    if (path.equals("") || path.endsWith("/")) {
-      path += "server.html";
-    }
-    path = String.format("assets/%s", path);
-    try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path)) {
-      var bytes = inputStream.readAllBytes();
-      return Response.ok().entity(bytes).build();
-    } catch (NullPointerException | IOException e) {
-      return Response.status(404).build();
-    }
-  }
 }
diff --git a/baremaps-server/src/main/java/org/apache/baremaps/server/DevResources.java b/baremaps-server/src/main/java/org/apache/baremaps/server/ViewerResources.java
similarity index 86%
rename from baremaps-server/src/main/java/org/apache/baremaps/server/DevResources.java
rename to baremaps-server/src/main/java/org/apache/baremaps/server/ViewerResources.java
index 1bce008b..0519f58a 100644
--- a/baremaps-server/src/main/java/org/apache/baremaps/server/DevResources.java
+++ b/baremaps-server/src/main/java/org/apache/baremaps/server/ViewerResources.java
@@ -47,9 +47,9 @@ import org.slf4j.LoggerFactory;
 
 @Singleton
 @javax.ws.rs.Path("/")
-public class DevResources {
+public class ViewerResources {
 
-  private static final Logger logger = LoggerFactory.getLogger(DevResources.class);
+  private static final Logger logger = LoggerFactory.getLogger(ViewerResources.class);
 
   private final ConfigReader configReader = new ConfigReader();
 
@@ -72,7 +72,7 @@ public class DevResources {
   public static final String TILE_TYPE = "application/vnd.mapbox-vector-tile";
 
   @Inject
-  public DevResources(@Named("tileset") Path tileset, @Named("style") Path style,
+  public ViewerResources(@Named("tileset") Path tileset, @Named("style") Path style,
       DataSource dataSource, ObjectMapper objectMapper, Sse sse) {
     this.tileset = tileset.toAbsolutePath();
     this.style = style.toAbsolutePath();
@@ -149,19 +149,4 @@ public class DevResources {
       return Response.status(404).build();
     }
   }
-
-  @GET
-  @javax.ws.rs.Path("{path:.*}")
-  public Response get(@PathParam("path") String path) throws IOException {
-    if (path.equals("") || path.endsWith("/")) {
-      path += "viewer.html";
-    }
-    path = String.format("assets/%s", path);
-    try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path)) {
-      var bytes = inputStream.readAllBytes();
-      return Response.ok().entity(bytes).build();
-    } catch (NullPointerException | IOException e) {
-      return Response.status(404).build();
-    }
-  }
 }