Search in sources :

Example 11 with Container

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));
    }
}
Also used : Connection(java.sql.Connection) WebService(org.openremote.container.web.WebService) HashMap(java.util.HashMap) ValueUtil(org.openremote.model.util.ValueUtil) MapAccess.getInteger(org.openremote.container.util.MapAccess.getInteger) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) MapAccess.getString(org.openremote.container.util.MapAccess.getString) Level(java.util.logging.Level) API_PATH(org.openremote.manager.web.ManagerWebService.API_PATH) ResultSet(java.sql.ResultSet) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) UriBuilder(javax.ws.rs.core.UriBuilder) TextUtil(org.openremote.model.util.TextUtil) ManagerWebService(org.openremote.manager.web.ManagerWebService) Path(java.nio.file.Path) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Files(java.nio.file.Files) ContainerService(org.openremote.model.ContainerService) PreparedStatement(java.sql.PreparedStatement) Logger(java.util.logging.Logger) HttpHandler(io.undertow.server.HttpHandler) Container(org.openremote.model.Container) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Paths(java.nio.file.Paths) ProxyHandler(io.undertow.server.handlers.proxy.ProxyHandler) ResponseCodeHandler(io.undertow.server.handlers.ResponseCodeHandler) Optional(java.util.Optional) DriverManager(java.sql.DriverManager) WebService.pathStartsWithHandler(org.openremote.container.web.WebService.pathStartsWithHandler) HttpHandler(io.undertow.server.HttpHandler) WebService(org.openremote.container.web.WebService) ManagerWebService(org.openremote.manager.web.ManagerWebService) ProxyHandler(io.undertow.server.handlers.proxy.ProxyHandler) MapAccess.getString(org.openremote.container.util.MapAccess.getString) ManagerWebService(org.openremote.manager.web.ManagerWebService) UriBuilder(javax.ws.rs.core.UriBuilder)

Aggregations

Container (org.openremote.model.Container)11 Logger (java.util.logging.Logger)9 RouteBuilder (org.apache.camel.builder.RouteBuilder)6 MessageBrokerService (org.openremote.container.message.MessageBrokerService)5 ContainerService (org.openremote.model.ContainerService)5 java.util (java.util)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 Collectors (java.util.stream.Collectors)4 TimerService (org.openremote.container.timer.TimerService)4 ManagerIdentityService (org.openremote.manager.security.ManagerIdentityService)4 Consumer (java.util.function.Consumer)3 Level (java.util.logging.Level)3 EntityManager (javax.persistence.EntityManager)3 GlobalLock.withLock (org.openremote.container.concurrent.GlobalLock.withLock)3 PERSISTENCE_TOPIC (org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC)3 PersistenceService.isPersistenceEventForEntityType (org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType)3 MapAccess.getString (org.openremote.container.util.MapAccess.getString)3 AssetProcessingException (org.openremote.manager.asset.AssetProcessingException)3 AssetProcessingService (org.openremote.manager.asset.AssetProcessingService)3 AssetStorageService (org.openremote.manager.asset.AssetStorageService)3