From e299868bfbcc70450b8e9b2772876ebebda28bc5 Mon Sep 17 00:00:00 2001 From: Bertil Chapuis Date: Sat, 17 Jun 2023 14:29:35 +0200 Subject: [PATCH] Adapt geocoder and iploc command --- .../apache/baremaps/cli/geocoder/Serve.java | 12 +++++++++--- .../org/apache/baremaps/cli/iploc/Serve.java | 10 +++++++--- .../java/org/apache/baremaps/cli/map/Dev.java | 5 ++--- .../apache/baremaps/server/ChangeResource.java | 6 ++++-- .../baremaps/server/GeocoderResource.java | 16 ---------------- .../apache/baremaps/server/IpLocResource.java | 18 ------------------ 6 files changed, 22 insertions(+), 45 deletions(-) diff --git a/baremaps-cli/src/main/java/org/apache/baremaps/cli/geocoder/Serve.java b/baremaps-cli/src/main/java/org/apache/baremaps/cli/geocoder/Serve.java index 7a3282c1d..1d9abe0f7 100644 --- a/baremaps-cli/src/main/java/org/apache/baremaps/cli/geocoder/Serve.java +++ b/baremaps-cli/src/main/java/org/apache/baremaps/cli/geocoder/Serve.java @@ -18,6 +18,7 @@ import io.servicetalk.http.router.jersey.HttpJerseyRouterBuilder; import java.nio.file.Path; import java.util.concurrent.Callable; +import org.apache.baremaps.server.ClassPathResource; import org.apache.baremaps.server.CorsFilter; import org.apache.baremaps.server.GeocoderResource; import org.apache.lucene.search.SearcherFactory; @@ -51,11 +52,16 @@ public Integer call() throws Exception { try ( var directory = MMapDirectory.open(indexDirectory); var searcherManager = new SearcherManager(directory, new SearcherFactory())) { - // Configure the application - var application = new ResourceConfig().register(CorsFilter.class) - .register(GeocoderResource.class).register(new AbstractBinder() { + + var application = new ResourceConfig() + .register(CorsFilter.class) + .register(GeocoderResource.class) + .register(ClassPathResource.class) + .register(new AbstractBinder() { @Override protected void configure() { + bind("geocoder").to(String.class).named("directory"); + bind("index.html").to(String.class).named("index"); bind(searcherManager).to(SearcherManager.class).named("searcherManager"); } }); diff --git a/baremaps-cli/src/main/java/org/apache/baremaps/cli/iploc/Serve.java b/baremaps-cli/src/main/java/org/apache/baremaps/cli/iploc/Serve.java index 371c395be..86379fea1 100644 --- a/baremaps-cli/src/main/java/org/apache/baremaps/cli/iploc/Serve.java +++ b/baremaps-cli/src/main/java/org/apache/baremaps/cli/iploc/Serve.java @@ -22,6 +22,7 @@ import java.util.concurrent.Callable; import javax.sql.DataSource; import org.apache.baremaps.iploc.IpLocRepository; +import org.apache.baremaps.server.ClassPathResource; import org.apache.baremaps.server.CorsFilter; import org.apache.baremaps.server.IpLocResource; import org.glassfish.hk2.utilities.binding.AbstractBinder; @@ -56,16 +57,19 @@ public Integer call() throws Exception { config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); - // config.setReadOnly(true); DataSource dataSource = new HikariDataSource(config); IpLocRepository ipLocRepository = new IpLocRepository(dataSource); - // Configure the application - var application = new ResourceConfig().register(CorsFilter.class).register(IpLocResource.class) + var application = new ResourceConfig() + .register(CorsFilter.class) + .register(IpLocResource.class) + .register(ClassPathResource.class) .register(new AbstractBinder() { @Override protected void configure() { + bind("iploc").to(String.class).named("directory"); + bind("index.html").to(String.class).named("index"); bind(ipLocRepository).to(IpLocRepository.class).named("iplocRepository"); } }); 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 f11b70a85..619b0e447 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 @@ -116,11 +116,10 @@ public Integer call() throws Exception { .register(new AbstractBinder() { @Override protected void configure() { - bind(tilesetPath).to(Path.class).named("tileset"); - bind(stylePath).to(Path.class).named("style"); - bind("assets").to(String.class).named("directory"); bind("assets").to(String.class).named("directory"); bind("viewer.html").to(String.class).named("index"); + bind(tilesetPath).to(Path.class).named("tileset"); + bind(stylePath).to(Path.class).named("style"); bind(tileStoreSupplier).to(tileStoreType); bind(styleSupplier).to(styleSupplierType); bind(tileJSONSupplier).to(tileJSONSupplierType); diff --git a/baremaps-server/src/main/java/org/apache/baremaps/server/ChangeResource.java b/baremaps-server/src/main/java/org/apache/baremaps/server/ChangeResource.java index 183ff19c7..0ee7cbb91 100644 --- a/baremaps-server/src/main/java/org/apache/baremaps/server/ChangeResource.java +++ b/baremaps-server/src/main/java/org/apache/baremaps/server/ChangeResource.java @@ -17,6 +17,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import java.io.IOException; import java.nio.file.*; import javax.inject.Inject; import javax.inject.Named; @@ -104,8 +105,9 @@ public void run() { } key.reset(); } - } catch (Exception e) { - logger.error("Error while watching for changes", e); + } catch (InterruptedException | IOException e) { + logger.error(e.getMessage()); + Thread.currentThread().interrupt(); } } } diff --git a/baremaps-server/src/main/java/org/apache/baremaps/server/GeocoderResource.java b/baremaps-server/src/main/java/org/apache/baremaps/server/GeocoderResource.java index b28ad2245..4043db858 100644 --- a/baremaps-server/src/main/java/org/apache/baremaps/server/GeocoderResource.java +++ b/baremaps-server/src/main/java/org/apache/baremaps/server/GeocoderResource.java @@ -17,7 +17,6 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import java.io.IOException; -import java.io.InputStream; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -94,19 +93,4 @@ private GeocoderResult asResult(IndexSearcher indexSearcher, ScoreDoc scoreDoc) throw new RuntimeException(e); } } - - @GET - @javax.ws.rs.Path("/{path:.*}") - public Response get(@PathParam("path") String path) { - if (path.equals("") || path.endsWith("/")) { - path += "index.html"; - } - path = String.format("geocoder/%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/IpLocResource.java b/baremaps-server/src/main/java/org/apache/baremaps/server/IpLocResource.java index c06e7c2ff..79b7b0cfb 100644 --- a/baremaps-server/src/main/java/org/apache/baremaps/server/IpLocResource.java +++ b/baremaps-server/src/main/java/org/apache/baremaps/server/IpLocResource.java @@ -18,15 +18,12 @@ import com.google.common.net.InetAddresses; import io.servicetalk.http.api.StreamingHttpRequest; import io.servicetalk.transport.api.ConnectionContext; -import java.io.IOException; -import java.io.InputStream; import java.net.InetSocketAddress; import java.util.List; import java.util.Optional; import javax.inject.Inject; import javax.inject.Singleton; import javax.ws.rs.GET; -import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.*; import org.apache.baremaps.iploc.IpLocObject; @@ -99,21 +96,6 @@ public Response iploc( } } - @GET - @javax.ws.rs.Path("/{path:.*}") - public Response get(@PathParam("path") String path) { - if (path.equals("") || path.endsWith("/")) { - path += "index.html"; - } - path = String.format("iploc/%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(); - } - } - public record InetnumLocationDto( String address, String inetStart,