use of org.openremote.model.Container in project openremote by openremote.
the class MapService method init.
@Override
public void init(Container container) throws Exception {
mapTilesPath = Paths.get(getString(container.getConfig(), MAP_TILES_PATH, MAP_TILES_PATH_DEFAULT));
if (!Files.isRegularFile(mapTilesPath)) {
LOG.warning("Map tiles data file not found '" + mapTilesPath.toAbsolutePath() + "', falling back to built in map");
mapTilesPath = null;
}
mapSettingsPath = Paths.get(getString(container.getConfig(), MAP_SETTINGS_PATH, MAP_SETTINGS_PATH_DEFAULT));
if (!Files.isRegularFile(mapSettingsPath)) {
LOG.warning("Map settings file not found '" + mapSettingsPath.toAbsolutePath() + "', falling back to built in map settings");
mapSettingsPath = null;
}
if (mapTilesPath == null) {
if (Files.isRegularFile(Paths.get("/opt/map/mapdata.mbtiles"))) {
mapTilesPath = Paths.get("/opt/map/mapdata.mbtiles");
} else if (Files.isRegularFile(Paths.get("manager/src/map/mapdata.mbtiles"))) {
mapTilesPath = Paths.get("manager/src/map/mapdata.mbtiles");
}
}
if (mapSettingsPath == null) {
if (Files.isRegularFile(Paths.get("/opt/map/mapsettings.json"))) {
mapSettingsPath = Paths.get("/opt/map/mapsettings.json");
} else if (Files.isRegularFile(Paths.get("manager/src/map/mapsettings.json"))) {
mapSettingsPath = Paths.get("manager/src/map/mapsettings.json");
}
}
container.getService(ManagerWebService.class).addApiSingleton(new MapResourceImpl(this, container.getService(ManagerIdentityService.class)));
String tileServerHost = getString(container.getConfig(), MAP_TILESERVER_HOST, MAP_TILESERVER_HOST_DEFAULT);
int tileServerPort = getInteger(container.getConfig(), MAP_TILESERVER_PORT, MAP_TILESERVER_PORT_DEFAULT);
if (!TextUtil.isNullOrEmpty(tileServerHost)) {
WebService webService = container.getService(WebService.class);
UriBuilder tileServerUri = UriBuilder.fromPath("/").scheme("http").host(tileServerHost).port(tileServerPort);
@SuppressWarnings("deprecation") ProxyHandler proxyHandler = new ProxyHandler(new io.undertow.server.handlers.proxy.SimpleProxyClientProvider(tileServerUri.build()), getInteger(container.getConfig(), MAP_TILESERVER_REQUEST_TIMEOUT, MAP_TILESERVER_REQUEST_TIMEOUT_DEFAULT), ResponseCodeHandler.HANDLE_404).setReuseXForwarded(true);
HttpHandler proxyWrapper = exchange -> {
// Change request path to match what the tile server expects
String path = exchange.getRequestPath().substring(RASTER_MAP_TILE_PATH.length());
exchange.setRequestURI(TILESERVER_TILE_PATH + path, true);
exchange.setRequestPath(TILESERVER_TILE_PATH + path);
exchange.setRelativePath(TILESERVER_TILE_PATH + path);
proxyHandler.handleRequest(exchange);
};
webService.getRequestHandlers().add(0, pathStartsWithHandler("Raster Map Tile Proxy", RASTER_MAP_TILE_PATH, proxyWrapper));
}
}
Aggregations