| /* |
| * Copyright (C) 2020 The Baremaps Authors |
| * |
| * 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 com.baremaps.server.viewer; |
| |
| import static com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN; |
| import static com.google.common.net.HttpHeaders.CONTENT_ENCODING; |
| import static com.google.common.net.HttpHeaders.CONTENT_TYPE; |
| |
| import com.baremaps.blob.Blob; |
| import com.baremaps.blob.BlobStore; |
| import com.baremaps.blob.BlobStoreException; |
| import com.baremaps.tile.Tile; |
| import com.baremaps.tile.TileStore; |
| import com.baremaps.tile.TileStoreException; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.net.URI; |
| import javax.inject.Inject; |
| import javax.inject.Named; |
| import javax.ws.rs.GET; |
| import javax.ws.rs.Path; |
| import javax.ws.rs.PathParam; |
| import javax.ws.rs.Produces; |
| import javax.ws.rs.core.MediaType; |
| import javax.ws.rs.core.Response; |
| |
| @Path("/") |
| public class ViewerResources { |
| |
| private final byte[] style; |
| |
| private final byte[] tileset; |
| |
| private final TileStore tileStore; |
| |
| @Inject |
| public ViewerResources( |
| @Named("tileset") URI tileset, |
| @Named("style") URI style, |
| BlobStore blobStore, |
| TileStore tileStore) |
| throws BlobStoreException, IOException { |
| this.tileset = blobStore.get(tileset).getInputStream().readAllBytes(); |
| this.style = blobStore.get(style).getInputStream().readAllBytes(); |
| this.tileStore = tileStore; |
| } |
| |
| @GET |
| @Path("style.json") |
| @Produces(MediaType.APPLICATION_JSON) |
| public Response getStyle() { |
| return Response.ok(style).build(); |
| } |
| |
| @GET |
| @Path("tiles.json") |
| @Produces(MediaType.APPLICATION_JSON) |
| public Response getTileset() { |
| return Response.ok(tileset).build(); |
| } |
| |
| @GET |
| @Path("/tiles/{z}/{x}/{y}.mvt") |
| public Response getTile(@PathParam("z") int z, @PathParam("x") int x, @PathParam("y") int y) { |
| Tile tile = new Tile(x, y, z); |
| try { |
| Blob blob = tileStore.read(tile); |
| if (blob != null) { |
| return Response.status(200) // lgtm [java/xss] |
| .header(ACCESS_CONTROL_ALLOW_ORIGIN, "*") |
| .header(CONTENT_TYPE, blob.getContentType()) |
| .header(CONTENT_ENCODING, blob.getContentEncoding()) |
| .entity(blob.getInputStream()) |
| .build(); |
| } else { |
| return Response.status(204).build(); |
| } |
| } catch (TileStoreException ex) { |
| 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 += "index.html"; |
| } |
| path = String.format("viewer/%s", path); |
| try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path)) { |
| var bytes = inputStream.readAllBytes(); |
| return Response.ok().entity(bytes).build(); |
| } catch (IOException e) { |
| return Response.status(404).build(); |
| } |
| } |
| } |