From b5b9eb8207f42235f7ed2a52a6424f5629590992 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 18 Feb 2022 00:00:12 -0800 Subject: Migrate server API to storage --- src/main/java/org/traccar/MainModule.java | 6 ++++++ src/main/java/org/traccar/api/BaseResource.java | 3 ++- .../org/traccar/api/resource/ServerResource.java | 17 ++++++++------- .../java/org/traccar/database/DataManager.java | 4 ++++ src/main/java/org/traccar/web/WebServer.java | 24 ++++++++++++++++++++++ 5 files changed, 46 insertions(+), 8 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index aaa82adfc..d72e04588 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -84,6 +84,7 @@ import javax.ws.rs.client.Client; import io.netty.util.Timer; import org.traccar.speedlimit.OverpassSpeedLimitProvider; import org.traccar.speedlimit.SpeedLimitProvider; +import org.traccar.storage.Storage; public class MainModule extends AbstractModule { @@ -97,6 +98,11 @@ public class MainModule extends AbstractModule { return Context.getConfig(); } + @Provides + public static Storage provideStorage() { + return Context.getDataManager().getStorage(); + } + @Provides public static DataManager provideDataManager() { return Context.getDataManager(); diff --git a/src/main/java/org/traccar/api/BaseResource.java b/src/main/java/org/traccar/api/BaseResource.java index cc272df9c..56bf70cf0 100644 --- a/src/main/java/org/traccar/api/BaseResource.java +++ b/src/main/java/org/traccar/api/BaseResource.java @@ -15,11 +15,12 @@ */ package org.traccar.api; +import javax.ws.rs.core.Context; import javax.ws.rs.core.SecurityContext; public class BaseResource { - @javax.ws.rs.core.Context + @Context private SecurityContext securityContext; protected long getUserId() { diff --git a/src/main/java/org/traccar/api/resource/ServerResource.java b/src/main/java/org/traccar/api/resource/ServerResource.java index 8096c66fa..2d17d5e47 100644 --- a/src/main/java/org/traccar/api/resource/ServerResource.java +++ b/src/main/java/org/traccar/api/resource/ServerResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2022 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,9 +19,13 @@ import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.helper.LogAction; import org.traccar.model.Server; +import org.traccar.storage.Storage; import org.traccar.storage.StorageException; +import org.traccar.storage.query.Columns; +import org.traccar.storage.query.Request; import javax.annotation.security.PermitAll; +import javax.inject.Inject; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.PUT; @@ -36,14 +40,13 @@ import javax.ws.rs.core.Response; @Consumes(MediaType.APPLICATION_JSON) public class ServerResource extends BaseResource { + @Inject + private Storage storage; + @PermitAll @GET - public Server get(@QueryParam("force") boolean force) throws StorageException { - if (force) { - return Context.getDataManager().getServer(); - } else { - return Context.getPermissionsManager().getServer(); - } + public Server get() throws StorageException { + return storage.getObject(Server.class, new Request(new Columns.All())); } @PUT diff --git a/src/main/java/org/traccar/database/DataManager.java b/src/main/java/org/traccar/database/DataManager.java index 178cbbc57..9ac808a69 100644 --- a/src/main/java/org/traccar/database/DataManager.java +++ b/src/main/java/org/traccar/database/DataManager.java @@ -65,6 +65,10 @@ public class DataManager { private final Storage storage; + public Storage getStorage() { + return storage; + } + private final boolean forceLdap; public DataManager(Config config) throws Exception { diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index 604edfedc..46c1f3912 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -35,12 +35,18 @@ import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer; +import org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager; import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.server.spi.Container; +import org.glassfish.jersey.server.spi.ContainerLifecycleListener; import org.glassfish.jersey.servlet.ServletContainer; +import org.jvnet.hk2.guice.bridge.api.GuiceBridge; +import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; +import org.traccar.Main; import org.traccar.api.DateParameterConverterProvider; import org.traccar.config.Config; import org.traccar.api.AsyncSocketServlet; @@ -174,6 +180,24 @@ public class WebServer { JacksonFeature.class, ObjectMapperProvider.class, ResourceErrorHandler.class, SecurityRequestFilter.class, CorsResponseFilter.class, DateParameterConverterProvider.class); resourceConfig.packages(ServerResource.class.getPackage().getName()); + resourceConfig.register(new ContainerLifecycleListener() { + @Override + public void onStartup(Container container) { + var injectionManager = container.getApplicationHandler().getInjectionManager(); + var serviceLocator = ((ImmediateHk2InjectionManager) injectionManager).getServiceLocator(); + GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator); + var guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class); + guiceBridge.bridgeGuiceInjector(Main.getInjector()); + } + + @Override + public void onReload(Container container) { + } + + @Override + public void onShutdown(Container container) { + } + }); servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/api/*"); } -- cgit v1.2.3